Changeset 1813 for trunk/gsdl/lib


Ignore:
Timestamp:
2000-12-19T16:54:36+13:00 (23 years ago)
Author:
sjboddie
Message:

Fixed a bug in the file_tail function. I'm still not sure it was an
actual bug but it sure made weird things happen on Windows 95

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/lib/fileutil.cpp

    r1782 r1813  
    212212  // freeing this memory can cause a runtime error on some (particularly
    213213  // debug) versions of VC++
    214   delete dirpath;
     214  //  delete dirpath;
    215215 
    216216  if (hSearch == INVALID_HANDLE_VALUE) {
     
    335335}
    336336
    337 
     337// This is a fairly quick and nasty attempt at doing a "tail" on
     338// a file (i.e. returning the last numlines lines of filename).
     339// It has one important limitation in that it expects lines to be
     340// 256 characters long on average.
     341// This of course makes it possible that it won't return numlines
     342// lines if there are some very long lines.
     343// It also makes it fairly inefficient as it always reads in
     344// numlines*256 characters which is much more than generally
     345// necessary.
     346// For current needs it's fine though.
    338347text_t file_tail (const text_t &filename, int numlines) {
     348
    339349  if (numlines < 1) numlines = 1;
    340   int numchars = 256*numlines;
     350  streampos numchars = 256*numlines;
    341351
    342352  text_tarray lines;
     
    348358  delete filenamec;
    349359  if (file_in) {
    350 
    351     // this should be here to keep things reasonably fast
    352     // when there's a long file to tail but I can't work out
    353     // how to tell when it's rewound past the beginning of the file
    354     // (which causes some problems)
    355 
    356     //    file_in.seekg (-numchars, ios::end);
     360    file_in.seekg (0, ios::end);
     361    streampos file_length = file_in.tellg();
     362    if (file_length < numchars) numchars = file_length;
     363    file_in.seekg (-numchars, ios::end);
    357364
    358365    while (!file_in.eof()) {
     
    384391}
    385392
    386 // returns the last numlines lines (or last numlines*256
    387 // characters) of file
    388 /*
    389 text_t file_tail (const text_t &filename) {
    390 
    391 
    392   text_t return_str, tmpstr;
    393   char *filenamec = filename.getcstr();
    394   char linec[256];
    395 
    396   ifstream file_in (filenamec);
    397   delete filenamec;
    398   if (file_in) {
    399     file_in.seekg (-256, ios::end);
    400     file_in.getline (linec, 256, EOF);
    401     file_in.close();
    402 
    403     tmpstr.setcstr (linec);
    404     text_t::const_iterator here = tmpstr.begin();
    405     text_t::const_iterator end = tmpstr.end();
    406     while (here != end) {
    407       // handle trailing carriage returns
    408       while (*here == '\n') {
    409     here++;
    410     if (here == end) return return_str;
    411     else if (*here != '\n') return_str.clear();
    412       }
    413       if (*here == '\\') return_str.push_back ('\\');
    414       return_str.push_back (*here);
    415       here ++;
    416     }
    417   }
    418   return return_str;
    419 }
    420 */
    421393#ifdef __WIN32__
    422394
Note: See TracChangeset for help on using the changeset viewer.