Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Getting substrings in C (http://www.velocityreviews.com/forums/t440634-getting-substrings-in-c.html)

marcwentink@hotmail.com 12-23-2005 09:04 AM

Getting substrings in C
 
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++;
}
}


Richard Bos 12-23-2005 10:03 AM

Re: Getting substrings in C
 
marcwentink@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


All times are GMT. The time now is 05:40 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.