-
Notifications
You must be signed in to change notification settings - Fork 0
/
str_to_vals.c
226 lines (185 loc) · 4.82 KB
/
str_to_vals.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
/**************************************************************************
* str_to_vals.c: decodes a string into values, and loads memory addresses
*
* Examples of legal strings for this routine:
*
* "1 2 3 4 5"
* "1.0, 2.2, 19e9"
* "1*23.5, 7*1 13 12*3"
*
* Blanks, commas, tabs and newlines may delimit the values.
* The repeat count is optional, but must be greater than 0 if included.
* If the total number of entries is less than required, the sequence
* is repeated.
*
* $Id: str_to_vals.c 3058 2007-01-25 22:25:59Z rsregan $
*
$Revision: 3058 $
$Log: str_to_vals.c,v $
Revision 1.7 1996/02/19 20:01:17 markstro
Now lints pretty clean
Revision 1.6 1994/11/23 20:12:59 markstro
More malloc_dbg changes
* Revision 1.5 1994/11/22 17:20:37 markstro
* (1) Cleaned up dimensions and parameters.
* (2) Some changes due to use of malloc_dbg.
*
* Revision 1.4 1994/11/08 16:17:51 markstro
* (1) More proto type fine tuning
* (2) fixed up data file reading
*
* Revision 1.3 1994/09/30 14:55:25 markstro
* Initial work on function prototypes.
*
* Revision 1.2 1994/01/31 20:17:39 markstro
* Make sure that all source files have CVS log.
*
**************************************************************************/
#define STR_TO_VALS_C
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include <stdlib.h>
#include "mms.h"
#define S2V_ERROR 1l
#define S2V_SUCCESS 0l
/*--------------------------------------------------------------------*\
| FUNCTION : str_to_vals
| COMMENT :
| PARAMETERS :
| RETURN VALUE :
| RESTRICTIONS :
\*--------------------------------------------------------------------*/
long str_to_vals (char *encoded_string, long size, long type, char *store_addr) {
long i, isource;
long ndecoded, repeat;
char *scopy, *token, *valstr, *asterisk, *end_point;
char tcopy[MAXTOKLEN];
double dvalue, *dval;
float fvalue, *fval;
long lvalue, *lval;
/*
* set up pointer for data type
*/
switch (type) {
case M_DOUBLE:
dval = (double *) store_addr;
break;
case M_FLOAT:
fval = (float *) store_addr;
break;
case M_LONG:
lval = (long *) store_addr;
break;
}
/*
* copy encoded_string before tokenizing
*/
scopy = strdup (encoded_string);
token = strtok (scopy, " ,\t\n");
ndecoded = 0;
while (token != NULL) {
(void)strcpy(tcopy, token);
asterisk = strrchr(tcopy, '*'); /* search for '*' */
if (asterisk == NULL ) { /* no repeat count */
valstr = tcopy;
repeat = 1;
} else {
valstr = asterisk + 1;
*asterisk = '\0'; /* terminate repeat count str */
repeat = strtol(tcopy, &end_point, 10l);
if (repeat <= 0 || *end_point != '\0') {
(void)fprintf(stderr,
"ERROR - str_to_vals - decoding string into values.\n");
(void)fprintf(stderr, "Illegal repeat count.\n");
return S2V_ERROR;
}
}
/*
* set errno to 0 so that previous errors are cancelled
*/
errno = 0;
switch (type) {
case M_DOUBLE:
dvalue = strtod(valstr, &end_point);
break;
case M_FLOAT:
fvalue = (float) strtod(valstr, &end_point);
break;
case M_LONG:
lvalue = strtol(valstr, &end_point, 10);
break;
}
if (errno == EDOM) {
(void)fprintf(stderr,
"ERROR - str_to_vals - decoding string into values.\n");
(void)fprintf(stderr, "Illegal value.\n");
return S2V_ERROR;
}
if (errno == ERANGE) {
(void)fprintf(stderr,
"ERROR - str_to_vals - decoding string into values.\n");
(void)fprintf(stderr, "Value out of range.\n");
return S2V_ERROR;
}
if (ndecoded + repeat > size) {
repeat = size - ndecoded;
}
switch (type) {
case M_DOUBLE:
for (i = 0; i < repeat; i++) {
dval[ndecoded] = dvalue;
ndecoded++;
}
break;
case M_FLOAT:
for (i = 0; i < repeat; i++) {
fval[ndecoded] = fvalue;
ndecoded++;
}
break;
case M_LONG:
for (i = 0; i < repeat; i++) {
lval[ndecoded] = lvalue;
ndecoded++;
}
break;
}
token = strtok(NULL, " ,\n\t");
}
/*
* If too few elements decoded, repeat the sequence
*/
if (ndecoded < size) {
isource = 0;
switch (type) {
case M_DOUBLE:
for (i = ndecoded; i < size; i++) {
dval[i] = dval[isource];
isource++;
if (isource == ndecoded)
isource = 0;
}
break;
case M_FLOAT:
for (i = ndecoded; i < size; i++) {
fval[i] = fval[isource];
isource++;
if (isource == ndecoded)
isource = 0;
}
break;
case M_LONG:
for (i = ndecoded; i < size; i++) {
lval[i] = lval[isource];
isource++;
if (isource == ndecoded)
isource = 0;
}
break;
}
}
//ufree(scopy);
return S2V_SUCCESS;
}