File: util.h

package info (click to toggle)
eboard 1.1.1-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,548 kB
  • ctags: 2,803
  • sloc: cpp: 24,724; perl: 1,012; sh: 866; makefile: 66
file content (335 lines) | stat: -rw-r--r-- 7,238 bytes parent folder | download | duplicates (5)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* $Id: util.h,v 1.20 2007/06/09 11:35:06 bergo Exp $ */

/*

    eboard - chess client
    http://eboard.sourceforge.net
    Copyright (C) 2000-2001 Felipe Paulo Guazzi Bergo
    bergo@seul.org

    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, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/



#ifndef EBOARD_UTIL_H
#define EBOARD_UTIL_H 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stl.h"

class FileFinder {
 public:
  FileFinder();
  ~FileFinder();
  void addDirectory(char *dir);
  void addDirectory(string &dir);
  void addMyDirectory(char *dir);
  void addMyDirectory(string &dir);

  int  find(char *name,char *fullpath);
  int  find(string &name, string &out);

  int getPathCount();
  string &getPath(unsigned int i);

 private:
  vector<string> path;
};

class EboardFileFinder : public FileFinder {
 public:
  EboardFileFinder();
};

// pattern matching wizardry

class Pattern {
 public:
  Pattern();
  virtual ~Pattern();

  // results: 0=match -1 = no match 1=multiple matches; on
  // matches, last contains the first character after the match
  virtual int tryMatch(list<char>::iterator & first,
		       list<char>::iterator & last)=0;
  virtual void reset();
  
  bool eternal;
};

class ExactStringPat : public Pattern {
 public:
  ExactStringPat(char *pat);
  virtual ~ExactStringPat() {}

  virtual int tryMatch(list<char>::iterator & first,
		       list<char>::iterator & last);
 protected:
  list<char> pattern;
};

// same thing, case insensitive
class CIExactStringPat : public ExactStringPat {
 public:
  CIExactStringPat(char *pat) : ExactStringPat(pat) { }
  virtual ~CIExactStringPat() {}
  
  int tryMatch(list<char>::iterator & first,
	       list<char>::iterator & last);
};

// you probably know this simply as the '*' wildcard
class KleeneStarPat : public Pattern {
 public:
  KleeneStarPat();
  int tryMatch(list<char>::iterator & first,
	       list<char>::iterator & last);
  void reset();
 private:
  int CallCount;
};

// string of any size (will try to maximize) made up of
// characters from a given set
class SetPat : public Pattern {
 public:
  SetPat();

  void addToSet(char c);
  void addToSet(char *s);
  void clearSet();

  void includeUppercase();
  void includeLowercase();
  void includeLetters();
  void includeNumbers();

  int tryMatch(list<char>::iterator & first,
	       list<char>::iterator & last);
 protected:
  bool *myset;
 private:
  bool inSet(char c);
};

// generic (eats 256 bytes per object -- unwise)
class CharSetPat : public SetPat {
 public:
  CharSetPat();
 private:
  bool theset[256];
};

// calling include___() or addToSet() or clearSet() on
// any object of the *SetPat classes below is a shooting
// offense -> you'll mess with all other objects of the
// same class.
// these classes eat 256 bytes _per class_, not per instance.

// %n - digits, '+' and '-'
class PercNSetPat : public SetPat {
 public:
  PercNSetPat();
 private:
  static bool theset[256];
};

// %s - A-Z,a-z
class PercSSetPat : public SetPat {
 public:
  PercSSetPat();
 private:
  static bool theset[256];
};

// %S - A-Z,a-z,0-9,_
class PercUpperSSetPat : public SetPat {
 public:
  PercUpperSSetPat();
 private:
  static bool theset[256];
};

// %a - A-Z,a-z,()*
class PercASetPat : public SetPat {
 public:
  PercASetPat();
 private:
  static bool theset[256];
};

// %A - %a U [0-9]
class PercUpperASetPat : public SetPat {
 public:
  PercUpperASetPat();
 private:
  static bool theset[256];
};

// %b - spaces and tabs
class PercBSetPat : public SetPat {
 public:
  PercBSetPat();
 private:
  static bool theset[256];
};

// %r - almost everything except spaces and tabs
class PercRSetPat : public SetPat {
 public:
  PercRSetPat();
 private:
  static bool theset[256];
};

// %N - digits only
class PercUpperNSetPat : public SetPat {
 public:
  PercUpperNSetPat();
 private:
  static bool theset[256];
};

class PatternMatcher {
 public:
  PatternMatcher();
  virtual ~PatternMatcher();

  void append(Pattern *p);
  void reset();
  int  match(const char *stryng); // binds local 'data' list
  int  match();             // uses PatternBinder bound data

  char *getToken(int index);
  
  void bindData(list<char> *newdata);

 protected:
  void cleanUp();

 private:
  int recursiveMatch(list<Pattern *>::iterator & pat,
		     list<char>::iterator & p0,
		     list<char>::iterator & p1);

  void lesserCleanUp();

  list<Pattern *> pattern;
  list<char>      data;
  list<list<char>::iterator *> matches;
  list<char> *    bound;
  
  int bufsize;
  char *token;
};

class ExtPatternMatcher : public PatternMatcher {
 public:
  ExtPatternMatcher();

  void set(char *hrp); // set from a human readable pattern

  // for patterns created with "set", these are the %s , %n
  char *getSToken(int index);
  char *getNToken(int index);
  char *getAToken(int index);
  char *getBToken(int index);
  char *getRToken(int index);
  char *getStarToken(int index);

 private:
  char *getXToken(vector<int> &v, int index);

  vector<int> atokens;
  vector<int> btokens;
  vector<int> rtokens;
  vector<int> stokens;
  vector<int> ntokens;
  vector<int> startokens;
};

// helper class to encapsulate char * -> list<char>
// conversion for pattern matchers that will perform
// matching over a common string
class PatternBinder {
 public:
  void add(PatternMatcher *pm0,...); // arguments are pointers to PatternMatcher objects
  void prepare(const char *target);
  
 private:
  list<PatternMatcher *> group;
  list<char> data;
};

// read from a (possibly) compressed stream. currently
// supports gzip only
// somewhat compatible to istream (not everything is implemented)
// gzip must be in the path. /tmp must be writeable and must have enough
// space for the uncompressed file

// whether the file is gzip'ed or not, it's determined by the .gz
// extension in the name

class zifstream {
  
 public:
  zifstream();
  zifstream(char *name);

  void          open(char *name);
  bool          getline(char *ptr, int len, char delim='\n');
  
  bool          seekg(unsigned long offset);
  unsigned long tellg();
  
  bool          operator!();
  bool          eof();
  void          close();
  bool          fail();
  bool          is_open();

 private:
  ifstream x;
  bool isopen;
  bool compressed;
  int  failure;
  char tmpfile[280];
  char ungzfile[280];
  char origfile[256];

  void copen();
  void cclose();

}; 

class Timestamp {
 public:
  Timestamp(int sec, int usec);
  Timestamp();

  Timestamp & operator=(Timestamp &t);
  double operator-(Timestamp &t);
  static Timestamp & now();

 private:
  int S,U;

};

#endif