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