midrange.com code scratchpad
Name:
URIPARSE
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
12/07/2018 04:17:40 pm
IP:
Logged
Description:
Parse a URI out into Path and Query. Break the Query down to two arrays - Key and Value.
Code:
  1.      H dftactgrp(*no)
  2.      H actgrp('QILE')
  3.  
  4.      D #GetTok         PR          8192a   Varying
  5.      D   string                   65535a   Varying Const Options(*Varsize)
  6.      D   delims                      50a   Varying Const
  7.      D   pos                         10i 0
  8.  
  9.       /Free
  10.  
  11.         Dcl-S URI Char(100);
  12.         Dcl-S SearchPos Int(10);
  13.         Dcl-S Count Int(5);
  14.         Dcl-S Path Varchar(30) Dim(100);
  15.         Dcl-S PathString Char(100);
  16.         Dcl-S Query Varchar(50) Dim(100);
  17.         Dcl-S QueryString Char(100);
  18.         Dcl-S SplitQuery VarChar(50) Dim(100);
  19.         Dcl-S Key Varchar(50) Dim(100);
  20.         Dcl-S Value Varchar(50) Dim(100);
  21.  
  22.         URI = 'https://abc.xyz.com/lookup/part/3425673?size=3&count=100&len=20';
  23.         URI = %Subst(URI:8);
  24.         If %Subst(URI:1:1) = '/';
  25.           URI = %Subst(URI:2);
  26.         EndIf;
  27.         Clear Path;
  28.         Clear Query;
  29.         Clear PathString;
  30.         Clear QueryString;
  31.  
  32.         SearchPos = %Scan('?':URI);
  33.         If SearchPos > 0;
  34.           PathString = %Subst(URI:1:SearchPos - 1);
  35.           QueryString = %Subst(URI:SearchPos + 1);
  36.         Else;
  37.           PathString = URI;
  38.           QueryString = *Blanks;
  39.         EndIf;
  40.         SearchPos = 0;
  41.         Path(*) = #GetTok(PathString:'/':SearchPos);
  42.         SearchPos = 0;
  43.         Query(*) = #GetTok(QueryString:'&':SearchPos);
  44.         For Count = 1 to 100;
  45.           If Query(Count) <> *Blanks;
  46.             SearchPos = 0;
  47.             SplitQuery(*) = #GetTok(Query(Count):'=':SearchPos);
  48.             Key(Count) = SplitQuery(1);
  49.             Value(Count) = SplitQuery(2);
  50.           EndIf;
  51.         EndFor;
  52.  
  53.         Dump(A);
  54.  
  55.         *Inlr = *On;
  56.         Return;
  57.  
  58.       /End-Free
  59.  
  60.      P #GetTok         B
  61.      D #GetTok         PI          8192A   varying
  62.      D   string                   65535A   varying const options(*varsize)
  63.      D   delims                      50A   varying const
  64.      D   pos                         10I 0
  65.  
  66.      D res             s           8192A   varying
  67.      D start           s             10I 0
  68.      D c               s              1A
  69.  
  70.       /free
  71.  
  72.           start = pos + 1;
  73.           %len(res) = 0;
  74.  
  75.           for pos = start to %len(string);
  76.  
  77.              c = %subst(string:pos:1);
  78.  
  79.              if %check(delims:c) = 1;
  80.                 res = res + c;
  81.              else;
  82.                 leave;
  83.              endif;
  84.  
  85.           endfor;
  86.  
  87.           return res;
  88.  
  89.       /end-free
  90.      P #GetTok         E    
© 2004-2019 by midrange.com generated in 0.004s valid xhtml & css