2003-08-23 00:17:00 +00:00
|
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
|
* \file DepTable.cpp
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* 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
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2007-07-17 17:46:54 +00:00
|
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
|
|
using support::FileName;
|
|
|
|
|
using support::ltrim;
|
|
|
|
|
using support::onlyFilename;
|
|
|
|
|
using support::suffixIs;
|
|
|
|
|
using support::sum;
|
|
|
|
|
|
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
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
void DepTable::insert(FileName const & f, bool upd)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
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) {
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::DEPEND) << " CRC..." << flush;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
di.crc_cur = sum(f);
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::DEPEND) << "done." << endl;
|
2001-11-27 02:56:55 +00:00
|
|
|
|
struct stat f_info;
|
2006-11-26 21:30:39 +00:00
|
|
|
|
stat(f.toFilesystemEncoding().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 {
|
2007-04-01 10:09:49 +00:00
|
|
|
|
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()
|
|
|
|
|
{
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::DEPEND) << "Updating DepTable..." << endl;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
time_type const start_time = 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;
|
2006-11-26 21:30:39 +00:00
|
|
|
|
if (stat(itr->first.toFilesystemEncoding().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;
|
2007-04-01 10:09:49 +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;
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::DEPEND) << itr->first << " CRC... " << flush;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
di.crc_cur = sum(itr->first);
|
2007-04-01 10:09:49 +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.
|
2007-04-01 10:09:49 +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
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
time_type const time_sec = current_time() - start_time;
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::DEPEND) << "Finished updating DepTable ("
|
2002-01-16 22:17:38 +00:00
|
|
|
|
<< 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
bool DepTable::haschanged(FileName const & fil) const
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
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) {
|
2006-11-26 21:30:39 +00:00
|
|
|
|
if (suffixIs(cit->first.absFilename(), 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) {
|
2006-11-26 21:30:39 +00:00
|
|
|
|
if (suffixIs(cit->first.absFilename(), 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
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
bool DepTable::exist(FileName const & fil) const
|
2002-01-16 22:17:38 +00:00
|
|
|
|
{
|
|
|
|
|
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) {
|
2006-11-26 21:30:39 +00:00
|
|
|
|
if (suffixIs(cit->first.absFilename(), 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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
void DepTable::remove_file(FileName const & filename)
|
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) {
|
2006-11-26 21:30:39 +00:00
|
|
|
|
if (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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
void DepTable::write(FileName const & f) const
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2006-11-26 21:30:39 +00:00
|
|
|
|
ofstream ofs(f.toFilesystemEncoding().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) {
|
2007-04-01 15:09:08 +00:00
|
|
|
|
// Store the second (most recently calculated)
|
|
|
|
|
// CRC value.
|
|
|
|
|
// The older one is effectively set to 0 upon re-load.
|
|
|
|
|
LYXERR(Debug::DEPEND) << "Write dep: "
|
|
|
|
|
<< cit->second.crc_cur << ' '
|
|
|
|
|
<< cit->second.mtime_cur << ' '
|
|
|
|
|
<< cit->first << endl;
|
|
|
|
|
|
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
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
bool DepTable::read(FileName const & f)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2006-11-26 21:30:39 +00:00
|
|
|
|
ifstream ifs(f.toFilesystemEncoding().c_str());
|
1999-11-04 01:40:20 +00:00
|
|
|
|
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);
|
2007-04-01 15:09:08 +00:00
|
|
|
|
|
|
|
|
|
LYXERR(Debug::DEPEND) << "Read dep: "
|
|
|
|
|
<< di.crc_cur << ' ' << di.mtime_cur << ' ' << nome << endl;
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
deplist[FileName(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
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|