updated top-level README and version_decl for V4.5 (#1847)
[WRF.git] / tools / my_strtok.c
blob2e3be23924cee610f80a2ca971650bab46e502a2
1 #include <stdio.h>
2 #include "registry.h"
3 #include "protos.h"
4 #include "ctype.h"
7 /* work sort of like strtok but mind quote chars */
8 static char * tokpos = NULL ;
9 char *
10 my_strtok( char * s1 )
12 char *p, *retval ;
13 int state ;
14 state = 0 ;
15 retval = NULL ;
16 if ( s1 == NULL && tokpos == NULL ) return( NULL ) ;
17 if ( s1 != NULL ) tokpos = s1 ;
18 for ( p = tokpos ; *p ; p++ )
20 if ( state == 0 && (*p == ' ' || *p == '\t') ) continue ;
21 if ( state == 0 && !(*p == ' ' || *p == '\t') ) { state = 1 ; retval = p ; } ;
22 if ( state == 1 && (*p == '"') ) { state = 2 ; }
23 else if ( state == 2 && (*p == '"') ) { state = 1 ; }
24 if ( state == 1 && (*p == ' ' || *p == '\t') ) { *p = '\0' ; p++ ; break ; }
26 tokpos = p ;
27 return( retval ) ;
31 /* posix like rentrant strtok; not quote safe, and not quite strtok -- new version; skips multi delims */
32 char *
33 strtok_rentr( char * s1 , char * s2, char ** tokpos )
35 char *p, *q, *retval ;
36 int match ;
37 retval = NULL ;
38 if ( s1 == NULL && s2 == NULL ) return( NULL ) ;
39 if ( s1 != NULL ) { *tokpos = s1 ; }
40 if ( **tokpos ) retval = *tokpos ;
41 for ( p = *tokpos ; *p ; p++ )
43 for ( q = s2 ; *q ; q++ )
45 if ( *p == *q ) { *p = '\0' ; p++ ; goto foundit ; }
48 foundit:
49 /* skip over multi-delims */
50 for ( ; *p ; p++ )
52 match = 0 ;
53 for ( q = s2 ; *q ; q++ )
55 if ( *p == *q ) { *p = '\0' ; match++ ; }
57 if ( match == 0 ) { break ; }
59 *tokpos = p ;
60 return( retval ) ;
63 #if 0
64 /* posix like rentrant strtok; not quote safe, and not quite strtok -- won't skip over multiple delims */
65 char *
66 strtok_rentr( char * s1 , char * s2, char ** tokpos )
68 char *p, *q, *retval ;
69 retval = NULL ;
70 if ( s1 == NULL && s2 == NULL ) return( NULL ) ;
71 if ( s1 != NULL ) { *tokpos = s1 ; }
72 if ( **tokpos ) retval = *tokpos ;
73 for ( p = *tokpos ; *p ; p++ )
75 for ( q = s2 ; *q ; q++ )
77 if ( *p == *q ) { *p = '\0' ; p++ ; goto foundit ; }
80 foundit:
81 *tokpos = p ;
82 return( retval ) ;
84 #endif
86 int
87 make_lower( char * s1 )
89 char * p ;
90 int state ;
91 state = 0 ;
92 for ( p = s1 ; *p ; p++ )
94 if ( state == 0 && *p == '"' ) state = 1 ;
95 else if ( state == 1 && *p == '"' ) state = 0 ;
96 if ( state == 0 )
98 *p = tolower(*p) ;
101 return(0) ;