File: adstring.h

package info (click to toggle)
menu 2.1.5-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,476 kB
  • ctags: 1,251
  • sloc: cpp: 6,222; ansic: 2,306; sh: 453; makefile: 296; sed: 93
file content (315 lines) | stat: -rw-r--r-- 7,378 bytes parent folder | download
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
//   -*- mode: c++; -*-

//#include <Regex.h>

#ifndef ADSTRING_H
#define ADSTRING_H

#include "regex-c.h"
#include <iostream.h>
#include <fstream.h>
#include <vector.h>
#include <string>
#include <locale.h>
#include <libintl.h>
#include <stdio.h>

#define LOCALEDIR "/usr/share/locale"
#define _(String) gettext(String)

#define MAX_LINE 10240  


// ************* String.h replacement code:
class Regex;

class String:public string{
public:
  //  operator const char*() const{
  //  return c_str();
  //};
  String():string(){};
  String(string r):string(r){};
  String(char *s):string(s){};
  string after(size_type pos){
    return substr(pos);
  }
  bool contains(const string &sub, size_type pos=0) const{
    size_type i;
    if(this->length() < (sub.length()+pos))
      return false;
    for(i=0; 
	(i<sub.length())&&(sub[i]==(*this)[i+pos]); 
	i++)
      ;
    return i==sub.length();
  }
  bool contains(char c){
    return find(c) != npos;
  }
};

typedef vector <String> StrVec;
typedef vector <const String> cStrVec;


template <class charT, class Alloc>
bool
operator== (const string &left, const char *right) {
    return left.compare(string(right)) == 0;
}


class Regex{
  struct re_pattern_buffer* patt;
public:
  Regex(const char *s);
  struct re_pattern_buffer* pattern() const {return patt;};
};


extern String upcase(const String& x);
extern String Sprintf(const char *, String &);
int readline(istream& s, String& x, 
		char terminator = '\n');//,int discard_terminator = 1);



// ************* "own" string handling code:


class parsestream;
class parseinfo{
public:
  vector<int> lineno;
  String buffer;
  string::size_type pos;
  vector<String> fname;

public:
  int    linenumber(){return lineno[lineno.size()-1];}
  void set_linenumber(int l){
    lineno[lineno.size()-1]=l;
  }
  String filename(){  
    return fname [fname.size()-1];
  }
  void set_filename(const String &s){
    fname[fname.size()-1]=s;
  }
  virtual ~parseinfo();
};

// ************* exceptions:

class genexcept{
public:
  virtual void report(){
    cerr<<message()<<endl;
  }
  virtual String message(){
    return "Unknown Error";
  }
  virtual ~genexcept(){};
};

class except_pi: public genexcept{
public:
  parsestream *pi;
  except_pi(parsestream *p);
  void report();
};

class except_String: public genexcept{
public:
  String msg;
  except_String(String s){
    msg=s;
  };
  String message(){
    return Sprintf(_("Unknown Error, message=%s"),msg);
  }
};

class except_pi_String: public except_pi{
public:
  String msg;
  except_pi_String(parsestream *p, String s);
  String message(){
    return Sprintf(_("Unknown Error, message=%s"),msg);
  }
};


class endoffile: public except_pi{
public:
  endoffile(parsestream *p):except_pi(p){};
  String message(){return _("Unexpected end of file");}
};
class endofline: public except_pi{
public:
  endofline(parsestream *p):except_pi(p){};
  String message(){return _("Unexpected end of line");}
};
class ident_expected: public except_pi{
public:
  ident_expected(parsestream *p):except_pi(p){};
  const char *errormsg(){return _("Identifier expected");}
};
class char_expected: public except_pi_String{
public:
  char_expected(parsestream *p, String s):except_pi_String(p,s){};
  String message(){
    return Sprintf(_("Expected: \"%s\"."), msg);
  };
};
class char_unexpected: public except_pi_String{
public:
  char_unexpected(parsestream *p, String s):except_pi_String(p,s){};
  String message(){
    return Sprintf(_("Unexpected character :\"%s\"."),msg);
  };
};  

