rllib  1
Public Member Functions | Private Attributes | List of all members
rlString Class Reference

#include <rlstring.h>

Public Member Functions

 rlString (const char *text="")
 
 rlString (rlString &text)
 
 rlString (rlString *text)
 
 rlString (const rlString &text)
 
virtual ~rlString ()
 
rlStringoperator= (const char *s2)
 
rlStringoperator= (rlString &s2)
 
rlStringoperator+ (const char *s2)
 
rlStringoperator+ (rlString &s2)
 
rlStringoperator+= (const char *s2)
 
rlStringoperator+= (rlString &s2)
 
int operator== (const char *s2)
 
int operator== (rlString &s2)
 
int operator== (const rlString &s2)
 
int operator!= (const char *s2)
 
int operator!= (rlString &s2)
 
char * text ()
 
char * text () const
 
int setText (const char *text)
 
int printf (const char *format,...)
 
int strcpy (const char *text)
 
int cat (const char *text)
 
int upper ()
 
int lower ()
 
int startsWith (const char *startstr)
 
int strnocasecmp (const char *other)
 
int strnnocasecmp (const char *other, int n)
 
char * strstr (const char *substring)
 
char * strchr (int c)
 
char * strrchr (int c)
 
int removeQuotas (char c='"')
 
int removeNewline ()
 
int read (const char *filename)
 
int write (const char *filename)
 
const char * toFilename ()
 
const char * toDirname ()
 

Private Attributes

char * txt
 
char * tmp
 

Detailed Description

class for a simple ANSI-C like string.

Definition at line 27 of file rlstring.h.

Constructor & Destructor Documentation

◆ rlString() [1/4]

rlString::rlString ( const char *  text = "")
construct the string

Definition at line 25 of file rlstring.cpp.

26 {
27  txt = new char [strlen(text)+1];
28  tmp = NULL;
29  ::strcpy(txt, text);
30 }
char * tmp
Definition: rlstring.h:158
char * text()
Definition: rlstring.cpp:126
int strcpy(const char *text)
Definition: rlstring.cpp:161
char * txt
Definition: rlstring.h:157

◆ rlString() [2/4]

rlString::rlString ( rlString text)

Definition at line 32 of file rlstring.cpp.

33 {
34  txt = new char [strlen(s2.text())+1];
35  tmp = NULL;
36  ::strcpy(txt,s2.text());
37 }
char * tmp
Definition: rlstring.h:158
int strcpy(const char *text)
Definition: rlstring.cpp:161
char * txt
Definition: rlstring.h:157

◆ rlString() [3/4]

rlString::rlString ( rlString text)

Definition at line 39 of file rlstring.cpp.

40 {
41  txt = new char [strlen(s2->text())+1];
42  tmp = NULL;
43  ::strcpy(txt,s2->text());
44 }
char * tmp
Definition: rlstring.h:158
int strcpy(const char *text)
Definition: rlstring.cpp:161
char * txt
Definition: rlstring.h:157

◆ rlString() [4/4]

rlString::rlString ( const rlString text)

Definition at line 46 of file rlstring.cpp.

47 {
48  txt = new char [strlen(s2.text())+1];
49  tmp = NULL;
50  ::strcpy(txt,s2.text());
51 }
char * tmp
Definition: rlstring.h:158
int strcpy(const char *text)
Definition: rlstring.cpp:161
char * txt
Definition: rlstring.h:157

◆ ~rlString()

rlString::~rlString ( )
virtual
destruct the string

Definition at line 54 of file rlstring.cpp.

55 {
56  delete [] txt;
57  if(tmp != NULL) delete [] tmp;
58 }
char * tmp
Definition: rlstring.h:158
char * txt
Definition: rlstring.h:157

Member Function Documentation

◆ cat()

int rlString::cat ( const char *  text)
append text

Definition at line 171 of file rlstring.cpp.

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 }
char * text()
Definition: rlstring.cpp:126
int strcpy(const char *text)
Definition: rlstring.cpp:161
char * txt
Definition: rlstring.h:157

◆ lower()

int rlString::lower ( )
converst string to upper case

Definition at line 193 of file rlstring.cpp.

194 {
196 }
int rllower(char *str)
Definition: rlcutil.cpp:738
char * txt
Definition: rlstring.h:157

◆ operator!=() [1/2]

int rlString::operator!= ( const char *  s2)

Definition at line 114 of file rlstring.cpp.

