]> git.lyx.org Git - lyx.git/blob - src/lastfiles.C
code cosmetics to the iterator fix
[lyx.git] / src / lastfiles.C
1 /**
2  * \file lastfiles.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "lastfiles.h"
14 #include "debug.h"
15
16 #include <boost/filesystem/operations.hpp>
17
18 #include <algorithm>
19 #include <fstream>
20 #include <iterator>
21
22 namespace fs = boost::filesystem;
23
24 using std::copy;
25 using std::endl;
26 using std::find;
27 using std::getline;
28 using std::string;
29 using std::ifstream;
30 using std::ofstream;
31 using std::ostream_iterator;
32
33
34 LastFiles::LastFiles(string const & filename, bool st, unsigned int num)
35         : dostat(st)
36 {
37         setNumberOfFiles(num);
38         readFile(filename);
39 }
40
41
42 void LastFiles::setNumberOfFiles(unsigned int no)
43 {
44         if (0 < no && no <= ABSOLUTEMAXLASTFILES)
45                 num_files = no;
46         else {
47                 lyxerr << "LyX: lastfiles: too many files\n"
48                         "\tdefault (=" << int(DEFAULTFILES)
49                        << ") used." << endl;
50                 num_files = DEFAULTFILES;
51         }
52 }
53
54
55 void LastFiles::readFile(string const & filename)
56 {
57         // we will not complain if we can't find filename nor will
58         // we issue a warning. (Lgb)
59         ifstream ifs(filename.c_str());
60         string tmp;
61
62         while (getline(ifs, tmp) && files.size() < num_files) {
63                 if (dostat && !fs::exists(tmp))
64                                 continue;
65                 files.push_back(tmp);
66         }
67 }
68
69
70 void LastFiles::writeFile(string const & filename) const
71 {
72         ofstream ofs(filename.c_str());
73         if (ofs) {
74                 copy(files.begin(), files.end(),
75                      ostream_iterator<string>(ofs, "\n"));
76         } else
77                 lyxerr << "LyX: Warning: unable to save LastFiles: "
78                        << filename << endl;
79 }
80
81
82 void LastFiles::newFile(string const & file)
83 {
84         // If file already exist, delete it and reinsert at front.
85         Files::iterator it = find(files.begin(), files.end(), file);
86         if (it != files.end())
87                 files.erase(it);
88         files.push_front(file);
89         if (files.size() > num_files)
90                 files.pop_back();
91 }
92
93
94 string const LastFiles::operator[](unsigned int i) const
95 {
96         if (i < files.size())
97                 return files[i];
98         return string();
99 }