]> git.lyx.org Git - lyx.git/blob - src/lastfiles.C
remove Last when NEW_INSETS is defined
[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-2000 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 #include <algorithm>
20
21 #include "support/FileInfo.h"
22 #include "lastfiles.h"
23 #include "debug.h"
24
25 using std::ifstream;
26 using std::getline;
27 using std::ofstream;
28 using std::copy;
29 using std::ostream_iterator;
30 using std::find;
31 using std::endl;
32
33 LastFiles::LastFiles(string const & filename, bool st, unsigned int num)
34         : dostat(st)
35 {
36         setNumberOfFiles(num);
37         readFile(filename);
38 }
39
40
41 void LastFiles::setNumberOfFiles(unsigned int no)
42 {
43         if (0 < no && no <= ABSOLUTEMAXLASTFILES)
44                 num_files = no;
45         else {
46                 lyxerr << "LyX: lastfiles: too many files\n"
47                         "\tdefault (=" << int(DEFAULTFILES)
48                        << ") used." << endl;
49                 num_files = DEFAULTFILES;
50         }
51 }
52
53
54 void LastFiles::readFile(string const & filename)
55 {
56         // we will not complain if we can't find filename nor will
57         // we issue a warning. (Lgb)
58         ifstream ifs(filename.c_str());
59         string tmp;
60         FileInfo fileInfo;
61
62         while(getline(ifs, tmp) && files.size() < num_files) {
63                 if (dostat) {
64                         if (!(fileInfo.newFile(tmp).exist() &&
65                               fileInfo.isRegular()))
66                                 continue;
67                 }
68                 files.push_back(tmp);
69         }
70 }
71
72
73 void LastFiles::writeFile(string const & filename) const
74 {
75         ofstream ofs(filename.c_str());
76         if (ofs) {
77                 copy(files.begin(), files.end(),
78                      ostream_iterator<string>(ofs, "\n"));
79         } else
80                 lyxerr << "LyX: Warning: unable to save LastFiles: "
81                        << filename << endl;
82 }
83
84
85 void LastFiles::newFile(string const & file)
86 {
87         // If file already exist, delete it and reinsert at front.
88         Files::iterator it = find(files.begin(), files.end(), file);
89         if (it != files.end())
90                 files.erase(it);
91         files.push_front(file);
92         if (files.size() > num_files)
93                 files.pop_back();
94 }
95
96
97 string const LastFiles::operator[](unsigned int i) const
98 {
99         if (i < files.size())
100                 return files[i];
101         return string();
102 }