rllib  1
rlstring.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  rlmailbox.cpp - description
3  -------------------
4  begin : Wed Jan 02 2008
5  copyright : (C) Lehrig Software Enigineering
6  email : lehrig@t-online.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as *
13  * published by the Free Software Foundation *
14  * *
15  ***************************************************************************/
16 
17 #include <stdarg.h>
18 #include <sys/stat.h>
19 
20 #include "rlstring.h"
21 #include "rlcutil.h"
22 
23 const char rlCRLF[3] = {0x0d, 0x0a, 0};
24 
25 rlString::rlString(const char *text)
26 {
27  txt = new char [strlen(text)+1];
28  tmp = NULL;
29  ::strcpy(txt, text);
30 }
31 
33 {
34  txt = new char [strlen(s2.text())+1];
35  tmp = NULL;
36  ::strcpy(txt,s2.text());
37 }
38 
40 {
41  txt = new char [strlen(s2->text())+1];
42  tmp = NULL;
43  ::strcpy(txt,s2->text());
44 }
45 
47 {
48  txt = new char [strlen(s2.text())+1];
49  tmp = NULL;
50  ::strcpy(txt,s2.text());
51 }
52 
53 
55 {
56  delete [] txt;
57  if(tmp != NULL) delete [] tmp;
58 }
59 
60 rlString& rlString::operator=(const char *s2)
61 {
62  this->setText(s2);
63  return *this;
64 }
65 
67 {
68  this->setText(s2.text());
69  return *this;
70 }
71 
72 rlString& rlString::operator+(const char *s2)
73 {
74  this->cat(s2);
75  return *this;
76 }
77 
79 {
80  this->cat(s2.text());
81  return *this;
82 }
83 
85 {
86  this->cat(s2);
87  return *this;
88 }
89 
91 {
92  this->cat(s2.text());
93  return *this;
94 }
95 
96 int rlString::operator==(const char *s2)
97 {
98  if(strcmp(txt,s2) == 0) return 1;
99  return 0;
100 }
101 
103 {
104  if(strcmp(txt,s2.text()) == 0) return 1;
105  return 0;
106 }
107 
109 {
110  if(strcmp(txt,s2.text()) == 0) return 1;
111  return 0;
112 }
113 
114 int rlString::operator!=(const char *s2)
115 {
116  if(strcmp(txt,s2) != 0) return 1;
117  return 0;
118 }
119 
121 {
122  if(strcmp(txt,s2.text()) != 0) return 1;
123  return 0;
124 }
125 
127 {
128  return txt;
129 }
130 
131 char * rlString::text() const
132 {
133  return txt;
134 }
135 
136 int rlString::setText(const char *text)
137 {
138  int ret = strlen(text);
139  delete [] txt;
140  txt = new char [ret+1];
141  ::strcpy(txt, text);
142  return ret;
143 }
144 
145 int rlString::printf(const char *format, ...)
146 {
147  int ret;
148  char mystring[rl_PRINTF_LENGTH]; // should be big enough
149 
150  va_list ap;
151  va_start(ap,format);
152  ret = rlvsnprintf(mystring, rl_PRINTF_LENGTH - 1, format, ap);
153  va_end(ap);
154  if(ret < 0) return ret;
155  delete [] txt;
156  txt = new char [strlen(mystring)+1];
157  ::strcpy(txt, mystring);
158  return ret;
159 }
160 
161 int rlString::strcpy(const char *text)
162 {
163  char *cptr = txt;
164  int len = strlen(text);
165  txt = new char [len+1];
166  ::strcpy(txt, text);
167  delete [] cptr;
168  return len;
169 }
170 
171 int rlString::cat(const char *text)
172 {
173  char *cptr = txt;
174  int len = strlen(txt) + strlen(text);
175  txt = new char [len+1];
176  ::strcpy(txt, cptr);
177  ::strcat(txt, text);
178  delete [] cptr;
179  return len;
180 }
181 
182 char *rlString::strstr(const char *substring)
183 {
184  if(substring == NULL) return NULL;
185  return ::strstr(txt,substring);
186 }
187 
189 {
191 }
192 
194 {
196 }
197 
198 int rlString::startsWith(const char *startstr)
199 {
200  return ::rlStartsWith(txt,startstr);
201 }
202 
203 int rlString::strnocasecmp(const char *other)
204 {
205  rlString my,o;
206  my.setText(txt);
207  my.lower();
208  o.setText(other);
209  o.lower();
210  return ::strcmp(my.text(),o.text());
211 }
212 
213 int rlString::strnnocasecmp(const char *other, int n)
214 {
215  rlString my,o;
216  my.setText(txt);
217  my.lower();
218  o.setText(other);
219  o.lower();
220  return ::strncmp(my.text(),o.text(),n);
221 }
222 
223 char *rlString::strchr(int c)
224 {
225  return ::strchr(txt,c);
226 }
227 
228 char *rlString::strrchr(int c)
229 {
230  return ::strrchr(txt,c);
231 }
232 
234 {
235  char c;
236  int state=0, inquotas=0, j=0;
237  int len = strlen(txt);
238 
239  for(int i=0;i<len;i++)
240  {
241  c = txt[i];
242  switch(state)
243  {
244  case 0: //START
245  if(c==q)
246  {
247  state=1;
248  inquotas=1;
249  break;
250  }
251  state=1;
252  case 1: // BODY
253  if(inquotas==1)
254  {
255  if(c==q)
256  {
257  inquotas=0;
258  break;
259  }
260  }
261  txt[j++]=c;
262  break;
263  default:
264  break;
265  }
266  }
267  txt[j++]=0;
268  return j;
269 }
270 
272 {
273  if(txt == NULL) return -1;
274  char *cptr = ::strchr(txt,'\n');
275  if(cptr != NULL) *cptr = '\0';
276  return 0;
277 }
278 
279 int rlString::read(const char *filename)
280 {
281  struct stat statbuf;
282 #ifdef RLUNIX
283  if(lstat(filename,&statbuf)) return -1;
284 #else
285  if(stat(filename,&statbuf)) return -1;
286 #endif
287  FILE *fin = fopen(filename,"r");
288  if(fin == NULL) return -1;
289  if(txt != NULL) delete [] txt;
290  txt = new char [statbuf.st_size+1];
291  fread(txt, 1, statbuf.st_size, fin);
292  txt[statbuf.st_size] = '\0';
293  fclose(fin);
294  return statbuf.st_size;
295 }
296 
297 int rlString::write(const char *filename)
298 {
299  if(txt == NULL) return -1;
300  FILE *fout = fopen(filename,"w");
301  if(fout == NULL) return -1;
302  int len = strlen(txt);
303  fprintf(fout,"%s",txt);
304  fclose(fout);
305  return len;
306 }
307 
308 const char *rlString::toFilename()
309 {
310 #ifdef RLUNIX
311  int len = ::strlen(txt);
312  if(tmp != NULL) delete [] tmp;
313  tmp = new char [len+1];
314  ::strcpy(tmp,txt);
315  return tmp;
316 #endif
317 #ifdef RLWIN32
318  char *cptr = &txt[0];
319  if(*cptr == '/') cptr++;
320  int len = ::strlen(cptr);
321  if(tmp != NULL) delete [] tmp;
322  tmp = new char [len+1];
323  ::strcpy(tmp,cptr);
324  cptr = &tmp[0];
325  while(*cptr != '\0')
326  {
327  if(*cptr == '/') *cptr = '\\';
328  cptr++;
329  }
330  return tmp;
331 #endif
332 #ifdef __VMS
333  char *cptr = &txt[0];
334  if(*cptr == '/') cptr++;
335  int len = ::strlen(cptr);
336  if(tmp != NULL) delete [] tmp;
337  tmp = new char [len+1];
338  ::strcpy(tmp,cptr);
339  cptr = ::strchr(&tmp[0],'/');
340  if(cptr != NULL) *cptr = '[';
341  cptr = ::strrchr(&tmp[0],'/');
342  if(cptr != NULL) *cptr = ']';
343  cptr = &tmp[0];
344  while(*cptr != '\0')
345  {
346  if(*cptr == '/') *cptr = '.';
347  cptr++;
348  }
349  return tmp;
350 #endif
351 }
352 
353 const char *rlString::toDirname()
354 {
355 #ifdef RLUNIX
356  int len = ::strlen(txt);
357  if(tmp != NULL) delete [] tmp;
358  tmp = new char [len+1];
359  ::strcpy(tmp,txt);
360  return tmp;
361 #endif
362 #ifdef RLWIN32
363  char *cptr = &txt[0];
364  if(*cptr == '/') cptr++;
365  int len = ::strlen(cptr);
366  if(tmp != NULL) delete [] tmp;
367  tmp = new char [len+1];
368  ::strcpy(tmp,cptr);
369  cptr = &tmp[0];
370  while(*cptr != '\0')
371  {
372  if(*cptr == '/') *cptr = '\\';
373  cptr++;
374  }
375  return tmp;
376 #endif
377 #ifdef __VMS
378  char *cptr = &txt[0];
379  if(*cptr == '/') cptr++;
380  int len = ::strlen(cptr);
381  if(tmp != NULL) delete [] tmp;
382  tmp = new char [len+2];
383  ::strcpy(tmp,cptr);
384  cptr = ::strchr(&tmp[0],'/');
385  if(cptr != NULL) *cptr = '[';
386  cptr = &tmp[0];
387  while(*cptr != '\0')
388  {
389  if(*cptr == '/') *cptr = '.';
390  cptr++;
391  }
392  ::strcat(tmp,"]");
393  return tmp;
394 #endif
395 }
396 
char * tmp
Definition: rlstring.h:158
int read(const char *filename)
Definition: rlstring.cpp:279
const char * toFilename()
Definition: rlstring.cpp:308
const char rlCRLF[3]
Definition: rlstring.cpp:23
int removeQuotas(char c='"')
Definition: rlstring.cpp:233
int rllower(char *str)
Definition: rlcutil.cpp:738
int startsWith(const char *startstr)
Definition: rlstring.cpp:198
int operator!=(const char *s2)
Definition: rlstring.cpp:114
int lower()
Definition: rlstring.cpp:193
int cat(const char *text)
Definition: rlstring.cpp:171
#define rl_PRINTF_LENGTH
Definition: rldefine.h:71
char * strstr(const char *substring)
Definition: rlstring.cpp:182
rlString & operator=(const char *s2)
Definition: rlstring.cpp:60
char * text()
Definition: rlstring.cpp:126
const char * toDirname()
Definition: rlstring.cpp:353
int strcpy(const char *text)
Definition: rlstring.cpp:161
int rlvsnprintf(char *text, int len, const char *format, va_list ap)
Definition: rlcutil.cpp:197
int setText(const char *text)
Definition: rlstring.cpp:136
int removeNewline()
Definition: rlstring.cpp:271
int upper()
Definition: rlstring.cpp:188
char * strrchr(int c)
Definition: rlstring.cpp:228
int strnnocasecmp(const char *other, int n)
Definition: rlstring.cpp:213
int strnocasecmp(const char *other)
Definition: rlstring.cpp:203
int printf(const char *format,...)
Definition: rlstring.cpp:145
virtual ~rlString()
Definition: rlstring.cpp:54
int operator==(const char *s2)
Definition: rlstring.cpp:96
int rlStartsWith(const char *str, const char *startstr)
Definition: rlcutil.cpp:749
rlString & operator+(const char *s2)
Definition: rlstring.cpp:72
int write(const char *filename)
Definition: rlstring.cpp:297
char * txt
Definition: rlstring.h:157
rlString & operator+=(const char *s2)
Definition: rlstring.cpp:84
char * strchr(int c)
Definition: rlstring.cpp:223
rlString(const char *text="")
Definition: rlstring.cpp:25
int rlupper(char *str)
Definition: rlcutil.cpp:727