Utils.cpp 19 KB

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