CvString.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "CvGameCoreDLL.h"
  2. const CvString EmptySS("");
  3. const CvString CommaSS(",");
  4. const CvWString EmptyWS(L"");
  5. const CvWString CommaWS(L", ");
  6. const CvWString DelimWS(L": ");
  7. //
  8. // CvString
  9. //
  10. void CvString::Copy(const wchar* w)
  11. {
  12. if (w) {
  13. int len = WideCharToMultiByte(CP_ACP, 0, w, -1, NULL, 0, NULL, NULL);
  14. if (len)
  15. {
  16. reserve(len-1);
  17. len = WideCharToMultiByte(CP_ACP, 0, w, -1, const_cast<char *>(c_str()), len, NULL, NULL);
  18. _Eos((len==0) ? 0 : (len-1));
  19. }
  20. }
  21. }
  22. void CvWString::Copy(const char* s)
  23. {
  24. if (s) {
  25. int len = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, s, -1, NULL, 0);
  26. if (len)
  27. {
  28. reserve(len-1);
  29. len = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, s, -1, const_cast<wchar *>(c_str()), len);
  30. _Eos((len==0) ? 0 : (len-1));
  31. }
  32. }
  33. }
  34. //
  35. // static
  36. //
  37. bool CvString::formatv(CvString& out, const char * fmt, va_list args)
  38. {
  39. int len = 0;
  40. if(fmt) {
  41. char buf[2048];
  42. len = _vsnprintf(buf, 2048, fmt, args);
  43. if(len >= 0)
  44. {
  45. out.assign(buf, len);
  46. return true;
  47. }
  48. else
  49. {
  50. size_t maxlength = (out.capacity() > 4097) ? out.capacity() : 4097;
  51. out._Eos(0);
  52. do
  53. {
  54. out.reserve(maxlength);
  55. len = _vsnprintf(const_cast<char *>( out.c_str() ), out.capacity(), fmt, args);
  56. if (len >= 0) // If that worked, return the string.
  57. {
  58. out._Eos(len);
  59. break;
  60. }
  61. if(maxlength >= 81919) // 81920 == 80K limit
  62. {
  63. out.erase();
  64. break;
  65. }
  66. // twice the old size
  67. maxlength += out.capacity() + 1;
  68. } while(true);
  69. }
  70. }
  71. else
  72. out.erase();
  73. return (len >= 0);
  74. }
  75. //
  76. // static
  77. //
  78. bool CvWString::formatv(CvWString& out, const wchar * fmt, va_list args)
  79. {
  80. int len = 0;
  81. if(fmt) {
  82. wchar buf[2048];
  83. len = _vsnwprintf(buf, 2048, fmt, args);
  84. if(len >= 0)
  85. {
  86. out.assign(buf, len);
  87. return true;
  88. }
  89. else
  90. {
  91. size_t maxlength = (out.capacity() > 4097) ? out.capacity() : 4097;
  92. out._Eos(0);
  93. do
  94. {
  95. out.reserve(maxlength);
  96. len = _vsnwprintf(const_cast<wchar *>( out.c_str() ), out.capacity(), fmt, args);
  97. if (len >= 0) // If that worked, return the string.
  98. {
  99. out._Eos(len);
  100. break;
  101. }
  102. if(maxlength >= 81919) // 81920 == 80K limit
  103. {
  104. out.erase();
  105. break;
  106. }
  107. // twice the old size
  108. maxlength += out.capacity() + 1;
  109. } while(true);
  110. }
  111. }
  112. else
  113. out.erase();
  114. return (len >= 0);
  115. }
  116. //
  117. // static
  118. //
  119. bool CvWString::appendfmtv(CvWString& out, const wchar * fmt, va_list args)
  120. {
  121. int len = 0;
  122. if(fmt) {
  123. len = _vscwprintf(fmt, args);
  124. if(len > 0) {
  125. out.reserve(out.length() + len);
  126. len = _vsnwprintf(const_cast<wchar *>( out.c_str() ) + out.length(), out.capacity()-out.length(), fmt, args);
  127. if( len >= 0 )
  128. out._Eos(out.length() + len);
  129. }
  130. }
  131. return (len >= 0);
  132. }
  133. // Small hash intended for version checking only.
  134. unsigned short CvWStringBuffer::getShortHash() const
  135. {
  136. unsigned short shortHash = 0;
  137. for (int iI = 0; iI < m_iLength; iI++)
  138. {
  139. // Hashes shouldn't care about overflow.
  140. shortHash = shortHash * 101 + m_pBuffer[iI];
  141. }
  142. return shortHash;
  143. }
  144. void CvWStringBuffer::_grow(int newCapacity)
  145. {
  146. m_iCapacity = 2 * newCapacity; //grow by %100
  147. wchar *newBuffer = new wchar [m_iCapacity];
  148. //copy data
  149. if(m_pBuffer)
  150. {
  151. memcpy(newBuffer, m_pBuffer, sizeof(wchar) * (m_iLength + 1/*NULL*/));
  152. //erase old memory
  153. delete [] m_pBuffer;
  154. }
  155. else
  156. {
  157. newBuffer[0] = 0; //null character
  158. }
  159. m_pBuffer = newBuffer;
  160. }