home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / BC++ Builder / DATA.Z / DSTRING.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-10  |  16.7 KB  |  549 lines

  1. //------------------------------------------------------------------------
  2. //    dstring.cpp - support for delphi strings in cpp
  3. //---------------------------------------------------------------------------
  4. // $Revision:   1.35  $
  5. //-------------------------------------------------------------------------
  6. //    copyright (c) 1997 Borland International
  7. //----------------------------------------------------------------------------
  8. #pragma inline
  9.  
  10. #include <windows.hpp>
  11. #include <sysutils.hpp>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <dstring.h>
  15.  
  16. #ifdef near
  17. #undef near
  18. #endif
  19.  
  20. namespace System
  21. {
  22.  
  23.  
  24. //---------------------------------------------------------------------------
  25. __fastcall      AnsiString::AnsiString(const char*)
  26. {
  27.     asm
  28.     {
  29.         extrn   @System@@LStrFromPChar$qqrv:near
  30.         xor     ecx,ecx
  31.         mov     eax, this
  32.         mov     [eax], ecx
  33.         call    @System@@LStrFromPChar$qqrv
  34.     }
  35. }
  36. //---------------------------------------------------------------------------
  37. __fastcall      AnsiString::AnsiString(const AnsiString&)
  38. {
  39.     asm
  40.     {
  41.         extrn   @System@@LStrAsg$qqrv:near
  42.         mov     edx,[edx]  
  43.     xor     ecx,ecx
  44.         mov     eax, this
  45.         mov     [eax],ecx
  46.         call    @System@@LStrAsg$qqrv
  47.     }
  48. }
  49. //---------------------------------------------------------------------------
  50. __fastcall AnsiString::AnsiString(const char*, unsigned char len)
  51. {
  52.     asm
  53.     {
  54.         extrn   @System@@LStrFromArray$qqrv:near
  55.         xor     ecx,ecx
  56.         mov     eax, this
  57.         mov     [eax],ecx
  58.         mov     cl, len
  59.         call    @System@@LStrFromArray$qqrv
  60.     }
  61. }
  62. //---------------------------------------------------------------------------
  63. __fastcall AnsiString::AnsiString(const wchar_t* src)
  64. {
  65.     int len = WideCharToMultiByte(CP_ACP, 0, const_cast<wchar_t*>(src), -1, 0,
  66.       0, 0, 0);
  67.  
  68.     if (len == 1)
  69.         Data = 0;
  70.     else
  71.     {
  72.         char* buffer = new char[len];
  73.         len = WideCharToMultiByte(CP_ACP, 0, const_cast<wchar_t*>(src), -1,
  74.           buffer, len, 0, 0);
  75.         asm
  76.         {
  77.             extrn   @System@@LStrFromArray$qqrv:near
  78.             mov     eax, this
  79.             xor     ecx,ecx
  80.             mov     [eax],ecx
  81.             mov     ecx, len
  82.             mov     edx, buffer
  83.             call    @System@@LStrFromArray$qqrv
  84.         }
  85.     // Save the return value across the call to delete[]
  86.     asm     push    eax
  87.         delete[] buffer;
  88.         asm     pop     eax
  89.     }
  90. }
  91. //---------------------------------------------------------------------------
  92. __fastcall      AnsiString::AnsiString(char src)
  93. {
  94.   char s[2];
  95.   char* sptr = s;
  96.   s[0] = src;
  97.   s[1] = 0;
  98.   asm
  99.   {
  100.     extrn   @System@@LStrFromPChar$qqrv:near
  101.     xor     ecx,ecx
  102.     mov     eax, this
  103.     mov     [eax], ecx
  104.     mov     edx, sptr
  105.     call    @System@@LStrFromPChar$qqrv
  106.   }
  107. }
  108. //---------------------------------------------------------------------------
  109. __fastcall AnsiString::AnsiString(int src)
  110. {
  111.   Data = 0;
  112.   *this = Sysutils::IntToStr(src);
  113. }
  114. //---------------------------------------------------------------------------
  115. __fastcall AnsiString::AnsiString(double src)
  116. {
  117.   Data = 0;
  118.   *this = Sysutils::FloatToStr(src);
  119. }
  120. //---------------------------------------------------------------------------
  121. __fastcall      AnsiString::~AnsiString()
  122. {
  123.     asm
  124.     {
  125.         extrn   @System@@LStrClr$qqrv:near
  126.         mov     eax,this
  127.         call    @System@@LStrClr$qqrv
  128.     }
  129. }
  130. //---------------------------------------------------------------------------
  131. AnsiString& __fastcall AnsiString::operator=(const AnsiString&)
  132. {
  133.     asm
  134.     {
  135.         extrn   @System@@LStrAsg$qqrv:near
  136.         mov     edx,[edx]
  137.         call    @System@@LStrAsg$qqrv
  138.     }
  139.     return *this;
  140. }
  141. //---------------------------------------------------------------------------
  142. AnsiString& __fastcall AnsiString::operator+=(const AnsiString&)
  143. {
  144.     asm
  145.     {
  146.         extrn   @System@@LStrCat$qqrv:near
  147.         mov     edx,[edx]
  148.         call    @System@@LStrCat$qqrv
  149.     }
  150.     return *this;
  151. }
  152. //---------------------------------------------------------------------------
  153. AnsiString __fastcall AnsiString::operator+(const AnsiString& rhs) const
  154. {
  155.     AnsiString tmp;
  156.     AnsiString* ptmp = &tmp;
  157.     asm
  158.     {
  159.         extrn   @System@@LStrCat3$qqrv:near
  160.     mov     edx, this
  161.     mov     edx, [edx]
  162.     mov     eax, ptmp
  163.         mov     ecx, rhs
  164.     mov     ecx, [ecx]
  165.         call    @System@@LStrCat3$qqrv
  166.     }
  167.     return tmp;
  168. }
  169. //---------------------------------------------------------------------------
  170. bool __fastcall AnsiString::operator==(const AnsiString&) const
  171. {
  172.     asm
  173.     {
  174.         extrn   @System@@LStrCmp$qqrv:near
  175.         mov     eax, [eax]
  176.         mov     edx, [edx]
  177.         call    @System@@LStrCmp$qqrv
  178.         sete    al
  179.         and     eax, 1
  180.     }
  181. }
  182. //---------------------------------------------------------------------------
  183. bool __fastcall AnsiString::operator!=(const AnsiString&) const
  184. {
  185.     asm
  186.     {
  187.         extrn   @System@@LStrCmp$qqrv:near
  188.         mov     eax, [eax]
  189.         mov     edx, [edx]
  190.         call    @System@@LStrCmp$qqrv
  191.         setne   al
  192.         and     eax, 1
  193.     }
  194. }
  195. //---------------------------------------------------------------------------
  196. bool __fastcall AnsiString::operator<(const AnsiString&) const
  197. {
  198.     asm
  199.     {
  200.         extrn   @System@@LStrCmp$qqrv:near
  201.         mov     eax, [eax]
  202.         mov     edx, [edx]
  203.         call    @System@@LStrCmp$qqrv
  204.         setb    al
  205.         and     eax, 1
  206.     }
  207. }
  208. //---------------------------------------------------------------------------
  209. bool __fastcall AnsiString::operator>(const AnsiString&) const
  210. {
  211.     asm
  212.     {
  213.         extrn   @System@@LStrCmp$qqrv:near
  214.         mov     eax, [eax]
  215.         mov     edx, [edx]
  216.         call    @System@@LStrCmp$qqrv
  217.         seta    al
  218.         and     eax, 1
  219.     }
  220. }
  221. //---------------------------------------------------------------------------
  222. bool __fastcall AnsiString::operator<=(const AnsiString& rhs) const
  223. {
  224.     return !operator>(rhs);
  225. }
  226.  
  227. //---------------------------------------------------------------------------
  228. bool __fastcall AnsiString::operator >=(const AnsiString& rhs) const
  229. {
  230.     return !operator<(rhs);
  231. }
  232. //---------------------------------------------------------------------------
  233. int __fastcall AnsiString::AnsiCompare(const AnsiString& rhs) const
  234. {
  235.   return Sysutils::AnsiCompareStr(*this, rhs);
  236. }
  237. //---------------------------------------------------------------------------
  238. int __fastcall AnsiString::AnsiCompareIC(const AnsiString& rhs) const
  239. {
  240.   return Sysutils::AnsiCompareText(*this, rhs);
  241. }
  242. //---------------------------------------------------------------------------
  243. int __fastcall AnsiString::Length() const
  244. {
  245.     asm
  246.     {
  247.         extrn   @System@@LStrLen$qqrv:near
  248.         mov     eax, [eax]
  249.         call    @System@@LStrLen$qqrv
  250.     }
  251. }
  252. //---------------------------------------------------------------------------
  253. bool __fastcall AnsiString::IsEmpty() const
  254. {
  255.     asm
  256.     {
  257.         mov     eax, [eax]
  258.         test    eax, eax
  259.         sete    al
  260.         and     eax, 1
  261.     }
  262. }
  263. //---------------------------------------------------------------------------
  264. // Return AnsiString of count chars of value ch
  265. AnsiString __fastcall AnsiString::StringOfChar(char ch, int count)
  266. {
  267.   char* s = new char[count+1];
  268.   for (int i = 0; i < count; i++)
  269.     s[i] = ch;
  270.   s[count] = 0;
  271.   AnsiString tmp(s);
  272.   delete [] s;
  273.   return tmp;
  274. }
  275. //---------------------------------------------------------------------------
  276. AnsiString __fastcall AnsiString::LoadStr(int ident)
  277. {
  278.   return Sysutils::LoadStr(ident);
  279. }
  280. //---------------------------------------------------------------------------
  281. AnsiString __fastcall AnsiString::FmtLoadStr(int ident, const TVarRec* args,
  282.   int size)
  283. {
  284.   return Sysutils::FmtLoadStr(ident, args, size);
  285. }
  286. //---------------------------------------------------------------------------
  287. AnsiString __fastcall AnsiString::Format(const AnsiString& format,
  288.   const TVarRec *args, int size)
  289. {
  290.   return Sysutils::Format(format, args, size);
  291. }
  292. //---------------------------------------------------------------------------
  293. AnsiString __fastcall AnsiString::FormatFloat(const AnsiString& format,
  294.   const long double& value)
  295. {
  296.   return Sysutils::FormatFloat(format, value);
  297. }
  298. //---------------------------------------------------------------------------
  299. AnsiString __fastcall AnsiString::FloatToStrF(long double value,
  300.       TStringFloatFormat format, int precision, int digits)
  301. {
  302.   return Sysutils::FloatToStrF(value, TFloatFormat(format), precision, digits);
  303. }
  304. //---------------------------------------------------------------------------
  305. AnsiString __fastcall AnsiString::IntToHex(int value, int digits)
  306. {
  307.   return Sysutils::IntToHex(value, digits);
  308. }
  309. //---------------------------------------------------------------------------
  310. AnsiString __fastcall AnsiString::CurrToStr(Currency value)
  311. {
  312.   return Sysutils::CurrToStr(value);
  313. }
  314. //---------------------------------------------------------------------------
  315. AnsiString __fastcall AnsiString::CurrToStrF(Currency value,
  316.       TStringFloatFormat format, int digits)
  317. {
  318.   return Sysutils::CurrToStrF(value, TFloatFormat(format), digits);
  319. }
  320. //---------------------------------------------------------------------------
  321. int __fastcall AnsiString::WideCharBufSize() const
  322. {
  323.   // return size of buffer required to call WideChar()
  324.   return MultiByteToWideChar(CP_ACP,0,(Data)? Data: (const PChar)"", -1, 0,0);
  325. }
  326. //---------------------------------------------------------------------------
  327. //Convert to Unicode
  328. wchar_t* __fastcall AnsiString::WideChar(wchar_t* dest, int destSize) const
  329. {
  330.   MultiByteToWideChar(CP_ACP,0,(Data)?Data: (const PChar)"",-1,dest,destSize);
  331.   return dest;
  332. }
  333. //---------------------------------------------------------------------------
  334. void __fastcall AnsiString::Unique()
  335. {
  336.   System::UniqueString(*this);
  337. }
  338. //---------------------------------------------------------------------------
  339. void __fastcall AnsiString::Insert(const String& source, int index)
  340. {
  341.   asm
  342.   {
  343.     extrn   @System@@LStrInsert$qqrv:near
  344.     // edx must be a pointer to the characters
  345.     mov     edx, this
  346.     // eax must be the characters themselves
  347.     mov     eax, source
  348.     mov     eax, [eax]
  349.     mov     ecx, index
  350.     call    @System@@LStrInsert$qqrv
  351.   }
  352. }
  353. //---------------------------------------------------------------------------
  354. void __fastcall AnsiString::Delete(int index, int count)
  355. {
  356.   asm
  357.   {
  358.     extrn   @System@@LStrDelete$qqrv:near
  359.     // eax must be a pointer to the characters
  360.     mov     eax, this
  361.     mov     edx, index
  362.     mov     ecx, count
  363.     call    @System@@LStrDelete$qqrv
  364.   }
  365. }
  366. //---------------------------------------------------------------------------
  367. void __fastcall AnsiString::SetLength(int newLength)
  368. {
  369.   asm
  370.   {
  371.     extrn   @System@@LStrSetLength$qqrv:near
  372.     //  eax must be a pointer to the characters
  373.     mov     eax, this
  374.     mov     edx, newLength
  375.     call    @System@@LStrSetLength$qqrv
  376.   }
  377. }
  378. //---------------------------------------------------------------------------
  379. int __fastcall AnsiString::Pos(const AnsiString& subStr) const
  380. {
  381.   asm
  382.   {
  383.     extrn   @System@@LStrPos$qqrv:near
  384.     // edx must be the characters themselves
  385.     mov     edx, this
  386.     mov     edx, [edx]
  387.     // eax must be the characters themselves!
  388.     mov     eax, subStr
  389.     mov     eax, [eax]
  390.     call    @System@@LStrPos$qqrv
  391.   //Result left in eax
  392.   }
  393. }
  394. //---------------------------------------------------------------------------
  395. AnsiString __fastcall AnsiString::LowerCase() const
  396. {
  397.   return Sysutils::AnsiLowerCase(*this);
  398. }
  399. //---------------------------------------------------------------------------
  400. AnsiString __fastcall AnsiString::UpperCase() const
  401. {
  402.   return Sysutils::AnsiUpperCase(*this);
  403. }
  404. //---------------------------------------------------------------------------
  405. AnsiString __fastcall AnsiString::Trim() const
  406. {
  407.   return Sysutils::Trim(*this);
  408. }
  409. //---------------------------------------------------------------------------
  410. AnsiString __fastcall AnsiString::TrimLeft() const
  411. {
  412.   return Sysutils::TrimLeft(*this);
  413. }
  414. //---------------------------------------------------------------------------
  415. AnsiString __fastcall AnsiString::TrimRight() const
  416. {
  417.   return Sysutils::TrimRight(*this);
  418. }
  419. //---------------------------------------------------------------------------
  420. AnsiString __fastcall AnsiString::SubString(int index, int count) const
  421. {
  422.   AnsiString tmp;
  423.   AnsiString* ptmp = &tmp;
  424.   asm
  425.   {
  426.     extrn   @System@@LStrCopy$qqrv:near
  427.     //  eax must be a pointer to the characters
  428.     mov     eax, ptmp
  429.     push    eax
  430.     mov     ecx, count
  431.     mov     edx, index
  432.     //  eax must be the characters themselves
  433.     mov     eax, this
  434.     mov     eax, [eax]
  435.     call    @System@@LStrCopy$qqrv
  436.   }
  437.   return tmp;
  438. }
  439. //---------------------------------------------------------------------------
  440. int __fastcall AnsiString::ToInt() const
  441. {
  442.   return Sysutils::StrToInt(*this);
  443. }
  444. //---------------------------------------------------------------------------
  445. int __fastcall AnsiString::ToIntDef(int defaultValue) const
  446. {
  447.   return Sysutils::StrToIntDef(*this, defaultValue);
  448. }
  449. //---------------------------------------------------------------------------
  450. double __fastcall AnsiString::ToDouble() const
  451. {
  452.   return Sysutils::StrToFloat(*this);
  453. }
  454. //---------------------------------------------------------------------------
  455. bool __fastcall AnsiString::IsDelimiter(const AnsiString& delimiters, int index) const
  456. {
  457.   return Sysutils::IsDelimiter(delimiters, *this, index);
  458. }
  459. //---------------------------------------------------------------------------
  460. int __fastcall AnsiString::AnsiPos(const AnsiString& subStr) const
  461. {
  462.   return Sysutils::AnsiPos(subStr, *this);
  463. }
  464. //---------------------------------------------------------------------------
  465. bool __fastcall AnsiString::IsPathDelimiter(int index) const
  466. {
  467.   return Sysutils::IsPathDelimiter(*this, index);
  468. }
  469. //---------------------------------------------------------------------------
  470. int __fastcall AnsiString::LastDelimiter(const AnsiString& delimiters) const
  471. {
  472.   return Sysutils::LastDelimiter(delimiters, *this);
  473. }
  474. //---------------------------------------------------------------------------
  475. AnsiString::TStringMbcsByteType __fastcall AnsiString::ByteType(int index) const
  476. {
  477.   //!GCD this code requires TStringMbcsByteType and TMbcsByteType to be identical
  478.   return TStringMbcsByteType(Sysutils::ByteType(*this, index));
  479. }
  480. //---------------------------------------------------------------------------
  481. bool __fastcall AnsiString::IsLeadByte(int index) const
  482. {
  483.   return ByteType(index) == mbLeadByte;
  484. }
  485. //---------------------------------------------------------------------------
  486. bool __fastcall AnsiString::IsTrailByte(int index) const
  487. {
  488.   return ByteType(index) == mbTrailByte;
  489. }
  490. //---------------------------------------------------------------------------
  491. char* __fastcall AnsiString::AnsiLastChar() const
  492. {
  493.   return Sysutils::AnsiLastChar(*this);
  494. }
  495. //---------------------------------------------------------------------------
  496. AnsiString __fastcall operator+(const char* lhs, const AnsiString& rhs)
  497. {
  498.   AnsiString tmp(lhs);
  499.   AnsiString* ptmp = &tmp;
  500.   asm
  501.   {
  502.     extrn   @System@@LStrCat$qqrv:near
  503.     mov     eax,ptmp
  504.     mov     edx,rhs
  505.     mov     edx,[edx]
  506.     call    @System@@LStrCat$qqrv
  507.   }
  508.   return tmp;
  509. }
  510. //---------------------------------------------------------------------------
  511. static void read_to_delim(istream& strm, char delim, AnsiString& str);
  512. istream& operator >>(istream& is, AnsiString& arg)
  513.   {read_to_delim(is, 0, arg);}
  514. //---------------------------------------------------------------------------
  515. static void read_to_delim(istream& strm, char delim, AnsiString& str)
  516. {
  517.     char ch;
  518.     int nchars = 0;
  519.     char array[4096];
  520.     int capacity = sizeof array;
  521.  
  522.     while ( 1 )
  523.         {
  524.  
  525.         // Read as many characters as we can, up to the delimitor:
  526.         strm.get( array+nchars, capacity-nchars+1, delim );
  527.  
  528.         // This is the new string length:
  529.         nchars += strlen( array+nchars );
  530.  
  531.         // What stopped us?  An EOF?
  532.         if( !strm.good() )
  533.             break;          // EOF encountered (or worse!)
  534.  
  535.         // Nope.  Was it the delimiter?
  536.         strm.get(ch);
  537.         if(ch==delim)
  538.             break;  // Yup. We're done.  Don't put it back on the stream.
  539.         else
  540.         strm.putback(ch);   // Nope, Put it back and keep going.
  541.  
  542.         }
  543.         str = array;
  544. }
  545. //---------------------------------------------------------------------------
  546. }
  547.  
  548.  
  549.