Getting substrings in C

M

marcwentink

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=*afterGETSLASH;
i++;
afterGETSLASH++;
}

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

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

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

Richard Bos

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=*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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top