Changeset 1485 for trunk/gsdl/lib


Ignore:
Timestamp:
2000-08-31T20:07:53+12:00 (24 years ago)
Author:
sjboddie
Message:

More improvements to collector

Location:
trunk/gsdl/lib
Files:
2 edited

Legend:

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

    r1476 r1485  
    280280}
    281281
    282 // returns the last line (or last 256 characters)
    283 // of filename file
     282
     283text_t file_tail (const text_t &filename, int numlines) {
     284  if (numlines < 1) numlines = 1;
     285  int numchars = 256*numlines;
     286
     287  text_tarray lines;
     288  text_t ret;
     289
     290  char *filenamec = filename.getcstr();
     291  char linec[256];
     292  ifstream file_in (filenamec);
     293  delete filenamec;
     294  if (file_in) {
     295
     296    // this should be here to keep things reasonably fast
     297    // when there's a long file to tail but I can't work out
     298    // how to tell when it's rewound past the beginning of the file
     299    // (which causes some problems)
     300
     301    //    file_in.seekg (-numchars, ios::end);
     302
     303    while (!file_in.eof()) {
     304      file_in.getline (linec, 256);
     305      ret.setcstr(linec);
     306      text_t::const_iterator here = ret.begin();
     307      text_t::const_iterator end = ret.end();
     308      // make sure line has content
     309      while (here != end) {
     310    if (*here != '\n' && *here != ' ') {
     311      lines.push_back (ret);
     312      break;
     313    }
     314    here ++;
     315      }
     316    }
     317    file_in.close();
     318  }
     319
     320  ret.clear();
     321  int numlinesgot = lines.size();
     322  int sindex = 0;
     323  if (numlinesgot > numlines) sindex = numlinesgot - numlines;
     324  for (int i = sindex; i < numlinesgot; i++) {
     325    ret += lines[i] + "\n";
     326  }
     327
     328  return ret;
     329}
     330
     331// returns the last numlines lines (or last numlines*256
     332// characters) of file
     333/*
    284334text_t file_tail (const text_t &filename) {
     335
    285336
    286337  text_t return_str, tmpstr;
    287338  char *filenamec = filename.getcstr();
    288339  char linec[256];
    289 
    290340
    291341  ifstream file_in (filenamec);
     
    313363  return return_str;
    314364}
    315 
     365*/
    316366#ifdef __WIN32__
    317367
     
    343393
    344394#endif
     395
     396// read in file from filename and load into content
     397bool read_file (const text_t &filename, text_t &content) {
     398
     399  content.clear();
     400
     401  char *filenamec = filename.getcstr();
     402#ifdef GSDL_USE_IOS_H
     403  ifstream file_in (filenamec, ios::in | ios::nocreate);
     404#else
     405  ifstream file_in (filenamec, ios::in);
     406#endif
     407  delete filenamec;
     408
     409  if (file_in) {
     410    char c;
     411    file_in.get(c);
     412    while (!file_in.eof ()) {
     413      content.push_back(c);
     414      file_in.get(c);
     415    }
     416    file_in.close();
     417  } else {
     418    return false;
     419  }
     420  return true;
     421}
  • trunk/gsdl/lib/fileutil.h

    r1456 r1485  
    5353bool file_copy (const text_t &fromfile, const text_t &tofile);
    5454
    55 text_t file_tail (const text_t &filename);
     55text_t file_tail (const text_t &filename, int numlines);
    5656
    5757// returns true if directory created successfully
    5858bool mk_dir (const text_t &dirname);
    5959
     60bool read_file (const text_t &filename, text_t &content);
     61
    6062#endif
Note: See TracChangeset for help on using the changeset viewer.