]> git.lyx.org Git - lyx.git/blob - src/Session.cpp
Avoid asserting when reading the session file
[lyx.git] / src / Session.cpp
1 /**
2  * \file Session.cpp
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  * \author Bo Peng
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "Session.h"
15
16 #include "support/debug.h"
17 #include "support/filetools.h"
18 #include "support/Package.h"
19
20 #include <fstream>
21 #include <sstream>
22 #include <algorithm>
23 #include <iterator>
24
25 using namespace std;
26 using namespace lyx::support;
27
28 namespace {
29
30 string const sec_lastfiles = "[recent files]";
31 string const sec_lastfilepos = "[cursor positions]";
32 string const sec_lastopened = "[last opened files]";
33 string const sec_bookmarks = "[bookmarks]";
34 string const sec_session = "[session info]";
35 string const sec_toolbars = "[toolbars]";
36 string const sec_lastcommands = "[last commands]";
37 string const sec_authfiles = "[auth files]";
38 string const sec_shellescape = "[shell escape files]";
39
40 } // namespace
41
42
43 namespace lyx {
44
45 LastFilesSection::LastFilesSection(unsigned int num) :
46         default_num_last_files(4),
47         absolute_max_last_files(100)
48 {
49         setNumberOfLastFiles(num);
50 }
51
52
53 void LastFilesSection::read(istream & is)
54 {
55         string tmp;
56         do {
57                 char c = is.peek();
58                 if (c == '[')
59                         break;
60                 getline(is, tmp);
61                 if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ' || !FileName::isAbsolute(tmp))
62                         continue;
63
64                 // read lastfiles
65                 FileName const file(tmp);
66                 if (file.exists() && !file.isDirectory()
67                     && lastfiles.size() < num_lastfiles)
68                         lastfiles.push_back(file);
69                 else
70                         LYXERR(Debug::INIT, "LyX: Warning: Ignore last file: " << tmp);
71         } while (is.good());
72 }
73
74
75 void LastFilesSection::write(ostream & os) const
76 {
77         os << '\n' << sec_lastfiles << '\n';
78         copy(lastfiles.begin(), lastfiles.end(),
79              ostream_iterator<FileName>(os, "\n"));
80 }
81
82
83 void LastFilesSection::add(FileName const & file)
84 {
85         // If file already exist, delete it and reinsert at front.
86         LastFiles::iterator it = find(lastfiles.begin(), lastfiles.end(), file);
87         if (it != lastfiles.end())
88                 lastfiles.erase(it);
89         lastfiles.insert(lastfiles.begin(), file);
90         if (lastfiles.size() > num_lastfiles)
91                 lastfiles.pop_back();
92 }
93
94
95 void LastFilesSection::setNumberOfLastFiles(unsigned int no)
96 {
97         if (0 < no && no <= absolute_max_last_files)
98                 num_lastfiles = no;
99         else {
100                 LYXERR(Debug::INIT, "LyX: session: too many last files\n"
101                         << "\tdefault (=" << default_num_last_files << ") used.");
102                 num_lastfiles = default_num_last_files;
103         }
104 }
105
106
107 void LastOpenedSection::read(istream & is)
108 {
109         string tmp;
110         do {
111                 char c = is.peek();
112                 if (c == '[')
113                         break;
114                 getline(is, tmp);
115                 if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ')
116                         continue;
117
118                 try {
119                         LastOpenedFile lof;
120                         istringstream itmp(tmp);
121                         itmp >> lof.active;
122                         itmp.ignore(2);  // ignore ", "
123                         string fname;
124                         getline(itmp, fname);
125                         if (!FileName::isAbsolute(fname))
126                                 continue;
127
128                         FileName const file(fname);
129                         if (file.exists() && !file.isDirectory()) {
130                                 lof.file_name = file;
131                                 lastopened.push_back(lof);
132                         } else {
133                                 LYXERR(Debug::INIT,
134                                         "LyX: Warning: Ignore last opened file: " << tmp);
135                         }
136                 } catch (...) {
137                         LYXERR(Debug::INIT,
138                                 "LyX: Warning: unknown state of last opened file: " << tmp);
139                 }
140         } while (is.good());
141 }
142
143
144 void LastOpenedSection::write(ostream & os) const
145 {
146         os << '\n' << sec_lastopened << '\n';
147         for (size_t i = 0; i < lastopened.size(); ++i)
148                 os << lastopened[i].active << ", " << lastopened[i].file_name << '\n';
149 }
150
151
152 void LastOpenedSection::add(FileName const & file, bool active)
153 {
154         LastOpenedFile lof(file, active);
155         // check if file is already recorded (this can happen
156         // with multiple buffer views). We do only record each
157         // file once, since we cannot restore multiple views
158         // currently, we even crash in some cases (see #9483).
159         // FIXME: Add session support for multiple views of
160         //        the same buffer (split-view etc.).
161         for (size_t i = 0; i < lastopened.size(); ++i) {
162                 if (lastopened[i].file_name == file)
163                         return;
164         }
165         lastopened.push_back(lof);
166 }
167
168
169 void LastOpenedSection::clear()
170 {
171         lastopened.clear();
172 }
173
174
175 void LastFilePosSection::read(istream & is)
176 {
177         string tmp;
178         do {
179                 char c = is.peek();
180                 if (c == '[')
181                         break;
182                 getline(is, tmp);
183                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
184                         continue;
185
186                 try {
187                         // read lastfilepos
188                         // pos, file\n
189                         FilePos filepos;
190                         string fname;
191                         istringstream itmp(tmp);
192                         itmp >> filepos.pit;
193                         itmp.ignore(2);  // ignore ", "
194                         itmp >> filepos.pos;
195                         itmp.ignore(2);  // ignore ", "
196                         getline(itmp, fname);
197                         if (!FileName::isAbsolute(fname))
198                                 continue;
199                         FileName const file(fname);
200                         if (file.exists() && !file.isDirectory()
201                             && lastfilepos.size() < num_lastfilepos)
202                                 lastfilepos[file] = filepos;
203                         else
204                                 LYXERR(Debug::INIT, "LyX: Warning: Ignore pos of last file: " << fname);
205                 } catch (...) {
206                         LYXERR(Debug::INIT, "LyX: Warning: unknown pos of last file: " << tmp);
207                 }
208         } while (is.good());
209 }
210
211
212 void LastFilePosSection::write(ostream & os) const
213 {
214         os << '\n' << sec_lastfilepos << '\n';
215         for (FilePosMap::const_iterator file = lastfilepos.begin();
216                 file != lastfilepos.end(); ++file) {
217                 os << file->second.pit << ", " << file->second.pos << ", "
218                    << file->first << '\n';
219         }
220 }
221
222
223 void LastFilePosSection::save(FileName const & fname, FilePos const & pos)
224 {
225         lastfilepos[fname] = pos;
226 }
227
228
229 LastFilePosSection::FilePos LastFilePosSection::load(FileName const & fname) const
230 {
231         FilePosMap::const_iterator entry = lastfilepos.find(fname);
232         // Has position information, return it.
233         if (entry != lastfilepos.end())
234                 return entry->second;
235         // Not found, return the first paragraph
236         return FilePos();
237 }
238
239
240 void BookmarksSection::clear()
241 {
242         // keep bookmark[0], the temporary one
243         bookmarks.resize(1);
244         bookmarks.resize(max_bookmarks + 1);
245 }
246
247
248 void BookmarksSection::read(istream & is)
249 {
250         string tmp;
251         do {
252                 char c = is.peek();
253                 if (c == '[')
254                         break;
255                 getline(is, tmp);
256                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
257                         continue;
258
259                 try {
260                         // read bookmarks
261                         // idx, pit, pos, file\n
262                         unsigned int idx;
263                         pit_type pit;
264                         pos_type pos;
265                         string fname;
266                         istringstream itmp(tmp);
267                         itmp >> idx;
268                         itmp.ignore(2);  // ignore ", "
269                         itmp >> pit;
270                         itmp.ignore(2);  // ignore ", "
271                         itmp >> pos;
272                         itmp.ignore(2);  // ignore ", "
273                         getline(itmp, fname);
274                         if (!FileName::isAbsolute(fname))
275                                 continue;
276                         FileName const file(fname);
277                         // only load valid bookmarks
278                         if (file.exists() && !file.isDirectory() && idx <= max_bookmarks)
279                                 bookmarks[idx] = Bookmark(file, pit, pos, 0, 0);
280                         else
281                                 LYXERR(Debug::INIT, "LyX: Warning: Ignore bookmark of file: " << fname);
282                 } catch (...) {
283                         LYXERR(Debug::INIT, "LyX: Warning: unknown Bookmark info: " << tmp);
284                 }
285         } while (is.good());
286 }
287
288
289 void BookmarksSection::write(ostream & os) const
290 {
291         os << '\n' << sec_bookmarks << '\n';
292         for (size_t i = 0; i <= max_bookmarks; ++i) {
293                 if (isValid(i))
294                         os << i << ", "
295                            << bookmarks[i].bottom_pit << ", "
296                            << bookmarks[i].bottom_pos << ", "
297                            << bookmarks[i].filename << '\n';
298         }
299 }
300
301
302 void BookmarksSection::save(FileName const & fname,
303         pit_type bottom_pit, pos_type bottom_pos,
304         int top_id, pos_type top_pos, unsigned int idx)
305 {
306         // silently ignore bookmarks when idx is out of range
307         if (idx <= max_bookmarks)
308                 bookmarks[idx] = Bookmark(fname, bottom_pit, bottom_pos, top_id, top_pos);
309 }
310
311
312 bool BookmarksSection::isValid(unsigned int i) const
313 {
314         return i <= max_bookmarks && !bookmarks[i].filename.empty();
315 }
316
317
318 bool BookmarksSection::hasValid() const
319 {
320         for (size_t i = 1; i <= size(); ++i) {
321                 if (isValid(i))
322                         return true;
323         }
324         return false;
325 }
326
327
328 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
329 {
330         return bookmarks[i];
331 }
332
333
334 LastCommandsSection::LastCommandsSection(unsigned int num) :
335         default_num_last_commands(30),
336         absolute_max_last_commands(100)
337 {
338         setNumberOfLastCommands(num);
339 }
340
341
342 void LastCommandsSection::read(istream & is)
343 {
344         string tmp;
345         do {
346                 char c = is.peek();
347                 if (c == '[')
348                         break;
349                 getline(is, tmp);
350                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
351                         continue;
352
353                 lastcommands.push_back(tmp);
354         } while (is.good());
355 }
356
357
358 void LastCommandsSection::write(ostream & os) const
359 {
360         os << '\n' << sec_lastcommands << '\n';
361         copy(lastcommands.begin(), lastcommands.end(),
362                 ostream_iterator<std::string>(os, "\n"));
363 }
364
365
366 void LastCommandsSection::setNumberOfLastCommands(unsigned int no)
367 {
368         if (0 < no && no <= absolute_max_last_commands)
369                 num_lastcommands = no;
370         else {
371                 LYXERR(Debug::INIT, "LyX: session: too many last commands\n"
372                         << "\tdefault (=" << default_num_last_commands << ") used.");
373                 num_lastcommands = default_num_last_commands;
374         }
375 }
376
377
378 void LastCommandsSection::add(std::string const & string)
379 {
380         lastcommands.push_back(string);
381 }
382
383
384 void LastCommandsSection::clear()
385 {
386         lastcommands.clear();
387 }
388
389
390 Session::Session(unsigned int num_last_files, unsigned int num_last_commands) :
391         last_files(num_last_files), last_commands(num_last_commands)
392 {
393         // locate the session file
394         // note that the session file name 'session' is hard-coded
395         session_file = FileName(addName(package().user_support().absFileName(), "session"));
396         //
397         readFile();
398 }
399
400
401 void Session::readFile()
402 {
403         // we will not complain if we can't find session_file nor will
404         // we issue a warning. (Lgb)
405         ifstream is(session_file.toFilesystemEncoding().c_str());
406         string tmp;
407
408         while (getline(is, tmp)) {
409                 // Ignore comments, empty line or line stats with ' '
410                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
411                         continue;
412
413                 // Determine section id
414                 if (tmp == sec_lastfiles)
415                         lastFiles().read(is);
416                 else if (tmp == sec_lastopened)
417                         lastOpened().read(is);
418                 else if (tmp == sec_lastfilepos)
419                         lastFilePos().read(is);
420                 else if (tmp == sec_bookmarks)
421                         bookmarks().read(is);
422                 else if (tmp == sec_lastcommands)
423                         lastCommands().read(is);
424                 else if (tmp == sec_authfiles)
425                         authFiles().read(is);
426                 else if (tmp == sec_shellescape)
427                         shellescapeFiles().read(is);
428
429                 else
430                         LYXERR(Debug::INIT, "LyX: Warning: unknown Session section: " << tmp);
431         }
432 }
433
434
435 void Session::writeFile() const
436 {
437         ofstream os(session_file.toFilesystemEncoding().c_str());
438         if (os) {
439                 os << "## Automatically generated lyx session file \n"
440                     << "## Editing this file manually may cause lyx to crash.\n";
441
442                 lastFiles().write(os);
443                 lastOpened().write(os);
444                 lastFilePos().write(os);
445                 lastCommands().write(os);
446                 bookmarks().write(os);
447                 authFiles().write(os);
448                 shellescapeFiles().write(os);
449         } else
450                 LYXERR(Debug::INIT, "LyX: Warning: unable to save Session: "
451                        << session_file);
452 }
453
454
455 AuthFilesSection::AuthFilesSection() {  }
456
457
458 void AuthFilesSection::read(istream & is)
459 {
460         string tmp;
461         do {
462                 char c = is.peek();
463                 if (c == '[')
464                         break;
465                 getline(is, tmp);
466                 if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ' || !FileName::isAbsolute(tmp))
467                         continue;
468
469                 // read lastfiles
470                 FileName const file(tmp);
471                 if (file.exists() && !file.isDirectory())
472                         auth_files_.insert(tmp);
473                 else
474                         LYXERR(Debug::INIT, "LyX: Warning: Ignore auth file: " << tmp);
475         } while (is.good());
476 }
477
478
479 void AuthFilesSection::write(ostream & os) const
480 {
481         os << '\n' << sec_authfiles << '\n';
482         copy(auth_files_.begin(), auth_files_.end(),
483              ostream_iterator<std::string>(os, "\n"));
484 }
485
486
487 bool AuthFilesSection::find(string const & name) const
488 {
489         if (auth_files_.find(name) != auth_files_.end())
490                 return true;
491
492         return false;
493 }
494
495
496 void AuthFilesSection::insert(string const & name)
497 {
498         auth_files_.insert(name);
499 }
500
501
502 void ShellEscapeSection::read(istream & is)
503 {
504         string s;
505         do {
506                 char c = is.peek();
507                 if (c == '[')
508                         break;
509                 getline(is, s);
510                 if (s.empty() || s[0] == '#' || s[0] == ' ' || !FileName::isAbsolute(s))
511                         continue;
512
513                 // read shellescape files
514                 FileName const file(s.substr(0, s.length() - 2));
515                 if (file.exists() && !file.isDirectory())
516                         shellescape_files_.insert(s);
517                 else
518                         LYXERR(Debug::INIT, "LyX: Warning: Ignore shellescape file: " << file);
519         } while (is.good());
520 }
521
522
523 void ShellEscapeSection::write(ostream & os) const
524 {
525         os << '\n' << sec_shellescape << '\n';
526         copy(shellescape_files_.begin(), shellescape_files_.end(),
527              ostream_iterator<std::string>(os, "\n"));
528 }
529
530
531 bool ShellEscapeSection::find(string const & name) const
532 {
533         if (shellescape_files_.find(name + ",0") != shellescape_files_.end())
534                 return true;
535
536         return findAuth(name);
537 }
538
539
540 bool ShellEscapeSection::findAuth(string const & name) const
541 {
542         if (shellescape_files_.find(name + ",1") != shellescape_files_.end())
543                 return true;
544
545         return false;
546 }
547
548
549 void ShellEscapeSection::insert(string const & name, bool auth)
550 {
551         set<string>::iterator it;
552         string const name0 = name + ",0";
553         string const name1 = name + ",1";
554
555         if (auth) {
556                 it = shellescape_files_.find(name0);
557                 if (it != shellescape_files_.end())
558                         shellescape_files_.erase(it);
559                 shellescape_files_.insert(name1);
560         } else {
561                 it = shellescape_files_.find(name1);
562                 if (it != shellescape_files_.end())
563                         shellescape_files_.erase(it);
564                 shellescape_files_.insert(name0);
565         }
566 }
567
568
569 void ShellEscapeSection::remove(string const & name)
570 {
571         set<string>::iterator it = shellescape_files_.find(name + ",0");
572         if (it == shellescape_files_.end())
573                 it = shellescape_files_.find(name + ",1");
574         if (it != shellescape_files_.end())
575                 shellescape_files_.erase(it);
576 }
577
578
579 } // namespace lyx