]> git.lyx.org Git - lyx.git/blob - src/lastfiles.C
remove wrong sections
[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::ofstream;
27 using std::copy;
28 using std::ostream_iterator;
29 using std::find;
30 using std::endl;
31
32 LastFiles::LastFiles(string const & filename, bool st, unsigned int num)
33         : dostat(st)
34 {
35         setNumberOfFiles(num);
36         readFile(filename);
37 }
38
39
40 void LastFiles::setNumberOfFiles(unsigned int no)
41 {
42         if (0 < no && no <= ABSOLUTEMAXLASTFILES)
43                 num_files = no;
44         else {
45                 lyxerr << "LyX: lastfiles: too many files\n"
46                         "\tdefault (=" << int(DEFAULTFILES)
47                        << ") used." << endl;
48                 num_files = DEFAULTFILES;
49         }
50 }
51
52
53 void LastFiles::readFile(string const & filename)
54 {
55         // we will not complain if we can't find filename nor will
56         // we issue a warning. (Lgb)
57         ifstream ifs(filename.c_str());
58         string tmp;
59         FileInfo fileInfo;
60
61         while(getline(ifs, tmp) && files.size() < num_files) {
62                 if (dostat) {
63                         if (!(fileInfo.newFile(tmp).exist() &&
64                               fileInfo.isRegular()))
65                                 continue;
66                 }
67                 files.push_back(tmp);
68         }
69 }
70
71
72 void LastFiles::writeFile(string const & filename) const
73 {
74         ofstream ofs(filename.c_str());
75         if (ofs) {
76                 copy(files.begin(), files.end(),
77                      ostream_iterator<string>(ofs, "\n"));
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 }