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.

299 lines
8.8 KiB

  1. /*++
  2. Copyright (c) 1991-1999, Microsoft Corporation All rights reserved.
  3. Module Name:
  4. geo.c
  5. Abstract:
  6. This file contains functions necessary to parse amd write the GEO
  7. specific tables to a data file.
  8. External Routines in this file:
  9. ParseWriteGEO
  10. Revision History:
  11. 11-02-99 WeiWu Created.
  12. 03-10-00 lguindon Began GEO API port.
  13. 09-12-00 JulieB Fixed buffer sizes and other problems.
  14. --*/
  15. //
  16. // Include Files.
  17. //
  18. #include "nlstrans.h"
  19. ////////////////////////////////////////////////////////////////////////////
  20. //
  21. // ParseWriteGEO
  22. //
  23. // This routine parses the input file for the GEO specific tables, and
  24. // then writes the data to the output file.
  25. //
  26. ////////////////////////////////////////////////////////////////////////////
  27. int
  28. ParseWriteGEO(
  29. PSZ pszKeyWord)
  30. {
  31. int nSize = 0;
  32. int Ctr;
  33. FILE *pOutputFile;
  34. GEOTABLEHDR GeoTableHdr;
  35. if ((pOutputFile = fopen(GEOFILE, "w+b")) == 0)
  36. {
  37. printf("Error opening output file %s.\n", GEOFILE);
  38. return (1);
  39. }
  40. //
  41. // Prepare GEO table header.
  42. //
  43. wcscpy(GeoTableHdr.szSig, L"geo");
  44. //
  45. // Write GEO table header place holder.
  46. //
  47. if (FileWrite(pOutputFile, &GeoTableHdr, sizeof(GEOTABLEHDR), 1, "GEOHEADER"))
  48. {
  49. fclose(pOutputFile);
  50. return (1);
  51. }
  52. //
  53. // Scan GEO text file.
  54. //
  55. while (fscanf(pInputFile, "%s", pszKeyWord) == 1)
  56. {
  57. //
  58. // GEOINFO table.
  59. //
  60. if (_stricmp(pszKeyWord, "GEOINFO") == 0)
  61. {
  62. PGEODATA pGeoData = NULL;
  63. if (Verbose)
  64. {
  65. printf("\n\nFound GEOINFO keyword.\n");
  66. }
  67. GetSize(&nSize);
  68. if (nSize)
  69. {
  70. pGeoData = (PGEODATA)malloc(sizeof(GEODATA) * nSize);
  71. }
  72. if (pGeoData)
  73. {
  74. for (Ctr = 0; Ctr < nSize; Ctr++)
  75. {
  76. CHAR szLatitude[MAX_LATITUDE];
  77. CHAR szLongitude[MAX_LONGITUDE];
  78. CHAR szISO3166Abbrev2[MAX_ISO_ABBREV];
  79. CHAR szISO3166Abbrev3[MAX_ISO_ABBREV];
  80. //
  81. // Scan Values
  82. //
  83. int NumItems = 0;
  84. NumItems = fscanf( pInputFile,
  85. "%ld %s %s %lu %ld %s %s %lu ;%*[^\n]",
  86. &pGeoData[Ctr].GeoId,
  87. szLatitude,
  88. szLongitude,
  89. &pGeoData[Ctr].GeoClass,
  90. &pGeoData[Ctr].ParentGeoId,
  91. szISO3166Abbrev2,
  92. szISO3166Abbrev3,
  93. &pGeoData[Ctr].wISO3166 );
  94. //
  95. // Convert value to UNICODE
  96. //
  97. if (MultiByteToWideChar( CP_ACP,
  98. 0,
  99. szLatitude,
  100. -1,
  101. pGeoData[Ctr].szLatitude,
  102. MAX_LATITUDE ) == 0)
  103. {
  104. printf("Error converting latitude string in file %s.\n", GEOFILE);
  105. fclose(pOutputFile);
  106. free(pGeoData);
  107. return (1);
  108. }
  109. if (MultiByteToWideChar( CP_ACP,
  110. 0,
  111. szLongitude,
  112. -1,
  113. pGeoData[Ctr].szLongitude,
  114. MAX_LONGITUDE ) == 0)
  115. {
  116. printf("Error converting longitude string in file %s.\n", GEOFILE);
  117. fclose(pOutputFile);
  118. free(pGeoData);
  119. return (1);
  120. }
  121. if (MultiByteToWideChar( CP_ACP,
  122. 0,
  123. szISO3166Abbrev2,
  124. -1,
  125. pGeoData[Ctr].szISO3166Abbrev2,
  126. MAX_ISO_ABBREV ) == 0)
  127. {
  128. printf("Error converting 2-char abbreviated name in file %s.\n", GEOFILE);
  129. fclose(pOutputFile);
  130. free(pGeoData);
  131. return (1);
  132. }
  133. if (MultiByteToWideChar( CP_ACP,
  134. 0,
  135. szISO3166Abbrev3,
  136. -1,
  137. pGeoData[Ctr].szISO3166Abbrev3,
  138. MAX_ISO_ABBREV ) == 0)
  139. {
  140. printf("Error converting 3-char abbreviated name in file %s.\n", GEOFILE);
  141. fclose(pOutputFile);
  142. free(pGeoData);
  143. return (1);
  144. }
  145. //
  146. // Print if in verbose mode
  147. //
  148. if (Verbose)
  149. {
  150. printf("ID:%ld, LAT:%s, LON:%s, CLASS:%lu, PARENT:%ld, ISO3166-2:%s, IS03166-3:%s, ISO3166:%lu \n",
  151. pGeoData[Ctr].GeoId,
  152. szLatitude,
  153. szLongitude,
  154. pGeoData[Ctr].GeoClass,
  155. pGeoData[Ctr].ParentGeoId,
  156. szISO3166Abbrev2,
  157. szISO3166Abbrev3,
  158. pGeoData[Ctr].wISO3166 );
  159. }
  160. }
  161. //
  162. // Update GEO table header.
  163. //
  164. GeoTableHdr.dwOffsetGeoInfo = ftell(pOutputFile);
  165. GeoTableHdr.nGeoInfo = nSize;
  166. //
  167. // Write GEOINFO.
  168. //
  169. if (FileWrite(pOutputFile, pGeoData, sizeof(GEODATA), nSize, "GEOINFO"))
  170. {
  171. fclose(pOutputFile);
  172. free(pGeoData);
  173. return (1);
  174. }
  175. free(pGeoData);
  176. }
  177. }
  178. //
  179. // GEOLCID table.
  180. //
  181. else if (_stricmp(pszKeyWord, "GEOLCID") == 0)
  182. {
  183. PGEOLCID pGeoLCID = NULL;
  184. if (Verbose)
  185. printf("\n\nFound GEOLCID keyword.\n");
  186. GetSize(&nSize);
  187. if (nSize)
  188. {
  189. pGeoLCID = (PGEOLCID)malloc(sizeof(GEOLCID) * nSize);
  190. }
  191. if (pGeoLCID)
  192. {
  193. for (Ctr = 0; Ctr < nSize; Ctr++)
  194. {
  195. //
  196. // Scan values
  197. //
  198. fscanf( pInputFile,
  199. "%ld %x %lx ;%*[^\n]",
  200. &pGeoLCID[Ctr].GeoId,
  201. &pGeoLCID[Ctr].LangId,
  202. &pGeoLCID[Ctr].lcid);
  203. //
  204. // Print the values if in Verbose Mode
  205. //
  206. if (Verbose)
  207. {
  208. printf("ID:%ld, LANGID:0x%04x, LCID:0x%08x\n",
  209. pGeoLCID[Ctr].GeoId,
  210. pGeoLCID[Ctr].LangId,
  211. pGeoLCID[Ctr].lcid);
  212. }
  213. }
  214. GeoTableHdr.dwOffsetGeoLCID = ftell(pOutputFile);
  215. GeoTableHdr.nGeoLCID = nSize;
  216. if (FileWrite(pOutputFile, pGeoLCID, sizeof(GEOLCID), nSize, "GEOLCID"))
  217. {
  218. fclose(pOutputFile);
  219. free(pGeoLCID);
  220. return (1);
  221. }
  222. free(pGeoLCID);
  223. }
  224. }
  225. }
  226. //
  227. // Get file size.
  228. //
  229. GeoTableHdr.nFileSize = ftell(pOutputFile);
  230. //
  231. // Rewind Output file.
  232. //
  233. fseek(pOutputFile, 0, SEEK_SET);
  234. //
  235. // Update GEO table header.
  236. //
  237. if (FileWrite(pOutputFile, &GeoTableHdr, sizeof(GEOTABLEHDR), 1, "GEOHEADER"))
  238. {
  239. fclose(pOutputFile);
  240. return (1);
  241. }
  242. //
  243. // Close the output file.
  244. //
  245. fclose(pOutputFile);
  246. //
  247. // Return success.
  248. //
  249. printf("\nSuccessfully wrote output file %s\n", GEOFILE);
  250. return (0);
  251. }