CMake netCDF Compatibility with WPS (#2121)
[WRF.git] / tools / CodeBase / util.c
blob81b1b44a4db05243c917a5cd1cf71b1643719088
1 #include <stdio.h>
2 #include <string.h>
4 #define INLINELEN (4*8192)
6 /* if sf is a string that ends in .f, end in .F, or vice verrsa */
8 int switch_little_big_f ( char s[] )
10 int n ;
11 n = strlen(s) ;
12 if ( !strcmp( &(s[n-2]) , ".f" ) ) { s[n-1] = 'F' ; }
13 else if ( !strcmp( &(s[n-2]) , ".F" ) ) { s[n-1] = 'f' ; }
14 return(0) ;
17 int contains_str( char *s1, char *s2 )
19 int i ;
20 char *p, *q, *r ;
21 if ( s2 == NULL || s1 == NULL ) return ( 0 ) ;
22 if ( *s2 == '\0' || *s1 == '\0' ) return ( 0 ) ;
23 p = s1 ;
24 while ( *p ) {
25 if ((r = (char *)index( p , *s2 )) == NULL ) { return( 0 ) ; }
26 for ( q = s2 ; *q && *r == *q ; r++ , q++ ) ;
27 if ( *q == '\0' ) return (1) ;
28 p++ ;
30 return( 0 ) ;
33 int
34 find_str( char *s1, char *s2, char **strp )
36 int i ;
37 char *p, *q, *r ;
38 if ( s2 == NULL || s1 == NULL ) return ( 0 ) ;
39 if ( *s2 == '\0' || *s1 == '\0' ) return ( 0 ) ;
40 p = s1 ;
41 while ( *p ) {
42 *strp = NULL ;
43 if ((r = (char *)index( p , *s2 )) == NULL ) { return( 0 ) ; }
44 *strp = r ;
45 for ( q = s2 ; *q && *r == *q ; r++ , q++ ) ;
46 if ( *q == '\0' ) return (1) ;
47 p++ ;
49 return( 0 ) ;
53 int contains_tok( char *s1, char *s2, char *delims )
55 char *p ;
56 char tempstr[INLINELEN] ;
57 int i ;
59 strcpy( tempstr , s1 ) ;
60 p = strtok ( tempstr, delims ) ;
61 i = 0 ;
62 while ( p != NULL )
64 if ( !strcmp ( p , s2 ) ) { return(i) ;}
65 i++ ;
66 p = strtok( NULL, delims ) ;
68 return(0) ;
71 int
72 get_token_n ( char *s1, char *delims , int n, char* retval )
74 char *p ;
75 int i ;
76 static char tempstr[INLINELEN] ;
77 strcpy( tempstr , s1 ) ;
78 p = strtok ( tempstr, delims ) ;
79 i = 0 ;
80 while ( p != NULL )
82 if ( i == n ) { strcpy( retval, p ) ; return( 1 ) ; }
83 p = strtok( NULL, delims ) ;
84 i++ ;
86 return( 0 ) ;
89 int
90 get_arg_n ( char *s1, int n, char *retval )
92 char *p, *q ;
93 int i, arg, inquote = -1, inparen = -1 ;
94 static char tempstr[INLINELEN] ;
95 char quotes[INLINELEN] ;
96 char parens[INLINELEN] ;
97 strcpy( tempstr , s1 ) ;
98 strcpy( retval, "" ) ;
100 if ( (p = index(tempstr, '(')) == NULL ) return(0) ;
101 p++ ;
102 arg = 0 ;
103 while ( *p && arg < n+1 ) {
104 q = p ;
105 inquote = -1 ;
106 for ( ; *p ; p++ )
108 if ( *p == '\'' || *p == '"' ) {
109 if ( inquote >= 0 ) {
110 if ( quotes[inquote] == *p ) {
111 inquote-- ;
112 } else {
113 inquote++ ;
114 quotes[inquote] = *p ;
116 } else {
117 inquote++ ;
118 quotes[inquote] = *p ;
121 if ( inquote < 0 ) {
122 if ( *p == '(' ) inparen++ ;
123 if ( *p == ')' ) {
124 if ( inparen >= 0 ) { inparen-- ; }
125 else { *p = '\0' ; arg++ ; break ;}
128 if ( inquote < 0 && inparen < 0 ) {
129 if ( *p == ',' ) { arg++ ; *p = '\0' ; p++ ; break ; }
133 if ( arg == n+1 ) {
134 for ( ; *q ; q++ ) { if ( *q != ' ' && *q != '\t' ) break ; }
135 strcpy( retval, q ) ;
136 return (1) ;
137 } else {
138 strcpy( retval, "" ) ;
139 return(0) ;
144 empty ( char *s )
146 char *p ;
147 for ( p = s ; *p ; p++ )
149 if ( ! ( *p == ' ' || *p == '\t' || *p == '\n' ) ) return( 0 ) ;
151 return( 1 ) ;
154 remove_nl ( char *s )
156 char *p ;
157 if (( p = index( s , '\n' )) != NULL ) *p = '\0' ;
161 remove_comments ( char *s )
163 int inquote = -1 ;
164 char quotes[INLINELEN] ;
165 char *p ;
166 for ( p = s ; *p ; p++ )
168 if ( *p == '\'' || *p == '"' ) {
169 if ( inquote >= 0 ) {
170 if ( quotes[inquote] == *p ) {
171 inquote-- ;
172 } else {
173 inquote++ ;
174 quotes[inquote] = *p ;
176 } else {
177 inquote++ ;
178 quotes[inquote] = *p ;
181 if ( inquote < 0 ) {
182 if ( *p == '!' ) { *p = '\0' ; break ; }
188 remove_chars ( char *s, char *r, char replace )
190 int inquote = -1 ;
191 int retval = 0 ;
192 char quotes[INLINELEN] ;
193 char *p, *q ;
194 for ( p = s ; *p ; p++ )
196 if ( *p == '\'' || *p == '"' ) {
197 if ( inquote >= 0 ) {
198 if ( quotes[inquote] == *p ) {
199 inquote-- ;
200 } else {
201 inquote++ ;
202 quotes[inquote] = *p ;
204 } else {
205 inquote++ ;
206 quotes[inquote] = *p ;
209 if ( inquote < 0 ) {
210 for ( q = r ; *q ; q++ ) {
211 if ( *p == *q ) { *p = replace ; retval = 1 ; }
215 return(retval) ;
219 remove_whitespace ( char *s )
221 char *p, *q ;
222 for ( p = s, q = s ; *p ; p++ )
224 if ( ! (*p == ' ' || *p == '\t') ) {
225 *q++ = *p ;
228 *q = '\0' ;
229 return(0) ;
234 iswhite( char *s )
236 char *p ;
237 for ( p = s ; *p ; p++ ) if ( *p != ' ' && *p != '\t' ) return(0) ;
238 return(1) ;
242 remove_ampersands ( char *s )
244 char * p, * q ;
245 int retval ;
246 if (( p = rindex ( s, '&' )) != NULL )
248 if ( iswhite( p+1 ) ) retval = 1 ;
249 else retval = 0 ;
251 else
253 retval = 0 ;
255 remove_chars ( s , "&", ' ' ) ;
256 return(retval) ;
259 lower_case_str ( char *s )
261 char * p ;
262 for ( p = s ; *p ; p++ )
264 if ( *p >= 'A' && *p <= 'Z' ) *p = *p - 'A' + 'a' ;
268 upper_case_str ( char *s )
270 char * p ;
271 for ( p = s ; *p ; p++ )
273 if ( *p >= 'a' && *p <= 'z' ) *p = *p - 'a' + 'A' ;