]> git.lyx.org Git - lyx.git/blob - src/lastfiles.C
Move some using directives at the beginninc of files
[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 using std::copy;
22 using std::ostream_iterator;
23
24 #include <algorithm>
25 using std::find;
26
27 #include "support/FileInfo.h"
28 #include "lastfiles.h"
29 #include "debug.h"
30
31 LastFiles::LastFiles(string const & filename, bool st, unsigned int num)
32         : dostat(st)
33 {
34         setNumberOfFiles(num);
35         readFile(filename);
36 }
37
38
39 void LastFiles::setNumberOfFiles(unsigned int no)
40 {
41         if (0 < no && no <= ABSOLUTEMAXLASTFILES)
42                 num_files = no;
43         else {
44                 lyxerr << "LyX: lastfiles: too many files\n"
45                         "\tdefault (=" << int(DEFAULTFILES)
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         ifstream ifs(filename.c_str());
57         string tmp;
58         FileInfo fileInfo;
59
60         while(getline(ifs, tmp) && files.size() < num_files) {
61                 if (dostat) {
62                         if (!(fileInfo.newFile(tmp).exist() &&
63                               fileInfo.isRegular()))
64                                 continue;
65                 }
66                 files.push_back(tmp);
67         }
68 }
69
70
71 void LastFiles::writeFile(string const & filename) const
72 {
73         ofstream ofs(filename.c_str());
74         if (ofs) {
75 #if 0
76                 for (Files::const_iterator cit = files.begin();
77                      cit != files.end();
78                      ++cit) {
79                         ofs << (*cit) << '\n';
80                 }
81 #else
82                 // Ok, ok. It is not required to do it this way...but it
83                 // is kindo nice and shows the versiality of iterators and
84                 // algorithms. I'll leave this in, and if I get reports
85                 // about compilations errors I take it out again before
86                 // 1.1.4. (Lgb)
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 }