Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Getting substrings in C

Reply
Thread Tools

Getting substrings in C

 
 
marcwentink@hotmail.com
Guest
Posts: n/a
 
      12-23-2005
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++;
}
}

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      12-23-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
regex: How to extract substrings? Markus Dehmann Java 2 12-10-2005 06:35 AM
Compile typedef-ed substrings mrstephengross C++ 7 04-06-2005 06:33 PM
Binary files, substrings and (un)packing. Leandro Pardini Perl 1 10-27-2003 07:57 PM
C++: Comparing Substrings In An Array Ying Yang C++ 9 09-26-2003 11:52 PM
Getting substrings within a String Tony Perl Misc 7 08-03-2003 06:19 PM



Advertisments