forked from lxm1117/pDE_codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjString.c
275 lines (233 loc) · 7.92 KB
/
jString.c
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* ========================================================================
pDE: Parallel version of Storn and Price's Differential evolution
applied to NEURON modeling
c2005 Jose Ambros-Ingerson jose@kiubo.net
$Date: 2007/10/01 20:57:57 $ $Log: jString.c,v $
$Date: 2007/10/01 20:57:57 $ Revision 1.4 2007/10/01 20:57:57 jose
$Date: 2007/10/01 20:57:57 $ changed default alloc for jString to avoid reallocs (the cause trouble in BALE cluster)
$Date: 2007/10/01 20:57:57 $
$Date: 2007/10/01 20:57:57 $ Revision 1.3 2006/09/22 23:11:47 jose
$Date: 2007/10/01 20:57:57 $ Changed so that returns NULL tok when no more tokens
$Date: 2007/10/01 20:57:57 $
$Date: 2007/10/01 20:57:57 $ Revision 1.2 2005/06/01 20:54:31 jose
$Date: 2007/10/01 20:57:57 $ worked out most warnings in biowulf.biosci.ohiou.edu
$Date: 2007/10/01 20:57:57 $
$Date: 2007/10/01 20:57:57 $ Revision 1.1.1.1 2005/05/20 19:49:14 jose
$Date: 2007/10/01 20:57:57 $ Parallel Differential Evolution
$Date: 2007/10/01 20:57:57 $
======================================================================== */
/* ================================================================================
String related utilities
================================================================================ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jLib.h"
/* ================================================================================
return pointer to freshly allocated jString
================================================================================ */
jString* jString_new(){
jString* jS;
jS = (jString *) xmalloc( sizeof( jString ));
jS->s = (char *) xmalloc( 2048 * sizeof( char ));
jS->sz = 2048; /* large to avoid reallocs; bale cluster crashes */
return jS;
}
/* ================================================================================
free jString
================================================================================ */
void jString_free( jString* jS ){
xfree( (char*) jS->s );
xfree( (char*) jS );
}
/* ================================================================================
Read a line (up to \n) from file fd and return as a jString
return EOF upon end of file.
Assumes jS has been allocated using jString_new
================================================================================ */
int jLine_read( FILE *fp, jString *jS )
{
int i, c;
i = 0;
while( EOF!=(c=getc(fp)) && c!='\n' ) {
if( i >= jS->sz+1 ) {
if( jS->sz == 0 ){
jS->s = NULL;
jS->sz = 2048; /* large to avoid reallocs; bale cluster crashes */
}
jS->sz *= 2;
jS->s = (char *) xrealloc( jS->s, jS->sz*sizeof( char ) );
}
jS->s[i++] = (char) c;
}
if( c==EOF && i==0 ) return( EOF );
jS->s[i] = '\0';
return( i );
}
/* ================================================================================
str2TYPE converts the string to a number of the given TYPE if possible using the
strtoTYPE routines in the c-library.
Returns 1 on success; -1 on error
================================================================================ */
int str2int( char *s, int *i )
{
char *ptr;
*i = (int) strtol( s, &ptr, 10 );
if( strEQ( ptr, "" ))
return 1;
else
return -1;
}
/* ================================================================================ */
int str2short( char *s, short *si )
{
int i;
if( 1 == str2int( s, &i )){
*si = (short) i;
return 1;
}
else
return -1;
}
/* ================================================================================ */
int str2double( char *s, double *d )
{
char *ptr;
*d = strtod( s, &ptr );
if( strEQ( ptr, "" ))
return 1;
else
return -1;
}
/* ================================================================================ */
int str2float( char *s, float *f )
{
double d;
if( 1 == str2double( s, &d )){
*f = (float) d;
return 1;
}
else
return -1;
}
/* =========================================================================
return tokens in str separated by any char in delims.
A token is defined as any non-empty string delimited by any char in delims.
The token is searched starting at position s and placed in tok (NULL
terminated) which should be large enough to contain it
and the position to start search for next token is returned.
-1 is returned when no tokens remain.
Typical use is:
s = 0; while( -1 != (s=strtoken(str," \t",s,tok)) ) foo( tok );
================================================================================ */
int strtoken( char *str, char *delims, int s, char *tok )
{
char *fs, *d;
fs = str;
/* find begining of token */
for( str=str+s; *str; ++str ){
for( d=delims; *d; ++d )
if( *d == *str ) break;
if( ! *d ) break;
}
if(! *str ){
*tok = (char) NULL;
return -1;
}
/* find end of token */
for( ; *str; *tok++ = *str++ ){
for( d=delims; *d; ++d )
if( *d == *str ) break;
if( *d ) break;
}
*tok = (char) NULL;
return( str - fs );
}
/* ================================================================================
return tokens on repeated calls reading from file fd.
A token is defined as any non-empty string delimited by any char in delims.
The token is placed in tok (NULL terminated) which should be large enough to
contain it.
The char that delimits the token on the right is returned as end_del.
returns 1 on success; EOF on end-of-file.
================================================================================ */
int fp_token( FILE *fp, char *delims, char *tok, int *end_del )
{
char *d;
int c;
while( EOF != ( c = getc( fp )) ){
for( d=delims; *d; ++d )
if( *d == (char)c ) break;
if( ! *d ) break;
}
if( c == EOF ) return EOF;
*tok++ = (char) c;
while( EOF != ( c= getc( fp )) ){
for( d=delims; *d; ++d )
if( *d == (char) c ) break;
if( *d ) break;
else *tok++ = (char) c;
}
*tok = (char) NULL;
*end_del = (int) *d;
return 1;
}
/* ================================================================================
return tokens on repeated calls reading from file fd.
A token is defined as any non-empty string delimited by any char in delims.
The token is placed in tok (NULL terminated).
tok is allocated inside xfp_token and should be freed by the calling routine
when done using xfree.
The char that delimits the token on the right is returned as end_del.
returns 1 on success; EOF on end-of-file.
See below for usage example
================================================================================ */
int xfp_token( FILE *fp, char *delims, char **tokp, int *end_del )
{
char *d, *tok;
int c, sz;
static int tok_sz;
if( *tokp == NULL ) {
tok_sz = 32;
*tokp = (char *) xmalloc( tok_sz * sizeof( char ) );
}
tok = *tokp;
while( EOF != ( c = getc( fp )) ){
for( d=delims; *d; ++d )
if( *d == (char)c ) break;
if( ! *d ) break;
}
if( c == EOF ) return EOF;
sz = 1;
*tok++ = (char) c;
while( EOF != ( c= getc( fp )) ){
for( d=delims; *d; ++d )
if( *d == (char) c ) break;
if( *d ) break;
else {
if( sz >= tok_sz -1 ) { /* Make sure there's space for NULL termination */
tok_sz *= 2;
*tokp = (char *) xrealloc( *tokp, tok_sz * sizeof( char ) );
tok = *tokp + sz; /* Advance tok pointer to position before realloc */
}
*tok++ = (char) c;
sz++;
}
}
*tok = (char) NULL;
*end_del = (int) *d;
return 1;
}
/* ================================================================================ */
#ifdef Test_xfp_token
main( int argc, char* argv[] ){
char *tok;
int end_tok;
FILE *fd;
tok = NULL;
fd = fopen( argv[1], "r" );
while( -1 != xfp_token( fd, " \t\n\r", &tok, &end_tok ) ) /* \r is carriage return*/
printf( "RETURNED_TOK '%s'\n'%x'\n", tok, end_tok );
xfree( tok );
}
#endif