]> git.lyx.org Git - lyx.git/blobdiff - src/lastfiles.C
remove unneeded functions
[lyx.git] / src / lastfiles.C
index 2d327fbaa0688383c0ac91ed7174853672e886ee..ef200b2c9d992c2f9941e77d107082edd069d337 100644 (file)
-/* This file is part of
-* ======================================================
-* 
-*           LyX, The Document Processor
-*       
-*          Copyright (C) 1995 Matthias Ettrich
-*           Copyright (C) 1995-1998 The LyX Team.
-*
-*======================================================*/
+/**
+ * \file lastfiles.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Lars Gullik Bjønnes
+ *
+ * Full author contact details are available in file CREDITS.
+ */
 
 #include <config.h>
 
+#include "lastfiles.h"
+#include "debug.h"
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
+#include "support/FileInfo.h"
 
-//#include "definitions.h"
-#include "lyxlex.h"
-#include "FileInfo.h"
-#include "lastfiles.h"
-#include "filetools.h"
-#include "error.h"
+#include <algorithm>
+#include <fstream>
+#include <iterator>
 
-//     $Id: lastfiles.C,v 1.1 1999/09/27 18:44:37 larsbj Exp $ 
+using lyx::support::FileInfo;
 
-#if !defined(lint) && !defined(WITH_WARNINGS)
-static char vcid[] = "$Id: lastfiles.C,v 1.1 1999/09/27 18:44:37 larsbj Exp $";
-#endif /* lint */
+using std::copy;
+using std::endl;
+using std::find;
+using std::getline;
+using std::string;
+using std::ifstream;
+using std::ofstream;
+using std::ostream_iterator;
 
-LastFiles::LastFiles(LString const & filename, bool st, char num)
+
+LastFiles::LastFiles(string const & filename, bool st, unsigned int num)
        : dostat(st)
 {
        setNumberOfFiles(num);
-       files = new LString[num_files];
        readFile(filename);
 }
 
 
-LastFiles::~LastFiles()
-{
-       delete[] files;
-}
-
-
-void LastFiles::setNumberOfFiles(char no)
+void LastFiles::setNumberOfFiles(unsigned int no)
 {
-       if (1 <= no && no <= ABSOLUTEMAXLASTFILES)
+       if (0 < no && no <= ABSOLUTEMAXLASTFILES)
                num_files = no;
        else {
-               lyxerr.print(LString("LyX: lastfiles: too many files\n"
-                       "\tdefault (=") + int(DEFAULTFILES) + ") used.");
+               lyxerr << "LyX: lastfiles: too many files\n"
+                       "\tdefault (=" << int(DEFAULTFILES)
+                      << ") used." << endl;
                num_files = DEFAULTFILES;
        }
 }
 
 
-void LastFiles::readFile(LString const & filename)
+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(NULL, 0); /* LyXLex should be changed
-                             * to allow constructor with
-                             * no parameters. */
-       bool error = false;
-
-       lex.setFile(filename);
-
-       if (!lex.IsOK()) return;
-
-       LString tmp;
+       // 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(LString const & filename) const
+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<string>(ofs, "\n"));
        } else
-               lyxerr.print("LyX: Warning: unable to save LastFiles: "
-                             + filename);
+               lyxerr << "LyX: Warning: unable to save LastFiles: "
+                      << filename << endl;
+}
+
+
+void LastFiles::newFile(string const & 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();
 }
 
 
-void LastFiles::newFile(LString const & file)
+string const LastFiles::operator[](unsigned int i) const
 {
-       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 (i < files.size())
+               return files[i];
+       return string();
 }