]> git.lyx.org Git - lyx.git/blobdiff - src/DepTable.C
white-space changes, removed definitions.h several enum changes because of this,...
[lyx.git] / src / DepTable.C
index 15aba4ae879bfb96a7b99f891da7168f1839c1a1..f28f744859d65c94d130122ac8079975f4ee6b22 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of
- * ======================================================
+ * ====================================================== 
  * 
  *           LyX, The Document Processor
  *          Copyright (C) 1995 Matthias Ettrich
  *           This file is Copyright (C) 1996-1998
  *           Lars Gullik Bjønnes
  *
- * ======================================================
+ * ====================================================== 
  */
 
 #include <config.h>
 
-#include "DepTable.h"
-#include "lyxlib.h"
-#include "filetools.h"
-
-DepTable::DepTable()
-{
-       new_sum = 0;
-       old_sum = 0;
-       next = NULL;
-}
-
-
-DepTable::DepTable(LString const & f,
-                  bool upd,
-                  unsigned long one,
-                  unsigned long two)
-{
-       // not quite sure if this is the correct place for MakeAbsPath
-       file = MakeAbsPath(f);
-       new_sum = 0; old_sum = 0;
-       if (one != 0)
-               new_sum = one;
-       if (two != 0)
-               old_sum = two;
-       if (upd) {
-               old_sum = new_sum;
-               new_sum = lyxsum(file.c_str());
-       }
-       if (lyxerr.debugging()) {
-               char tmp1[256];
-               char tmp2[256];
-               sprintf(tmp1, "%lu", new_sum);
-               sprintf(tmp2, "%lu", old_sum);
-               lyxerr.debug("New file inserted in deplog: " + 
-                            file + " " +
-                            tmp1 + " " + tmp2);
-       }
-       next = NULL;
-}
-
+#ifdef __GNUG__
+#pragma implementation
+#endif
 
