1999-09-27 18:44:28 +00:00
|
|
|
|
/* This file is part of
|
1999-11-15 12:01:38 +00:00
|
|
|
|
* ======================================================
|
1999-09-27 18:44:28 +00:00
|
|
|
|
*
|
|
|
|
|
* LyX, The Document Processor
|
2000-03-09 03:36:48 +00:00
|
|
|
|
* Copyright 1995 Matthias Ettrich
|
2001-05-30 13:53:44 +00:00
|
|
|
|
* Copyright 1995-2001 The LyX Team.
|
1999-09-27 18:44:28 +00:00
|
|
|
|
*
|
2001-05-30 13:53:44 +00:00
|
|
|
|
* This file is Copyright 1996-2001
|
1999-09-27 18:44:28 +00:00
|
|
|
|
* Lars Gullik Bj<EFBFBD>nnes
|
2002-01-16 22:17:38 +00:00
|
|
|
|
* Ben Stanley
|
1999-09-27 18:44:28 +00:00
|
|
|
|
*
|
1999-11-15 12:01:38 +00:00
|
|
|
|
* ======================================================
|
1999-09-27 18:44:28 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
1999-11-04 01:40:20 +00:00
|
|
|
|
#ifdef __GNUG__
|
|
|
|
|
#pragma implementation
|
|
|
|
|
#endif
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#include "DepTable.h"
|
2001-11-27 02:56:55 +00:00
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
#include "support/lyxlib.h"
|
|
|
|
|
#include "support/filetools.h"
|
2001-07-29 17:39:01 +00:00
|
|
|
|
#include "support/lstrings.h"
|
2001-11-27 02:56:55 +00:00
|
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
1999-11-04 01:40:20 +00:00
|
|
|
|
#include <fstream>
|
2000-03-28 02:18:55 +00:00
|
|
|
|
|
1999-11-08 13:54:04 +00:00
|
|
|
|
using std::make_pair;
|
|
|
|
|
using std::ofstream;
|
|
|
|
|
using std::ifstream;
|
2000-03-28 02:18:55 +00:00
|
|
|
|
using std::endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2002-01-16 22:17:38 +00:00
|
|
|
|
inline bool DepTable::dep_info::changed() const
|
|
|
|
|
{
|
|
|
|
|
return crc_prev != crc_cur && crc_cur != 0;
|
|
|
|
|
}
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
void DepTable::insert(string const & fi,
|
2002-01-16 22:17:38 +00:00
|
|
|
|
bool upd)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
|
|
|
|
// not quite sure if this is the correct place for MakeAbsPath
|
1999-10-02 16:21:10 +00:00
|
|
|
|
string f = MakeAbsPath(fi);
|
1999-11-04 01:40:20 +00:00
|
|
|
|
if (deplist.find(f) == deplist.end()) {
|
2002-01-16 22:17:38 +00:00
|
|
|
|
dep_info di;
|
|
|
|
|
di.crc_prev = 0;
|
1999-11-04 01:40:20 +00:00
|
|
|
|
if (upd) {
|
2002-01-16 22:17:38 +00:00
|
|
|
|
lyxerr[Debug::DEPEND] << " CRC..." << flush;
|
|
|
|
|
di.crc_cur = lyx::sum(f);
|
|
|
|
|
lyxerr[Debug::DEPEND] << "done." << endl;
|
2001-11-27 02:56:55 +00:00
|
|
|
|
struct stat f_info;
|
|
|
|
|
stat(fi.c_str(), &f_info);
|
2002-01-16 22:17:38 +00:00
|
|
|
|
di.mtime_cur = f_info.st_mtime;
|
|
|
|
|
} else {
|
|
|
|
|
di.crc_cur = 0;
|
|
|
|
|
di.mtime_cur = 0;
|
1999-11-04 01:40:20 +00:00
|
|
|
|
}
|
2001-11-27 02:56:55 +00:00
|
|
|
|
deplist[f] = di;
|
2002-01-16 22:17:38 +00:00
|
|
|
|
} else {
|
|
|
|
|
lyxerr[Debug::DEPEND] << " Already in DepTable" << endl;
|
1999-11-04 01:40:20 +00:00
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DepTable::update()
|
|
|
|
|
{
|
2002-01-16 22:17:38 +00:00
|
|
|
|
lyxerr[Debug::DEPEND] << "Updating DepTable..." << endl;
|
|
|
|
|
time_t start_time = time(0);
|
2001-11-27 02:56:55 +00:00
|
|
|
|
|
2002-01-16 22:17:38 +00:00
|
|
|
|
DepList::iterator itr = deplist.begin();
|
|
|
|
|
while (itr != deplist.end()) {
|
|
|
|
|
dep_info &di = itr->second;
|
|
|
|
|
|
|
|
|
|
struct stat f_info;
|
|
|
|
|
if (0 == stat(itr->first.c_str(), &f_info) ) {
|
|
|
|
|
if (di.mtime_cur == f_info.st_mtime) {
|
|
|
|
|
di.crc_prev = di.crc_cur;
|
|
|
|
|
lyxerr[Debug::DEPEND] << itr->first << " same mtime";
|
|
|
|
|
} else {
|
|
|
|
|
di.crc_prev = di.crc_cur;
|
|
|
|
|
lyxerr[Debug::DEPEND] << itr->first << " CRC... ";
|
|
|
|
|
di.crc_cur = lyx::sum(itr->first);
|
|
|
|
|
lyxerr[Debug::DEPEND] << "done";
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// file doesn't exist
|
|
|
|
|
// remove stale files - if it's re-created, it
|
|
|
|
|
// will be re-inserted by deplog.
|
|
|
|
|
lyxerr[Debug::DEPEND] << itr->first
|
|
|
|
|
<< " doesn't exist. removing from DepTable." << endl;
|
|
|
|
|
DepList::iterator doomed = itr++;
|
|
|
|
|
deplist.erase(doomed);
|
|
|
|
|
continue;
|
2001-11-27 02:56:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-01-25 12:35:27 +00:00
|
|
|
|
if (lyxerr.debugging(Debug::DEPEND)) {
|
2002-01-16 22:17:38 +00:00
|
|
|
|
if (di.changed())
|
2000-01-25 12:35:27 +00:00
|
|
|
|
lyxerr << " +";
|
|
|
|
|
lyxerr << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2002-01-16 22:17:38 +00:00
|
|
|
|
++itr;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2002-01-16 22:17:38 +00:00
|
|
|
|
time_t time_sec = time(0) - start_time;
|
|
|
|
|
lyxerr[Debug::DEPEND] << "Finished updating DepTable ("
|
|
|
|
|
<< time_sec << " sec)." << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1999-11-26 06:57:35 +00:00
|
|
|
|
bool DepTable::sumchange() const
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
1999-11-04 01:40:20 +00:00
|
|
|
|
for (DepList::const_iterator cit = deplist.begin();
|
|
|
|
|
cit != deplist.end();
|
|
|
|
|
++cit) {
|
2002-01-16 22:17:38 +00:00
|
|
|
|
if ((*cit).second.changed()) return true;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
1999-11-04 01:40:20 +00:00
|
|
|
|
return false;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1999-11-26 06:57:35 +00:00
|
|
|
|
bool DepTable::haschanged(string const & f) const
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
|
|
|
|
// not quite sure if this is the correct place for MakeAbsPath
|
1999-10-02 16:21:10 +00:00
|
|
|
|
string fil = MakeAbsPath(f);
|
1999-11-04 01:40:20 +00:00
|
|
|
|
DepList::const_iterator cit = deplist.find(fil);
|
|
|
|
|
if (cit != deplist.end()) {
|
2002-01-16 22:17:38 +00:00
|
|
|
|
if (cit->second.changed())
|
1999-11-04 01:40:20 +00:00
|
|
|
|
return true;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
1999-11-04 01:40:20 +00:00
|
|
|
|
return false;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
1999-11-26 06:57:35 +00:00
|
|
|
|
bool DepTable::extchanged(string const & ext) const
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
1999-11-04 01:40:20 +00:00
|
|
|
|
for (DepList::const_iterator cit = deplist.begin();
|
|
|
|
|
cit != deplist.end();
|
|
|
|
|
++cit) {
|
2001-07-12 11:11:10 +00:00
|
|
|
|
if (suffixIs(cit->first, ext)) {
|
2002-01-16 22:17:38 +00:00
|
|
|
|
if (cit->second.changed())
|
1999-11-04 01:40:20 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-01-16 22:17:38 +00:00
|
|
|
|
bool DepTable::ext_exist(const string& ext ) const
|
1999-11-26 06:57:35 +00:00
|
|
|
|
{
|
2002-01-16 22:17:38 +00:00
|
|
|
|
for (DepList::const_iterator cit = deplist.begin();
|
|
|
|
|
cit != deplist.end(); ++cit ) {
|
|
|
|
|
|
|
|
|
|
if ( suffixIs(cit->first, ext) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-11-26 06:57:35 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2002-01-16 22:17:38 +00:00
|
|
|
|
bool DepTable::exist(string const & fil) const
|
|
|
|
|
{
|
|
|
|
|
return deplist.find(fil) != deplist.end();
|
|
|
|
|
}
|
|
|
|
|
|
1999-11-26 06:57:35 +00:00
|
|
|
|
|
|
|
|
|
void DepTable::remove_files_with_extension(string const & suf)
|
|
|
|
|
{
|
2002-01-16 22:17:38 +00:00
|
|
|
|
DepList::iterator cit = deplist.begin();
|
|
|
|
|
while (cit != deplist.end()) {
|
|
|
|
|
if (suffixIs(cit->first, suf)) {
|
|
|
|
|
// Can't erase the current iterator, but we can increment and then erase.
|
|
|
|
|
// deplist is a map so only the erased iterator is invalidated.
|
|
|
|
|
DepList::iterator doomed = cit++;
|
|
|
|
|
deplist.erase(doomed);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
cit++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DepTable::remove_file(string const & filename)
|
|
|
|
|
{
|
|
|
|
|
DepList::iterator cit = deplist.begin();
|
|
|
|
|
while (cit != deplist.end()) {
|
|
|
|
|
if (OnlyFilename(cit->first) == filename) {
|
|
|
|
|
// Can't erase the current iterator, but we can increment and then erase.
|
|
|
|
|
// deplist is a map so only the erased iterator is invalidated.
|
|
|
|
|
DepList::iterator doomed = cit++;
|
|
|
|
|
deplist.erase(doomed);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
cit++;
|
1999-11-26 06:57:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DepTable::write(string const & f) const
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
1999-11-04 01:40:20 +00:00
|
|
|
|
ofstream ofs(f.c_str());
|
|
|
|
|
for (DepList::const_iterator cit = deplist.begin();
|
2000-09-26 13:54:57 +00:00
|
|
|
|
cit != deplist.end(); ++cit) {
|
2000-01-25 12:35:27 +00:00
|
|
|
|
if (lyxerr.debugging(Debug::DEPEND)) {
|
2002-01-16 22:17:38 +00:00
|
|
|
|
// Store the second (most recently calculated) CRC value.
|
|
|
|
|
// The older one is effectively set to 0 upon re-load.
|
1999-11-04 01:40:20 +00:00
|
|
|
|
lyxerr << "Write dep: "
|
2001-07-12 11:11:10 +00:00
|
|
|
|
<< cit->first << " "
|
2002-01-16 22:17:38 +00:00
|
|
|
|
<< cit->second.crc_cur << " "
|
|
|
|
|
<< cit->second.mtime_cur << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2001-07-12 11:11:10 +00:00
|
|
|
|
ofs << cit->first << " "
|
2002-01-16 22:17:38 +00:00
|
|
|
|
<< cit->second.crc_cur << " "
|
|
|
|
|
<< cit->second.mtime_cur << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1999-11-26 06:57:35 +00:00
|
|
|
|
|
|
|
|
|
void DepTable::read(string const & f)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
1999-11-04 01:40:20 +00:00
|
|
|
|
ifstream ifs(f.c_str());
|
|
|
|
|
string nome;
|
2002-01-16 22:17:38 +00:00
|
|
|
|
dep_info di;
|
|
|
|
|
// This doesn't change through the loop.
|
|
|
|
|
di.crc_prev = 0;
|
2001-11-27 02:56:55 +00:00
|
|
|
|
|
2002-01-16 22:17:38 +00:00
|
|
|
|
while (ifs >> nome >> di.crc_cur >> di.mtime_cur) {
|
2000-01-25 12:35:27 +00:00
|
|
|
|
if (lyxerr.debugging(Debug::DEPEND)) {
|
|
|
|
|
lyxerr << "Read dep: "
|
1999-11-04 01:40:20 +00:00
|
|
|
|
<< nome << " "
|
2002-01-16 22:17:38 +00:00
|
|
|
|
<< di.crc_cur << " "
|
|
|
|
|
<< di.mtime_cur << endl;
|
1999-11-04 01:40:20 +00:00
|
|
|
|
}
|
2001-11-27 02:56:55 +00:00
|
|
|
|
deplist[nome] = di;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2002-01-16 22:17:38 +00:00
|
|
|
|
|