]> git.lyx.org Git - lyx.git/blob - src/lastfiles.C
d2005145b20e92cecb8b03a822ad4b7d4488266d
[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 "debug.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 << "LyX: lastfiles: too many files\n"
45                         "\tdefault (=" << int(DEFAULTFILES) // int() only because of anon enum
46                        << ") used." << endl;
47                 num_files = DEFAULTFILES;
48         }
49 }
50
51
52 void LastFiles::readFile(string const & filename)
53 {
54         // we will not complain if we can't find filename nor will
55         // we issue a warning. Lgb.
56         LyXLex lex(0, 0); /* LyXLex should be changed
57                               * to allow constructor with
58                               * no parameters. */
59         bool error = false;
60
61         lex.setFile(filename);
62
63         if (!lex.IsOK()) return;
64
65         string tmp;
66         FileInfo fileInfo;
67         int i = 0;
68
69         while (lex.IsOK() && !error && i < num_files) {
70                 switch(lex.lex()) {
71                 case LyXLex::LEX_FEOF:
72                         error = true;
73                         break;
74                 default:
75                         tmp = lex.GetString();
76                         // Check if the file exist
77                         if (dostat) {
78                                 if (!(fileInfo.newFile(tmp).exist() &&
79                                       fileInfo.isRegular()))
80                                         break; // the file does not exist
81                         }
82                         files[i] = tmp;
83                         i++;
84                         break;
85                 }
86         }
87 }
88
89
90 void LastFiles::writeFile(string const & filename) const
91 {
92         FilePtr fd(filename, FilePtr::write);
93         if (fd()) {
94                 for (int i = 0; i < num_files; i++) {
95                         if (!files[i].empty())
96                                 fprintf(fd, "\"%s\"\n", files[i].c_str());
97                 }
98         } else
99                 lyxerr << "LyX: Warning: unable to save LastFiles: "
100                        << filename << endl;
101 }
102
103
104 void LastFiles::newFile(string const & file)
105 {
106         int n;
107         // Find this file in list. If not in list, point to last entry
108         for(n = 0; n < (num_files - 1); n++)
109                 if(files[n] == file) break;
110
111         for(int i = n; i >= 1; i--)
112                 files[i] = files[i - 1];
113         files[0] = file;
114 }