Advertisement
Tyler_Elric

SmartString.cpp

Jan 17th, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1. #include "SmartString.h"
  2. #include "SmartArray.h"
  3. #include "SmartString.h"
  4. #include <cstring>
  5. #include <cctype>
  6. #include <iostream>
  7. #include <stdexcept>
  8.  
  9.  
  10. //SplitPoint::NoSplit.
  11. SplitPoint::NoSplit::NoSplit(unsigned int len):mLen(len){}
  12. SplitPoint::NoSplit::operator unsigned int()
  13. {
  14. return mLen;
  15. }
  16. //SplitPoint::Split.
  17. SplitPoint::Split::Split(unsigned int len,char* ptr):SplitPoint::NoSplit(len)
  18. {
  19. newString = new SmartString(ptr,mLen);
  20. }
  21. SmartString* SplitPoint::Split::operator () ()
  22. {
  23. return dynamic_cast<SmartString*>(&newString());
  24. }
  25.  
  26. //SplitPoint.
  27. SplitPoint::SplitPoint():mPoints(""),mKeys(){}
  28. SplitPoint& SplitPoint::AddPoint( const char* src)
  29. {
  30. mKeys.Append(new SmartString( src) );
  31. return *this;
  32. }
  33. SplitPoint& SplitPoint::AddPoint( const char src )
  34. {
  35. mPoints.Append( src );
  36. return *this;
  37. }
  38.  
  39. unsigned int SplitPoint::operator()(char* ptr) const
  40. {
  41. char* splitpoints = (char*)static_cast<const char*>(mPoints);
  42. for(unsigned int i=0, end=mKeys.Length(); i<end; i++)
  43. {
  44. try
  45. {
  46. SmartString& key = dynamic_cast<SmartString&>(mKeys[i]());
  47. if( key == ptr)
  48. {
  49. std::cout << "Dur..." << std::endl
  50. << key << "::" << ptr;
  51. throw SplitPoint::NoSplit(strlen(key));
  52. }
  53. }
  54. catch( std::bad_cast ){ continue; }
  55. }
  56. if(strchr(splitpoints,*ptr))
  57. {
  58. char* o = ptr;
  59. while(strchr(splitpoints,*ptr)) ptr++;
  60. throw SplitPoint::NoSplit(((unsigned int)(ptr-o)));
  61. }
  62. else
  63. {
  64. char* o = ptr;
  65. while(strchr(splitpoints,*ptr)==NULL) ptr++;
  66. throw SplitPoint::Split(((unsigned int)(ptr-o)),o);
  67. }
  68. return 1;
  69. }
  70.  
  71. //SmartString.
  72. unsigned int SmartString::gBufferSize = 10;
  73.  
  74. void SmartString::Allocate( unsigned int len ) throw (Iterable::UnneededAllocation)
  75. {
  76. char* nmStr = static_cast<char*>(memcpy( new char[ mAlloc = newSize(len) ], mStr, mUsed ));
  77. delete mStr;
  78. mStr = nmStr;
  79. }
  80.  
  81. SmartString SmartString::operator [] ( const Iterable::Slice& aSlice ){
  82. try{
  83. unsigned int lStart = aSlice.start(mUsed), lEnd = aSlice.end(mUsed);
  84. return SmartString(mStr + lStart,lEnd - lStart);
  85. } catch ( std::range_error ){
  86. return SmartString("Invalid slice was provided.");
  87. }
  88. }
  89.  
  90. char SmartString::operator [] ( int aIndice){
  91. return mStr[SmartString::Slice(0,mUsed).start(aIndice)];
  92. }
  93.  
  94. SmartString::operator const char *() const
  95. {
  96. return mStr;
  97. }
  98.  
  99. SmartString& SmartString::Empty()
  100. {
  101. delete[] mStr;
  102. mStr = new char[mAlloc = mUsed = 1];
  103. mStr[0] = '\0';
  104. return *this;
  105. }
  106.  
  107. const SmartArray SmartString::Explode( const SplitPoint& sp )
  108. {
  109. SmartArray ret;
  110. for( char* ptr=mStr,*end=mStr+mUsed-1; ptr<end; ptr++)
  111. {
  112. try{ sp(ptr); }
  113. catch( SplitPoint::Split& lSplit )
  114. {
  115. ret.Append(lSplit());
  116. ptr += static_cast<unsigned int>(lSplit);
  117. }
  118. catch( SplitPoint::NoSplit& noSplit )
  119. {
  120. ptr += static_cast<unsigned int>(noSplit);
  121. }
  122. }
  123. return ret;
  124. }
  125.  
  126. SmartString& SmartString::Append(const char* src)
  127. {
  128. return Append(src,strlen(src));
  129. }
  130.  
  131. SmartString& SmartString::Append(const char* src, unsigned int aLen)
  132. {
  133. try{
  134. Allocate(aLen);
  135. } catch ( SmartString::UnneededAllocation ){}
  136. memcpy(mStr+mUsed-1,src,aLen);
  137. mUsed += aLen;
  138. mStr[mUsed-1] = '\0';
  139. return *this;
  140. }
  141.  
  142. SmartString& SmartString::Append( const char src )
  143. {
  144. return Append( &src, 1 );
  145. }
  146.  
  147. SmartString& SmartString::Insert(const char* src, int aWhere )
  148. {
  149. return Insert(src,strlen(src),aWhere);
  150. }
  151.  
  152. SmartString& SmartString::Insert(const char* src, unsigned int len, int aWhere)
  153. {
  154. try{ Allocate(len); }
  155. catch( Iterable::UnneededAllocation ){}
  156. aWhere = Iterable::Slice(0,aWhere).end(mUsed);
  157. char* temp = static_cast<char*>(memcpy(new char[mUsed-aWhere],mStr + aWhere,mUsed-aWhere));
  158. memcpy(mStr + aWhere,src,len);
  159. memcpy(mStr + aWhere + len,temp,mUsed - aWhere);
  160. mUsed += len;
  161. delete [] temp;
  162. return *this;
  163. }
  164.  
  165. SmartString& SmartString::Prepend( const char* src )
  166. {
  167. return Prepend( src, strlen(src) );
  168. }
  169.  
  170. SmartString& SmartString::Prepend( const char* src, unsigned int len )
  171. {
  172. SmartString temp(src);
  173. temp.Append(mStr);
  174. char* stemp = mStr;
  175. mStr = temp.mStr;
  176. temp.mStr = stemp;
  177. mUsed = temp.mUsed;
  178. mAlloc = temp.mAlloc;
  179. return *this;
  180. }
  181.  
  182.  
  183. SmartString::SmartString( const SmartString& src):mStr(NULL)
  184. {
  185. Empty().Append(src.mStr);
  186. }
  187.  
  188. SmartString::SmartString(const char* src):mStr(NULL)
  189. {
  190. Empty().Append(src);
  191. }
  192.  
  193. SmartString::SmartString(const char* src,unsigned int aLen):mStr(NULL)
  194. {
  195. Empty().Append(src,aLen);
  196. }
  197.  
  198. SmartString::SmartString(void):mStr(NULL)
  199. {
  200. Empty();
  201. }
  202.  
  203. SmartString::SmartString( const SmartArray& sa, const char spacer ):mStr(NULL)
  204. {
  205. Empty();
  206. if(spacer != '\0' )
  207. for(unsigned int i=0,end=sa.Length();i<end;i++)
  208. {
  209. try
  210. {
  211. Append( dynamic_cast<SmartString&>(sa[i]()));
  212. if(i+1<end) Append( spacer );
  213. }
  214. catch(std::bad_cast){}
  215. }
  216. else
  217. for(unsigned int i=0,end=sa.Length();i<end;i++)
  218. {
  219. try
  220. {
  221. Append( dynamic_cast<SmartString&>(sa[i]()));
  222. }
  223. catch(std::bad_cast){}
  224. }
  225. }
  226. SmartString::SmartString( const SmartArray& sa, const char* spacer ):mStr(NULL)
  227. {
  228. Empty();
  229. if(spacer)
  230. for(unsigned int i=0,end=sa.Length();i<end;i++)
  231. {
  232. try
  233. {
  234. Append( dynamic_cast<SmartString&>(sa[i]()));
  235. if(i+1<end) Append( spacer );
  236. }
  237. catch(std::bad_cast){}
  238. }
  239. else
  240. for(unsigned int i=0,end=sa.Length();i<end;i++)
  241. {
  242. try
  243. {
  244. Append( dynamic_cast<SmartString&>(sa[i]()));
  245. }
  246. catch(std::bad_cast){}
  247. }
  248. }
  249.  
  250. SmartString::~SmartString()
  251. {
  252. delete [] mStr;
  253. }
  254.  
  255. int SmartString::Similar ( const char* src )
  256. {
  257. char* ptr = mStr;
  258. do
  259. {
  260. while(isspace(*ptr))ptr++;
  261. while(isspace(*src))src++;
  262. if( toupper(*ptr) ^ toupper(*src) )
  263. return -1 * ( ptr - mStr );
  264. } while(*++ptr&&*++src);
  265. return 1;
  266. }
  267.  
  268. unsigned int SmartString::Contains( const char* src, bool aSingle ) const
  269. {
  270. unsigned int ret = 0;
  271. //if(aSingle){
  272. for(unsigned int i = 0; i<mUsed; i++){
  273. for( char* ptr = ((char*)src);*ptr;ptr++){
  274. if( toupper(*ptr) ^ toupper(mStr[i]) ) continue;
  275. ret++;
  276. break;
  277. }
  278. }
  279. //} else if{
  280.  
  281. //}
  282. return ret;
  283. }
  284.  
  285. bool SmartString::operator==( const char* src ) const
  286. {
  287. std::cout << "\"" << mStr << "\"::\"" << src << "\"" << std::endl;
  288. for( char* ptr = mStr; *ptr&&*src; ptr++,src++ )
  289. if(*ptr^*src) return false;
  290. return true;
  291. }
  292.  
  293. void SmartString::ImportFromFile( std::istream& input )
  294. {
  295. Empty();
  296. unsigned int len;
  297. input.read((char*)&len,4);
  298. Allocate(len);
  299. input.read(mStr,mUsed=len);
  300. }
  301.  
  302. void SmartString::ExportToFile( std::ostream& output )
  303. {
  304. output.write( (char*)&mUsed, 4);
  305. output.write( mStr, mUsed);
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement