By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,897 Members | 1,969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,897 IT Pros & Developers. It's quick & easy.

Getting substrings in C

P: n/a
I wonder if there is no other way to substract a substring from a
string then just using a while lus. This below works ok, but looks like
a lot of code and statements if I compare it with the use of
std::string. Is this a bit ok? It should get information from a HTTP
request between the chars "GET /" and "HTTP".

Trying to combine <string> and "*.h" C includes and compiling "C++"
instead of just "C" also lead to a disaster, so that alternative does
not look appealing either.

Here is my code:

void getInfo(const char * pszBuf, char * pstrOms, char * pstrAGB, char
* pstrCat, char * pstrPIN)
{
char * afterGETSLASH = strstr(pszBuf, "GET /");
afterGETSLASH = strchr(afterGETSLASH, '[');afterGETSLASH++;

char* pStrHTTP = NULL;
pStrHTTP = strstr(afterGETSLASH, "HTTP");
pStrHTTP--;

int i=0;
while ( (afterGETSLASH != pStrHTTP) && (*afterGETSLASH != ',') &&
(*afterGETSLASH != ']') )
{
pstrOms[i]=*afterGETSLASH;
i++;
afterGETSLASH++;
}

afterGETSLASH++;
i=0;
while ( (afterGETSLASH != pStrHTTP) && (*afterGETSLASH != ',') &&
(*afterGETSLASH != ']') )
{
pstrAGB[i]=*afterGETSLASH;
i++;
afterGETSLASH++;
}

afterGETSLASH++;
i=0;
while ( (afterGETSLASH != pStrHTTP) && (*afterGETSLASH != ',') &&
(*afterGETSLASH != ']') )
{
pstrCat[i]=*afterGETSLASH;
i++;
afterGETSLASH++;
}

afterGETSLASH++;
i=0;
while ( (afterGETSLASH != pStrHTTP) && (*afterGETSLASH != ',') &&
(*afterGETSLASH != ']') )
{
pstrPIN[i]=*afterGETSLASH;
i++;
afterGETSLASH++;
}
}

Dec 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
ma*********@hotmail.com wrote:
I wonder if there is no other way to substract a substring from a
string then just using a while lus. This below works ok, but looks like
a lot of code and statements if I compare it with the use of
std::string. Is this a bit ok? It should get information from a HTTP
request between the chars "GET /" and "HTTP".
Yes, there is. However, you're doing a bit more than just getting a
substring.
Trying to combine <string> and "*.h" C includes and compiling "C++"
instead of just "C" also lead to a disaster, so that alternative does
not look appealing either.
Very wise decision. That's a recipe for tears, unless you know what
you're doing - and when you do, the result is C++, not C.
void getInfo(const char * pszBuf, char * pstrOms, char * pstrAGB, char
* pstrCat, char * pstrPIN)
{
char * afterGETSLASH = strstr(pszBuf, "GET /");
afterGETSLASH = strchr(afterGETSLASH, '[');afterGETSLASH++;
Be aware that mixing declarations and code like this can be confusing to
debug unless you're careful, and is not valid in C89 (but is valid C99).
char* pStrHTTP = NULL;
pStrHTTP = strstr(afterGETSLASH, "HTTP");
pStrHTTP--;

int i=0;
while ( (afterGETSLASH != pStrHTTP) && (*afterGETSLASH != ',') &&
(*afterGETSLASH != ']') )
{
pstrOms[i]=*afterGETSLASH;
i++;
afterGETSLASH++;
}


You could do something here involving strcspn() to find the first ',' or
']', pointer comparison to discover whether that's before or after
"HTTP", and then do *pstr0ms='\0' and strncat().

If you know that you will always be able to write to the string, you
could take a leaf out of strtok()'s book, and do *pStrHTTP='\0' before
copying, and *pStrHTTP='H' (or whatever used to be before the 'H', but
be careful of malformed input! Perhaps look for " HTTP" instead. You
don't want to introduce an exploitable buffer overrun.) afterwards. Then
you won't need the pointer comparison. In that case, though, this
writability requirement should go into the function's documentation.

Richard
Dec 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.