Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
6.5 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: misc.cpp
  4. //
  5. // Module: CMPBK32.DLL
  6. //
  7. // Synopsis: Miscellaneous phone book utility functions.
  8. //
  9. // Copyright (c) 1998 Microsoft Corporation
  10. //
  11. // Author: quintinb created header 08/17/99
  12. //
  13. //+----------------------------------------------------------------------------
  14. // ############################################################################
  15. // Miscellaneous support routines
  16. #include "cmmaster.h"
  17. /* Not used anywhere, take out
  18. #define irgMaxSzs 5
  19. char szStrTable[irgMaxSzs][256];
  20. // ############################################################################
  21. PSTR GetSz(WORD wszID)
  22. {
  23. static int iSzTable=0;
  24. PSTR psz = (PSTR) szStrTable[iSzTable];
  25. iSzTable++;
  26. if (iSzTable >= irgMaxSzs)
  27. iSzTable = 0;
  28. if (!LoadString(g_hInst, wszID, psz, 256))
  29. {
  30. CMTRACE1("LoadString failed %d\n", (DWORD) wszID);
  31. *psz = 0;
  32. }
  33. return (psz);
  34. }
  35. */
  36. // ############################################################################
  37. void SzCanonicalFromAE (char *psz, PACCESSENTRY pAE, LPLINECOUNTRYENTRY pLCE)
  38. {
  39. if (NO_AREA_CODE == pAE->dwAreaCode)
  40. {
  41. wsprintf(psz, "+%lu %s", pLCE->dwCountryCode, pAE->szAccessNumber);
  42. }
  43. else
  44. {
  45. wsprintf(psz, "+%lu (%s) %s", pLCE->dwCountryCode, pAE->szAreaCode, pAE->szAccessNumber);
  46. }
  47. return;
  48. }
  49. // ############################################################################
  50. void SzNonCanonicalFromAE (char *psz, PACCESSENTRY pAE, LPLINECOUNTRYENTRY pLCE)
  51. {
  52. if (NO_AREA_CODE == pAE->dwAreaCode)
  53. {
  54. wsprintf(psz, "%lu %s", pLCE->dwCountryCode, pAE->szAccessNumber);
  55. }
  56. else
  57. {
  58. wsprintf(psz, "%lu %s %s", pLCE->dwCountryCode, pAE->szAreaCode, pAE->szAccessNumber);
  59. }
  60. return;
  61. }
  62. // ############################################################################
  63. int MyStrcmp(PVOID pv1, PVOID pv2)
  64. {
  65. char *pc1 = (char*) pv1;
  66. char *pc2 = (char*) pv2;
  67. int iRC = 0;
  68. // loop while not pointed at the ending NULL character and no difference has been found
  69. while (*pc1 && *pc2 && !iRC)
  70. {
  71. iRC = (int)(*pc1 - *pc2);
  72. pc1++;
  73. pc2++;
  74. }
  75. // if we exited because we got to the end of one string before we found a difference
  76. // return -1 if pv1 is longer, else return the character pointed at by pv2. If pv2
  77. // is longer than pv1 then the value at pv2 will be greater than 0. If both strings
  78. // ended at the same time, then pv2 will point to 0.
  79. if (!iRC)
  80. {
  81. iRC = (*pc1) ? -1 : (*pc2);
  82. }
  83. return iRC;
  84. }
  85. // ############################################################################
  86. int __cdecl CompareIDLookUpElements(const void*e1, const void*e2)
  87. {
  88. if (((PIDLOOKUPELEMENT)e1)->dwID > ((PIDLOOKUPELEMENT)e2)->dwID)
  89. return 1;
  90. if (((PIDLOOKUPELEMENT)e1)->dwID < ((PIDLOOKUPELEMENT)e2)->dwID)
  91. return -1;
  92. return 0;
  93. }
  94. // ############################################################################
  95. int __cdecl CompareCntryNameLookUpElementsA(const void*e1, const void*e2)
  96. {
  97. PCNTRYNAMELOOKUPELEMENT pCUE1 = (PCNTRYNAMELOOKUPELEMENT)e1;
  98. PCNTRYNAMELOOKUPELEMENT pCUE2 = (PCNTRYNAMELOOKUPELEMENT)e2;
  99. return CompareStringA(LOCALE_USER_DEFAULT,0,pCUE1->psCountryName,
  100. pCUE1->dwNameSize,pCUE2->psCountryName,
  101. pCUE2->dwNameSize) - 2;
  102. }
  103. // ############################################################################
  104. int __cdecl CompareCntryNameLookUpElementsW(const void*e1, const void*e2)
  105. {
  106. PCNTRYNAMELOOKUPELEMENTW pCUE1 = (PCNTRYNAMELOOKUPELEMENTW)e1;
  107. PCNTRYNAMELOOKUPELEMENTW pCUE2 = (PCNTRYNAMELOOKUPELEMENTW)e2;
  108. return CompareStringW(LOCALE_USER_DEFAULT,0,pCUE1->psCountryName,
  109. pCUE1->dwNameSize,pCUE2->psCountryName,
  110. pCUE2->dwNameSize) - 2;
  111. }
  112. // ############################################################################
  113. int __cdecl CompareIdxLookUpElements(const void*e1, const void*e2)
  114. {
  115. PIDXLOOKUPELEMENT pidx1 = (PIDXLOOKUPELEMENT) e1;
  116. PIDXLOOKUPELEMENT pidx2 = (PIDXLOOKUPELEMENT) e2;
  117. if (pidx1->dwIndex > pidx2->dwIndex)
  118. {
  119. return 1;
  120. }
  121. if (pidx1->dwIndex < pidx2->dwIndex)
  122. {
  123. return -1;
  124. }
  125. return 0;
  126. }
  127. // ############################################################################
  128. int __cdecl CompareIdxLookUpElementsFileOrder(const void *pv1, const void *pv2)
  129. {
  130. PACCESSENTRY pae1, pae2;
  131. int iSort;
  132. pae1 = (PACCESSENTRY) (((PIDXLOOKUPELEMENT)pv1)->iAE);
  133. pae2 = (PACCESSENTRY) (((PIDXLOOKUPELEMENT)pv2)->iAE);
  134. // sort empty enteries to the end of the list
  135. if (!(pae1 && pae2))
  136. {
  137. // return ((int)pae1) ? -1 : ((int)pae2);
  138. return (pae1 ? -1 : (pae2 ? 1 : 0));
  139. }
  140. // country ASC, state ASC, city ASC, toll free DESC, flip DESC, con spd max DESC
  141. if (pae1->dwCountryID != pae2->dwCountryID)
  142. {
  143. return (int) (pae1->dwCountryID - pae2->dwCountryID);
  144. }
  145. if (pae1->wStateID != pae2->wStateID)
  146. {
  147. return (pae1->wStateID - pae2->wStateID);
  148. }
  149. iSort = MyStrcmp((PVOID)pae1->szCity, (PVOID)pae2->szCity);
  150. if (iSort)
  151. {
  152. return (iSort);
  153. }
  154. if (pae1->fType != pae2->fType)
  155. {
  156. return (int) (pae2->fType - pae1->fType);
  157. }
  158. if (pae1->bFlipFactor != pae2->bFlipFactor)
  159. {
  160. return (pae2->bFlipFactor - pae1->bFlipFactor);
  161. }
  162. if (pae1->dwConnectSpeedMax != pae2->dwConnectSpeedMax)
  163. {
  164. return (int) (pae2->dwConnectSpeedMax - pae1->dwConnectSpeedMax);
  165. }
  166. return 0;
  167. }
  168. // ############################################################################
  169. //inline BOOL FSz2Dw(PCSTR pSz,DWORD *dw)
  170. BOOL FSz2Dw(PCSTR pSz,DWORD *dw)
  171. {
  172. DWORD val = 0;
  173. while (*pSz)
  174. {
  175. if (*pSz >= '0' && *pSz <= '9')
  176. {
  177. val *= 10;
  178. val += *pSz++ - '0';
  179. }
  180. else
  181. {
  182. return FALSE; //bad number
  183. }
  184. }
  185. *dw = val;
  186. return (TRUE);
  187. }
  188. // ############################################################################
  189. //inline BOOL FSz2W(PCSTR pSz,WORD *w)
  190. BOOL FSz2W(PCSTR pSz,WORD *w)
  191. {
  192. DWORD dw;
  193. if (FSz2Dw(pSz,&dw))
  194. {
  195. *w = (WORD)dw;
  196. return TRUE;
  197. }
  198. return FALSE;
  199. }
  200. // ############################################################################
  201. //inline BOOL FSz2B(PCSTR pSz,BYTE *pb)
  202. BOOL FSz2B(PCSTR pSz,BYTE *pb)
  203. {
  204. DWORD dw;
  205. if (FSz2Dw(pSz,&dw))
  206. {
  207. *pb = (BYTE)dw;
  208. return TRUE;
  209. }
  210. return FALSE;
  211. }