Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

MString.cxx

Go to the documentation of this file.
00001 /*+---------------------- Copyright notice ----------------------------+*/
00002 /*| Copyright (C) 1995, Guy Barrand, LAL Orsay, (barrand@lal.in2p3.fr) |*/
00003 /*|  Permission to use, copy, modify, and distribute this software     |*/
00004 /*| and its documentation for any purpose and without fee is hereby    |*/
00005 /*| granted, provided that the above copyright notice appear in all    |*/
00006 /*| copies and that both that copyright notice and this permission     |*/
00007 /*| notice appear in supporting documentation.  This software is       |*/
00008 /*| provided "as is" without express or implied warranty.              |*/
00009 /*+---------------------- Copyright notice ----------------------------+*/
00010 
00011 #include <cstdio>
00012 #include <cstdlib>
00013 #include <cstring>
00014 
00015 #include "CalRecon/Midnight.h"
00016 
00017 #define changeBlockSize realloc
00018 #define freeBlock free
00019 #define allocateBlock malloc
00020 
00021 static void DeleteString(char*);
00022 static char* DuplicateString(const char*);
00023 static char* ConcatenateString(char*,const char*);
00024 static char* Create(int);
00025 
00026 #define MINIMUM(a,b) ((a)<(b)?a:b)
00027 
00029 MString::MString(
00030 )
00031 :fString(NULL)
00034 {
00035   fString = DuplicateString("");
00036 }
00038 MString::MString(
00039  const char* aString
00040 )
00043 {
00044   fString = DuplicateString(aString==NULL?"":aString);
00045 }
00047 MString::MString(
00048  const MString& aFrom
00049 )
00052 {
00053   fString = DuplicateString(aFrom.fString);
00054 }
00056 MString::~MString(
00057 )
00060 {
00061   DeleteString(fString);
00062 }
00064 const char* MString::data(
00065 ) const
00068 {
00069   return fString;
00070 }
00072 int MString::length(
00073 ) const
00076 {
00077   if(fString==NULL) return 0;
00078   return strlen(fString);
00079 }
00081 void MString::resize(
00082  int aLength
00083 )
00085 // Truncate or add blanks as necessary.
00087 {
00088   if(fString==NULL) return;
00089   if(aLength<0) return;
00090   int l = strlen(fString);
00091   if(aLength<l) {
00092     fString[aLength] = '\0';
00093   } else {
00094     char* s = Create(aLength-l);
00095     if(s!=NULL) {
00096       fString = ConcatenateString(fString,s);
00097       freeBlock(s);
00098     }
00099   }
00100 }
00102 MString& MString::replace(
00103  int aStart
00104 ,int aLength
00105 ,const MString& aString
00106 )
00109 {
00110   if(fString==NULL) return *this;
00111   if(aString.fString==NULL) return *this;
00112   if( (aStart<0) || (aStart>=length()) ) return *this;
00113   int begin = aStart;
00114   int end = MINIMUM(aStart + aLength - 1,length()-1);
00115   int l = aString.length();
00116   int pos = 0;
00117   for(int count=begin;count<=end;count++,pos++) {
00118     if(pos<l) 
00119       fString[count] = aString.fString[pos];
00120     else
00121       fString[count] = ' ';
00122   }
00123   return *this;
00124 }
00126 MString& MString::operator +=(
00127  const char* aString
00128 )
00131 {
00132   fString = ConcatenateString(fString,aString);
00133   return *this;  
00134 }
00136 MString& MString::operator +=(
00137  const MString& aString
00138 )
00141 {
00142   *this += aString.fString;
00143   return *this;
00144 }
00146 MString& MString::operator =(
00147  const char* aString
00148 )
00151 {
00152   if((fString!=NULL) && (aString==fString) ) return *this; 
00153   DeleteString(fString);
00154   fString = DuplicateString(aString==NULL?"":aString);
00155   return *this;
00156 }
00158 MString& MString::operator =(
00159  const MString& aFrom
00160 )
00163 {
00164   return (*this = aFrom.fString);
00165 }
00167 MString& MString::operator =(
00168  char aChar
00169 )
00172 {
00173   DeleteString(fString);
00174   fString = Create(1);
00175   fString[0] = aChar;
00176   return *this;
00177 }
00179 MString::operator const char*(
00180 ) const 
00183 { 
00184   return fString;
00185 }
00187 char& MString::operator[](
00188  int aIndex
00189 )
00191 // To do x[i] = c;
00193 { 
00194   if((aIndex<0)||(aIndex>=length())) {
00195     printf("MString::operator[] : bad index %d %d\n",aIndex,length());
00196     //exit(EXIT_FAILURE);
00197     aIndex = 0;
00198   }
00199   return fString[aIndex]; 
00200 }
00202 char MString::operator[](
00203  int aIndex
00204 ) const 
00207 { 
00208   if((aIndex<0)||(aIndex>=length())) {
00209     printf("MString::operator[] : bad index %d %d\n",aIndex,length());
00210     //exit(EXIT_FAILURE);
00211     aIndex = 0;
00212   }
00213   return fString[aIndex]; 
00214 }
00216 char& MString::operator()(
00217  int aIndex
00218 )
00220 // To do x(i) = c;
00222 { 
00223   return (*this)[aIndex]; 
00224 }
00226 char MString::operator()(
00227  int aIndex
00228 ) const 
00231 { 
00232   return (*this)[aIndex]; 
00233 }
00235 MString MString::operator()(
00236  int aStart
00237 ,int aLength
00238 )
00241 {
00242   MString s;
00243   if(fString==NULL) return s;
00244   if( (aStart<0) || (aStart>=length()) ) return s;
00245   int begin = aStart;
00246   int end = MINIMUM(aStart + aLength - 1,length()-1);
00247     DeleteString(s.fString);
00248   s.fString = Create(end - begin + 1);
00249   int pos = 0;
00250   for(int count=begin;count<=end;count++,pos++) {
00251     s.fString[pos] = fString[count];
00252   }
00253   return s;
00254 }
00256 int operator ==(
00257  const MString& a1
00258 ,const MString& a2
00259 )
00262 {
00263   if( a1.fString ==  a2.fString) return 1; 
00264   if( (a1.fString==NULL) || (a2.fString==NULL) ) return 0; 
00265   return (strcmp(a1.fString,a2.fString)==0 ? 1: 0); 
00266 }
00268 int operator !=(
00269  const MString& a1
00270 ,const MString& a2
00271 )
00274 {
00275   return (a1==a2 ? 0 : 1);
00276 }
00278 int operator ==(
00279  const MString& a1
00280 ,const char* a2
00281 )
00284 {
00285   if( a1.fString ==  a2) return 1; 
00286   if( (a1.fString==NULL) || (a2==NULL) ) return 0; 
00287   return (strcmp(a1.fString,a2)==0 ? 1: 0); 
00288 }
00290 int operator !=(
00291  const MString& a1
00292 ,const char* a2
00293 )
00296 {
00297   return (a1==a2 ? 0 : 1);
00298 }
00302 char* ConcatenateString (
00303  char* aString
00304 ,const char* aFrom 
00305 )
00308 {
00309   if(aFrom==NULL) return aString;
00310   if(aString==NULL) {
00311     aString = DuplicateString(aFrom);
00312   } else if(*aString=='\0') {
00313     DeleteString(aString);
00314     aString = DuplicateString(aFrom);
00315   } else { 
00316     int lto = strlen(aString);
00317     int lfrom = strlen(aFrom);
00318     int length = lto+lfrom;
00319     char* str = 
00320       (char*)changeBlockSize(aString,(size_t)((length+1)*sizeof(char)));
00321     if(str==NULL) return aString;
00322     str[length] = '\0';
00323     aString = str;
00324     strncpy(aString+lto,aFrom,lfrom);
00325     aString[lto+lfrom] = '\0';
00326   }
00327   return aString;
00328 }
00330 static void DeleteString(
00331  char* This
00332 )
00335 {
00336   if(This==NULL) return;
00337   freeBlock(This);
00338 }
00340 static char* DuplicateString(
00341  const char* This
00342 )
00345 {
00346   if(This==NULL) return NULL;
00347   int length = strlen(This);
00348   char* string = (char*)allocateBlock((length+1)*sizeof(char));
00349   if(string==NULL) return NULL;
00350   string[length] = '\0';
00351   return strcpy(string,This);
00352 }
00354 static char* Create(
00355  int a_length
00356 )
00359 {
00360   if(a_length<=0) a_length = 0;
00361   char* string = (char*)allocateBlock((a_length+1)*sizeof(char));
00362   if(string==NULL) return NULL;
00363   string[a_length] = '\0';
00364   for(int count=0;count<a_length;count++) string[count] = ' ';
00365   return string;
00366 }
00367 

Generated on Thu Nov 29 16:38:51 2001 by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001