class unexpectedtrailing: public except_pi_String{
public:
  unexpectedtrailing(parsestream *p, String s):except_pi_String(p,s){};
  String message(){
    return Sprintf(_("Trailing chars on line :\"%s\"."),msg);
  };
};  

class boolean_expected: public except_pi_String{
public:
  boolean_expected(parsestream *p, String s):except_pi_String(p,s){};
  String message(){
    return Sprintf(_("Boolean (eighter true or false) expeced.\n"
		     "Found: \"%s\""),msg);
  };
};  

class ferror_open: public except_pi_String{
public:
  ferror_open(parsestream *p, String s):except_pi_String(p,s){};
  String message(){
    return Sprintf(_("Unable to open file \"%s\""),msg);
  };
};

class unknown_compat: public except_pi_String{
public:
  unknown_compat(parsestream *p, String s):except_pi_String(p,s){};
  String message(){
    return Sprintf(_("Unknown compat mode: \"%s\""),msg);
  };
};



class parsestream: public parseinfo{
public:
  enum eol_type {eol_newline, eol_semicolon};
private:
  vector<istream *> i;
  bool in_constructor;
  void new_line();
  void preprocess(String &s);
  istream *current_istr(){return i[i.size()-1];};
  void close_file();
  void new_file(const String &s);
  bool stdin_file;
  eol_type eolmode;
  String otherdir;
public:
  void init(istream *in, String s, bool stin){
    stdin_file=stin;

    if(!in_constructor){
      if(!*in||!in->good())
	throw ferror_open(this, s);
    }
    pos=0;
    eolmode=eol_newline;
    i.push_back(in); 
    lineno.push_back(0);
    fname.push_back(s);

    new_line();
  }
  parsestream(istream &in, String other=""){

    in_constructor=true;
    otherdir=other;
    init(&in,_("(probably) stdin"),true);
    in_constructor=false;
  };
  parsestream(const String &s, String other=""){
    istream *f=new ifstream(s.c_str());

    if(!f && (s[0]!='/')){
      String add=other+"/"+s;
      f=new ifstream(add.c_str());
    }
    in_constructor=true;
    otherdir=other;
    init(f,s,false);
    in_constructor=false;
  };
  ~parsestream(){
    //delete i;
  };
  bool good(){
    return i.size();//&&current_istr() && (*current_istr());
  };
  char get_char();
  char put_back(char);
  String get_name();
  String get_name(const Regex &);
  String get_eq_name();
  String get_Stringconst();
  String get_eq_Stringconst();
  bool   get_boolean();
  bool   get_eq_boolean();
  int    get_integer();
  int    get_eq_integer();
  double get_double();
  double get_eq_double();
  String get_line();
  void skip_line();
  void skip_space();
  void skip_char(char expect);
  void seteolmode(eol_type mode);
  //int linenumber(){return linenumber();};
  //String filename(){return fname;};
};

extern String rmtrailingspace(String &s);
extern String escape_doublequotes(const String &s);
extern String escape_String(const String &s, const String &esc);
extern String escapewith_String(const String &s, 
				const String &esc, 
				const String &with);
extern String tolower_String(const String &s);
extern String toupper_String(const String &s);
extern String replacewith_String(const String &s, 
				 const String &replace, 
				 const String &with);
extern String cppesc_String(const String &s);
extern String replace(String s,char match, char replace);
extern int Stringtoi(const String &s);
extern String itoString(int i);
extern String itohexString(int i);
extern String sort_hotkey(String s);
extern String String_parent(String s);
extern String String_basename(String s);
extern String String_stripdir(String s);
extern String String_lastname(String s);
extern void break_char(const String &sec,StrVec &sec_vec, char);
extern void break_slashes(const String &sec,StrVec &sec_vec);
extern void break_commas(const String &sec,StrVec &sec_vec);
extern const char *ldgettext(const char *language, 
			     const char *domain,
			     const char *msgid);


#endif /*ADSTRING_H*/