-void DepTable::insert(LString const & fi,
+#include "DepTable.h"
+#include "support/lyxlib.h"
+#include "support/filetools.h"
+#include <fstream>
+using std::make_pair;
+using std::ofstream;
+using std::ifstream;
+
+void DepTable::insert(string const & fi,
                      bool upd,
                      unsigned long one,
                      unsigned long two)
 {
        // not quite sure if this is the correct place for MakeAbsPath
-       LString f = MakeAbsPath(fi);
-       if (f == file) return; // exist already in the log
-       if (next)
-               next->insert(f, upd, one, two);
-       else
-               next = new DepTable(f, upd, one, two);
+       string f = MakeAbsPath(fi);
+       if (deplist.find(f) == deplist.end()) {
+               if (upd) {
+                       one = two;
+                       two = lyxsum(f.c_str());
+               }
+               deplist[f] = make_pair(one, two);
+       }
 }
                
 
 void DepTable::update()
 {
-       if (!file.empty()) {
-               old_sum = new_sum;
-               new_sum = lyxsum(file.c_str());
+       for(DepList::iterator itr = deplist.begin();
+           itr != deplist.end();
+           ++itr) {
+               unsigned long one = (*itr).second.second;
+               unsigned long two = lyxsum((*itr).first.c_str());
+               (*itr).second = make_pair(one, two);
                if (lyxerr.debugging()) {
-                       char tmp1[256];
-                       char tmp2[256];
-                       sprintf(tmp1, "%lu", new_sum);
-                       sprintf(tmp2, "%lu", old_sum);
-                       lyxerr.debug("update: " + file + " " +
-                                    tmp1 + " " + tmp2);
+                       lyxerr << "update: " << (*itr).first << " "
+                              << one << " " << two << endl;
                }
        }
-       if (next) next->update();
 }
 
 
 bool DepTable::sumchange()
 {
-       bool ret = false;
-       
-       if (!file.empty()) {
-               if (old_sum != new_sum) ret = true;
+       for (DepList::const_iterator cit = deplist.begin();
+            cit != deplist.end();
+            ++cit) {
+               if ((*cit).second.first != (*cit).second.second) return true;
        }
-       if (!ret && next) ret = next->sumchange();
-
-       return ret;
+       return false;
 }
 
 
-bool DepTable::haschanged(LString const & f)
+bool DepTable::haschanged(string const & f)
 {
        // not quite sure if this is the correct place for MakeAbsPath
-       LString fil = MakeAbsPath(f);
-       bool ret = false;
-
-       if (!fil.empty() && !file.empty() && fil == file) {
-               if (new_sum != old_sum && new_sum != 0)
-                       ret = true;
+       string fil = MakeAbsPath(f);
+       DepList::const_iterator cit = deplist.find(fil);
+       if (cit != deplist.end()) {
+               if ((*cit).second.first != (*cit).second.second
+                   && (*cit).second.second != 0)
+                       return true;
        }
-       if (!ret && next) ret = next->haschanged(fil);
-       return ret;
+       return false;
 }
 
 
-void DepTable::write(LString const&f)
+bool DepTable::extchanged(string const & ext)
 {
-       FilePtr fp(f, FilePtr::write);
-       if (fp() && next) next->write(fp());
+       for (DepList::const_iterator cit = deplist.begin();
+            cit != deplist.end();
+            ++cit) {
+               if (suffixIs((*cit).first, ext.c_str())) {
+                       if ((*cit).second.first != (*cit).second.second)
+                               return true;
+               }
+       }
+                    
+       return false;
 }
 
 
-void DepTable::read(LString const &f)
+void DepTable::write(string const & f)
 {
-       FilePtr fp(f, FilePtr::read);
-       if (fp()) { // file opened
-               char nome[256];
-               unsigned long one = 0;
-               unsigned long two = 0;
-               // scan the file line by line
-               // return true if the two numbers on the line is different
-               int ret = 0;
-               while (!feof(fp())) {
-                       ret = fscanf(fp(), "%s %lu %lu",
-                                    nome, &one, &two);
-                       if (ret !=3) continue;
-                       if (lyxerr.debugging()) {
-                               char tmp1[255];
-                               char tmp2[255];
-                               sprintf(tmp1, "%lu", one);
-                               sprintf(tmp2, "%lu", two);
-                               lyxerr.debug(LString("read dep: ") +
-                                            nome + " " + tmp1 +
-                                            " " + tmp2);
-                       }
-                       insert(LString(nome), false, one, two);
+       ofstream ofs(f.c_str());
+       for (DepList::const_iterator cit = deplist.begin();
+            cit != deplist.end();
+            ++cit) {
+               if (lyxerr.debugging()) {
+                       lyxerr << "Write dep: "
+                              << (*cit).first << " "
+                              << (*cit).second.first << " "
+                              << (*cit).second.second << endl;
                }
+               ofs << (*cit).first << " "
+                   << (*cit).second.first << " "
+                   << (*cit).second.second << endl;
        }
 }
 
-
-void DepTable::write(FILE * f)
+void DepTable::read(string const &f)
 {
-       if (lyxerr.debugging()) {
-               char tmp1[255];
-               char tmp2[255];
-               sprintf(tmp1, "%lu", new_sum);
-               sprintf(tmp2, "%lu", old_sum);
-               lyxerr.print("Write dep: " + file + " " +
-                            tmp1 + " " + tmp2);
+       ifstream ifs(f.c_str());
+       string nome;
+       unsigned long one = 0;
+       unsigned long two = 0;
+       while(ifs >> nome >> one >> two) {
+               if (lyxerr.debugging()) {
+                       lyxerr << "read dep: "
+                              << nome << " "
+                              << one << " "
+                              << two << endl;
+               }
+               deplist[nome] = make_pair(one, two);
        }
-       fprintf(f, "%s %lu %lu\n", file.c_str(),
-               new_sum, old_sum);
-       if (next)
-               next->write(f);
 }