Utils.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. #include "stdafx.h"
  2. #include "Utils.h"
  3. namespace DuiLib
  4. {
  5. /////////////////////////////////////////////////////////////////////////////////////
  6. //
  7. //
  8. STRINGorID::STRINGorID(LPCTSTR lpString) : m_lpstr(lpString)
  9. {
  10. }
  11. STRINGorID::STRINGorID(unsigned int nID) : m_lpstr(MAKEINTRESOURCE(nID))
  12. {
  13. }
  14. /////////////////////////////////////////////////////////////////////////////////////
  15. //
  16. //
  17. CDuiPoint::CDuiPoint()
  18. {
  19. x = y = 0;
  20. }
  21. CDuiPoint::CDuiPoint(const POINT& src)
  22. {
  23. x = src.x;
  24. y = src.y;
  25. }
  26. CDuiPoint::CDuiPoint(long _x, long _y)
  27. {
  28. x = _x;
  29. y = _y;
  30. }
  31. CDuiPoint::CDuiPoint(LPARAM lParam)
  32. {
  33. x = GET_X_LPARAM(lParam);
  34. y = GET_Y_LPARAM(lParam);
  35. }
  36. CDuiPoint::CDuiPoint(LPCTSTR pstrValue)
  37. {
  38. if (pstrValue == NULL || *pstrValue == _T('\0')) x = y = 0;
  39. LPTSTR pstr = NULL;
  40. x = y = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  41. y = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  42. }
  43. CDuiString CDuiPoint::ToString()
  44. {
  45. CDuiString sPoint;
  46. sPoint.SmallFormat(_T("%ld,%ld"), x, y);
  47. return sPoint;
  48. }
  49. /////////////////////////////////////////////////////////////////////////////////////
  50. //
  51. //
  52. CDuiSize::CDuiSize()
  53. {
  54. cx = cy = 0;
  55. }
  56. CDuiSize::CDuiSize(const SIZE& src)
  57. {
  58. cx = src.cx;
  59. cy = src.cy;
  60. }
  61. CDuiSize::CDuiSize(const RECT rc)
  62. {
  63. cx = rc.right - rc.left;
  64. cy = rc.bottom - rc.top;
  65. }
  66. CDuiSize::CDuiSize(long _cx, long _cy)
  67. {
  68. cx = _cx;
  69. cy = _cy;
  70. }
  71. CDuiSize::CDuiSize(LPCTSTR pstrValue)
  72. {
  73. if (pstrValue == NULL || *pstrValue == _T('\0')) cx = cy = 0;
  74. LPTSTR pstr = NULL;
  75. cx = cy = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  76. cy = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  77. }
  78. CDuiString CDuiSize::ToString()
  79. {
  80. CDuiString sSize;
  81. sSize.SmallFormat(_T("%ld,%ld"), cx, cy);
  82. return sSize;
  83. }
  84. /////////////////////////////////////////////////////////////////////////////////////
  85. //
  86. //
  87. CDuiRect::CDuiRect()
  88. {
  89. left = top = right = bottom = 0;
  90. }
  91. CDuiRect::CDuiRect(const RECT& src)
  92. {
  93. left = src.left;
  94. top = src.top;
  95. right = src.right;
  96. bottom = src.bottom;
  97. }
  98. CDuiRect::CDuiRect(long iLeft, long iTop, long iRight, long iBottom)
  99. {
  100. left = iLeft;
  101. top = iTop;
  102. right = iRight;
  103. bottom = iBottom;
  104. }
  105. CDuiRect::CDuiRect(LPCTSTR pstrValue)
  106. {
  107. if (pstrValue == NULL || *pstrValue == _T('\0')) left = top = right = bottom = 0;
  108. LPTSTR pstr = NULL;
  109. left = top = right = bottom = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
  110. top = bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  111. right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  112. bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  113. }
  114. CDuiString CDuiRect::ToString()
  115. {
  116. CDuiString sRect;
  117. sRect.SmallFormat(_T("%ld,%ld,%ld,%ld"), left, top, right, bottom);
  118. return sRect;
  119. }
  120. int CDuiRect::GetWidth() const
  121. {
  122. return right - left;
  123. }
  124. int CDuiRect::GetHeight() const
  125. {
  126. return bottom - top;
  127. }
  128. void CDuiRect::Empty()
  129. {
  130. left = top = right = bottom = 0;
  131. }
  132. bool CDuiRect::IsNull() const
  133. {
  134. return (left == 0 && right == 0 && top == 0 && bottom == 0);
  135. }
  136. void CDuiRect::Join(const RECT& rc)
  137. {
  138. if( rc.left < left ) left = rc.left;
  139. if( rc.top < top ) top = rc.top;
  140. if( rc.right > right ) right = rc.right;
  141. if( rc.bottom > bottom ) bottom = rc.bottom;
  142. }
  143. void CDuiRect::ResetOffset()
  144. {
  145. ::OffsetRect(this, -left, -top);
  146. }
  147. void CDuiRect::Normalize()
  148. {
  149. if( left > right ) { int iTemp = left; left = right; right = iTemp; }
  150. if( top > bottom ) { int iTemp = top; top = bottom; bottom = iTemp; }
  151. }
  152. void CDuiRect::Offset(int cx, int cy)
  153. {
  154. ::OffsetRect(this, cx, cy);
  155. }
  156. void CDuiRect::Inflate(int cx, int cy)
  157. {
  158. ::InflateRect(this, cx, cy);
  159. }
  160. void CDuiRect::Deflate(int cx, int cy)
  161. {
  162. ::InflateRect(this, -cx, -cy);
  163. }
  164. void CDuiRect::Union(CDuiRect& rc)
  165. {
  166. ::UnionRect(this, this, &rc);
  167. }
  168. /////////////////////////////////////////////////////////////////////////////////////
  169. //
  170. //
  171. CDuiPtrArray::CDuiPtrArray(int iPreallocSize) : m_ppVoid(NULL), m_nCount(0), m_nAllocated(iPreallocSize)
  172. {
  173. ASSERT(iPreallocSize>=0);
  174. if( iPreallocSize > 0 ) m_ppVoid = static_cast<LPVOID*>(malloc(iPreallocSize * sizeof(LPVOID)));
  175. }
  176. CDuiPtrArray::CDuiPtrArray(const CDuiPtrArray& src) : m_ppVoid(NULL), m_nCount(0), m_nAllocated(0)
  177. {
  178. for(int i=0; i<src.GetSize(); i++)
  179. Add(src.GetAt(i));
  180. }
  181. CDuiPtrArray::~CDuiPtrArray()
  182. {
  183. if( m_ppVoid != NULL ) free(m_ppVoid);
  184. }
  185. void CDuiPtrArray::Empty()
  186. {
  187. if( m_ppVoid != NULL ) free(m_ppVoid);
  188. m_ppVoid = NULL;
  189. m_nCount = m_nAllocated = 0;
  190. }
  191. void CDuiPtrArray::Resize(int iSize)
  192. {
  193. Empty();
  194. m_ppVoid = static_cast<LPVOID*>(malloc(iSize * sizeof(LPVOID)));
  195. ::ZeroMemory(m_ppVoid, iSize * sizeof(LPVOID));
  196. m_nAllocated = iSize;
  197. m_nCount = iSize;
  198. }
  199. bool CDuiPtrArray::IsEmpty() const
  200. {
  201. return m_nCount == 0;
  202. }
  203. bool CDuiPtrArray::Add(LPVOID pData)
  204. {
  205. if( ++m_nCount >= m_nAllocated) {
  206. int nAllocated = m_nAllocated * 2;
  207. if( nAllocated == 0 ) nAllocated = 11;
  208. LPVOID* ppVoid = static_cast<LPVOID*>(realloc(m_ppVoid, nAllocated * sizeof(LPVOID)));
  209. if( ppVoid != NULL ) {
  210. m_nAllocated = nAllocated;
  211. m_ppVoid = ppVoid;
  212. }
  213. else {
  214. --m_nCount;
  215. return false;
  216. }
  217. }
  218. m_ppVoid[m_nCount - 1] = pData;
  219. return true;
  220. }
  221. bool CDuiPtrArray::InsertAt(int iIndex, LPVOID pData)
  222. {
  223. if( iIndex == m_nCount ) return Add(pData);
  224. if( iIndex < 0 || iIndex > m_nCount ) return false;
  225. if( ++m_nCount >= m_nAllocated) {
  226. int nAllocated = m_nAllocated * 2;
  227. if( nAllocated == 0 ) nAllocated = 11;
  228. LPVOID* ppVoid = static_cast<LPVOID*>(realloc(m_ppVoid, nAllocated * sizeof(LPVOID)));
  229. if( ppVoid != NULL ) {
  230. m_nAllocated = nAllocated;
  231. m_ppVoid = ppVoid;
  232. }
  233. else {
  234. --m_nCount;
  235. return false;
  236. }
  237. }
  238. memmove(&m_ppVoid[iIndex + 1], &m_ppVoid[iIndex], (m_nCount - iIndex - 1) * sizeof(LPVOID));
  239. m_ppVoid[iIndex] = pData;
  240. return true;
  241. }
  242. bool CDuiPtrArray::SetAt(int iIndex, LPVOID pData)
  243. {
  244. if( iIndex < 0 || iIndex >= m_nCount ) return false;
  245. m_ppVoid[iIndex] = pData;
  246. return true;
  247. }
  248. bool CDuiPtrArray::Remove(int iIndex, int iCount)
  249. {
  250. if( iIndex < 0 || iCount <= 0 || iIndex + iCount > m_nCount ) return false;
  251. if (iIndex + iCount < m_nCount) ::CopyMemory(m_ppVoid + iIndex, m_ppVoid + iIndex + iCount, (m_nCount - iIndex - iCount) * sizeof(LPVOID));
  252. m_nCount -= iCount;
  253. return true;
  254. }
  255. int CDuiPtrArray::Find(LPVOID pData) const
  256. {
  257. for( int i = 0; i < m_nCount; i++ ) if( m_ppVoid[i] == pData ) return i;
  258. return -1;
  259. }
  260. int CDuiPtrArray::GetSize() const
  261. {
  262. return m_nCount;
  263. }
  264. LPVOID* CDuiPtrArray::GetData()
  265. {
  266. return m_ppVoid;
  267. }
  268. LPVOID CDuiPtrArray::GetAt(int iIndex) const
  269. {
  270. if( iIndex < 0 || iIndex >= m_nCount ) return NULL;
  271. return m_ppVoid[iIndex];
  272. }
  273. LPVOID CDuiPtrArray::operator[] (int iIndex) const
  274. {
  275. ASSERT(iIndex>=0 && iIndex<m_nCount);
  276. return m_ppVoid[iIndex];
  277. }
  278. /////////////////////////////////////////////////////////////////////////////////////
  279. //
  280. //
  281. CDuiValArray::CDuiValArray(int iElementSize, int iPreallocSize /*= 0*/) :
  282. m_pVoid(NULL),
  283. m_nCount(0),
  284. m_iElementSize(iElementSize),
  285. m_nAllocated(iPreallocSize)
  286. {
  287. ASSERT(iElementSize>0);
  288. ASSERT(iPreallocSize>=0);
  289. if( iPreallocSize > 0 ) m_pVoid = static_cast<LPBYTE>(malloc(iPreallocSize * m_iElementSize));
  290. }
  291. CDuiValArray::~CDuiValArray()
  292. {
  293. if( m_pVoid != NULL ) free(m_pVoid);
  294. }
  295. void CDuiValArray::Empty()
  296. {
  297. m_nCount = 0; // NOTE: We keep the memory in place
  298. }
  299. bool CDuiValArray::IsEmpty() const
  300. {
  301. return m_nCount == 0;
  302. }
  303. bool CDuiValArray::Add(LPCVOID pData)
  304. {
  305. if( ++m_nCount >= m_nAllocated) {
  306. int nAllocated = m_nAllocated * 2;
  307. if( nAllocated == 0 ) nAllocated = 11;
  308. LPBYTE pVoid = static_cast<LPBYTE>(realloc(m_pVoid, nAllocated * m_iElementSize));
  309. if( pVoid != NULL ) {
  310. m_nAllocated = nAllocated;
  311. m_pVoid = pVoid;
  312. }
  313. else {
  314. --m_nCount;
  315. return false;
  316. }
  317. }
  318. ::CopyMemory(m_pVoid + ((m_nCount - 1) * m_iElementSize), pData, m_iElementSize);
  319. return true;
  320. }
  321. bool CDuiValArray::Remove(int iIndex, int iCount)
  322. {
  323. if( iIndex < 0 || iCount <= 0 || iIndex + iCount > m_nCount ) return false;
  324. if (iIndex + iCount < m_nCount) ::CopyMemory(m_pVoid + (iIndex * m_iElementSize), m_pVoid + (iIndex + iCount) * m_iElementSize, (m_nCount - iIndex - iCount) * m_iElementSize);
  325. m_nCount -= iCount;
  326. return true;
  327. }
  328. int CDuiValArray::GetSize() const
  329. {
  330. return m_nCount;
  331. }
  332. LPVOID CDuiValArray::GetData()
  333. {
  334. return static_cast<LPVOID>(m_pVoid);
  335. }
  336. LPVOID CDuiValArray::GetAt(int iIndex) const
  337. {
  338. if( iIndex < 0 || iIndex >= m_nCount ) return NULL;
  339. return m_pVoid + (iIndex * m_iElementSize);
  340. }
  341. LPVOID CDuiValArray::operator[] (int iIndex) const
  342. {
  343. ASSERT(iIndex>=0 && iIndex<m_nCount);
  344. return m_pVoid + (iIndex * m_iElementSize);
  345. }
  346. /////////////////////////////////////////////////////////////////////////////////////
  347. //
  348. //
  349. CDuiString::CDuiString() : m_pstr(m_szBuffer)
  350. {
  351. m_szBuffer[0] = _T('\0');
  352. }
  353. CDuiString::CDuiString(const TCHAR ch) : m_pstr(m_szBuffer)
  354. {
  355. m_szBuffer[0] = ch;
  356. m_szBuffer[1] = _T('\0');
  357. }
  358. CDuiString::CDuiString(LPCTSTR lpsz, int nLen) : m_pstr(m_szBuffer)
  359. {
  360. ASSERT(!::IsBadStringPtr(lpsz,-1) || lpsz==NULL);
  361. m_szBuffer[0] = _T('\0');
  362. Assign(lpsz, nLen);
  363. }
  364. CDuiString::CDuiString(const CDuiString& src) : m_pstr(m_szBuffer)
  365. {
  366. m_szBuffer[0] = _T('\0');
  367. Assign(src.m_pstr);
  368. }
  369. CDuiString::~CDuiString()
  370. {
  371. if( m_pstr != m_szBuffer ) free(m_pstr);
  372. }
  373. CDuiString CDuiString::ToString()
  374. {
  375. return m_pstr;
  376. }
  377. int CDuiString::GetLength() const
  378. {
  379. return (int) _tcslen(m_pstr);
  380. }
  381. CDuiString::operator LPCTSTR() const
  382. {
  383. return m_pstr;
  384. }
  385. void CDuiString::Append(LPCTSTR pstr)
  386. {
  387. int nNewLength = GetLength() + (int) _tcslen(pstr);
  388. if( nNewLength >= MAX_LOCAL_STRING_LEN ) {
  389. if( m_pstr == m_szBuffer ) {
  390. m_pstr = static_cast<LPTSTR>(malloc((nNewLength + 1) * sizeof(TCHAR)));
  391. _tcscpy(m_pstr, m_szBuffer);
  392. _tcscat(m_pstr, pstr);
  393. }
  394. else {
  395. m_pstr = static_cast<LPTSTR>(realloc(m_pstr, (nNewLength + 1) * sizeof(TCHAR)));
  396. _tcscat(m_pstr, pstr);
  397. }
  398. }
  399. else {
  400. if( m_pstr != m_szBuffer ) {
  401. free(m_pstr);
  402. m_pstr = m_szBuffer;
  403. }
  404. _tcscat(m_szBuffer, pstr);
  405. }
  406. }
  407. void CDuiString::Assign(LPCTSTR pstr, int cchMax)
  408. {
  409. if( pstr == NULL ) pstr = _T("");
  410. cchMax = (cchMax < 0 ? (int) _tcslen(pstr) : cchMax);
  411. if( cchMax < MAX_LOCAL_STRING_LEN ) {
  412. if( m_pstr != m_szBuffer ) {
  413. free(m_pstr);
  414. m_pstr = m_szBuffer;
  415. }
  416. }
  417. else if( cchMax > GetLength() || m_pstr == m_szBuffer ) {
  418. if( m_pstr == m_szBuffer ) m_pstr = NULL;
  419. m_pstr = static_cast<LPTSTR>(realloc(m_pstr, (cchMax + 1) * sizeof(TCHAR)));
  420. }
  421. _tcsncpy(m_pstr, pstr, cchMax);
  422. m_pstr[cchMax] = _T('\0');
  423. }
  424. bool CDuiString::IsEmpty() const
  425. {
  426. return m_pstr[0] == _T('\0');
  427. }
  428. void CDuiString::Empty()
  429. {
  430. if( m_pstr != m_szBuffer ) free(m_pstr);
  431. m_pstr = m_szBuffer;
  432. m_szBuffer[0] = _T('\0');
  433. }
  434. LPCTSTR CDuiString::GetData() const
  435. {
  436. return m_pstr;
  437. }
  438. TCHAR CDuiString::GetAt(int nIndex) const
  439. {
  440. return m_pstr[nIndex];
  441. }
  442. TCHAR CDuiString::operator[] (int nIndex) const
  443. {
  444. return m_pstr[nIndex];
  445. }
  446. const CDuiString& CDuiString::operator=(const CDuiString& src)
  447. {
  448. Assign(src);
  449. return *this;
  450. }
  451. const CDuiString& CDuiString::operator=(LPCTSTR lpStr)
  452. {
  453. if ( lpStr )
  454. {
  455. ASSERT(!::IsBadStringPtr(lpStr,-1));
  456. Assign(lpStr);
  457. }
  458. else
  459. {
  460. Empty();
  461. }
  462. return *this;
  463. }
  464. #ifdef _UNICODE
  465. const CDuiString& CDuiString::operator=(LPCSTR lpStr)
  466. {
  467. if ( lpStr )
  468. {
  469. ASSERT(!::IsBadStringPtrA(lpStr,-1));
  470. int cchStr = (int) strlen(lpStr) + 1;
  471. LPWSTR pwstr = (LPWSTR) _alloca(cchStr);
  472. if( pwstr != NULL ) ::MultiByteToWideChar(::GetACP(), 0, lpStr, -1, pwstr, cchStr) ;
  473. Assign(pwstr);
  474. }
  475. else
  476. {
  477. Empty();
  478. }
  479. return *this;
  480. }
  481. const CDuiString& CDuiString::operator+=(LPCSTR lpStr)
  482. {
  483. if ( lpStr )
  484. {
  485. ASSERT(!::IsBadStringPtrA(lpStr,-1));
  486. int cchStr = (int) strlen(lpStr) + 1;
  487. LPWSTR pwstr = (LPWSTR) _alloca(cchStr);
  488. if( pwstr != NULL ) ::MultiByteToWideChar(::GetACP(), 0, lpStr, -1, pwstr, cchStr) ;
  489. Append(pwstr);
  490. }
  491. return *this;
  492. }
  493. #else
  494. const CDuiString& CDuiString::operator=(LPCWSTR lpwStr)
  495. {
  496. if ( lpwStr )
  497. {
  498. ASSERT(!::IsBadStringPtrW(lpwStr,-1));
  499. int cchStr = ((int) wcslen(lpwStr) * 2) + 1;
  500. LPSTR pstr = (LPSTR) _alloca(cchStr);
  501. if( pstr != NULL ) ::WideCharToMultiByte(::GetACP(), 0, lpwStr, -1, pstr, cchStr, NULL, NULL);
  502. Assign(pstr);
  503. }
  504. else
  505. {
  506. Empty();
  507. }
  508. return *this;
  509. }
  510. const CDuiString& CDuiString::operator+=(LPCWSTR lpwStr)
  511. {
  512. if ( lpwStr )
  513. {
  514. ASSERT(!::IsBadStringPtrW(lpwStr,-1));
  515. int cchStr = ((int) wcslen(lpwStr) * 2) + 1;
  516. LPSTR pstr = (LPSTR) _alloca(cchStr);
  517. if( pstr != NULL ) ::WideCharToMultiByte(::GetACP(), 0, lpwStr, -1, pstr, cchStr, NULL, NULL);
  518. Append(pstr);
  519. }
  520. return *this;
  521. }
  522. #endif // _UNICODE
  523. const CDuiString& CDuiString::operator=(const TCHAR ch)
  524. {
  525. Empty();
  526. m_szBuffer[0] = ch;
  527. m_szBuffer[1] = _T('\0');
  528. return *this;
  529. }
  530. CDuiString CDuiString::operator+(const CDuiString& src) const
  531. {
  532. CDuiString sTemp = *this;
  533. sTemp.Append(src);
  534. return sTemp;
  535. }
  536. CDuiString CDuiString::operator+(LPCTSTR lpStr) const
  537. {
  538. if ( lpStr )
  539. {
  540. ASSERT(!::IsBadStringPtr(lpStr,-1));
  541. CDuiString sTemp = *this;
  542. sTemp.Append(lpStr);
  543. return sTemp;
  544. }
  545. return *this;
  546. }
  547. const CDuiString& CDuiString::operator+=(const CDuiString& src)
  548. {
  549. Append(src);
  550. return *this;
  551. }
  552. const CDuiString& CDuiString::operator+=(LPCTSTR lpStr)
  553. {
  554. if ( lpStr )
  555. {
  556. ASSERT(!::IsBadStringPtr(lpStr,-1));
  557. Append(lpStr);
  558. }
  559. return *this;
  560. }
  561. const CDuiString& CDuiString::operator+=(const TCHAR ch)
  562. {
  563. TCHAR str[] = { ch, _T('\0') };
  564. Append(str);
  565. return *this;
  566. }
  567. bool CDuiString::operator == (LPCTSTR str) const { return (Compare(str) == 0); };
  568. bool CDuiString::operator != (LPCTSTR str) const { return (Compare(str) != 0); };
  569. bool CDuiString::operator <= (LPCTSTR str) const { return (Compare(str) <= 0); };
  570. bool CDuiString::operator < (LPCTSTR str) const { return (Compare(str) < 0); };
  571. bool CDuiString::operator >= (LPCTSTR str) const { return (Compare(str) >= 0); };
  572. bool CDuiString::operator > (LPCTSTR str) const { return (Compare(str) > 0); };
  573. void CDuiString::SetAt(int nIndex, TCHAR ch)
  574. {
  575. ASSERT(nIndex>=0 && nIndex<GetLength());
  576. m_pstr[nIndex] = ch;
  577. }
  578. int CDuiString::Compare(LPCTSTR lpsz) const
  579. {
  580. return _tcscmp(m_pstr, lpsz);
  581. }
  582. int CDuiString::CompareNoCase(LPCTSTR lpsz) const
  583. {
  584. return _tcsicmp(m_pstr, lpsz);
  585. }
  586. void CDuiString::MakeUpper()
  587. {
  588. _tcsupr(m_pstr);
  589. }
  590. void CDuiString::MakeLower()
  591. {
  592. _tcslwr(m_pstr);
  593. }
  594. CDuiString CDuiString::Left(int iLength) const
  595. {
  596. if( iLength < 0 ) iLength = 0;
  597. if( iLength > GetLength() ) iLength = GetLength();
  598. return CDuiString(m_pstr, iLength);
  599. }
  600. CDuiString CDuiString::Mid(int iPos, int iLength) const
  601. {
  602. if( iLength < 0 ) iLength = GetLength() - iPos;
  603. if( iPos + iLength > GetLength() ) iLength = GetLength() - iPos;
  604. if( iLength <= 0 ) return CDuiString();
  605. return CDuiString(m_pstr + iPos, iLength);
  606. }
  607. CDuiString CDuiString::Right(int iLength) const
  608. {
  609. int iPos = GetLength() - iLength;
  610. if( iPos < 0 ) {
  611. iPos = 0;
  612. iLength = GetLength();
  613. }
  614. return CDuiString(m_pstr + iPos, iLength);
  615. }
  616. int CDuiString::Find(TCHAR ch, int iPos /*= 0*/) const
  617. {
  618. ASSERT(iPos>=0 && iPos<=GetLength());
  619. if( iPos != 0 && (iPos < 0 || iPos >= GetLength()) ) return -1;
  620. LPCTSTR p = _tcschr(m_pstr + iPos, ch);
  621. if( p == NULL ) return -1;
  622. return (int)(p - m_pstr);
  623. }
  624. int CDuiString::Find(LPCTSTR pstrSub, int iPos /*= 0*/) const
  625. {
  626. ASSERT(!::IsBadStringPtr(pstrSub,-1));
  627. ASSERT(iPos>=0 && iPos<=GetLength());
  628. if( iPos != 0 && (iPos < 0 || iPos > GetLength()) ) return -1;
  629. LPCTSTR p = _tcsstr(m_pstr + iPos, pstrSub);
  630. if( p == NULL ) return -1;
  631. return (int)(p - m_pstr);
  632. }
  633. int CDuiString::ReverseFind(TCHAR ch) const
  634. {
  635. LPCTSTR p = _tcsrchr(m_pstr, ch);
  636. if( p == NULL ) return -1;
  637. return (int)(p - m_pstr);
  638. }
  639. int CDuiString::Replace(LPCTSTR pstrFrom, LPCTSTR pstrTo)
  640. {
  641. CDuiString sTemp;
  642. int nCount = 0;
  643. int iPos = Find(pstrFrom);
  644. if( iPos < 0 ) return 0;
  645. int cchFrom = (int) _tcslen(pstrFrom);
  646. int cchTo = (int) _tcslen(pstrTo);
  647. while( iPos >= 0 ) {
  648. sTemp = Left(iPos);
  649. sTemp += pstrTo;
  650. sTemp += Mid(iPos + cchFrom);
  651. Assign(sTemp);
  652. iPos = Find(pstrFrom, iPos + cchTo);
  653. nCount++;
  654. }
  655. return nCount;
  656. }
  657. int CDuiString::Format(LPCTSTR pstrFormat, ...)
  658. {
  659. LPTSTR szSprintf = NULL;
  660. va_list argList;
  661. int nLen;
  662. va_start(argList, pstrFormat);
  663. nLen = _vsntprintf(NULL, 0, pstrFormat, argList);
  664. szSprintf = (TCHAR*)malloc((nLen + 1) * sizeof(TCHAR));
  665. ZeroMemory(szSprintf, (nLen + 1) * sizeof(TCHAR));
  666. int iRet = _vsntprintf(szSprintf, nLen + 1, pstrFormat, argList);
  667. va_end(argList);
  668. Assign(szSprintf);
  669. free(szSprintf);
  670. return iRet;
  671. }
  672. int CDuiString::SmallFormat(LPCTSTR pstrFormat, ...)
  673. {
  674. CDuiString sFormat = pstrFormat;
  675. TCHAR szBuffer[64] = { 0 };
  676. va_list argList;
  677. va_start(argList, pstrFormat);
  678. int iRet = ::wvsprintf(szBuffer, sFormat, argList);
  679. va_end(argList);
  680. Assign(szBuffer);
  681. return iRet;
  682. }
  683. /////////////////////////////////////////////////////////////////////////////
  684. //
  685. //
  686. struct TITEM
  687. {
  688. CDuiString Key;
  689. LPVOID Data;
  690. struct TITEM* pPrev;
  691. struct TITEM* pNext;
  692. };
  693. static UINT HashKey(LPCTSTR Key)
  694. {
  695. UINT i = 0;
  696. SIZE_T len = _tcslen(Key);
  697. while( len-- > 0 ) i = (i << 5) + i + Key[len];
  698. return i;
  699. }
  700. static UINT HashKey(const CDuiString& Key)
  701. {
  702. return HashKey((LPCTSTR)Key);
  703. };
  704. CDuiStringPtrMap::CDuiStringPtrMap(int nSize) : m_nCount(0)
  705. {
  706. if( nSize < 16 ) nSize = 16;
  707. m_nBuckets = nSize;
  708. m_aT = new TITEM*[nSize];
  709. memset(m_aT, 0, nSize * sizeof(TITEM*));
  710. }
  711. CDuiStringPtrMap::~CDuiStringPtrMap()
  712. {
  713. if( m_aT ) {
  714. int len = m_nBuckets;
  715. while( len-- ) {
  716. TITEM* pItem = m_aT[len];
  717. while( pItem ) {
  718. TITEM* pKill = pItem;
  719. pItem = pItem->pNext;
  720. delete pKill;
  721. }
  722. }
  723. delete [] m_aT;
  724. m_aT = NULL;
  725. }
  726. }
  727. void CDuiStringPtrMap::RemoveAll()
  728. {
  729. this->Resize(m_nBuckets);
  730. }
  731. void CDuiStringPtrMap::Resize(int nSize)
  732. {
  733. if( m_aT ) {
  734. int len = m_nBuckets;
  735. while( len-- ) {
  736. TITEM* pItem = m_aT[len];
  737. while( pItem ) {
  738. TITEM* pKill = pItem;
  739. pItem = pItem->pNext;
  740. delete pKill;
  741. }
  742. }
  743. delete [] m_aT;
  744. m_aT = NULL;
  745. }
  746. if( nSize < 0 ) nSize = 0;
  747. if( nSize > 0 ) {
  748. m_aT = new TITEM*[nSize];
  749. memset(m_aT, 0, nSize * sizeof(TITEM*));
  750. }
  751. m_nBuckets = nSize;
  752. m_nCount = 0;
  753. }
  754. LPVOID CDuiStringPtrMap::Find(LPCTSTR key, bool optimize) const
  755. {
  756. if( m_nBuckets == 0 || GetSize() == 0 ) return NULL;
  757. UINT slot = HashKey(key) % m_nBuckets;
  758. for( TITEM* pItem = m_aT[slot]; pItem; pItem = pItem->pNext ) {
  759. if( pItem->Key == key ) {
  760. if (optimize && pItem != m_aT[slot]) {
  761. if (pItem->pNext) {
  762. pItem->pNext->pPrev = pItem->pPrev;
  763. }
  764. pItem->pPrev->pNext = pItem->pNext;
  765. pItem->pPrev = NULL;
  766. pItem->pNext = m_aT[slot];
  767. pItem->pNext->pPrev = pItem;
  768. //½«itemÒÆ¶¯ÖÁÁ´ÌõÍ·²¿
  769. m_aT[slot] = pItem;
  770. }
  771. return pItem->Data;
  772. }
  773. }
  774. return NULL;
  775. }
  776. bool CDuiStringPtrMap::Insert(LPCTSTR key, LPVOID pData)
  777. {
  778. if( m_nBuckets == 0 ) return false;
  779. if( Find(key) ) return false;
  780. // Add first in bucket
  781. UINT slot = HashKey(key) % m_nBuckets;
  782. TITEM* pItem = new TITEM;
  783. pItem->Key = key;
  784. pItem->Data = pData;
  785. pItem->pPrev = NULL;
  786. pItem->pNext = m_aT[slot];
  787. if (pItem->pNext)
  788. pItem->pNext->pPrev = pItem;
  789. m_aT[slot] = pItem;
  790. m_nCount++;
  791. return true;
  792. }
  793. LPVOID CDuiStringPtrMap::Set(LPCTSTR key, LPVOID pData)
  794. {
  795. if( m_nBuckets == 0 ) return pData;
  796. if (GetSize()>0) {
  797. UINT slot = HashKey(key) % m_nBuckets;
  798. // Modify existing item
  799. for( TITEM* pItem = m_aT[slot]; pItem; pItem = pItem->pNext ) {
  800. if( pItem->Key == key ) {
  801. LPVOID pOldData = pItem->Data;
  802. pItem->Data = pData;
  803. return pOldData;
  804. }
  805. }
  806. }
  807. Insert(key, pData);
  808. return NULL;
  809. }
  810. bool CDuiStringPtrMap::Remove(LPCTSTR key)
  811. {
  812. if( m_nBuckets == 0 || GetSize() == 0 ) return false;
  813. UINT slot = HashKey(key) % m_nBuckets;
  814. TITEM** ppItem = &m_aT[slot];
  815. while( *ppItem ) {
  816. if( (*ppItem)->Key == key ) {
  817. TITEM* pKill = *ppItem;
  818. *ppItem = (*ppItem)->pNext;
  819. if (*ppItem)
  820. (*ppItem)->pPrev = pKill->pPrev;
  821. delete pKill;
  822. m_nCount--;
  823. return true;
  824. }
  825. ppItem = &((*ppItem)->pNext);
  826. }
  827. return false;
  828. }
  829. int CDuiStringPtrMap::GetSize() const
  830. {
  831. #if 0//def _DEBUG
  832. int nCount = 0;
  833. int len = m_nBuckets;
  834. while( len-- ) {
  835. for( const TITEM* pItem = m_aT[len]; pItem; pItem = pItem->pNext ) nCount++;
  836. }
  837. ASSERT(m_nCount==nCount);
  838. #endif
  839. return m_nCount;
  840. }
  841. LPCTSTR CDuiStringPtrMap::GetAt(int iIndex) const
  842. {
  843. if( m_nBuckets == 0 || GetSize() == 0 ) return false;
  844. int pos = 0;
  845. int len = m_nBuckets;
  846. while( len-- ) {
  847. for( TITEM* pItem = m_aT[len]; pItem; pItem = pItem->pNext ) {
  848. if( pos++ == iIndex ) {
  849. return pItem->Key.GetData();
  850. }
  851. }
  852. }
  853. return NULL;
  854. }
  855. LPCTSTR CDuiStringPtrMap::operator[] (int nIndex) const
  856. {
  857. return GetAt(nIndex);
  858. }
  859. /////////////////////////////////////////////////////////////////////////////////////
  860. //
  861. //
  862. CWaitCursor::CWaitCursor()
  863. {
  864. m_hOrigCursor = ::SetCursor(::LoadCursor(NULL, IDC_WAIT));
  865. }
  866. CWaitCursor::~CWaitCursor()
  867. {
  868. ::SetCursor(m_hOrigCursor);
  869. }
  870. } // namespace DuiLib