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