IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 39217


Ignore:
Timestamp:
Dec 3, 2015, 4:06:46 PM (11 years ago)
Author:
eugene
Message:

handle the case of a file without a <return> on the last line

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/cmd.data/read_vectors.c

    r38989 r39217  
    4141  time_t TimeReference;
    4242  int i, j, Nskip, Narg, IsCSV, VERBOSE;
    43   int Nbytes, Nstart, NELEM, Nelem, nread;
     43  int Nbytes, NELEM, nread;
    4444  char *colstr, *c0, *c1, *extname;
    4545  char varname[1024];  // used as a buffer for the names of string fields
     
    188188  }
    189189 
     190  // we allocate one extra byte into which we never read so there will always be a NULL terminating the string
    190191  ALLOCATE (buffer, char, 0x10001);
    191192  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   */
    202193
    203194  int Nline_read = 0; // track number of lines read so far (use to skip lines as well)
     
    209200  // we treat \n\r pair as a single EOL char to handle mac files:
    210201
    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) {
    214206    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);
    216209    nread = fread (&buffer[Nstart], 1, Nbytes, f);
    217210    if (ferror (f)) {
     
    219212      break;
    220213    }
    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    }
    223218   
    224219    int bufferStatus = TRUE;
     
    227222      c1 = strchr (c0, '\n'); // find the end of this current line (also valid for a Mac: \r\n)
    228223      if (!c1) {
    229         c1 = strchr (c0, '\r'); // try \r for non-UNIX files
    230       }
    231       if (c1 == (char *) NULL) {
     224        c1 = strchr (c0, '\r'); // try \r for Windows files
     225      }
     226      if (!c1) {
    232227        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        }
    236238      }
    237239      *c1 = 0; // mark the end of the line
    238240      Nline_read ++;
    239241
    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; }
    244246
    245247      // parse the vectors in this line.  this code is a bit inefficient: each column
     
    319321        }
    320322      }
    321       c0 = c1 + 1;
     323      if (!EndOfFile) {
     324        c0 = c1 + 1;
     325      }
    322326    }
    323327  }
Note: See TracChangeset for help on using the changeset viewer.