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
|
|
|
*
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Lars Gullik Bjønnes
|
2003-08-23 00:17:00 +00:00
|
|
|
* \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
|
|
|
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/debug.h"
|
2020-10-19 08:51:00 +00:00
|
|
|
#include "support/FileName.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
|
|
|
|
1999-11-04 01:40:20 +00:00
|
|
|
#include <fstream>
|
2000-03-28 02:18:55 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
using namespace lyx::support;
|
2007-07-17 17:46:54 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
|
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;
|
2017-03-10 10:23:55 +00:00
|
|
|
if (upd && f.exists()) {
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::DEPEND, " CRC...");
|
2007-11-30 09:23:44 +00:00
|
|
|
di.crc_cur = f.checksum();
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::DEPEND, "done.");
|
2017-03-10 10:23:55 +00:00
|
|
|
di.mtime_cur = f.lastModified();
|
2002-01-16 22:17:38 +00:00
|
|
|
} 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-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::DEPEND, " Already in DepTable");
|
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-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::DEPEND, "Updating DepTable...");
|
2007-11-29 08:53:39 +00:00
|
|
|
time_t 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()) {
|
2017-03-10 10:23:55 +00:00
|
|
|
FileName const & fn = itr->first;
|
2002-01-16 22:17:38 +00:00
|
|
|
dep_info &di = itr->second;
|
|
|
|
|
2017-03-10 10:23:55 +00:00
|
|
|
if (fn.exists()) {
|
|
|
|
if (di.mtime_cur == fn.lastModified()) {
|
2002-01-16 22:17:38 +00:00
|
|
|
di.crc_prev = di.crc_cur;
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::DEPEND, itr->first << " same mtime");
|
2002-01-16 22:17:38 +00:00
|
|
|
} else {
|
|
|
|
di.crc_prev = di.crc_cur;
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::DEPEND, itr->first << " CRC... ");
|
2017-03-10 10:23:55 +00:00
|
|
|
di.crc_cur = fn.checksum();
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::DEPEND, "done");
|
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.
|
2017-03-10 10:23:55 +00:00
|
|
|
LYXERR(Debug::DEPEND, fn << " doesn't exist. removing from DepTable.");
|
2002-01-16 22:17:38 +00:00
|
|
|
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
|
|
|
}
|
2007-11-29 08:53:39 +00:00
|
|
|
time_t const time_sec = current_time() - start_time;
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::DEPEND, "Finished updating DepTable ("
|
2007-11-29 08:53:39 +00:00
|
|
|
<< long(time_sec) << " sec).");
|
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) {
|
2012-10-27 13:45:27 +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
|
|
|
}
|
|
|
|
|
|
|
|
|
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) {
|
2010-04-21 01:19:09 +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) {
|
2012-10-27 13:45:27 +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) {
|
2010-04-21 01:19:09 +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.
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::DEPEND, "Write dep: "
|
2007-04-01 15:09:08 +00:00
|
|
|
<< cit->second.crc_cur << ' '
|
|
|
|
<< cit->second.mtime_cur << ' '
|
2007-11-15 20:04:51 +00:00
|
|
|
<< cit->first);
|
2007-04-01 15:09:08 +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
|
|
|
|
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
|
|
|
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::DEPEND, "Read dep: "
|
|
|
|
<< di.crc_cur << ' ' << di.mtime_cur << ' ' << nome);
|
2007-04-01 15:09:08 +00:00
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
deplist[FileName(nome)] = di;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2020-10-05 10:38:09 +00:00
|
|
|
return !deplist.empty();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|