]> git.lyx.org Git - lyx.git/blob - src/DepTable.C
7426d1eb4df1bc93409d3f98f7ac50cc1437d99c
[lyx.git] / src / DepTable.C
1 /* This file is part of
2  * ====================================================== 
3  * 
4  *           LyX, The Document Processor
5  *           Copyright 1995 Matthias Ettrich
6  *           Copyright 1995-2001 The LyX Team.
7  *
8  *           This file is Copyright 1996-2001
9  *           Lars Gullik Bjønnes
10  *
11  * ====================================================== 
12  */
13
14 #include <config.h>
15
16 #ifdef __GNUG__
17 #pragma implementation
18 #endif
19
20 #include "DepTable.h"
21 #include "support/lyxlib.h"
22 #include "support/filetools.h"
23 #include <fstream>
24 #include "debug.h"
25
26 using std::make_pair;
27 using std::ofstream;
28 using std::ifstream;
29 using std::endl;
30
31 void DepTable::insert(string const & fi,
32                       bool upd,
33                       unsigned long one,
34                       unsigned long two)
35 {
36         // not quite sure if this is the correct place for MakeAbsPath
37         string f = MakeAbsPath(fi);
38         if (deplist.find(f) == deplist.end()) {
39                 if (upd) {
40                         one = two;
41                         two = lyx::sum(f);
42                 }
43                 deplist[f] = make_pair(one, two);
44         }
45 }
46                 
47
48 void DepTable::update()
49 {
50         for (DepList::iterator itr = deplist.begin();
51             itr != deplist.end();
52             ++itr) {
53                 unsigned long const one = itr->second.second;
54                 unsigned long const two = lyx::sum(itr->first);
55                 itr->second = make_pair(one, two);
56                 if (lyxerr.debugging(Debug::DEPEND)) {
57                         lyxerr << "Update dep: " << itr->first << " "
58                                << one << " " << two;
59                         if (one != two)
60                                 lyxerr << " +";
61                         lyxerr << endl;
62                 }
63         }
64 }
65
66
67 bool DepTable::sumchange() const
68 {
69         for (DepList::const_iterator cit = deplist.begin();
70              cit != deplist.end();
71              ++cit) {
72                 if ((*cit).second.first != cit->second.second) return true;
73         }
74         return false;
75 }
76
77
78 bool DepTable::haschanged(string const & f) const
79 {
80         // not quite sure if this is the correct place for MakeAbsPath
81         string fil = MakeAbsPath(f);
82         DepList::const_iterator cit = deplist.find(fil);
83         if (cit != deplist.end()) {
84                 if (cit->second.first != cit->second.second
85                     && cit->second.second != 0)
86                         return true;
87         }
88         return false;
89 }
90
91
92 bool DepTable::extchanged(string const & ext) const
93 {
94         for (DepList::const_iterator cit = deplist.begin();
95              cit != deplist.end();
96              ++cit) {
97                 if (suffixIs(cit->first, ext)) {
98                         if (cit->second.first != cit->second.second)
99                                 return true;
100                 }
101         }
102         return false;
103 }
104
105
106 bool DepTable::exist(string const & fil) const
107 {
108         DepList::const_iterator cit = deplist.find(fil);
109         if (cit != deplist.end()) return true;
110         return false;
111 }
112
113
114 void DepTable::remove_files_with_extension(string const & suf)
115 {
116         DepList tmp;
117         // we want const_iterator (Lgb)
118         for (DepList::iterator cit = deplist.begin();
119              cit != deplist.end(); ++cit) {
120                 if (!suffixIs(cit->first, suf))
121                         tmp[cit->first] = cit->second;
122         }
123         deplist.swap(tmp);
124 }
125
126
127 void DepTable::write(string const & f) const
128 {
129         ofstream ofs(f.c_str());
130         for (DepList::const_iterator cit = deplist.begin();
131              cit != deplist.end(); ++cit) {
132                 if (lyxerr.debugging(Debug::DEPEND)) {
133                         lyxerr << "Write dep: "
134                                << cit->first << " "
135                                << cit->second.first << " "
136                                << cit->second.second << endl;
137                 }
138                 ofs << cit->first << " "
139                     << cit->second.first << " "
140                     << cit->second.second << endl;
141         }
142 }
143
144
145 void DepTable::read(string const & f)
146 {
147         ifstream ifs(f.c_str());
148         string nome;
149         unsigned long one = 0;
150         unsigned long two = 0;
151         while(ifs >> nome >> one >> two) {
152                 if (lyxerr.debugging(Debug::DEPEND)) {
153                         lyxerr << "Read dep: "
154                                << nome << " "
155                                << one << " "
156                                << two << endl;
157                 }
158                 deplist[nome] = make_pair(one, two);
159         }
160 }