LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   A question about \r\n in C... (https://www.linuxquestions.org/questions/programming-9/a-question-about-%5Cr%5Cn-in-c-4175520339/)

trist007 09-27-2014 11:45 PM

A question about \r\n in C...
 
I have a string that has several carriage returns line feeds in it. Of course a string ends in C when it is NULL terminated.

The pointer to the string I am working with is a const char *. I need to pull out the data between each \r\n. I can't use strtok() because the pointer is a const and strtok changes the string. I don't want to write a function that checks each byte for the \r\n sequence. Are there any other functions that would do this?

-Tristan

trist007 09-27-2014 11:51 PM

I suppose I can make a non const copy of the string and use strtok on that. This is for a packet analyzer of web traffic so I want it to be as efficient as possible. It just seems so expensive to do this on each HTTP request.

-Tristan

psionl0 09-28-2014 03:00 AM

You could also try strcspn(). http://www.cplusplus.com/reference/cstring/strcspn/

trist007 09-28-2014 08:07 AM

Great thank you. I will try that.

-Tristan

smeezekitty 10-10-2014 04:46 PM

Writing a function to perform the check is quite trivial.

All you need to do is literate through the string and check for the first character e.g. "\r"
and if it matches then check the current position+1 and see if it matches the second character e.g. "\n"
and if that matches return the position. If there is no match, then increment and try again until hitting \0

If you need to find multiple matches in a string you can either add them to a list stored in an array
or you can have your search function take an argument as to where to start in the string.


All times are GMT -5. The time now is 05:18 PM.