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:
- H dftactgrp(*no)
- H actgrp('QILE')
-
- D #GetTok PR 8192a Varying
- D string 65535a Varying Const Options(*Varsize)
- D delims 50a Varying Const
- D pos 10i 0
-
- /Free
-
- Dcl-S URI Char(100);
- Dcl-S SearchPos Int(10);
- Dcl-S Count Int(5);
- Dcl-S Path Varchar(30) Dim(100);
- Dcl-S PathString Char(100);
- Dcl-S Query Varchar(50) Dim(100);
- Dcl-S QueryString Char(100);
- Dcl-S SplitQuery VarChar(50) Dim(100);
- Dcl-S Key Varchar(50) Dim(100);
- Dcl-S Value Varchar(50) Dim(100);
-
- URI = 'https://abc.xyz.com/lookup/part/3425673?size=3&count=100&len=20';
- URI = %Subst(URI:8);
- If %Subst(URI:1:1) = '/';
- URI = %Subst(URI:2);
- EndIf;
- Clear Path;
- Clear Query;
- Clear PathString;
- Clear QueryString;
-
- SearchPos = %Scan('?':URI);
- If SearchPos > 0;
- PathString = %Subst(URI:1:SearchPos - 1);
- QueryString = %Subst(URI:SearchPos + 1);
- Else;
- PathString = URI;
- QueryString = *Blanks;
- EndIf;
- SearchPos = 0;
- Path(*) = #GetTok(PathString:'/':SearchPos);
- SearchPos = 0;
- Query(*) = #GetTok(QueryString:'&':SearchPos);
- For Count = 1 to 100;
- If Query(Count) <> *Blanks;
- SearchPos = 0;
- SplitQuery(*) = #GetTok(Query(Count):'=':SearchPos);
- Key(Count) = SplitQuery(1);
- Value(Count) = SplitQuery(2);
- EndIf;
- EndFor;
-
- Dump(A);
-
- *Inlr = *On;
- Return;
-
- /End-Free
-
- P #GetTok B
- D #GetTok PI 8192A varying
- D string 65535A varying const options(*varsize)
- D delims 50A varying const
- D pos 10I 0
-
- D res s 8192A varying
- D start s 10I 0
- D c s 1A
-
- /free
-
- start = pos + 1;
- %len(res) = 0;
-
- for pos = start to %len(string);
-
- c = %subst(string:pos:1);
-
- if %check(delims:c) = 1;
- res = res + c;
- else;
- leave;
- endif;
-
- endfor;
-
- return res;
-
- /end-free
- P #GetTok E
|
|
|