ccutil/strngs.cpp

Go to the documentation of this file.
00001 
00020 #include          "mfcpch.h"     //precompiled headers
00021 #include          "tprintf.h"
00022 #include          "strngs.h"
00023 
00024 /* ================== */
00028 STRING & STRING::operator= (     //assign char*
00029 const char *string               //string to copy
00030 ) {
00031   if (string != NULL) {
00032     INT32
00033       length = strlen (string) + 1;//length of source
00034 
00035     if (ptr == NULL)
00036                                  //get space
00037         ptr = alloc_string (length);
00038                                  //got wrong size
00039     else if (strlen (ptr) != (UINT32) length - 1) {
00040       free_string(ptr);  //free old space
00041                                  //get new space
00042       ptr = alloc_string (length);
00043     }
00044     if (ptr == NULL) {
00045       tprintf ("No memory to allocate string");
00046       return *this;
00047     }
00048     strcpy(ptr, string);  //copy string
00049   }
00050   else {
00051     if (ptr == NULL)
00052       ptr = alloc_string (1);    //get space
00053                                  //got wrong size
00054     else if (strlen (ptr) != 0) {
00055       free_string(ptr);  //free old space
00056       ptr = alloc_string (1);    //get new space
00057     }
00058     if (ptr == NULL) {
00059       tprintf ("No memory to allocate string");
00060       return *this;
00061     }
00062     *ptr = '\0';                 //copy string
00063   }
00064 
00065   return *this;
00066 }
00067 
00068 
00069 /* ================== */
00073 STRING
00074 STRING::operator+ (              //concatenation
00075 const STRING & string            //second string
00076 ) const
00077 {
00078   INT32 length;                  //length of 1st op
00079   STRING result;                 //concatenated string
00080 
00081   if (ptr == NULL)
00082     length = 0;
00083   else
00084     length = strlen (ptr);
00085   result.ptr = alloc_string (length + strlen (string.ptr) + 1);
00086   //total length
00087   if (result.ptr == NULL) {
00088     tprintf ("No memory to allocate string");
00089     return result;
00090   }
00091   result.ptr[0] = '\0';
00092   if (ptr != NULL)
00093     strcpy (result.ptr, ptr);
00094   if (string.ptr != NULL)
00095                                  //put together
00096     strcpy (result.ptr + length, string.ptr);
00097   return result;
00098 }
00099 
00100 
00101 /* ================== */
00105 STRING
00106 STRING::operator+ (              //concatenation
00107 const char ch                    //char
00108 ) const
00109 {
00110   INT32 length;                  //length of 1st op
00111   STRING result;                 //concatenated string
00112 
00113   if (ptr == NULL)
00114     length = 0;
00115   else
00116     length = strlen (ptr);
00117                                  //total length
00118   result.ptr = alloc_string (length + 2);
00119   if (result.ptr == NULL) {
00120     tprintf ("No memory to allocate string");
00121     return result;
00122   }
00123   if (ptr != NULL)
00124     strcpy (result.ptr, ptr);
00125   result.ptr[length] = ch;       //put together
00126   result.ptr[length + 1] = '\0';
00127   return result;
00128 }
00129 
00130 
00131 /* ================== */
00137 STRING & STRING::operator+= (    //inplace cat
00138 const char *string               //string to add
00139 ) {
00140   INT32
00141     length;                      //length of 1st op
00142   char *
00143     src;                         //source string
00144 
00145   if (string == NULL || string[0] == '\0')
00146     return *this;                //unchanged
00147   if (ptr == NULL)
00148     length = 0;
00149   else
00150     length = strlen (ptr);       //length of 1st op
00151   src = ptr;                     //temp copy
00152                                  //new length
00153   ptr = alloc_string (length + strlen (string) + 1);
00154   if (ptr == NULL) {
00155     tprintf ("No memory to allocate string");
00156     ptr = src;
00157     return *this;
00158   }
00159   if (src != NULL) {
00160     strcpy(ptr, src);  //copy old
00161     free_string(src);  //free old one
00162   }
00163   strcpy (ptr + length, string); //add new
00164   return *this;
00165 }
00166 
00167 
00168 /* ================== */
00172 STRING & STRING::operator+= (    //inplace cat
00173 const char ch                    //char to add
00174 ) {
00175   INT32
00176     length;                      //length of 1st op
00177   char *
00178     src;                         //source string
00179 
00180   if (ch == '\0')
00181     return *this;                //unchanged
00182   if (ptr == NULL)
00183     length = 0;
00184   else
00185     length = strlen (ptr);       //length of 1st op
00186   src = ptr;                     //temp copy
00187                                  //new length
00188   ptr = alloc_string (length + 2);
00189   if (ptr == NULL) {
00190     tprintf ("No memory to allocate string");
00191     ptr = src;
00192     return *this;
00193   }
00194   if (src != NULL) {
00195     strcpy(ptr, src);  //copy old
00196     free_string(src);  //free old one
00197   }
00198   ptr[length] = ch;              //add new char
00199   ptr[length + 1] = '\0';
00200   return *this;
00201 }

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