From "C" to Rebol:
[1/3] from: tim::johnsons-web::com at: 31-Jul-2001 15:50
Okay all: here we go:
On Tue, Jul 31, 2001 at 10:45:39AM -0500, Joel Neely wrote:
> ...
> > Now, here's what I propose: If two or three of you "real rebol
<<quoted lines omitted: 10>>
> lately, so I can't promise immediate turnaround, but would
> be glad to contribute as time permits.
;The function below decodes data read either as a query string
; or from stdin in a form posting
; As you will see: I use 3 variables with cgi-form as an
; intermediate, (as I would do in "C"). Works for me
; but this is an example a function to could use some optimization
; As I said before, I would be happy to create a series of
; before-and-after examples
decode-data: func[/local cgi-form data-string][
if data-string: read-data[
replace/all data-string "+" " "
cgi-form: parse/all data-string "&="
foreach item cgi-form[append pair-list dehex item]
pairs: (length? pair-list) / 2
]
]; end decode-data
Cheers!
--
Tim Johnson <[tim--johnsons-web--com]>
http://www.johnsons-web.com
[2/3] from: joel:neely:fedex at: 31-Jul-2001 16:55
Hi, Tim,
I'm not sure if I've missed the point on this one, but...
Tim Johnson wrote:
> ;The function below decodes data read either as a query string
> ; or from stdin in a form posting
<<quoted lines omitted: 11>>
> ]
> ]; end decode-data
... there's a mezzanine word that does essentially the same thing:
>> ? decode-cgi
USAGE:
DECODE-CGI args
DESCRIPTION:
Converts CGI argument string to a list of words and value
strings.
DECODE-CGI is a function value.
ARGUMENTS:
args -- Starts at first argument word (Type: any-string)
>> source decode-cgi
decode-cgi: func [
{Converts CGI argument string to a list of words and value
strings.}
args [any-string!] "Starts at first argument word"
/local list equate value name val
][
plus-to-space: func [arg /local seek chr] [
if any [none? arg empty? arg] [return ""]
seek: arg
while [seek: find seek #"+"] [
change seek #" "
seek: next seek
]
head arg
]
list: make block! 8
equate: [
copy name to "=" "="
(append list to-set-word name)
value
]
value: [
"&" (append list copy "") |
[copy val to "&" "&" | copy val to end]
(append list either none? val [
copy ""
][
form dehex plus-to-space val
]
)
]
parse/all args [some equate | none]
list
]
You can use it to simply decode the query string to a block
of setword/value pairs...
>> decode-cgi {fee=eenie&fie=meenie&foe=miney&fum=moe}
== [fee: "eenie" fie: "meenie" foe: "miney" fum: "moe"]
...or use the result to make an object to encapsulate the
query...
>> foo: make object! decode-cgi {
fee=eenie&fie=meenie&foe=miney&fum=moe
}
>> source foo
foo:
make object! [
fee: "eenie"
fie: "meenie"
foe: "miney"
fum: "moe"
]
...and use its content, as in...
>> foo/fee
== "eenie"
>> foo/fum
== "moe"
HTH!
-jn-
--
------------------------------------------------------------
Programming languages: compact, powerful, simple ...
Pick any two!
joel'dot'neely'at'fedex'dot'com
[3/3] from: tim:johnsons-web at: 1-Aug-2001 9:22
On Tue, Jul 31, 2001 at 04:55:36PM -0500, Joel Neely wrote:
> Hi, Tim,
>
> I'm not sure if I've missed the point on this one, but...
>
> Tim Johnson wrote:
> ..
> ... there's a mezzanine word that does essentially the same thing:
Hey Joe:
Unfortunately, you caught me with a number of things on my plate today:
So I can't give it the immediate attention that it deserves... but -
1)When I originally wrote this module, I was not able to make
rebol's decode-cgi function decode that 'name part of the pair
example results below:
from >>cgi: make object! decode-cgi data-string
print mold cgi
; I get:
make object! [ required: {First Name;Last Name;Street address;State/Province;Country;Phone;E-mail;password;ccnum;expdate;Subscription
Type;City;Zip/Postal
Code} optional: "Fax;referred" First+Name: "Tim" Last+Name: "Johnson" Street+address:
"P.O. Box 3341" City: "Palmer" State%2FProvince: "Alaska"
Zip%2FPostal+Code: "99645" Country: "United States" Phone: "907.745.5392" Fax: "" E-mail:
"[tim--johnsons-web--com]" password: "extra" referred: "" ccnum:
234190345908
expdate: "01022002" Subscription+Type: "Selection Required!" ]
;and you see that only the 'values are decoded.
; Perhaps I missed something here?
; For my purposes, the 'names must be decoded as well, thus the hand-rolled
; function
; BTW: However, I believe I see the solution for my original question in
; the mezzanine code:
;Let me know what you think:
And thanks for your help with this thread
regards
tim
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted