Changeset 1456 for trunk/gsdl/lib


Ignore:
Timestamp:
2000-08-27T23:08:31+12:00 (24 years ago)
Author:
stefan
Message:

Support for collectoraction

Location:
trunk/gsdl/lib
Files:
4 edited

Legend:

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

    r1438 r1456  
    5656    here++;
    5757    path1.erase(here,path1.end());
    58 
     58   
    5959    // add one final slash
    6060#ifdef __WIN32__
     
    7474 
    7575  text_t fullpath = path1 + path2;
    76 
     76 
    7777  // make sure all the right slashes are used
    7878  here = fullpath.begin();
     
    103103}
    104104
     105text_t filename_cat (text_t path1, text_t path2, text_t path3, text_t path4,
     106             text_t path5, text_t path6) {
     107  return filename_cat(filename_cat(path1,path2,path3,path4,path5),path6);
     108}
     109
    105110// returns true if filename can be opened
    106111bool file_exists (const text_t &filename) {
     
    112117#endif
    113118  delete cstr;
    114 
     119 
    115120  if (filestream) {
    116121    // file exists
     
    118123    return true;
    119124  }
    120 
     125 
    121126  // file does not exist
    122127  return false;
     
    132137#endif
    133138  delete cstr;
    134 
     139 
    135140  if (filestream) {
    136141    // file exists
     
    138143    return true;
    139144  }
    140 
     145 
    141146  // file does not exist
    142147  return false;
     
    156161  hSearch = FindFirstFile(dirpath, &FileData);
    157162  delete dirpath;
    158 
     163 
    159164  if (hSearch == INVALID_HANDLE_VALUE) {
    160165    return false;
     
    172177  hSearch = FindFirstFile(dirpath, &FileData);
    173178  delete dirpath;
    174 
     179 
    175180  if (hSearch == INVALID_HANDLE_VALUE) {
    176181    return false;
     
    198203
    199204bool directory_exists (const text_t &dirname) {
    200 
     205 
    201206  char *tmp = dirname.getcstr();
    202207  DIR *dirin = opendir (tmp);
     
    209214
    210215bool read_dir (const text_t &dirname, text_tarray &filelist) {
    211 
     216 
    212217  filelist.erase (filelist.begin(), filelist.end());
    213218 
     
    219224 
    220225  dirent *dirp;
    221 
     226 
    222227  text_t filename;
    223228  while ((dirp = readdir (dirin)) != NULL) {
     
    234239
    235240// I'm sure there's a better way to do this - for now I don't have
    236 // time to find it though. -- This will probably break compiling
    237 // under windows ports of gcc
    238 void file_copy (const text_t &fromfile, const text_t &tofile) {
    239 
    240 #ifdef __WIN32__
     241// time to find it though
     242// returns true if things look like they happened ok
     243bool file_copy (const text_t &fromfile, const text_t &tofile) {
     244 
    241245  char *fromfilec = fromfile.getcstr();
    242246  char *tofilec = tofile.getcstr();
    243   CopyFile (fromfilec, tofilec, FALSE);
     247  bool fail = false;
     248  ifstream from (fromfilec);
     249  if (!from) {
     250    fail = true;
     251  } else {
     252    ofstream to (tofilec);
     253    if (!to) {
     254      fail = true;
     255      from.close();
     256    } else {
     257      char c;
     258      from.get(c);
     259      while (!from.eof ()) {
     260    to.put(c);
     261    from.get(c);
     262      }
     263      from.close();
     264      to.close();
     265    }
     266  }
    244267  delete fromfilec;
    245268  delete tofilec;
    246  
    247 #else
    248   text_t cp_cmd = "cp " + fromfile + " " + tofile;
    249   char *cp_cmdc = cp_cmd.getcstr();
    250   system (cp_cmdc);
    251   delete cp_cmdc;
    252  
    253 #endif
    254 }
     269  if (fail) return false;
     270  return true;
     271}
     272
     273/*
     274
     275#ifdef __WIN32__
     276char *fromfilec = fromfile.getcstr();
     277char *tofilec = tofile.getcstr();
     278CopyFile (fromfilec, tofilec, FALSE);
     279delete fromfilec;
     280delete tofilec;
     281
     282#else
     283text_t cp_cmd = "cp " + fromfile + " " + tofile;
     284char *cp_cmdc = cp_cmd.getcstr();
     285system (cp_cmdc);
     286delete cp_cmdc;
     287
     288#endif
     289*/
    255290
    256291// returns the last line (or last 256 characters)
     
    258293text_t file_tail (const text_t &filename) {
    259294
    260   text_t return_str;
     295  text_t return_str, tmpstr;
    261296  char *filenamec = filename.getcstr();
     297  char linec[256];
     298
    262299
    263300  ifstream file_in (filenamec);
     301  delete filenamec;
    264302  if (file_in) {
    265303    file_in.seekg (-256, ios::end);
    266     char c;
    267     file_in.get(c);
    268     while (!file_in.eof()) {
    269       if (c == '\n') {
    270     file_in.get(c);
    271     if (!file_in.eof()) {
    272       return_str.clear();
    273       if (c == '\\') return_str.push_back ('\\');
    274       return_str.push_back (c);
    275     }
    276       } else {
    277     if (c == '\\') return_str.push_back ('\\');
    278     return_str.push_back (c);
    279       }
    280       file_in.get(c);
     304    file_in.getline (linec, 256, EOF);
     305    file_in.close();
     306
     307    tmpstr.setcstr (linec);
     308    text_t::const_iterator here = tmpstr.begin();
     309    text_t::const_iterator end = tmpstr.end();
     310    while (here != end) {
     311      // handle trailing carriage returns
     312      while (*here == '\n') {
     313    here++;
     314    if (here == end) return return_str;
     315    else if (*here != '\n') return_str.clear();
     316      }
     317      if (*here == '\\') return_str.push_back ('\\');
     318      return_str.push_back (*here);
     319      here ++;
    281320    }
    282     file_in.close();
    283 
    284   }
    285   delete filenamec;
     321  }
    286322  return return_str;
    287323}
     324
     325// probably need windows version of this
     326#include <sys/stat.h>
     327#include <sys/types.h>
     328#include <fcntl.h>
     329#include <unistd.h>
     330
     331// returns true if directory was created successfully
     332bool mk_dir (const text_t &dirname) {
     333  mode_t mode = 0777;
     334  char *dirnamec = dirname.getcstr();
     335  int rv = mkdir (dirnamec, mode);
     336  delete dirnamec;
     337  if (rv == 0) return true;
     338  return false;
     339}
     340
  • trunk/gsdl/lib/fileutil.h

    r1437 r1456  
    3737text_t filename_cat (text_t path1, text_t path2, text_t path3, text_t path4,
    3838             text_t path5);
     39text_t filename_cat (text_t path1, text_t path2, text_t path3, text_t path4,
     40             text_t path5, text_t path6);
    3941
    4042// returns true if filename can be opened
     
    4951bool read_dir (const text_t &dirname, text_tarray &filelist);
    5052
    51 void file_copy (const text_t &fromfile, const text_t &tofile);
     53bool file_copy (const text_t &fromfile, const text_t &tofile);
    5254
    5355text_t file_tail (const text_t &filename);
    5456
     57// returns true if directory created successfully
     58bool mk_dir (const text_t &dirname);
     59
    5560#endif
  • trunk/gsdl/lib/gsdltools.cpp

    r1310 r1456  
    8484}
    8585
     86#else
     87void gsdl_system (char *cmd, ostream &logout) {
     88
     89  if (cmd == NULL) return;
     90  int pid = fork();
     91  if (pid == -1) return;
     92  if (pid == 0) {
     93    // child process
     94    char *argv[4];
     95    argv[0] = "sh";
     96    argv[1] = "-c";
     97    argv[2] = cmd;
     98    argv[3] = 0;
     99    execv("/bin/sh", argv);
     100    //    exit(127);
     101  }
     102}
     103
    86104#endif
  • trunk/gsdl/lib/gsdltools.h

    r1310 r1456  
    5050// system call and putting the spawned process in the background
    5151// (e.g. system (funcname options &);
    52 #if defined (__WIN32__)
     52//#if defined (__WIN32__)
    5353void gsdl_system (char *cmd, ostream &logout);
    54 #endif
     54//#endif
    5555
    5656#endif
Note: See TracChangeset for help on using the changeset viewer.