2003-08-23 00:17:00 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file DepTable.C
|
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-03-21 17:27:08 +00:00
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
|
|
|
|
* \author Jean-Marc Lasgouttes
|
|
|
|
|
* \author Ben Stanley
|
1999-09-27 18:44:28 +00:00
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* Full author contact details are available in file CREDITS.
|
1999-09-27 18:44:28 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include "DepTable.h"
|
2003-09-09 22:13:45 +00:00
|
|
|
|
|
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"
|
2003-02-08 19:18:01 +00:00
|
|
|
|
#include "support/lyxtime.h"
|
2001-11-27 02:56:55 +00:00
|
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
1999-11-04 01:40:20 +00:00
|
|
|
|
#include <fstream>
|
2000-03-28 02:18:55 +00:00
|
|
|
|
|
2002-06-10 07:57:39 +00:00
|
|
|
|
#ifndef CXX_GLOBAL_CSTD
|
|
|
|
|
using std::time;
|
|
|
|
|
#endif
|
|
|
|
|
|
2003-09-09 22:13:45 +00:00
|
|
|
|
using lyx::support::ltrim;
|
|
|
|
|
using lyx::support::MakeAbsPath;
|
|
|
|
|
using lyx::support::OnlyFilename;
|
|
|
|
|
using lyx::support::suffixIs;
|
|
|
|
|
using lyx::support::sum;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
|
2000-03-28 02:18:55 +00:00
|
|
|
|
using std::endl;
|
2003-09-08 00:33:41 +00:00
|
|
|
|
using std::flush;
|
2003-01-08 09:54:18 +00:00
|
|
|
|
using std::getline;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
2003-09-08 00:33:41 +00:00
|
|
|
|
using std::ofstream;
|
|
|
|
|
using std::ifstream;
|
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;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
di.crc_cur = sum(f);
|
2002-01-16 22:17:38 +00:00
|
|
|
|
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;
|
2003-02-08 19:18:01 +00:00
|
|
|
|
lyx::time_type const start_time = lyx::current_time();
|
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;
|
2003-10-13 01:01:33 +00:00
|
|
|
|
lyxerr[Debug::DEPEND] << itr->first << " same mtime" << endl;
|
2002-01-16 22:17:38 +00:00
|
|
|
|
} else {
|
|
|
|
|
di.crc_prev = di.crc_cur;
|
2003-10-13 01:01:33 +00:00
|
|
|
|
lyxerr[Debug::DEPEND] << itr->first << " CRC... " << flush;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
di.crc_cur = sum(itr->first);
|
2003-10-13 01:01:33 +00:00
|
|
|
|
lyxerr[Debug::DEPEND] << "done" << endl;
|
2002-01-16 22:17:38 +00:00
|
|
|
|
}
|
|
|
|
|
} 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
|
|
|
|
}
|
2003-02-08 19:18:01 +00:00
|
|
|
|
lyx::time_type const time_sec = lyx::current_time() - start_time;
|
2002-01-16 22:17:38 +00:00
|
|
|
|
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-07 20:17: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-07 20:17:40 +00:00
|
|
|
|
return deplist.size();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|