Use of strtok in c functions and implementation of win & BSD

Source: Internet
Author: User
Tags strtok

In the past few days, I have been studying TCP/IP sockets in C (2nd) Practical Guide for programmers and learning how to confirm that data transmission between the client and server is a piece of data begin and end, the author describes two methods:

First, you must coordinate between the client and server each time you transmit data of a fixed length.

Type 2: Specify the token-marker for begin and end. (Of course, if the data you transmit contains the token you specified, there is a solution. It is not what this article will talk about)

Okay, the problem arises. How can we identify the token to take out the real data between the token header and the token tail?

The author used the strtok function, the main character of this article, and translated "C programming language" (I also want man's, but deepin is not installed. It's strange ~~~) Let's see how to say:

I don't know if you can understand it. I don't understand it anyway, but I should be able to split the data between tokens ~~~

If you come back to the Internet and check it again, you can see how to use it. It would be better if it can be implemented ~~~

As follows:

Strtok sample code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* Strtok example */
# Include <stdio. h>
# Include <string. h>

Char * strtok_bsd (char * s, const char * delim );
Char * strtok_win (char * s, const char * delim );

Int main ()
{
Char STR [] = "-this, a sample string ";
Char * PCH;
Printf ("splitting string \" % s \ "into tokens: \ n", STR );
PCH = strtok (STR ,",.-");
While (PCH! = NULL)
{
Printf ("% s \ n", PCH );
PCH = strtok (null ,",.-");
}
Return 0;
}

Strtok_bsd code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Implementation of strtok in NetBSD:
Char * strtok_r_bsd (char * s, const char * delim)
{
Const char * spanp; // span indicates separation, and P indicates pointer
Char C, SC; // C indicates char characters, SC indicates span char
Char * tok; // token indicates the separated segments.
Static char * last; // set last as a static local variable to save the address of the remaining content.

// When both the start and end ends are null, it indicates that no character is searched, so null is returned.
If (S = NULL & (S = * Last) = NULL)
Return (null );
// A loop composed of goto is skipped when a string is scanned and needs to be matched.
Cont:
C = * s ++;
For (spanp = delim; (SC = * spanp ++ )! = 0 ;)
{
If (C = SC)
Goto cont;
}
// If the next character is 0, the search result is reached. Set last to null and return null.
If (C = 0)
{
* Last = NULL;
Return (null );
}
// Return the original string pointer.
Tok = s-1;
// Start to scan the string for matching characters, and then return the part before the matching character.
// This seems to be an infinite loop. However, when both the source string and the matching string end, that is, if both S and SC are null, the outermost loop ends with return (Tok) end the loop.
For (;;)
{
C = * s ++;
Spanp = delim;
Do
{
If (SC = * spanp ++) = C)
{
If (C = 0)
S = NULL;
Else
S [-1] = 0;
* Last = s;
Return (Tok );
}
}
While (SC! = 0 );
}
}
Strtok_win code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Implementation of strtok in Microsoft:
Char * strtok_win (char * s, const char * delim)
{
Static unsigned char * last; // Save the remaining parts after separation
Unsigned char * STR; // returned string
Const unsigned char * CTRL = (const unsigned char *) delim; // delimiter

// Place the delimiter in an index table. 32 is defined because the maximum ASCII limit table is 0 ~ 255, that is, the maximum 255 shifted three places to the right, that is, dividing by 8 will certainly be a number in 32.
Unsigned char map [32];
Int count;

// Clear all the maps to 0, and perform subsequent operations. The values of 0 and 0 are both 0.
For (COUNT = 0; count <32; count ++)
Map [count] = 0;

// Put matching characters in the table
// The algorithm is to shift the matching character three places to the right, which is equal to dividing by 8, and then adding the value (plus)
// Match the character with 7 to get 3 lower digits. The result is the number of digits shifted from 1 to the left. The maximum number of shifts to the left is 7, that is, the maximum value is 128,
Do
{
Map [* Ctrl> 3] | = (1 <(* CTRL & 7 ));
}
While (* CTRL ++ );

// Whether the original string is null. If it is null, the separator of the remaining characters is obtained for the second time.
If (s)
STR = (unsigned char *) S;
Else
STR = last;

// Check whether any matching character exists in the table. If any
While (Map [* STR> 3] & (1 <(* STR & 7) & * Str)
STR ++;
// Reset the string to be scanned
S = (char *) STR;
// Start scanning
For (; * STR; STR ++)
{
If (Map [* STR> 3] & (1 <(* STR & 7 )))
{
* STR ++ = '\ 0'; // when it is found, set the matching character to 0 and point STR to the next character.
Break; // exit the loop
}
}
Last = STR; // Save the pointer of the remaining string to the static variable last.

If (S = (char *) Str)
Return NULL; // no result is found, that is, no pointer is moved. null is returned.
Else
Return s; // found, returns the header pointer of the previous string
}

PS: replace strtok with strtol_bsd and strtok_win. Then you can perform single-step debugging to see ~~~

NetBSD saves space and time (its time complexity is N2), while Microsoft saves time (its time complexity is N ), sacrifice space (a 32-bit 8-bit space is opened.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.