Changeset 39217
- Timestamp:
- Dec 3, 2015, 4:06:46 PM (11 years ago)
- File:
-
- 1 edited
-
trunk/Ohana/src/opihi/cmd.data/read_vectors.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/opihi/cmd.data/read_vectors.c
r38989 r39217 41 41 time_t TimeReference; 42 42 int i, j, Nskip, Narg, IsCSV, VERBOSE; 43 int Nbytes, N start, NELEM, Nelem, nread;43 int Nbytes, NELEM, nread; 44 44 char *colstr, *c0, *c1, *extname; 45 45 char varname[1024]; // used as a buffer for the names of string fields … … 188 188 } 189 189 190 // we allocate one extra byte into which we never read so there will always be a NULL terminating the string 190 191 ALLOCATE (buffer, char, 0x10001); 191 192 bzero (buffer, 0x10001); 192 193 /*194 for (i = 0; i < Nskip; i++) {195 if (scan_line_maxlen (f, buffer, 0x10000) == EOF) {196 gprint (GP_ERR, "problem reading file %s\n", filename);197 read_vectors_cleanup();198 return FALSE;199 }200 }201 */202 193 203 194 int Nline_read = 0; // track number of lines read so far (use to skip lines as well) … … 209 200 // we treat \n\r pair as a single EOL char to handle mac files: 210 201 211 Nstart = 0; // location of the last valid byte in the buffer (start filling here) 212 Nelem = 0; // number of valid rows read (vector elements) 213 while (TRUE) { 202 int Nstart = 0; // location of the last valid byte in the buffer (start filling here) 203 int Nelem = 0; // number of valid rows read (vector elements) 204 int EndOfFile = FALSE; 205 while (!EndOfFile) { 214 206 Nbytes = 0x10000 - Nstart; 215 bzero (&buffer[Nstart], Nbytes); 207 // we have allocated one extra byte into which we never read so there will always be a NULL terminating the string 208 bzero (&buffer[Nstart], Nbytes + 1); 216 209 nread = fread (&buffer[Nstart], 1, Nbytes, f); 217 210 if (ferror (f)) { … … 219 212 break; 220 213 } 221 if (nread == 0) break; // end of the file 222 // nbytes = nread + Nstart; 214 // we still need to parse the rest of the buffer, but there might not be an EOL on the last line 215 if (nread == 0) { 216 EndOfFile = TRUE; 217 } 223 218 224 219 int bufferStatus = TRUE; … … 227 222 c1 = strchr (c0, '\n'); // find the end of this current line (also valid for a Mac: \r\n) 228 223 if (!c1) { 229 c1 = strchr (c0, '\r'); // try \r for non-UNIXfiles230 } 231 if ( c1 == (char *) NULL) {224 c1 = strchr (c0, '\r'); // try \r for Windows files 225 } 226 if (!c1) { 232 227 Nstart = strlen (c0); 233 memmove (buffer, c0, Nstart); 234 bufferStatus = FALSE; 235 continue; 228 if (EndOfFile) { 229 // if we have reached EOF, we need to do one last pass in case there is a line without a return 230 c1 = c0 + Nstart; 231 bufferStatus = FALSE; 232 } else { 233 // if we have not reached EOF, we need to shift the buffer to the start of this line and read more data 234 memmove (buffer, c0, Nstart); 235 bufferStatus = FALSE; 236 continue; 237 } 236 238 } 237 239 *c1 = 0; // mark the end of the line 238 240 Nline_read ++; 239 241 240 if (Nline_read <= Nskip) { c0 = c1 + 1; continue; }241 242 if (*c0 == '#') { c0 = c1 + 1;continue; }243 if (*c0 == '!') { c0 = c1 + 1;continue; }242 // skip to the next line (but if EOF, do not overrun buffer) 243 if (Nline_read <= Nskip) { if (!EndOfFile) { c0 = c1 + 1; } continue; } 244 if (*c0 == '#') { if (!EndOfFile) { c0 = c1 + 1; } continue; } 245 if (*c0 == '!') { if (!EndOfFile) { c0 = c1 + 1; } continue; } 244 246 245 247 // parse the vectors in this line. this code is a bit inefficient: each column … … 319 321 } 320 322 } 321 c0 = c1 + 1; 323 if (!EndOfFile) { 324 c0 = c1 + 1; 325 } 322 326 } 323 327 }
Note:
See TracChangeset
for help on using the changeset viewer.
