SHARE
TWEET

Untitled

a guest Jan 21st, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <string.h>
  2. #include <assert.h>
  3. #include <stdlib.h>
  4. #include "oostring.h"
  5.  
  6. const int MAX_LENGTH = 1024;        // largest size string for input
  7.  
  8. oostring::oostring()
  9. {
  10.     Capacity = 1;
  11.     CString = new char[Capacity];
  12.     CString[0] = '\0';           // make c-style string zero length
  13. }
  14.  
  15.  
  16. oostring::oostring(const char * s)
  17. {
  18.     Capacity = strlen(s) + 1;
  19.     CString = new char[Capacity];
  20.     strcpy(CString,s);
  21. }
  22.  
  23. oostring::oostring(const oostring & str)
  24. {
  25.     Capacity = str.length() + 1;
  26.     CString = new char[Capacity];
  27.     strcpy(CString,str.CString);
  28. }
  29.  
  30. oostring::~oostring()
  31. {
  32.     delete [] CString;                // free memory
  33. }
  34.  
  35. const oostring & oostring::operator = (const oostring & rhs)
  36. {
  37.     if (this != &rhs)                             // check aliasing
  38.     {
  39.     if (Capacity < rhs.length() + 1)        // more memory needed?
  40.     {
  41.         delete[] CString;                   // delete old string
  42.         Capacity = rhs.length() + 1;        // add 1 for '\0'
  43.         CString = new char[Capacity];
  44.     }
  45.     strcpy(CString,rhs.CString);
  46.     }
  47.     return *this;
  48. }
  49.  
  50.  
  51. const oostring & oostring::operator = (const char * s)
  52. {
  53.  
  54.     int len = 0;                         // length of newly constructed string
  55.     assert(s != 0);                      // make sure s non-NULL
  56.     len = strlen(s);                     // # of characters in string
  57.  
  58.     // free old string if necessary
  59.  
  60.     if (Capacity < len + 1)
  61.     {
  62.     delete[] CString;   // delete old string
  63.     Capacity = len + 1; // add 1 for '\0'
  64.     CString = new char[Capacity];
  65.     }
  66.     strcpy(CString,s);
  67.     return *this;
  68. }
  69.  
  70. const oostring & oostring::operator = (char ch)
  71. {
  72.     if (Capacity < 2)
  73.     {
  74.     delete [] CString;
  75.     Capacity = 2;
  76.     CString = new char[Capacity];
  77.     }
  78.     CString[0] = ch;    // make string one character long
  79.     CString[1] = '\0';
  80.     return *this;
  81. }
  82.  
  83. int oostring::length( ) const
  84. {
  85.   int myLength = 0;
  86.  
  87.   while(CString[myLength] != '\0')
  88.     myLength++;
  89.  
  90.   return(myLength);
  91. }
  92.  
  93. char * oostring::c_str() const
  94. {
  95.     return CString;
  96. }
  97.  
  98. char oostring::operator [] (int k) const
  99. {
  100.     if (k < 0 || strlen(CString) <= k)
  101.     {
  102.         cerr << "Index out of range: " << k << " string: " << CString
  103.             << endl;
  104.         assert(0 <= k && k < strlen(CString));
  105.     }
  106.     return CString[k];
  107. }
  108.  
  109. char & oostring::operator [] (int k)
  110. {
  111.     if (k < 0 || strlen(CString) <= k)
  112.     {
  113.     cerr << "index out of range: " << k << " string: " << CString
  114.         << endl;
  115.     assert(0 <= k && k < strlen(CString));
  116.     }
  117.     return CString[k];
  118. }
  119.  
  120. ostream & operator << (ostream & os, const oostring & str)
  121. {
  122.     return os << str.c_str();
  123. }
  124.  
  125. istream & operator >> (istream & is, oostring & str)
  126. {
  127.     char buf[MAX_LENGTH];
  128.     is >> buf;
  129.     str = buf;
  130.     return is;
  131. }
  132.  
  133.  
  134. istream & getline(istream & is, oostring & str)
  135. {
  136.     char buf[MAX_LENGTH];
  137.     is.get(buf,sizeof(buf));
  138.     is.ignore(sizeof(buf),'\n');
  139.     str = buf;
  140.     return is;
  141. }
  142.  
  143. const oostring & oostring::operator += (const oostring & str)
  144. {
  145.  
  146.     oostring copystring(str);         // copy to avoid aliasing problems
  147.  
  148.     int newLength = length() + str.length(); // self + added string
  149.     int lastLocation = length();         // index of '\0'
  150.  
  151.     // check to see if local buffer not big enough
  152.     if (newLength >= Capacity)
  153.     {
  154.     Capacity = newLength + 1;
  155.     char * newBuffer = new char[Capacity];
  156.     strcpy(newBuffer,CString); // copy into new buffer
  157.     delete [] CString;       // delete old string
  158.     CString = newBuffer;
  159.     }
  160.  
  161.     // now catenate str (copystring) to end of CString
  162.     strcpy(CString+lastLocation,copystring.c_str() );
  163.  
  164.     return *this;
  165. }
  166.  
  167. const oostring & oostring::operator += (const char * s)
  168. {
  169.  
  170.     int newLength = length() + strlen(s);    // self + added string
  171.     int lastLocation = length();         // index of '\0'
  172.  
  173.     // check to see if local buffer not big enough
  174.     if (newLength >= Capacity)
  175.     {
  176.     Capacity = newLength + 1;
  177.     char * newBuffer = new char[Capacity];
  178.     strcpy(newBuffer,CString); // copy into new buffer
  179.     delete [] CString;       // delete old string
  180.     CString = newBuffer;
  181.     }
  182.  
  183.     // now catenate s to end of CString
  184.     strcpy(CString+lastLocation,s);
  185.  
  186.     return *this;
  187. }
  188.  
  189. const oostring & oostring::operator += ( char ch )
  190. {
  191.     oostring temp;  // make string equivalent of ch
  192.     temp = ch;
  193.     *this += temp;
  194.     return *this;
  195. }
  196.  
  197. oostring operator + (const oostring & lhs, const oostring & rhs)
  198. {
  199.     oostring result(lhs); // copies lhs to result
  200.     result += rhs;    // catenate rhs
  201.     return result;    // returns a copy of result
  202. }
  203.  
  204. oostring operator + ( char ch, const oostring & str )
  205. {
  206.     oostring result;      // make string equivalent of ch
  207.     result = ch;
  208.     result += str;
  209.     return result;
  210. }
  211.  
  212. oostring operator + ( const oostring & str, char ch )
  213. {
  214.     oostring result(str);
  215.     result += ch;
  216.     return result;
  217. }
  218.  
  219.  
  220. oostring oostring::substr(int pos, int len) const
  221. {
  222.     oostring result(*this);           // make sure enough space allocated
  223.  
  224.     if(pos < 0)                       // start at front when pos < 0
  225.     {
  226.       pos = 0;
  227.     }
  228.  
  229.     if(pos >= strlen(CString))
  230.     {
  231.       result = "";       // empty string
  232.       return result;
  233.     }
  234.  
  235.     int lastIndex = pos + len - 1;      // last char's index (to copy)
  236.     if(lastIndex >= strlen(CString))    // off end of string?
  237.     {
  238.       lastIndex = strlen(CString)-1;
  239.     }
  240.  
  241.     int j,k;
  242.     for(j=0,k=pos; k <= lastIndex; j++,k++)
  243.     {
  244.     result.CString[j] = CString[k];
  245.     }
  246.     result.CString[j] = '\0';         // properly terminate C-string
  247.     return result;
  248. }
  249.  
  250. int oostring::find(const oostring & str)  const
  251. {
  252.     int len = str.length();
  253.     int lastIndex = length() - len;
  254.     int k;
  255.     for(k=0; k <= lastIndex; k++)
  256.     {
  257.     if (strncmp(CString + k,str.c_str(),len) == 0) return k;
  258.     }
  259.     return(-1);
  260. }
  261.  
  262. int oostring::find( char ch ) const
  263. {
  264.     int k;
  265.     for(k=0; k < strlen(CString); k++)
  266.     {
  267.     if (CString[k] == ch)
  268.     {
  269.         return k;
  270.     }
  271.     }
  272.     return(-1);
  273. }
  274.  
  275.  
  276. int oostring::converttoint() const
  277. {
  278.   return(atoi(CString));
  279. }
  280.  
  281. long oostring::converttolong() const
  282. {
  283.   return(atol(CString));
  284. }
  285.  
  286. float oostring::converttofloat() const
  287. {
  288.   return(atof(CString));
  289. }
  290.  
  291. double oostring::converttodouble() const
  292. {
  293.   return(atof(CString));
  294. }
  295.  
  296.  
  297. bool operator == ( const oostring & lhs, const oostring & rhs )
  298. {
  299.     return strcmp(lhs.c_str(), rhs.c_str()) == 0;
  300. }
  301.  
  302. bool operator != ( const oostring & lhs, const oostring & rhs )
  303. {
  304.     return ! (lhs == rhs);
  305. }
  306.  
  307. bool operator <  ( const oostring & lhs, const oostring & rhs )
  308. {
  309.     return strcmp(lhs.c_str(), rhs.c_str()) < 0;
  310. }
  311.  
  312. bool operator <= ( const oostring & lhs, const oostring & rhs )
  313. {
  314.     return lhs < rhs || lhs == rhs;
  315. }
  316. bool operator >  ( const oostring & lhs, const oostring & rhs )
  317. {
  318.     return rhs < lhs;
  319. }
  320.  
  321. bool operator >= ( const oostring & lhs, const oostring & rhs )
  322. {
  323.     return rhs <= lhs;
  324. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top