X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Flastfiles.C;h=615b9de7ee35c8e52338e9dd516aca4369f6526d;hb=98c966c64594611e469313314abd1e59524adb4a;hp=d2005145b20e92cecb8b03a822ad4b7d4488266d;hpb=35584afc1162dec2cf9fff79305e95cb3b75aefb;p=lyx.git diff --git a/src/lastfiles.C b/src/lastfiles.C index d2005145b2..615b9de7ee 100644 --- a/src/lastfiles.C +++ b/src/lastfiles.C @@ -1,12 +1,12 @@ /* This file is part of * ====================================================== - * + * * LyX, The Document Processor - * + * * Copyright 1995 Matthias Ettrich - * Copyright 1995-1999 The LyX Team. + * Copyright 1995-2001 The LyX Team. * - * ======================================================*/ + * ====================================================== */ #include @@ -15,34 +15,40 @@ #pragma implementation #endif -#include "lyxlex.h" -#include "support/FileInfo.h" #include "lastfiles.h" -#include "support/filetools.h" #include "debug.h" -LastFiles::LastFiles(string const & filename, bool st, char num) +#include "support/FileInfo.h" + +#include +#include +#include + + +using std::ifstream; +using std::ofstream; +using std::getline; +using std::endl; +using std::find; +using std::copy; +using std::ostream_iterator; + + +LastFiles::LastFiles(string const & filename, bool st, unsigned int num) : dostat(st) { setNumberOfFiles(num); - files = new string[num_files]; readFile(filename); } -LastFiles::~LastFiles() +void LastFiles::setNumberOfFiles(unsigned int no) { - delete[] files; -} - - -void LastFiles::setNumberOfFiles(char no) -{ - if (1 <= no && no <= ABSOLUTEMAXLASTFILES) + if (0 < no && no <= ABSOLUTEMAXLASTFILES) num_files = no; else { lyxerr << "LyX: lastfiles: too many files\n" - "\tdefault (=" << int(DEFAULTFILES) // int() only because of anon enum + "\tdefault (=" << int(DEFAULTFILES) << ") used." << endl; num_files = DEFAULTFILES; } @@ -52,49 +58,28 @@ void LastFiles::setNumberOfFiles(char no) void LastFiles::readFile(string const & filename) { // we will not complain if we can't find filename nor will - // we issue a warning. Lgb. - LyXLex lex(0, 0); /* LyXLex should be changed - * to allow constructor with - * no parameters. */ - bool error = false; - - lex.setFile(filename); - - if (!lex.IsOK()) return; - + // we issue a warning. (Lgb) + ifstream ifs(filename.c_str()); string tmp; FileInfo fileInfo; - int i = 0; - - while (lex.IsOK() && !error && i < num_files) { - switch(lex.lex()) { - case LyXLex::LEX_FEOF: - error = true; - break; - default: - tmp = lex.GetString(); - // Check if the file exist - if (dostat) { - if (!(fileInfo.newFile(tmp).exist() && - fileInfo.isRegular())) - break; // the file does not exist - } - files[i] = tmp; - i++; - break; + + while (getline(ifs, tmp) && files.size() < num_files) { + if (dostat) { + if (!(fileInfo.newFile(tmp).exist() && + fileInfo.isRegular())) + continue; } + files.push_back(tmp); } } void LastFiles::writeFile(string const & filename) const { - FilePtr fd(filename, FilePtr::write); - if (fd()) { - for (int i = 0; i < num_files; i++) { - if (!files[i].empty()) - fprintf(fd, "\"%s\"\n", files[i].c_str()); - } + ofstream ofs(filename.c_str()); + if (ofs) { + copy(files.begin(), files.end(), + ostream_iterator(ofs, "\n")); } else lyxerr << "LyX: Warning: unable to save LastFiles: " << filename << endl; @@ -103,12 +88,19 @@ void LastFiles::writeFile(string const & filename) const void LastFiles::newFile(string const & file) { - int n; - // Find this file in list. If not in list, point to last entry - for(n = 0; n < (num_files - 1); n++) - if(files[n] == file) break; - - for(int i = n; i >= 1; i--) - files[i] = files[i - 1]; - files[0] = file; + // If file already exist, delete it and reinsert at front. + Files::iterator it = find(files.begin(), files.end(), file); + if (it != files.end()) + files.erase(it); + files.push_front(file); + if (files.size() > num_files) + files.pop_back(); +} + + +string const LastFiles::operator[](unsigned int i) const +{ + if (i < files.size()) + return files[i]; + return string(); }