Utils.cpp 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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] = '\0';
  300. }
  301. CDuiString::CDuiString(const TCHAR ch) : m_pstr(m_szBuffer)
  302. {
  303. m_szBuffer[0] = ch;
  304. m_szBuffer[1] = '\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] = '\0';
  310. Assign(lpsz, nLen);
  311. }
  312. CDuiString::CDuiString(const CDuiString& src) : m_pstr(m_szBuffer)
  313. {
  314. m_szBuffer[0] = '\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] = '\0';
  367. }
  368. bool CDuiString::IsEmpty() const
  369. {
  370. return m_pstr[0] == '\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] = '\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] = '\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, '\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. CDuiString& CDuiString::TrimLeft()
  561. {
  562. // find first non-space character
  563. LPTSTR psz = this->m_pstr;
  564. while (::_istspace(*psz))
  565. {
  566. psz = ::CharNext(psz);
  567. }
  568. if (psz != this->m_pstr)
  569. {
  570. int iFirst = int(psz - this->m_pstr);
  571. Assign(psz, this->GetLength() - iFirst);
  572. }
  573. return(*this);
  574. }
  575. CDuiString& CDuiString::TrimRight()
  576. {
  577. LPTSTR psz = this->m_pstr;
  578. LPTSTR pszLast = NULL;
  579. while (*psz != 0)
  580. {
  581. if (::_istspace(*psz))
  582. {
  583. if (pszLast == NULL)
  584. pszLast = psz;
  585. }
  586. else
  587. {
  588. pszLast = NULL;
  589. }
  590. psz = ::CharNext(psz);
  591. }
  592. if (pszLast != NULL)
  593. {
  594. // truncate at trailing space start
  595. int iLast = int(pszLast - this->GetData());
  596. this->SetAt(iLast, 0);
  597. }
  598. return(*this);
  599. }
  600. CDuiString& CDuiString::Trim()
  601. {
  602. TrimLeft();
  603. TrimRight();
  604. return(*this);
  605. }
  606. int CDuiString::Find(TCHAR ch, int iPos /*= 0*/) const
  607. {
  608. ASSERT(iPos>=0 && iPos<=GetLength());
  609. if( iPos != 0 && (iPos < 0 || iPos >= GetLength()) ) return -1;
  610. LPCTSTR p = _tcschr(m_pstr + iPos, ch);
  611. if( p == NULL ) return -1;
  612. return (int)(p - m_pstr);
  613. }
  614. int CDuiString::Find(LPCTSTR pstrSub, int iPos /*= 0*/) const
  615. {
  616. ASSERT(!::IsBadStringPtr(pstrSub,-1));
  617. ASSERT(iPos>=0 && iPos<=GetLength());
  618. if( iPos != 0 && (iPos < 0 || iPos > GetLength()) ) return -1;
  619. LPCTSTR p = _tcsstr(m_pstr + iPos, pstrSub);
  620. if( p == NULL ) return -1;
  621. return (int)(p - m_pstr);
  622. }
  623. int CDuiString::ReverseFind(TCHAR ch) const
  624. {
  625. LPCTSTR p = _tcsrchr(m_pstr, ch);
  626. if( p == NULL ) return -1;
  627. return (int)(p - m_pstr);
  628. }
  629. int CDuiString::Replace(LPCTSTR pstrFrom, LPCTSTR pstrTo)
  630. {
  631. CDuiString sTemp;
  632. int nCount = 0;
  633. int iPos = Find(pstrFrom);
  634. if( iPos < 0 ) return 0;
  635. int cchFrom = (int) _tcslen(pstrFrom);
  636. int cchTo = (int) _tcslen(pstrTo);
  637. while( iPos >= 0 ) {
  638. sTemp = Left(iPos);
  639. sTemp += pstrTo;
  640. sTemp += Mid(iPos + cchFrom);
  641. Assign(sTemp);
  642. iPos = Find(pstrFrom, iPos + cchTo);
  643. nCount++;
  644. }
  645. return nCount;
  646. }
  647. int CDuiString::Format(LPCTSTR pstrFormat, ...)
  648. {
  649. int nRet;
  650. va_list Args;
  651. va_start(Args, pstrFormat);
  652. nRet = InnerFormat(pstrFormat, Args);
  653. va_end(Args);
  654. return nRet;
  655. }
  656. int CDuiString::SmallFormat(LPCTSTR pstrFormat, ...)
  657. {
  658. CDuiString sFormat = pstrFormat;
  659. TCHAR szBuffer[64] = { 0 };
  660. va_list argList;
  661. va_start(argList, pstrFormat);
  662. int iRet = ::_vsntprintf(szBuffer, sizeof(szBuffer), sFormat, argList);
  663. va_end(argList);
  664. Assign(szBuffer);
  665. return iRet;
  666. }
  667. int CDuiString::InnerFormat(LPCTSTR pstrFormat, va_list Args)
  668. {
  669. #if _MSC_VER <= 1400
  670. TCHAR *szBuffer = NULL;
  671. int size = 512, nLen, counts;
  672. szBuffer = (TCHAR*)malloc(size);
  673. ZeroMemory(szBuffer, size);
  674. while (TRUE){
  675. counts = size / sizeof(TCHAR);
  676. nLen = _vsntprintf (szBuffer, counts, pstrFormat, Args);
  677. if (nLen != -1 && nLen < counts){
  678. break;
  679. }
  680. if (nLen == -1){
  681. size *= 2;
  682. }else{
  683. size += 1 * sizeof(TCHAR);
  684. }
  685. if ((szBuffer = (TCHAR*)realloc(szBuffer, size)) != NULL){
  686. ZeroMemory(szBuffer, size);
  687. }else{
  688. break;
  689. }
  690. }
  691. Assign(szBuffer);
  692. free(szBuffer);
  693. return nLen;
  694. #else
  695. int nLen, totalLen;
  696. TCHAR *szBuffer;
  697. nLen = _vsntprintf(NULL, 0, pstrFormat, Args);
  698. totalLen = (nLen + 1)*sizeof(TCHAR);
  699. szBuffer = (TCHAR*)malloc(totalLen);
  700. ZeroMemory(szBuffer, totalLen);
  701. nLen = _vsntprintf(szBuffer, nLen + 1, pstrFormat, Args);
  702. Assign(szBuffer);
  703. free(szBuffer);
  704. return nLen;
  705. #endif
  706. }
  707. /////////////////////////////////////////////////////////////////////////////
  708. //
  709. //
  710. static UINT HashKey(LPCTSTR Key)
  711. {
  712. UINT i = 0;
  713. SIZE_T len = _tcslen(Key);
  714. while( len-- > 0 ) i = (i << 5) + i + Key[len];
  715. return i;
  716. }
  717. static UINT HashKey(const CDuiString& Key)
  718. {
  719. return HashKey((LPCTSTR)Key);
  720. };
  721. CStdStringPtrMap::CStdStringPtrMap(int nSize) : m_nCount(0)
  722. {
  723. if( nSize < 16 ) nSize = 16;
  724. m_nBuckets = nSize;
  725. m_aT = new TITEM*[nSize];
  726. memset(m_aT, 0, nSize * sizeof(TITEM*));
  727. }
  728. CStdStringPtrMap::~CStdStringPtrMap()
  729. {
  730. if( m_aT ) {
  731. int len = m_nBuckets;
  732. while( len-- ) {
  733. TITEM* pItem = m_aT[len];
  734. while( pItem ) {
  735. TITEM* pKill = pItem;
  736. pItem = pItem->pNext;
  737. delete pKill;
  738. }
  739. }
  740. delete [] m_aT;
  741. m_aT = NULL;
  742. }
  743. }
  744. void CStdStringPtrMap::RemoveAll()
  745. {
  746. this->Resize(m_nBuckets);
  747. }
  748. void CStdStringPtrMap::Resize(int nSize)
  749. {
  750. if( m_aT ) {
  751. int len = m_nBuckets;
  752. while( len-- ) {
  753. TITEM* pItem = m_aT[len];
  754. while( pItem ) {
  755. TITEM* pKill = pItem;
  756. pItem = pItem->pNext;
  757. delete pKill;
  758. }
  759. }
  760. delete [] m_aT;
  761. m_aT = NULL;
  762. }
  763. if( nSize < 0 ) nSize = 0;
  764. if( nSize > 0 ) {
  765. m_aT = new TITEM*[nSize];
  766. memset(m_aT, 0, nSize * sizeof(TITEM*));
  767. }
  768. m_nBuckets = nSize;
  769. m_nCount = 0;
  770. }
  771. LPVOID CStdStringPtrMap::Find(LPCTSTR key, bool optimize) const
  772. {
  773. if( m_nBuckets == 0 || GetSize() == 0 ) return NULL;
  774. UINT slot = HashKey(key) % m_nBuckets;
  775. for( TITEM* pItem = m_aT[slot]; pItem; pItem = pItem->pNext ) {
  776. if( pItem->Key == key ) {
  777. if (optimize && pItem != m_aT[slot]) {
  778. if (pItem->pNext) {
  779. pItem->pNext->pPrev = pItem->pPrev;
  780. }
  781. pItem->pPrev->pNext = pItem->pNext;
  782. pItem->pPrev = NULL;
  783. pItem->pNext = m_aT[slot];
  784. pItem->pNext->pPrev = pItem;
  785. //½«itemÒƶ¯ÖÁÁ´ÌõÍ·²¿
  786. m_aT[slot] = pItem;
  787. }
  788. return pItem->Data;
  789. }
  790. }
  791. return NULL;
  792. }
  793. bool CStdStringPtrMap::Insert(LPCTSTR key, LPVOID pData)
  794. {
  795. if( m_nBuckets == 0 ) return false;
  796. if( Find(key) ) return false;
  797. // Add first in bucket
  798. UINT slot = HashKey(key) % m_nBuckets;
  799. TITEM* pItem = new TITEM;
  800. pItem->Key = key;
  801. pItem->Data = pData;
  802. pItem->pPrev = NULL;
  803. pItem->pNext = m_aT[slot];
  804. if (pItem->pNext)
  805. pItem->pNext->pPrev = pItem;
  806. m_aT[slot] = pItem;
  807. m_nCount++;
  808. return true;
  809. }
  810. LPVOID CStdStringPtrMap::Set(LPCTSTR key, LPVOID pData)
  811. {
  812. if( m_nBuckets == 0 ) return pData;
  813. if (GetSize()>0) {
  814. UINT slot = HashKey(key) % m_nBuckets;
  815. // Modify existing item
  816. for( TITEM* pItem = m_aT[slot]; pItem; pItem = pItem->pNext ) {
  817. if( pItem->Key == key ) {
  818. LPVOID pOldData = pItem->Data;
  819. pItem->Data = pData;
  820. return pOldData;
  821. }
  822. }
  823. }
  824. Insert(key, pData);
  825. return NULL;
  826. }
  827. bool CStdStringPtrMap::Remove(LPCTSTR key)
  828. {
  829. if( m_nBuckets == 0 || GetSize() == 0 ) return false;
  830. UINT slot = HashKey(key) % m_nBuckets;
  831. TITEM** ppItem = &m_aT[slot];
  832. while( *ppItem ) {
  833. if( (*ppItem)->Key == key ) {
  834. TITEM* pKill = *ppItem;
  835. *ppItem = (*ppItem)->pNext;
  836. if (*ppItem)
  837. (*ppItem)->pPrev = pKill->pPrev;
  838. delete pKill;
  839. m_nCount--;
  840. return true;
  841. }
  842. ppItem = &((*ppItem)->pNext);
  843. }
  844. return false;
  845. }
  846. int CStdStringPtrMap::GetSize() const
  847. {
  848. #if 0//def _DEBUG
  849. int nCount = 0;
  850. int len = m_nBuckets;
  851. while( len-- ) {
  852. for( const TITEM* pItem = m_aT[len]; pItem; pItem = pItem->pNext ) nCount++;
  853. }
  854. ASSERT(m_nCount==nCount);
  855. #endif
  856. return m_nCount;
  857. }
  858. LPCTSTR CStdStringPtrMap::GetAt(int iIndex) const
  859. {
  860. if( m_nBuckets == 0 || GetSize() == 0 ) return false;
  861. int pos = 0;
  862. int len = m_nBuckets;
  863. while( len-- ) {
  864. for( TITEM* pItem = m_aT[len]; pItem; pItem = pItem->pNext ) {
  865. if( pos++ == iIndex ) {
  866. return pItem->Key.GetData();
  867. }
  868. }
  869. }
  870. return NULL;
  871. }
  872. LPCTSTR CStdStringPtrMap::operator[] (int nIndex) const
  873. {
  874. return GetAt(nIndex);
  875. }
  876. /////////////////////////////////////////////////////////////////////////////////////
  877. //
  878. //
  879. CWaitCursor::CWaitCursor()
  880. {
  881. m_hOrigCursor = ::SetCursor(::LoadCursor(NULL, IDC_WAIT));
  882. }
  883. CWaitCursor::~CWaitCursor()
  884. {
  885. ::SetCursor(m_hOrigCursor);
  886. }
  887. } // namespace DuiLib