ccutil/strngs.h

Go to the documentation of this file.
00001 
00020 #ifndef           STRNGS_H
00021 #define           STRNGS_H
00022 
00023 #include          <string.h>
00024 #include          "memry.h"
00025 #include          "serialis.h"
00026 
00031 class DLLSYM STRING
00032 {
00033   char *ptr;                     //ptr to the chars
00034 
00035   public:
00036     STRING() {  //constructor
00037       ptr = NULL;                //empty string
00038     }
00039 
00040     STRING(  //classwise copy
00041            const STRING &string) {
00042       if (string.ptr != NULL) {
00043                                  //length of source
00044         INT32 length = strlen (string.ptr) + 1;
00045 
00046                                  //get space
00047         ptr = alloc_string (length);
00048         strcpy (ptr, string.ptr);//and copy it
00049       }
00050       else {
00051         ptr = alloc_string (1);
00052         if (ptr != NULL)
00053           *ptr = '\0';
00054       }
00055     }
00056 
00057     STRING(  //contruct from char*
00058            const char *string) {
00059       if (string != NULL) {
00060                                  //length of source
00061         INT32 length = strlen (string) + 1;
00062 
00063                                  //get space
00064         ptr = alloc_string (length);
00065         if (ptr != NULL)
00066           strcpy(ptr, string);  //and copy it
00067       }
00068       else {
00069         ptr = alloc_string (1);
00070         if (ptr != NULL)
00071           *ptr = '\0';
00072       }
00073     }
00074 
00075     ~STRING () {                 //destructor
00076       if (ptr != NULL)
00077         free_string(ptr);  //give it back
00078     }
00079 
00080     char &operator[] (           //access function
00081       INT32 index) const         //string index
00082     {
00083       return ptr[index];         //no bounds checks
00084     }
00085 
00086     BOOL8 contains(  //char in string?
00087                    const char c) const {
00088       if ((ptr == NULL) || ((c != '\0') && strchr (ptr, c) == NULL))
00089         return FALSE;
00090       else
00091         return TRUE;
00092     }
00093 
00094     INT32 length() const {  //string length
00095       if (ptr != NULL)
00096         return strlen (ptr);
00097       else
00098         return 0;
00099     }
00100 
00101     const char *string() const {  //ptr to string
00102       return ptr;
00103     }
00104 
00105     BOOL8 operator== (           //string equality
00106       const STRING & string) const
00107     {
00108       if (ptr != NULL && string.ptr != NULL)
00109         return strcmp (ptr, string.ptr) == 0;
00110       else
00111         return (ptr == NULL || *ptr == '\0')
00112           && (string.ptr == NULL || *(string.ptr) == '\0');
00113     }
00114 
00115     BOOL8 operator!= (           //string equality
00116       const STRING & string) const
00117     {
00118       if (ptr != NULL && string.ptr != NULL)
00119         return strcmp (ptr, string.ptr) != 0;
00120       else
00121         return !((ptr == NULL || *ptr == '\0')
00122           && (string.ptr == NULL || *(string.ptr) == '\0'));
00123     }
00124 
00125     BOOL8 operator!= (           //string equality
00126       const char *string) const
00127     {
00128       if (ptr != NULL && string != NULL)
00129         return strcmp (ptr, string) != 0;
00130       else
00131         return !((ptr == NULL || *ptr == '\0')
00132           && (string == NULL || *string == '\0'));
00133     }
00134 
00135     STRING & operator= (         //assignment
00136       const char *string);       //of char*
00137 
00138     STRING & operator= (         //assignment
00139     const STRING & string) {     //of string
00140       *this = string.ptr;        //as for char*
00141       return *this;
00142     }
00143 
00144     STRING operator+ (           //concatenation
00145       const STRING & string) const;
00146 
00147     STRING operator+ (           //char concatenation
00148       const char ch) const;
00149 
00150     STRING & operator+= (        //inplace cat
00151       const char *string);
00152     STRING & operator+= (        //inplace cat
00153     const STRING & string) {
00154       *this += string.ptr;
00155       return *this;
00156     }
00157 
00158     STRING & operator+= (        //inplace char cat
00159       const char ch);
00160 
00161     void prep_serialise() {  //set ptrs to counts
00162       ptr = (char *) (length () + 1);
00163     }
00164 
00165     void dump(  //write external bits
00166               FILE *f) {
00167       serialise_bytes (f, (void *) ptr, (int) (length () + 1));
00168     }
00169 
00170     void de_dump(  //read external bits
00171                  FILE *f) {
00172       char *instring;            //input from read
00173 
00174       instring = (char *) de_serialise_bytes (f, (ptrdiff_t) ptr);
00175       ptr = NULL;
00176       *this = instring;
00177       free_mem(instring);
00178     }
00179 
00180     make_serialise (STRING)
00181 };
00182 #endif

Generated on Wed Feb 28 19:49:09 2007 for Tesseract by  doxygen 1.5.1