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.

247 lines
6.5 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #include <string.h>
  6. /*
  7. local.c - It will open a localization file and append the content of the
  8. localization file to each file indicated within the
  9. localization file.
  10. syntax: local < input file > < localize directory >
  11. */
  12. typedef struct _PLATFORM_DATA {
  13. //
  14. // Name of platform and platform's subdirectory in
  15. // the build tree.
  16. //
  17. PSTR PlatformName;
  18. //
  19. // Current output file for this platform.
  20. // If a line in the input file applies to this platform,
  21. // this handle will be used to write the line into the
  22. // output file being generated for this platform.
  23. //
  24. FILE *OutputFile;
  25. } PLATFORM_DATA, *PPLATFORM_DATA;
  26. //
  27. // When porting, simply bump up this number and add
  28. // a line to the PlatformData array below.
  29. //
  30. #define NUMBER_OF_PLATFORMS 4
  31. PLATFORM_DATA PlatformData[NUMBER_OF_PLATFORMS] =
  32. {{ "i386", NULL },
  33. { "mips", NULL },
  34. { "alpha", NULL },
  35. { "ppc", NULL }};
  36. //
  37. // Value to indicate that a line of input belongs in output file
  38. // for all platforms.
  39. //
  40. #define ALL_PLATFORMS NUMBER_OF_PLATFORMS
  41. //
  42. // Input buffer.
  43. //
  44. CHAR InputBuffer[1000];
  45. VOID
  46. CloseAllOpenOutputFiles(
  47. VOID
  48. )
  49. {
  50. unsigned Platform;
  51. for(Platform=0; Platform<NUMBER_OF_PLATFORMS; Platform++) {
  52. if(PlatformData[Platform].OutputFile) {
  53. fclose(PlatformData[Platform].OutputFile);
  54. PlatformData[Platform].OutputFile = NULL;
  55. }
  56. }
  57. }
  58. BOOL
  59. ProcessInputFile(
  60. IN FILE *InputFile,
  61. IN PSTR TargetDirectory
  62. )
  63. {
  64. CHAR InfFileName[MAX_PATH];
  65. CHAR PlatformName[100];
  66. CHAR OutputFileName[MAX_PATH];
  67. BOOL TotalSuccess;
  68. unsigned CurrentPlatform;
  69. unsigned Platform;
  70. HANDLE FindHandle;
  71. WIN32_FIND_DATA FindData;
  72. //
  73. // Assume overall success.
  74. //
  75. TotalSuccess = TRUE;
  76. //
  77. // Until a platform is specified, all platforms are selected.
  78. // Note that nothing will actually get written until a platform
  79. // is selected because no output files are open until then.
  80. //
  81. CurrentPlatform = ALL_PLATFORMS;
  82. //
  83. // Process each line in the input file.
  84. //
  85. while(TotalSuccess && fgets(InputBuffer,sizeof(InputBuffer),InputFile)) {
  86. //
  87. // If the line begins with ##### then this line specifies
  88. // which platform lines following it in the input file apply to.
  89. //
  90. // Otherwise the line will be copied to the appropriate output
  91. // file(s).
  92. //
  93. if(strncmp(InputBuffer,"#####",5)) {
  94. //
  95. // Plain old line. Place in output file for current platform
  96. // or all platforms as appropriate.
  97. //
  98. for(Platform=0; TotalSuccess && (Platform<NUMBER_OF_PLATFORMS); Platform++) {
  99. if(PlatformData[Platform].OutputFile
  100. && ((CurrentPlatform == ALL_PLATFORMS) || (Platform == CurrentPlatform))) {
  101. if(fputs(InputBuffer,PlatformData[Platform].OutputFile) == EOF) {
  102. fprintf(stderr,"unable to write to output file\n");
  103. TotalSuccess = FALSE;
  104. }
  105. }
  106. }
  107. } else {
  108. //
  109. // Platform specifier line. Next two values are output filename
  110. // and output file platform.
  111. //
  112. if (sscanf(InputBuffer,"#####%s %s",InfFileName,PlatformName) < 2) {
  113. fprintf(stderr,"invalid localization file\n");
  114. TotalSuccess = FALSE;
  115. break;
  116. }
  117. //
  118. // Attempt to determine the platform specified.
  119. // If the value in the input file is not recognized then
  120. // it specifies all platforms.
  121. //
  122. CurrentPlatform = ALL_PLATFORMS;
  123. for(Platform=0; Platform<NUMBER_OF_PLATFORMS; Platform++) {
  124. if(!_stricmp(PlatformName,PlatformData[Platform].PlatformName)) {
  125. CurrentPlatform = Platform;
  126. break;
  127. }
  128. }
  129. //
  130. // Close all open output files.
  131. //
  132. CloseAllOpenOutputFiles();
  133. //
  134. // Now open output files as appropriate. Note that this means
  135. // we will either open a single platform's output file or all
  136. // platforms' output files.
  137. //
  138. for(Platform=0; TotalSuccess && (Platform<NUMBER_OF_PLATFORMS); Platform++) {
  139. if((Platform == CurrentPlatform) || (CurrentPlatform == ALL_PLATFORMS)) {
  140. //
  141. // First we will determine whether the relevent platform's
  142. // directory exists. If not, we'll simply skip this platform.
  143. // This prevents us from erroring out when we're generating
  144. // 3.5" media, because there will be no mips, alpha, ppc, etc
  145. // subdirectories for 3.5" media.
  146. //
  147. sprintf(
  148. OutputFileName,
  149. "%s\\%s",
  150. TargetDirectory,
  151. PlatformData[Platform].PlatformName
  152. );
  153. FindHandle = FindFirstFile(OutputFileName,&FindData);
  154. if(FindHandle != INVALID_HANDLE_VALUE) {
  155. FindClose(FindHandle);
  156. strcat(OutputFileName,"\\");
  157. strcat(OutputFileName,InfFileName);
  158. if((PlatformData[Platform].OutputFile = fopen(OutputFileName,"a")) == NULL) {
  159. fprintf(stderr,"open file:%s fail.\n",OutputFileName);
  160. TotalSuccess = FALSE;
  161. }
  162. }
  163. }
  164. }
  165. }
  166. }
  167. //
  168. // Close all open output files.
  169. //
  170. CloseAllOpenOutputFiles();
  171. //
  172. // Return value indicating whether we were totally successful.
  173. //
  174. return TotalSuccess;
  175. }
  176. int
  177. __cdecl
  178. main(
  179. IN int argc,
  180. IN char *argv[]
  181. )
  182. {
  183. FILE *InputFile;
  184. int ReturnCode;
  185. if(argc != 3) {
  186. fprintf(stderr,"usage: local <file name> <directory name>\n");
  187. return 1;
  188. }
  189. if(InputFile = fopen(argv[1],"r")) {
  190. ReturnCode = ProcessInputFile(InputFile,argv[2]) ? 0 : 1;
  191. fclose(InputFile);
  192. } else {
  193. fprintf(stderr,"cannot open localizer file.");
  194. ReturnCode = 1;
  195. }
  196. return ReturnCode;
  197. }