]> git.lyx.org Git - lyx.git/blob - src/lastfiles.C
2d327fbaa0688383c0ac91ed7174853672e886ee
[lyx.git] / src / lastfiles.C
1 /* This file is part of
2 * ======================================================
3
4 *           LyX, The Document Processor
5 *        
6 *           Copyright (C) 1995 Matthias Ettrich
7 *           Copyright (C) 1995-1998 The LyX Team.
8 *
9 *======================================================*/
10
11 #include <config.h>
12
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 //#include "definitions.h"
19 #include "lyxlex.h"
20 #include "FileInfo.h"
21 #include "lastfiles.h"
22 #include "filetools.h"
23 #include "error.h"
24
25 //      $Id: lastfiles.C,v 1.1 1999/09/27 18:44:37 larsbj Exp $ 
26
27 #if !defined(lint) && !defined(WITH_WARNINGS)
28 static char vcid[] = "$Id: lastfiles.C,v 1.1 1999/09/27 18:44:37 larsbj Exp $";
29 #endif /* lint */
30
31 LastFiles::LastFiles(LString const & filename, bool st, char num)
32         : dostat(st)
33 {
34         setNumberOfFiles(num);
35         files = new LString[num_files];
36         readFile(filename);
37 }
38
39
40 LastFiles::~LastFiles()
41 {
42         delete[] files;
43 }
44
45
46 void LastFiles::setNumberOfFiles(char no)
47 {
48         if (1 <= no && no <= ABSOLUTEMAXLASTFILES)
49                 num_files = no;
50         else {
51                 lyxerr.print(LString("LyX: lastfiles: too many files\n"
52                         "\tdefault (=") + int(DEFAULTFILES) + ") used.");
53                 num_files = DEFAULTFILES;
54         }
55 }
56
57
58 void LastFiles::readFile(LString const & filename)
59 {
60         // we will not complain if we can't find filename nor will
61         // we issue a warning. Lgb.
62         LyXLex lex(NULL, 0); /* LyXLex should be changed
63                               * to allow constructor with
64                               * no parameters. */
65         bool error = false;
66
67         lex.setFile(filename);
68
69         if (!lex.IsOK()) return;
70
71         LString tmp;
72         FileInfo fileInfo;
73         int i = 0;
74
75         while (lex.IsOK() && !error && i < num_files) {
76                 switch(lex.lex()) {
77                 case LyXLex::LEX_FEOF:
78                         error = true;
79                         break;
80                 default:
81                         tmp = lex.GetString();
82                         // Check if the file exist
83                         if (dostat) {
84                                 if (!(fileInfo.newFile(tmp).exist() &&
85                                       fileInfo.isRegular()))
86                                         break; // the file does not exist
87                         }
88                         files[i] = tmp;
89                         i++;
90                         break;
91                 }
92         }
93 }
94
95
96 void LastFiles::writeFile(LString const & filename) const
97 {
98         FilePtr fd(filename, FilePtr::write);
99         if (fd()) {
100                 for (int i = 0; i < num_files; i++) {
101                         if (!files[i].empty())
102                                 fprintf(fd, "\"%s\"\n", files[i].c_str());
103                 }
104         } else
105                 lyxerr.print("LyX: Warning: unable to save LastFiles: "
106                               + filename);
107 }
108
109
110 void LastFiles::newFile(LString const & file)
111 {
112         int n;
113         // Find this file in list. If not in list, point to last entry
114         for(n = 0; n < (num_files - 1); n++)
115                 if(files[n] == file) break;
116
117         for(int i = n; i >= 1; i--)
118                 files[i] = files[i - 1];
119         files[0] = file;
120 }