OK, so I want to parse URL arguments send to (AJAX) pages generated by function calls.
I.e. MyAJAXPage.htm?parameter1=fish¶meter2=chips¶meter3=5.99
where parameter1 needs to be put in a variable entree, parameter2 into side, and parameter3 into price.
This is just an example. I'm not actually planning implementing Fast Food eCommerce from an SB70LC (though someone will undoubtedly try it!)
That means parsing Key:Value couplets and attaching them to data variables. What's the best way to go about this in C++?
I searched briefly through the forum, but didn't find anything. Probably used the wrong search criteria.
So... should I try using strtok or something in the STL? Forgive me, I'm very new to C++, and have, heretofore, only worked in C/ASM/BASIC for embedded development.
On a side tangent, I'm thinking I could probably use my Javascript to lump all the arguments into a single C++ friendly argument of hex characters (0-9A-F), but that still leaves the problem for some other person who will come along asking the same thing.
Parsing Arguments to Function Call pages
-
- Posts: 11
- Joined: Mon May 11, 2009 2:01 pm
Re: Parsing Arguments to Function Call pages
I would stay away from STL, it links in A LOT of additional code/libraries. Are you trying to learn how to do it in C++? How about just doing it in C?
-
- Posts: 11
- Joined: Mon May 11, 2009 2:01 pm
Re: Parsing Arguments to Function Call pages
Eh, either or.
I guess what I could to is set up a parsing system similar to one I made last year.
I wrote a parsing system that read in CSV style data and tied it to an array of structs containing pointers and data types. For this, which connects to silly things like single bits and strings, I'm thinking I could use a system like so:
struct{
unsigned long hash; //Hash of argumentName(and/or just the first 4 letters as bytes that make for fast numeric comparisons)
char *argumentName;//pointer to string containing name once hash is matched (I could probably even skip this if
void *pointerToData;//Where the data actually is.
enum dataType{BIT, BYTE, INT, LONG, FLOAT, DOUBLE, STRING};
unsigned char length; //Also bit #
}parse;
I guess what I could to is set up a parsing system similar to one I made last year.
I wrote a parsing system that read in CSV style data and tied it to an array of structs containing pointers and data types. For this, which connects to silly things like single bits and strings, I'm thinking I could use a system like so:
struct{
unsigned long hash; //Hash of argumentName(and/or just the first 4 letters as bytes that make for fast numeric comparisons)
char *argumentName;//pointer to string containing name once hash is matched (I could probably even skip this if
void *pointerToData;//Where the data actually is.
enum dataType{BIT, BYTE, INT, LONG, FLOAT, DOUBLE, STRING};
unsigned char length; //Also bit #
}parse;
Re: Parsing Arguments to Function Call pages
I just uploaded a new AJAX project http://forum.embeddedethernet.com/viewt ... ?f=7&t=548. There is code in the HtmlServices.cpp that does exactly what you want. I do use the STL but since I'm using a 5272 the Flash and RAM usage is very acceptable to me. Plus it makes for a very clean, small solution that is easy to extend. I haven't been using the STL very extensively so there may be downsides to it that I'm unaware of, but it seems a shame to ignore it unless there is a good reason.
In brief, I use an STL map<string,functionPtr> and just set up the strings that will be in the URLs and the associated function I want to invoke. The entries are created in the constructor and look like this:
where _requestToken= "request"
All my AJAX requests start with "request" so my parsing routine can be page independent and still verify that the URL is a valid request. So assume that url has content like this
"ajax.htm?request=machinestate"
The following method will end up on the line
ajax_iter->second(sock);
which will call WriteJsonMachineState(int sock)
Tod
In brief, I use an STL map<string,functionPtr> and just set up the strings that will be in the URLs and the associated function I want to invoke. The entries are created in the constructor and look like this:
Code: Select all
AjaxCommands.insert(make_pair(_requestToken+"=machinestate", &WriteJsonMachineState));
AjaxCommands.insert(make_pair(_requestToken+"=sampleData", &WriteSampleJsonData));
AjaxCommands.insert(make_pair(_requestToken+"=debugOptions", &WriteDebugOptionsJsonData));
All my AJAX requests start with "request" so my parsing routine can be page independent and still verify that the URL is a valid request. So assume that url has content like this
"ajax.htm?request=machinestate"
The following method will end up on the line
ajax_iter->second(sock);
which will call WriteJsonMachineState(int sock)
Code: Select all
void HtmlServices::ProcessAjaxFunction(int sock, string url)
{
string::size_type found_string = url.find(_requestToken);
if (found_string == string::npos)
{
ostringstream msg;
msg << "Url did not contain " << _requestToken << endl;
Debug::Write(msg.str());
return;
}
string command = url.substr(found_string);
AjaxFunctionMap::iterator ajax_iter = AjaxCommands.find(command);
if (ajax_iter != AjaxCommands.end())
{
//cout << "found a command " << ajax_iter->first << endl;
ajax_iter->second(sock);
}
else
{
cout << "could not find match for " << command << endl;
}
}