115 {
116  if(strcmp(txt,s2) != 0) return 1;
117  return 0;
118 }
char * txt
Definition: rlstring.h:157

◆ operator!=() [2/2]

int rlString::operator!= ( rlString s2)

Definition at line 120 of file rlstring.cpp.

121 {
122  if(strcmp(txt,s2.text()) != 0) return 1;
123  return 0;
124 }
char * text()
Definition: rlstring.cpp:126
char * txt
Definition: rlstring.h:157

◆ operator+() [1/2]

rlString & rlString::operator+ ( const char *  s2)

Definition at line 72 of file rlstring.cpp.

73 {
74  this->cat(s2);
75  return *this;
76 }
int cat(const char *text)
Definition: rlstring.cpp:171

◆ operator+() [2/2]

rlString & rlString::operator+ ( rlString s2)

Definition at line 78 of file rlstring.cpp.

79 {
80  this->cat(s2.text());
81  return *this;
82 }
int cat(const char *text)
Definition: rlstring.cpp:171
char * text()
Definition: rlstring.cpp:126

◆ operator+=() [1/2]

rlString & rlString::operator+= ( const char *  s2)

Definition at line 84 of file rlstring.cpp.

85 {
86  this->cat(s2);
87  return *this;
88 }
int cat(const char *text)
Definition: rlstring.cpp:171

◆ operator+=() [2/2]

rlString & rlString::operator+= ( rlString s2)

Definition at line 90 of file rlstring.cpp.

91 {
92  this->cat(s2.text());
93  return *this;
94 }
int cat(const char *text)
Definition: rlstring.cpp:171
char * text()
Definition: rlstring.cpp:126

◆ operator=() [1/2]

rlString & rlString::operator= ( const char *  s2)

Definition at line 60 of file rlstring.cpp.

61 {
62  this->setText(s2);
63  return *this;
64 }
int setText(const char *text)
Definition: rlstring.cpp:136

◆ operator=() [2/2]

rlString & rlString::operator= ( rlString s2)

Definition at line 66 of file rlstring.cpp.

67 {
68  this->setText(s2.text());
69  return *this;
70 }
char * text()
Definition: rlstring.cpp:126
int setText(const char *text)
Definition: rlstring.cpp:136

◆ operator==() [1/3]

int rlString::operator== ( const char *  s2)

Definition at line 96 of file rlstring.cpp.

97 {
98  if(strcmp(txt,s2) == 0) return 1;
99  return 0;
100 }
char * txt
Definition: rlstring.h:157

◆ operator==() [2/3]

int rlString::operator== ( rlString s2)

Definition at line 102 of file rlstring.cpp.

103 {
104  if(strcmp(txt,s2.text()) == 0) return 1;
105  return 0;
106 }
char * text()
Definition: rlstring.cpp:126
char * txt
Definition: rlstring.h:157

◆ operator==() [3/3]

int rlString::operator== ( const rlString s2)

Definition at line 108 of file rlstring.cpp.

109 {
110  if(strcmp(txt,s2.text()) == 0) return 1;
111  return 0;
112 }
char * text()
Definition: rlstring.cpp:126
char * txt
Definition: rlstring.h:157

◆ printf()

int rlString::printf ( const char *  format,
  ... 
)
printf the text

Definition at line 145 of file rlstring.cpp.

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 }
#define rl_PRINTF_LENGTH
Definition: rldefine.h:71
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
char * txt
Definition: rlstring.h:157

◆ read()

int rlString::read ( const char *  filename)
Read string from file.

Definition at line 279 of file rlstring.cpp.

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 }
char * txt
Definition: rlstring.h:157

◆ removeNewline()

int rlString::removeNewline ( )
Remove "\n" at end of string.

Definition at line 271 of file rlstring.cpp.

272 {
273  if(txt == NULL) return -1;
274  char *cptr = ::strchr(txt,'\n');
275  if(cptr != NULL) *cptr = '\0';
276  return 0;
277 }
char * txt
Definition: rlstring.h:157
char * strchr(int c)
Definition: rlstring.cpp:223

◆ removeQuotas()

int rlString::removeQuotas ( char  c = '"')
Remove quotas around a string.
This might be usefull together with CSV files.

Definition at line 233 of file rlstring.cpp.

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 }
char * txt
Definition: rlstring.h:157

◆ setText()

int rlString::setText ( const char *  text)
set the text

Definition at line 136 of file rlstring.cpp.

