]> git.lyx.org Git - lyx.git/blob - src/lastfiles.C
6497f8998ec51deaef11072e8880b980d539c722
[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                 for (Files::const_iterator cit = files.begin();
74                      cit != files.end();
75                      ++cit) {
76                         ofs << (*cit) << '\n';
77                 }
78         } else
79                 lyxerr << "LyX: Warning: unable to save LastFiles: "
80                        << filename << endl;
81 }
82
83
84 void LastFiles::newFile(string const & file)
85 {
86         // If file already exist, delete it and reinsert at front.
87         Files::iterator it = find(files.begin(), files.end(), file);
88         if (it != files.end())
89                 files.erase(it);
90         files.push_front(file);
91         if (files.size() > num_files)
92                 files.pop_back();
93 }
94
95
96 string LastFiles::operator[](unsigned int i) const
97 {
98         if (i < files.size())
99                 return files[i];
100         return string();
101 }