1999-09-27 18:44:28 +00:00
|
|
|
|
/* This file is part of
|
2002-03-21 17:27:08 +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
|
|
|
|
*
|
2002-03-21 17:27:08 +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>
|
2002-05-14 15:04:58 +00:00
|
|
|
|
#include <ctime>
|
2000-03-28 02:18:55 +00:00
|
|
|
|
|
2002-06-10 07:57:39 +00:00
|
|
|
|
#ifndef CXX_GLOBAL_CSTD
|
|
|
|
|
using std::time;
|
|
|
|
|
#endif
|
|
|
|
|
|
1999-11-08 13:54:04 +00:00
|
|
|
|
using std::make_pair;
|
|
|
|
|
using std::ofstream;
|
|
|
|
|
using std::ifstream;
|
2002-01-21 12:16:56 +00:00
|
|
|
|
using std::flush;
|
2000-03-28 02:18:55 +00:00
|
|
|
|
using std::endl;
|
2003-01-08 09:54:18 +00:00
|
|
|
|
using std::getline;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2002-01-21 12:16:56 +00:00
|
|
|
|
|
|
|
|
|
inline
|
|
|
|
|
bool DepTable::dep_info::changed() const
|
2002-01-16 22:17:38 +00:00
|
|
|
|
{
|
|
|
|
|
return crc_prev != crc_cur && crc_cur != 0;
|
|
|
|
|
}
|
|
|
|
|
|
2002-01-21 12:16:56 +00:00
|
|
|
|
|
|
|
|
|
void DepTable::insert(string const & fi, bool upd)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
|
|
|
|
// not quite sure if this is the correct place for MakeAbsPath
|
2002-01-21 12:16:56 +00:00
|
|
|
|
string const 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-21 12:16:56 +00:00
|
|
|
|
lyxerr[Debug::DEPEND] << " CRC..." << flush;
|
2002-01-16 22:17:38 +00:00
|
|
|
|
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
|
|
|
|
}
|
2002-03-21 17:27:08 +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;
|
2002-01-21 12:16:56 +00:00
|
|
|
|
if (stat(itr->first.c_str(), &f_info) == 0) {
|
2002-01-16 22:17:38 +00:00
|
|
|
|
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.
|
2002-03-21 17:27:08 +00:00
|
|
|
|
lyxerr[Debug::DEPEND] << itr->first
|
2002-01-16 22:17:38 +00:00
|
|
|
|
<< " doesn't exist. removing from DepTable." << endl;
|
|
|
|
|
DepList::iterator doomed = itr++;
|
|
|
|
|
deplist.erase(doomed);
|
|
|
|
|
continue;
|
2001-11-27 02:56:55 +00:00
|
|
|
|
}
|
2002-03-21 17:27:08 +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
|
|
|
|
{
|
2002-01-21 12:16:56 +00:00
|
|
|
|
DepList::const_iterator cit = deplist.begin();
|
|
|
|
|
DepList::const_iterator end = deplist.end();
|
|
|
|
|
for (; cit != end; ++cit) {
|
|
|
|
|
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
|
2002-01-21 12:16:56 +00:00
|
|
|
|
string const 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
|
|
|
|
{
|
2002-01-21 12:16:56 +00:00
|
|
|
|
DepList::const_iterator cit = deplist.begin();
|
|
|
|
|
DepList::const_iterator end = deplist.end();
|
|
|
|
|
for (; cit != 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-03-12 17:15:44 +00:00
|
|
|
|
bool DepTable::ext_exist(string const & ext) const
|
1999-11-26 06:57:35 +00:00
|
|
|
|
{
|
2002-01-21 12:16:56 +00:00
|
|
|
|
DepList::const_iterator cit = deplist.begin();
|
|
|
|
|
DepList::const_iterator end = deplist.end();
|
2002-02-16 15:59:55 +00:00
|
|
|
|
for (; cit != end; ++cit) {
|
2002-01-21 12:16:56 +00:00
|
|
|
|
if (suffixIs(cit->first, ext)) {
|
2002-01-16 22:17:38 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
1999-11-26 06:57:35 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2002-01-21 12:16:56 +00:00
|
|
|
|
|
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();
|
2002-01-21 12:16:56 +00:00
|
|
|
|
DepList::iterator end = deplist.end();
|
|
|
|
|
while (cit != end) {
|
2002-01-16 22:17:38 +00:00
|
|
|
|
if (suffixIs(cit->first, suf)) {
|
2002-01-21 12:16:56 +00:00
|
|
|
|
// Can't erase the current iterator, but we
|
|
|
|
|
// can increment and then erase.
|
|
|
|
|
// Deplist is a map so only the erased
|
|
|
|
|
// iterator is invalidated.
|
2002-01-16 22:17:38 +00:00
|
|
|
|
DepList::iterator doomed = cit++;
|
|
|
|
|
deplist.erase(doomed);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2002-01-21 12:16:56 +00:00
|
|
|
|
++cit;
|
2002-01-16 22:17:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DepTable::remove_file(string const & filename)
|
|
|
|
|
{
|
|
|
|
|
DepList::iterator cit = deplist.begin();
|
2002-01-21 12:16:56 +00:00
|
|
|
|
DepList::iterator end = deplist.end();
|
|
|
|
|
while (cit != end) {
|
2002-01-16 22:17:38 +00:00
|
|
|
|
if (OnlyFilename(cit->first) == filename) {
|
2002-01-21 12:16:56 +00:00
|
|
|
|
// Can't erase the current iterator, but we
|
|
|
|
|
// can increment and then erase.
|
|
|
|
|
// deplist is a map so only the erased
|
|
|
|
|
// iterator is invalidated.
|
2002-01-16 22:17:38 +00:00
|
|
|
|
DepList::iterator doomed = cit++;
|
|
|
|
|
deplist.erase(doomed);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2002-01-21 12:16:56 +00:00
|
|
|
|
++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());
|
2002-01-21 12:16:56 +00:00
|
|
|
|
DepList::const_iterator cit = deplist.begin();
|
|
|
|
|
DepList::const_iterator end = deplist.end();
|
|
|
|
|
for (; cit != end; ++cit) {
|
2000-01-25 12:35:27 +00:00
|
|
|
|
if (lyxerr.debugging(Debug::DEPEND)) {
|
2002-01-21 12:16:56 +00:00
|
|
|
|
// Store the second (most recently calculated)
|
|
|
|
|
// CRC value.
|
2002-01-16 22:17:38 +00:00
|
|
|
|
// The older one is effectively set to 0 upon re-load.
|
1999-11-04 01:40:20 +00:00
|
|
|
|
lyxerr << "Write dep: "
|
2002-11-27 10:30:28 +00:00
|
|
|
|
<< cit->second.crc_cur << ' '
|
2003-01-08 09:54:18 +00:00
|
|
|
|
<< cit->second.mtime_cur << ' '
|
|
|
|
|
<< cit->first << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2003-01-08 09:54:18 +00:00
|
|
|
|
ofs << cit->second.crc_cur << ' '
|
|
|
|
|
<< cit->second.mtime_cur << ' '
|
|
|
|
|
<< cit->first << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1999-11-26 06:57:35 +00:00
|
|
|
|
|
2003-03-09 17:41:40 +00:00
|
|
|
|
bool 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;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2003-01-08 09:54:18 +00:00
|
|
|
|
while (ifs >> di.crc_cur >> di.mtime_cur && getline(ifs, nome)) {
|
|
|
|
|
nome = ltrim(nome);
|
2000-01-25 12:35:27 +00:00
|
|
|
|
if (lyxerr.debugging(Debug::DEPEND)) {
|
|
|
|
|
lyxerr << "Read dep: "
|
2002-11-27 10:30:28 +00:00
|
|
|
|
<< di.crc_cur << ' '
|
2003-01-08 09:54:18 +00:00
|
|
|
|
<< di.mtime_cur << ' '
|
|
|
|
|
<< nome << 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
|
|
|
|
}
|
2003-03-09 17:41:40 +00:00
|
|
|
|
return deplist.size();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|