137 {
138  int ret = strlen(text);
139  delete [] txt;
140  txt = new char [ret+1];
141  ::strcpy(txt, text);
142  return ret;
143 }
char * text()
Definition: rlstring.cpp:126
int strcpy(const char *text)
Definition: rlstring.cpp:161
char * txt
Definition: rlstring.h:157

◆ startsWith()

int rlString::startsWith ( const char *  startstr)
test if string starts with startstr

Definition at line 198 of file rlstring.cpp.

199 {
200  return ::rlStartsWith(txt,startstr);
201 }
int rlStartsWith(const char *str, const char *startstr)
Definition: rlcutil.cpp:749
char * txt
Definition: rlstring.h:157

◆ strchr()

char * rlString::strchr ( int  c)
strchr()

Definition at line 223 of file rlstring.cpp.

224 {
225  return ::strchr(txt,c);
226 }
char * txt
Definition: rlstring.h:157

◆ strcpy()

int rlString::strcpy ( const char *  text)
copy text

Definition at line 161 of file rlstring.cpp.

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 }
char * text()
Definition: rlstring.cpp:126
int strcpy(const char *text)
Definition: rlstring.cpp:161
char * txt
Definition: rlstring.h:157

◆ strnnocasecmp()

int rlString::strnnocasecmp ( const char *  other,
int  n 
)
case insensitive string compare starting n characters

Definition at line 213 of file rlstring.cpp.

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 }
int lower()
Definition: rlstring.cpp:193
char * text()
Definition: rlstring.cpp:126
int setText(const char *text)
Definition: rlstring.cpp:136
char * txt
Definition: rlstring.h:157

◆ strnocasecmp()

int rlString::strnocasecmp ( const char *  other)
case insensitive string compare

Definition at line 203 of file rlstring.cpp.

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 }
int lower()
Definition: rlstring.cpp:193
char * text()
Definition: rlstring.cpp:126
int setText(const char *text)
Definition: rlstring.cpp:136
char * txt
Definition: rlstring.h:157

◆ strrchr()

char * rlString::strrchr ( int  c)
strchr()

Definition at line 228 of file rlstring.cpp.

229 {
230  return ::strrchr(txt,c);
231 }
char * txt
Definition: rlstring.h:157

◆ strstr()

char * rlString::strstr ( const char *  substring)
strstr()

Definition at line 182 of file rlstring.cpp.

183 {
184  if(substring == NULL) return NULL;
185  return ::strstr(txt,substring);
186 }
char * txt
Definition: rlstring.h:157

◆ text() [1/2]

char * rlString::text ( )
get the text

Definition at line 126 of file rlstring.cpp.

127 {
128  return txt;
129 }
char * txt
Definition: rlstring.h:157

◆ text() [2/2]

char * rlString::text ( ) const

Definition at line 131 of file rlstring.cpp.

132 {
133  return txt;
134 }
char * txt
Definition: rlstring.h:157

◆ toDirname()

const char * rlString::toDirname ( )
convert to platform independent directoryname

Definition at line 353 of file rlstring.cpp.

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 }
char * tmp
Definition: rlstring.h:158
int strcpy(const char *text)
Definition: rlstring.cpp:161
char * txt
Definition: rlstring.h:157
char * strchr(int c)
Definition: rlstring.cpp:223

◆ toFilename()

const char * rlString::toFilename ( )
convert to platform independent filename

Definition at line 308 of file rlstring.cpp.

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 }
char * tmp
Definition: rlstring.h:158
int strcpy(const char *text)
Definition: rlstring.cpp:161
char * strrchr(int c)
Definition: rlstring.cpp:228
char * txt
Definition: rlstring.h:157
char * strchr(int c)
Definition: rlstring.cpp:223

◆ upper()

int rlString::upper ( )
converst string to upper case

Definition at line 188 of file rlstring.cpp.

189 {
191 }
char * txt
Definition: rlstring.h:157
int rlupper(char *str)
Definition: rlcutil.cpp:727

◆ write()

int rlString::write ( const char *  filename)
Write string to file.

Definition at line 297 of file rlstring.cpp.

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 }
char * txt
Definition: rlstring.h:157

Member Data Documentation

◆ tmp

char* rlString::tmp
private

Definition at line 158 of file rlstring.h.

◆ txt

char* rlString::txt
private

Definition at line 157 of file rlstring.h.


The documentation for this class was generated from the following files: