Leaked source code of windows server 2003
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.

391 lines
13 KiB

  1. /** Update.c
  2. *
  3. * Resource update tool.
  4. *
  5. * Written by SteveBl
  6. *
  7. * Exported Functions:
  8. * int PrepareUpdate(TCHAR *szResourcePath,TCHAR *szMasterTokenFile);
  9. *
  10. * int Update(TCHAR *szMasterTokenFile, TCHAR *szLanguageTokenFile);
  11. *
  12. * History:
  13. * Initial version written January 31, 1992. -- SteveBl
  14. **/
  15. #include <windows.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <ctype.h>
  19. #include <io.h>
  20. #include <string.h>
  21. #include <tchar.h>
  22. #include "windefs.h"
  23. #include "restok.h"
  24. #include "custres.h"
  25. #include "update.h"
  26. #include "resread.h"
  27. extern char *gszTmpPrefix;
  28. extern UCHAR szDHW[];
  29. /** Function: Update
  30. * Updates a language token file from a master token file.
  31. * This step should be executed after a Prepare Update.
  32. *
  33. * Arguments:
  34. * szMasterTokenFile, token file created by PrepareUpdate.
  35. * szLanguageTokenFile, token file to be updated with new tokens.
  36. *
  37. * Returns:
  38. * updated token file
  39. *
  40. * Error Codes:
  41. * 0 - successfull execution
  42. * !0 - error
  43. *
  44. * History:
  45. * 1/92 - initial implementation -- SteveBl
  46. **/
  47. int Update(
  48. CHAR *szMasterTokenFile, //... Master token file to update from.
  49. CHAR *szLanguageTokenFile) //... Token file to update or create.
  50. {
  51. FILE *pfMTK = NULL;
  52. FILE *pfTOK = NULL;
  53. FILE *pfTmpTOK = NULL;
  54. int rc = 0;
  55. static TOKEN MstrTok;
  56. static TOKEN LangTok;
  57. static CHAR szTempTok[ MAX_PATH+1];
  58. MstrTok.szText = NULL;
  59. LangTok.szText = NULL;
  60. rc = MyGetTempFileName( 0, "", 0, szTempTok);
  61. pfMTK = FOPEN( szMasterTokenFile, "rt");
  62. if ( pfMTK == NULL )
  63. {
  64. QuitA( IDS_ENGERR_01, "Master token", szMasterTokenFile);
  65. }
  66. rc = _access( szLanguageTokenFile, 0);
  67. if ( rc != 0 )
  68. {
  69. // Token file does not exist CREAT IT
  70. if ( (pfTOK = FOPEN( szLanguageTokenFile, "wt")) == NULL )
  71. {
  72. FCLOSE( pfMTK);
  73. QuitA( IDS_ENGERR_02, szLanguageTokenFile, NULL);
  74. }
  75. do
  76. {
  77. rc = GetToken( pfMTK, &MstrTok);
  78. // If rc > 0, empty line or comment found
  79. // and will not be copied to token file.
  80. if ( rc == 0 )
  81. {
  82. if ( *(MstrTok.szText) == TEXT('\0') ) // Empty token (PW)
  83. {
  84. // Do not mark empty token as DIRTY
  85. MstrTok.wReserved = ST_TRANSLATED;
  86. }
  87. else
  88. {
  89. if (MstrTok.wReserved == ST_READONLY)
  90. {
  91. MstrTok.wReserved = ST_TRANSLATED | ST_READONLY;
  92. }
  93. else
  94. {
  95. MstrTok.wReserved = ST_TRANSLATED | ST_DIRTY;
  96. }
  97. }
  98. PutToken( pfTOK, &MstrTok);
  99. RLFREE( MstrTok.szText);
  100. }
  101. } while ( rc >= 0 );
  102. FCLOSE( pfMTK);
  103. FCLOSE( pfTOK);
  104. if ( rc == -2 )
  105. {
  106. QuitT( IDS_ENGERR_11, (LPTSTR)IDS_UPDMODE, NULL);
  107. }
  108. }
  109. else
  110. { // file exists -- UPDATE IT
  111. pfTOK = FOPEN(szLanguageTokenFile, "rt");
  112. if ( pfTOK == NULL)
  113. {
  114. FCLOSE( pfMTK);
  115. QuitA( IDS_ENGERR_01, "Language token", szLanguageTokenFile);
  116. }
  117. pfTmpTOK = FOPEN(szTempTok, "wt");
  118. if ( pfTmpTOK == NULL)
  119. {
  120. FCLOSE( pfMTK);
  121. FCLOSE( pfTOK);
  122. QuitA( IDS_ENGERR_02, szTempTok, NULL);
  123. }
  124. do
  125. {
  126. rc = GetToken( pfTOK, &LangTok);
  127. // If rc > 0, empty line or comment found
  128. // and will not be copied to token file.
  129. if ( rc == 0 )
  130. {
  131. if ( LangTok.wReserved & ST_TRANSLATED )
  132. {
  133. PutToken( pfTmpTOK, &LangTok);
  134. }
  135. RLFREE( LangTok.szText);
  136. }
  137. } while ( rc >= 0 );
  138. FCLOSE( pfTOK);
  139. FCLOSE( pfTmpTOK);
  140. if( rc == -2 )
  141. {
  142. QuitT( IDS_ENGERR_11, (LPTSTR)IDS_UPDMODE, NULL);
  143. }
  144. pfTmpTOK = FOPEN(szTempTok, "rt");
  145. if ( pfTmpTOK == NULL )
  146. {
  147. FCLOSE( pfMTK);
  148. QuitA( IDS_ENGERR_01, "temporary token", szTempTok);
  149. }
  150. pfTOK = FOPEN(szLanguageTokenFile, "wt");
  151. if ( pfTOK == NULL )
  152. {
  153. FCLOSE( pfMTK);
  154. FCLOSE( pfTOK);
  155. QuitA( IDS_ENGERR_02, szLanguageTokenFile, NULL);
  156. }
  157. do
  158. {
  159. rc = GetToken( pfMTK, &MstrTok);
  160. // If rc > 0, empty line or comment found
  161. // and will not be copied to token file.
  162. if ( rc == 0 )
  163. {
  164. int fTokenFound = 0;
  165. LangTok.wType = MstrTok.wType;
  166. LangTok.wName = MstrTok.wName;
  167. LangTok.wID = MstrTok.wID;
  168. LangTok.wFlag = MstrTok.wFlag;
  169. LangTok.wLangID = MstrTok.wLangID;
  170. LangTok.wReserved = ST_TRANSLATED;
  171. LangTok.szText = NULL;
  172. lstrcpy( LangTok.szType, MstrTok.szType);
  173. lstrcpy( LangTok.szName, MstrTok.szName);
  174. if ( MstrTok.wReserved & ST_READONLY )
  175. {
  176. fTokenFound = 1;
  177. LangTok.szText = (TCHAR *)FALLOC( 0);
  178. }
  179. else if ( MstrTok.wReserved != ST_CHANGED )
  180. {
  181. fTokenFound = FindToken( pfTmpTOK, &LangTok, ST_TRANSLATED);
  182. }
  183. if ( fTokenFound )
  184. {
  185. if ( MstrTok.wReserved & ST_READONLY )
  186. {
  187. // token marked read only in token file and
  188. // this token is not an old token
  189. MstrTok.wReserved = ST_READONLY | ST_TRANSLATED;
  190. PutToken( pfTOK, &MstrTok);
  191. }
  192. else if ( MstrTok.wReserved & ST_NEW )
  193. {
  194. // flagged as new but previous token existed
  195. if ( LangTok.szText[0] == TEXT('\0') )
  196. {
  197. // Put new text in token, easier for
  198. // the localizers to see.
  199. RLFREE( LangTok.szText);
  200. LangTok.szText =
  201. (TCHAR *) FALLOC(
  202. MEMSIZE( lstrlen( MstrTok.szText)+1));
  203. lstrcpy( LangTok.szText, MstrTok.szText);
  204. }
  205. LangTok.wReserved = ST_TRANSLATED|ST_DIRTY;
  206. PutToken( pfTOK, &LangTok);
  207. // write out as a new untranslated token
  208. MstrTok.wReserved = ST_NEW;
  209. PutToken( pfTOK, &MstrTok);
  210. }
  211. else if ( MstrTok.wReserved & ST_CHANGED )
  212. {
  213. // Language token is empty, but new
  214. // token contains text.
  215. if ( MstrTok.wReserved == (ST_CHANGED | ST_NEW) )
  216. {
  217. if ( LangTok.szText[0] == TEXT('\0') )
  218. {
  219. RLFREE( LangTok.szText);
  220. LangTok.szText = (TCHAR *)
  221. FALLOC(
  222. MEMSIZE( lstrlen( MstrTok.szText)+1));
  223. lstrcpy( LangTok.szText, MstrTok.szText);
  224. }
  225. LangTok.wReserved = ST_DIRTY|ST_TRANSLATED;
  226. PutToken( pfTOK, &LangTok);
  227. }
  228. // only write old token once
  229. MstrTok.wReserved &= ST_NEW;
  230. PutToken( pfTOK, &MstrTok);
  231. }
  232. else
  233. {
  234. // token did not change at all
  235. //If align info was added into Mtk, add it to Tok also.
  236. int l1, r1, t1, b1, l2, r2, t2, b2;
  237. TCHAR a1[20], a2[20], *ap;
  238. //Cordinates token?
  239. if ( (LangTok.wType == ID_RT_DIALOG)
  240. && (LangTok.wFlag&ISCOR)
  241. //Not including align info?
  242. && _stscanf( LangTok.szText, TEXT("%d %d %d %d %s"),
  243. &l1,&r1,&t1,&b1,a1) == 4
  244. //Including align info?
  245. && _stscanf( MstrTok.szText, TEXT("%d %d %d %d %s"),
  246. &l2,&r2,&t2,&b2,a2) == 5
  247. && (ap = _tcschr( MstrTok.szText,TEXT('('))) )
  248. {
  249. RLFREE( LangTok.szText );
  250. LangTok.szText = (TCHAR *)FALLOC(
  251. MEMSIZE( _tcslen( MstrTok.szText)+1));
  252. _stprintf( LangTok.szText,
  253. TEXT("%4hd %4hd %4hd %4hd %s"), l1, r1, t1, b1, ap );
  254. }
  255. //If LangToken is Version stamp and szTexts is "Translation",
  256. //it is 1.0 version format. So Translate it.
  257. if ( LangTok.wType == ID_RT_VERSION
  258. && ! _tcscmp( LangTok.szText, TEXT("Translation")) )
  259. {
  260. _stprintf( LangTok.szText,
  261. TEXT("%04x 04b0"),
  262. GetUserDefaultLangID());
  263. }
  264. PutToken( pfTOK, &LangTok);
  265. }
  266. RLFREE( LangTok.szText);
  267. }
  268. else
  269. {
  270. // BRAND NEW TOKEN
  271. // write out any token but a changed mstr token.
  272. if ( MstrTok.wReserved != ST_CHANGED )
  273. {
  274. // do not write out old changed tokens if
  275. // there is no token in target
  276. if ( MstrTok.wReserved == ST_READONLY )
  277. {
  278. MstrTok.wReserved = ST_TRANSLATED | ST_READONLY;
  279. }
  280. else
  281. {
  282. //If MstrTok is Version stamp and there are 1.0 format Version stamp,
  283. //insert 1.0 version stamp by 1.7 format but flag should be TRANSLATED.
  284. if ( MstrTok.wType == ID_RT_VERSION )
  285. {
  286. LangTok.szText = NULL;
  287. LangTok.wFlag = 1;
  288. _tcscpy( LangTok.szName, TEXT("VALUE") );
  289. if ( FindToken( pfTmpTOK, &LangTok, ST_TRANSLATED))
  290. {
  291. MstrTok.wReserved = ST_TRANSLATED;
  292. RLFREE( MstrTok.szText );
  293. MstrTok.szText = LangTok.szText;
  294. }
  295. else
  296. MstrTok.wReserved = ST_TRANSLATED|ST_DIRTY;
  297. }
  298. else
  299. MstrTok.wReserved = ST_TRANSLATED|ST_DIRTY;
  300. }
  301. if ( MstrTok.szText[0] == 0 )
  302. {
  303. MstrTok.wReserved = ST_TRANSLATED;
  304. }
  305. PutToken( pfTOK, &MstrTok);
  306. }
  307. }
  308. RLFREE( MstrTok.szText);
  309. }
  310. } while ( rc >= 0 );
  311. FCLOSE( pfMTK);
  312. FCLOSE( pfTmpTOK);
  313. FCLOSE( pfTOK);
  314. if ( rc == -2 )
  315. {
  316. QuitT( IDS_ENGERR_11, (LPTSTR)IDS_UPDMODE, NULL);
  317. }
  318. remove( szTempTok);
  319. }
  320. return( 0);
  321. }