]> git.lyx.org Git - lyx.git/blob - src/DepTable.C
bug 183
[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 "debug.h"
22
23 #include "support/lyxlib.h"
24 #include "support/filetools.h"
25 #include "support/lstrings.h"
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30
31 #include <fstream>
32
33 using std::make_pair;
34 using std::ofstream;
35 using std::ifstream;
36 using std::endl;
37
38 void DepTable::insert(string const & fi,
39                       bool upd,
40                       unsigned long one,
41                       unsigned long two)
42 {
43         // not quite sure if this is the correct place for MakeAbsPath
44         string f = MakeAbsPath(fi);
45         if (deplist.find(f) == deplist.end()) {
46                 long mtime = 0;
47                 if (upd) {
48                         one = two;
49                         two = lyx::sum(f);
50                         struct stat f_info;
51                         stat(fi.c_str(), &f_info);
52                         mtime = f_info.st_mtime;
53                 }
54                 dep_info di;
55                 di.first = one;
56                 di.second = two;
57                 di.mtime = mtime;
58 #if 0           
59                 deplist[f] = make_pair(one, two);
60 #else
61                 deplist[f] = di;
62 #endif
63         }
64 }
65                 
66
67 void DepTable::update()
68 {
69         for (DepList::iterator itr = deplist.begin();
70             itr != deplist.end();
71             ++itr) {
72                 unsigned long const one = itr->second.second;
73                 unsigned long two = one;
74                 long mtime = itr->second.mtime;
75                 struct stat f_info;
76                 stat(itr->first.c_str(), &f_info);
77
78                 if (mtime != f_info.st_mtime) {
79                         two = lyx::sum(itr->first);
80                         mtime = f_info.st_mtime;
81                 }
82                 
83 #if 0
84                 itr->second = make_pair(one, two);
85 #else
86                 dep_info di;
87                 di.first = one;
88                 di.second = two;
89                 di.mtime = mtime;
90                 
91                 itr->second = di;
92 #endif
93                 if (lyxerr.debugging(Debug::DEPEND)) {
94                         lyxerr << "Update dep: " << itr->first << " "
95                                << one << " " << two;
96                         if (one != two)
97                                 lyxerr << " +";
98                         lyxerr << endl;
99                 }
100         }
101 }
102
103
104 bool DepTable::sumchange() const
105 {
106         for (DepList::const_iterator cit = deplist.begin();
107              cit != deplist.end();
108              ++cit) {
109                 if ((*cit).second.first != cit->second.second) return true;
110         }
111         return false;
112 }
113
114
115 bool DepTable::haschanged(string const & f) const
116 {
117         // not quite sure if this is the correct place for MakeAbsPath
118         string fil = MakeAbsPath(f);
119         DepList::const_iterator cit = deplist.find(fil);
120         if (cit != deplist.end()) {
121                 if (cit->second.first != cit->second.second
122                     && cit->second.second != 0)
123                         return true;
124         }
125         return false;
126 }
127
128
129 bool DepTable::extchanged(string const & ext) const
130 {
131         for (DepList::const_iterator cit = deplist.begin();
132              cit != deplist.end();
133              ++cit) {
134                 if (suffixIs(cit->first, ext)) {
135                         if (cit->second.first != cit->second.second)
136                                 return true;
137                 }
138         }
139         return false;
140 }
141
142
143 bool DepTable::exist(string const & fil) const
144 {
145         DepList::const_iterator cit = deplist.find(fil);
146         if (cit != deplist.end()) return true;
147         return false;
148 }
149
150
151 void DepTable::remove_files_with_extension(string const & suf)
152 {
153         DepList tmp;
154         // we want const_iterator (Lgb)
155         for (DepList::iterator cit = deplist.begin();
156              cit != deplist.end(); ++cit) {
157                 if (!suffixIs(cit->first, suf))
158                         tmp[cit->first] = cit->second;
159         }
160         deplist.swap(tmp);
161 }
162
163
164 void DepTable::write(string const & f) const
165 {
166         ofstream ofs(f.c_str());
167         for (DepList::const_iterator cit = deplist.begin();
168              cit != deplist.end(); ++cit) {
169                 if (lyxerr.debugging(Debug::DEPEND)) {
170                         lyxerr << "Write dep: "
171                                << cit->first << " "
172                                << cit->second.first << " "
173                                << cit->second.second << " "
174                                << cit->second.mtime << endl;
175                 }
176                 ofs << cit->first << " "
177                     << cit->second.first << " "
178                     << cit->second.second << " "
179                     << cit->second.mtime << endl;
180         }
181 }
182
183
184 void DepTable::read(string const & f)
185 {
186         ifstream ifs(f.c_str());
187         string nome;
188         unsigned long one = 0;
189         unsigned long two = 0;
190         unsigned long mtime = 0;
191         
192         while (ifs >> nome >> one >> two >> mtime) {
193                 if (lyxerr.debugging(Debug::DEPEND)) {
194                         lyxerr << "Read dep: "
195                                << nome << " "
196                                << one << " "
197                                << two << " "
198                                << mtime << endl;
199                 }
200                 dep_info di;
201                 di.first = one;
202                 di.second = two;
203                 di.mtime = mtime;
204 #if 0           
205                 deplist[nome] = make_pair(one, two);
206 #else
207                 deplist[nome] = di;
208 #endif
209         }
210 }