]> git.lyx.org Git - lyx.git/blob - src/lastfiles.C
a8832032788662107f74975e006467d13b4fe768
[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 <fstream>
19 using std::ifstream;
20 using std::ofstream;
21
22 #include <algorithm>
23 using std::find;
24
25 #include "support/FileInfo.h"
26 #include "lastfiles.h"
27 #include "debug.h"
28
29 LastFiles::LastFiles(string const & filename, bool st, unsigned int num)
30         : dostat(st)
31 {
32         setNumberOfFiles(num);
33         readFile(filename);
34 }
35
36
37 void LastFiles::setNumberOfFiles(unsigned int no)
38 {
39         if (0 < no && no <= ABSOLUTEMAXLASTFILES)
40                 num_files = no;
41         else {
42                 lyxerr << "LyX: lastfiles: too many files\n"
43                         "\tdefault (=" << int(DEFAULTFILES)
44                        << ") used." << endl;
45                 num_files = DEFAULTFILES;
46         }
47 }
48
49
50 void LastFiles::readFile(string const & filename)
51 {
52         // we will not complain if we can't find filename nor will
53         // we issue a warning. (Lgb)
54         ifstream ifs(filename.c_str());
55         string tmp;
56         FileInfo fileInfo;
57
58         while(getline(ifs, tmp) && files.size() < num_files) {
59                 if (dostat) {
60                         if (!(fileInfo.newFile(tmp).exist() &&
61                               fileInfo.isRegular()))
62                                 continue;
63                 }
64                 files.push_back(tmp);
65         }
66 }
67
68
69 void LastFiles::writeFile(string const & filename) const
70 {
71         ofstream ofs(filename.c_str());
72         if (ofs) {
73 #if 0
74                 for (Files::const_iterator cit = files.begin();
75                      cit != files.end();
76                      ++cit) {
77                         ofs << (*cit) << '\n';
78                 }
79 #else
80                 // Ok, ok. It is not required to do it this way...but it
81                 // is kindo nice and shows the versiality of iterators and
82                 // algorithms. I'll leave this in, and if I get reports
83                 // about compilations errors I take it out again before
84                 // 1.1.4. (Lgb)
85                 using std::copy;
86                 using std::ostream_iterator;
87                 copy(files.begin(), files.end(),
88                      ostream_iterator<string>(ofs, "\n"));
89 #endif
90         } else
91                 lyxerr << "LyX: Warning: unable to save LastFiles: "
92                        << filename << endl;
93 }
94
95
96 void LastFiles::newFile(string const & file)
97 {
98         // If file already exist, delete it and reinsert at front.
99         Files::iterator it = find(files.begin(), files.end(), file);
100         if (it != files.end())
101                 files.erase(it);
102         files.push_front(file);
103         if (files.size() > num_files)
104                 files.pop_back();
105 }
106
107
108 string LastFiles::operator[](unsigned int i) const
109 {
110         if (i < files.size())
111                 return files[i];
112         return string();
113 }