]> git.lyx.org Git - lyx.git/blob - src/DepTable.C
e3a5acc2621217fcbad6bb0f92b206b590333a15
[lyx.git] / src / DepTable.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *           Copyright (C) 1995 Matthias Ettrich
6  *           Copyright (C) 1995-1998 The LyX Team.
7  *
8  *           This file is Copyright (C) 1996-1998
9  *           Lars Gullik Bjønnes
10  *
11  * ======================================================
12  */
13
14 #include <config.h>
15
16 #include "DepTable.h"
17 #include "support/lyxlib.h"
18 #include "support/filetools.h"
19
20 DepTable::DepTable()
21 {
22         new_sum = 0;
23         old_sum = 0;
24         next = 0;
25 }
26
27
28 DepTable::DepTable(string const & f,
29                    bool upd,
30                    unsigned long one,
31                    unsigned long two)
32 {
33         // not quite sure if this is the correct place for MakeAbsPath
34         file = MakeAbsPath(f);
35         new_sum = 0; old_sum = 0;
36         if (one != 0)
37                 new_sum = one;
38         if (two != 0)
39                 old_sum = two;
40         if (upd) {
41                 old_sum = new_sum;
42                 new_sum = lyxsum(file.c_str());
43         }
44         if (lyxerr.debugging()) {
45                 char tmp1[256];
46                 char tmp2[256];
47                 sprintf(tmp1, "%lu", new_sum);
48                 sprintf(tmp2, "%lu", old_sum);
49                 lyxerr.debug() << "New file inserted in deplog: "
50                                << file << " "
51                                << tmp1 << " " << tmp2 << endl;
52         }
53         next = 0;
54 }
55
56
57 void DepTable::insert(string const & fi,
58                       bool upd,
59                       unsigned long one,
60                       unsigned long two)
61 {
62         // not quite sure if this is the correct place for MakeAbsPath
63         string f = MakeAbsPath(fi);
64         if (f == file) return; // exist already in the log
65         if (next)
66                 next->insert(f, upd, one, two);
67         else
68                 next = new DepTable(f, upd, one, two);
69 }
70                 
71
72 void DepTable::update()
73 {
74         if (!file.empty()) {
75                 old_sum = new_sum;
76                 new_sum = lyxsum(file.c_str());
77                 if (lyxerr.debugging()) {
78                         char tmp1[256];
79                         char tmp2[256];
80                         sprintf(tmp1, "%lu", new_sum);
81                         sprintf(tmp2, "%lu", old_sum);
82                         lyxerr.debug() << "update: " << file << " "
83                                        << tmp1 << " " << tmp2 << endl;
84                 }
85         }
86         if (next) next->update();
87 }
88
89
90 bool DepTable::sumchange()
91 {
92         bool ret = false;
93         
94         if (!file.empty()) {
95                 if (old_sum != new_sum) ret = true;
96         }
97         if (!ret && next) ret = next->sumchange();
98
99         return ret;
100 }
101
102
103 bool DepTable::haschanged(string const & f)
104 {
105         // not quite sure if this is the correct place for MakeAbsPath
106         string fil = MakeAbsPath(f);
107         bool ret = false;
108
109         if (!fil.empty() && !file.empty() && fil == file) {
110                 if (new_sum != old_sum && new_sum != 0)
111                         ret = true;
112         }
113         if (!ret && next) ret = next->haschanged(fil);
114         return ret;
115 }
116
117
118 void DepTable::write(string const&f)
119 {
120         FilePtr fp(f, FilePtr::write);
121         if (fp() && next) next->write(fp());
122 }
123
124
125 void DepTable::read(string const &f)
126 {
127         FilePtr fp(f, FilePtr::read);
128         if (fp()) { // file opened
129                 char nome[256];
130                 unsigned long one = 0;
131                 unsigned long two = 0;
132                 // scan the file line by line
133                 // return true if the two numbers on the line is different
134                 int ret = 0;
135                 while (!feof(fp())) {
136                         ret = fscanf(fp(), "%s %lu %lu",
137                                      nome, &one, &two);
138                         if (ret !=3) continue;
139                         if (lyxerr.debugging()) {
140                                 char tmp1[255];
141                                 char tmp2[255];
142                                 sprintf(tmp1, "%lu", one);
143                                 sprintf(tmp2, "%lu", two);
144                                 lyxerr.debug() << "read dep: "
145                                                << nome << " " << tmp1
146                                                << " " << tmp2 << endl;
147                         }
148                         insert(string(nome), false, one, two);
149                 }
150         }
151 }
152
153
154 void DepTable::write(FILE * f)
155 {
156         if (lyxerr.debugging()) {
157                 char tmp1[255];
158                 char tmp2[255];
159                 sprintf(tmp1, "%lu", new_sum);
160                 sprintf(tmp2, "%lu", old_sum);
161                 lyxerr << "Write dep: " << file << " "
162                        << tmp1 << " " << tmp2 << endl;
163         }
164         fprintf(f, "%s %lu %lu\n", file.c_str(),
165                 new_sum, old_sum);
166         if (next)
167                 next->write(f);
168 }