/* This file is part of
- * ======================================================
+ * ======================================================
*
* LyX, The Document Processor
- * Copyright (C) 1995 Matthias Ettrich
- * Copyright (C) 1995-1998 The LyX Team.
+ * Copyright 1995 Matthias Ettrich
+ * Copyright 1995-2001 The LyX Team.
*
- * This file is Copyright (C) 1996-1998
+ * This file is Copyright 1996-2001
* Lars Gullik Bjønnes
*
- * ======================================================
+ * ======================================================
*/
#include <config.h>
+#ifdef __GNUG__
+#pragma implementation
+#endif
+
#include "DepTable.h"
+#include "debug.h"
+
#include "support/lyxlib.h"
#include "support/filetools.h"
+#include "support/lstrings.h"
-DepTable::DepTable()
-{
- new_sum = 0;
- old_sum = 0;
- next = 0;
-}
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fstream>
-DepTable::DepTable(string 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 << endl;
- }
- next = 0;
-}
-
+using std::make_pair;
+using std::ofstream;
+using std::ifstream;
+using std::endl;
void DepTable::insert(string const & fi,
bool upd,
{
// not quite sure if this is the correct place for MakeAbsPath
string 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);
+ if (deplist.find(f) == deplist.end()) {
+ long mtime = 0;
+ if (upd) {
+ one = two;
+ two = lyx::sum(f);
+ struct stat f_info;
+ stat(fi.c_str(), &f_info);
+ mtime = f_info.st_mtime;
+ }
+ dep_info di;
+ di.first = one;
+ di.second = two;
+ di.mtime = mtime;
+#if 0
+ deplist[f] = make_pair(one, two);
+#else
+ deplist[f] = di;
+#endif
+ }
}
void DepTable::update()
{
- if (!file.empty()) {
- 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() << "update: " << file << " "
- << tmp1 << " " << tmp2 << endl;
+ for (DepList::iterator itr = deplist.begin();
+ itr != deplist.end();
+ ++itr) {
+ unsigned long const one = itr->second.second;
+ unsigned long two = one;
+ long mtime = itr->second.mtime;
+ struct stat f_info;
+ stat(itr->first.c_str(), &f_info);
+
+ if (mtime != f_info.st_mtime) {
+ two = lyx::sum(itr->first);
+ mtime = f_info.st_mtime;
+ }
+
+#if 0
+ itr->second = make_pair(one, two);
+#else
+ dep_info di;
+ di.first = one;
+ di.second = two;
+ di.mtime = mtime;
+
+ itr->second = di;
+#endif
+ if (lyxerr.debugging(Debug::DEPEND)) {
+ lyxerr << "Update dep: " << itr->first << " "
+ << one << " " << two;
+ if (one != two)
+ lyxerr << " +";
+ lyxerr << endl;
}
}
- if (next) next->update();
}
-bool DepTable::sumchange()
+bool DepTable::sumchange() const
{
- 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(string const & f)
+bool DepTable::haschanged(string const & f) const
{
// not quite sure if this is the correct place for MakeAbsPath
string fil = MakeAbsPath(f);
- bool ret = false;
+ DepList::const_iterator cit = deplist.find(fil);
+ if (cit != deplist.end()) {
+ if (cit->second.first != cit->second.second
+ && cit->second.second != 0)
+ return true;
+ }
+ return false;
+}
- if (!fil.empty() && !file.empty() && fil == file) {
- if (new_sum != old_sum && new_sum != 0)
- ret = true;
+
+bool DepTable::extchanged(string const & ext) const
+{
+ for (DepList::const_iterator cit = deplist.begin();
+ cit != deplist.end();
+ ++cit) {
+ if (suffixIs(cit->first, ext)) {
+ if (cit->second.first != cit->second.second)
+ return true;
+ }
}
- if (!ret && next) ret = next->haschanged(fil);
- return ret;
+ return false;
}
-void DepTable::write(string const&f)
+bool DepTable::exist(string const & fil) const
{
- FilePtr fp(f, FilePtr::write);
- if (fp() && next) next->write(fp());
+ DepList::const_iterator cit = deplist.find(fil);
+ if (cit != deplist.end()) return true;
+ return false;
}
-void DepTable::read(string const &f)
+void DepTable::remove_files_with_extension(string const & suf)
{
- 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() << "read dep: "
- << nome << " " << tmp1
- << " " << tmp2 << endl;
- }
- insert(string(nome), false, one, two);
+ DepList tmp;
+ // we want const_iterator (Lgb)
+ for (DepList::iterator cit = deplist.begin();
+ cit != deplist.end(); ++cit) {
+ if (!suffixIs(cit->first, suf))
+ tmp[cit->first] = cit->second;
+ }
+ deplist.swap(tmp);
+}
+
+
+void DepTable::write(string const & f) const
+{
+ ofstream ofs(f.c_str());
+ for (DepList::const_iterator cit = deplist.begin();
+ cit != deplist.end(); ++cit) {
+ if (lyxerr.debugging(Debug::DEPEND)) {
+ lyxerr << "Write dep: "
+ << cit->first << " "
+ << cit->second.first << " "
+ << cit->second.second << " "
+ << cit->second.mtime << endl;
}
+ ofs << cit->first << " "
+ << cit->second.first << " "
+ << cit->second.second << " "
+ << cit->second.mtime << 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 << "Write dep: " << file << " "
- << tmp1 << " " << tmp2 << endl;
+ ifstream ifs(f.c_str());
+ string nome;
+ unsigned long one = 0;
+ unsigned long two = 0;
+ unsigned long mtime = 0;
+
+ while (ifs >> nome >> one >> two >> mtime) {
+ if (lyxerr.debugging(Debug::DEPEND)) {
+ lyxerr << "Read dep: "
+ << nome << " "
+ << one << " "
+ << two << " "
+ << mtime << endl;
+ }
+ dep_info di;
+ di.first = one;
+ di.second = two;
+ di.mtime = mtime;
+#if 0
+ deplist[nome] = make_pair(one, two);
+#else
+ deplist[nome] = di;
+#endif
}
- fprintf(f, "%s %lu %lu\n", file.c_str(),
- new_sum, old_sum);
- if (next)
- next->write(f);
}