Kiln » TortoiseHg » TortoiseHg
Clone URL:  
Pushed to one repository · View In Graph Contained in 0.8, 0.8.1, and 0.8.2

Directory: eliminate call recursion in getdir()

Changeset 5ab289f25d6a

Parent a58fba69ec2d

by Adrian Buehlmann

Changes to one file · Browse files at 5ab289f25d6a Showing diff from parent a58fba69ec2d Diff from another changeset...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
 
162
163
164
165
166
 
 
 
 
167
168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
 
161
162
163
164
165
 
166
167
168
169
170
 
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
   // Copyright (C) 2009 Benjamin Pollack  // Copyright (C) 2009 Adrian Buehlmann  //  // This program is free software: you can redistribute it and/or modify  // it under the terms of the GNU General Public License as published by  // the Free Software Foundation, either version 2 of the License, or  // (at your option) any later version.  //  // This program is distributed in the hope that it will be useful,  // but WITHOUT ANY WARRANTY; without even the implied warranty of  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  // GNU General Public License for more details.  //  // You should have received a copy of the GNU General Public License  // along with this program. If not, see <http://www.gnu.org/licenses/>.    #include "stdafx.h"    #include "Directory.h"  #include "Winstat.h"      Directory::Directory(   Directory* p, const std::string& n, const std::string& basepath  ):   parent_(p), name_(n), tickcount_(0), status_(-1)  {   path_ = basepath;   if (!n.empty())   path_ += "/" + n;  }      Directory::~Directory()  {   for (DirsT::iterator i = subdirs_.begin(); i != subdirs_.end(); ++i)   {   delete *i;   }  }      int splitbase(const std::string& n, std::string& base, std::string& rest)  {   if (n.empty())   return 0;     size_t x = n.find_first_of ('/');   if (x == std::string::npos)   {   base.clear();   rest = n;   return 1;   }     if (x == 0 || x == n.length()-1)   return 0;     base = n.substr(0, x);   rest = n.substr(x+1);     return 1;  }      int Directory::add(const std::string& n_in, Direntry& e)  {   std::string base;   std::string rest;     std::string n = n_in;   Directory* cur = this;     for (;;)   {     if (!splitbase(n, base, rest)) {   TDEBUG_TRACE("Directory(" << path() << ")::add(" << n_in   << "): splitbase returned 0");   return 0;   }     if (base.empty())   {   e.name = n;   cur->files_.push_back(e);   return 1;   }     Directory* d = 0;   for (DirsT::iterator i = cur->subdirs_.begin();   i != cur->subdirs_.end(); ++i)   {   if ((*i)->name_ == base) {   d = *i;   break;   }   }     if (!d)   {   d = new Directory(cur, base, cur->path());   cur->subdirs_.push_back(d);   }     n = rest;   cur = d;   }  }      const Direntry* Directory::get(const std::string& n_in) const  {   std::string base;   std::string rest;     std::string n = n_in;   const Directory* cur = this;     for (;;)   {   loopstart:     if (!splitbase(n, base, rest))   {   TDEBUG_TRACE("Directory(" << path() << ")::get("   << n_in << "): splitbase returned 0");   return 0;   }     if (base.empty())   {   for (FilesT::const_iterator i = cur->files_.begin();   i != cur->files_.end(); ++i)   {   if (i->name == n)   return &(*i);   }   return 0;   }     for (DirsT::const_iterator i = cur->subdirs_.begin();   i != cur->subdirs_.end(); ++i)   {   if ((*i)->name_ == base)   {   cur = *i;   n = rest;   goto loopstart;   }   }     TDEBUG_TRACE("Directory(" << path() << ")::get("   << n_in << "): unknown subdir");   return 0;   }  }     -Directory* Directory::getdir(const std::string& n) +Directory* Directory::getdir(const std::string& n_in)  {   std::string base;   std::string rest;   - if (!splitbase(n, base, rest)) + std::string n = n_in; + const Directory* cur = this; + + for (;;)   { - TDEBUG_TRACE("Directory(" << path() << ")::getdir(" << n << "): splitbase returned 0"); + loopstart: + + if (!splitbase(n, base, rest)) + { + TDEBUG_TRACE("Directory(" << path() << ")::getdir(" + << n_in << "): splitbase returned 0"); + return 0; + } + + const bool leaf = base.empty(); + const std::string& searchstr = (leaf ? n : base); + + for (DirsT::const_iterator i = cur->subdirs_.begin(); + i != cur->subdirs_.end(); ++i) + { + if ((*i)->name_ == searchstr) + { + if (leaf) + return *i; + cur = *i; + n = rest; + goto loopstart; + } + } +   return 0;   } - - const bool leaf = base.empty(); - const std::string& searchstr = (leaf ? n : base); - - for (DirsT::const_iterator i = subdirs_.begin(); i != subdirs_.end(); ++i) - { - if ((*i)->name_ == searchstr) - { - if (leaf) - return *i; - return (*i)->getdir(rest); - } - } - - return 0;  }      char Directory::status_imp(const std::string& hgroot)  {   bool added = false;     std::vector<Directory*> todo;   todo.push_back(this);     Winstat stat;   std::string basepath;     while (!todo.empty())   {   Directory* const d = todo.back();   todo.pop_back();     //TDEBUG_TRACE("Directory(" << path() << ")::status_imp: "   // "popped '" << d->path() << "'");     if (!d->files_.empty())   {   basepath = hgroot + "/" + d->path() + "/";     for (FilesT::iterator i = d->files_.begin(); i != d->files_.end(); ++i)   {   if (i->state == 'r')   return 'M'; // file was removed, report dir as modified     std::string p = basepath + i->name;   if (0 != stat.lstat(p.c_str()))   return 'M'; // file is missing, report dir as modified     char s = i->status(stat);     if (s == 'M')   return 'M';   if (s == 'A')   added = true;   }   }     todo.insert(todo.end(), d->subdirs_.begin(), d->subdirs_.end());   }     if (added)   return 'A';     return 'C';  }      char Directory::status(const std::string& hgroot)  {   if (status_ != -1)   {   unsigned tc = GetTickCount();   if (tc - tickcount_ < 3000) {   return status_;   }   }     status_ = status_imp(hgroot);   tickcount_ = GetTickCount();     return status_;  }      void Directory::print() const  {   for (DirsT::const_iterator i = subdirs_.begin(); i != subdirs_.end(); ++i)   {   const Directory* d = *i;   if (!d)   {   TDEBUG_TRACE("Directory(" << path() << ")::print: error: d is 0");   return;   }   d->print();   }     std::string base = path();     time_t t;   std::string s;   char* ctime_res = 0;     for (FilesT::const_iterator i = files_.begin(); i != files_.end(); ++i)   {   std::string p = (!base.empty() ? base + "/" + i->name : i->name);   t = i->mtime;   ctime_res = ctime(&t);   if (ctime_res) {   s = ctime_res;   s.resize(s.size() - 1); // strip ending '\n'   }   else {   s = "unset";   }   printf(   "%c %6o %10u %-24s %s\n",   i->state, i->mode, i->size, s.c_str(), p.c_str()   );   }  }