home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / lyx-0.13.2.tar.gz / lyx-0.13.2.tar / lyx-0.13.2 / src / lastfiles.C < prev    next >
C/C++ Source or Header  |  1998-04-23  |  3KB  |  121 lines

  1. /* This file is part of
  2. * ======================================================
  3. *           LyX, The Document Processor
  4. *      
  5. *        Copyright (C) 1995 Matthias Ettrich
  6. *           Copyright (C) 1995-1998 The LyX Team.
  7. *
  8. *======================================================*/
  9.  
  10. #include <config.h>
  11.  
  12.  
  13. #ifdef __GNUG__
  14. #pragma implementation
  15. #endif
  16.  
  17. //#include "definitions.h"
  18. #include "lyxlex.h"
  19. #include "FileInfo.h"
  20. #include "lastfiles.h"
  21. #include "filetools.h"
  22. #include "error.h"
  23.  
  24. //     $Id: lastfiles.C,v 1.1.1.1 1998/04/23 16:02:52 larsbj Exp $    
  25.  
  26. #if !defined(lint) && !defined(WITH_WARNINGS)
  27. static char vcid[] = "$Id: lastfiles.C,v 1.1.1.1 1998/04/23 16:02:52 larsbj Exp $";
  28. #endif /* lint */
  29.  
  30. LastFiles::LastFiles(LString const & filename, bool st, char num)
  31.     : dostat(st)
  32. {
  33.     setNumberOfFiles(num);
  34.     files = new LString[num_files];
  35.     readFile(filename);
  36. }
  37.  
  38.  
  39. LastFiles::~LastFiles()
  40. {
  41.     delete[] files;
  42. }
  43.  
  44.  
  45. void LastFiles::setNumberOfFiles(char no)
  46. {
  47.     if (1 <= no && no <= ABSOLUTEMAXLASTFILES)
  48.         num_files = no;
  49.     else {
  50.         lyxerr.print(LString("LyX: lastfiles: too many files\n"
  51.             "\tdefault (=") + int(DEFAULTFILES) + ") used.");
  52.         num_files = DEFAULTFILES;
  53.     }
  54. }
  55.  
  56.  
  57. void LastFiles::readFile(LString const & filename)
  58. {
  59.     // we will not complain if we can't find filename nor will
  60.     // we issue a warning. Lgb.
  61.     LyXLex lex(NULL, 0); /* LyXLex should be changed
  62.                   * to allow constructor with
  63.                   * no parameters. */
  64.     bool error = false;
  65.  
  66.     lex.setFile(filename);
  67.  
  68.     if (!lex.IsOK()) return;
  69.  
  70.     LString tmp;
  71.     FileInfo fileInfo;
  72.     int i = 0;
  73.  
  74.     while (lex.IsOK() && !error && i < num_files) {
  75.         switch(lex.lex()) {
  76.         case LyXLex::LEX_FEOF:
  77.             error = true;
  78.             break;
  79.         default:
  80.             tmp = lex.GetString();
  81.             // Check if the file exist
  82.             if (dostat) {
  83.                 if (!(fileInfo.newFile(tmp).exist() &&
  84.                       fileInfo.isRegular()))
  85.                     break; // the file does not exist
  86.             }
  87.             files[i] = tmp;
  88.             i++;
  89.             break;
  90.         }
  91.     }
  92. }
  93.  
  94.  
  95. void LastFiles::writeFile(LString const & filename) const
  96. {
  97.     FilePtr fd(filename, FilePtr::write);
  98.     if (fd()) {
  99.          for (int i = 0; i < num_files; i++) {
  100.              if (!files[i].empty())
  101.                  fprintf(fd, "\"%s\"\n", files[i].c_str());
  102.          }
  103.     } else
  104.         lyxerr.print("LyX: Warning: unable to save LastFiles: "
  105.                   +    filename);
  106. }
  107.  
  108.  
  109. void LastFiles::newFile(LString const & file)
  110. {
  111.     int n;
  112.     // Find this file in list. If not in list, point to last entry
  113.     for(n = 0; n < (num_files - 1); n++)
  114.         if(files[n] == file) break;
  115.  
  116.     for(int i = n; i >= 1; i--)
  117.         files[i] = files[i - 1];
  118.     files[0] = file;
  119. }
  120.