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.

366 lines
9.4 KiB

  1. /**************************************************/
  2. /* */
  3. /* */
  4. /* Registry Key Function */
  5. /* */
  6. /* */
  7. /* Copyright (c) 1997-1999 Microsoft Corporation. */
  8. /**************************************************/
  9. #include "stdafx.h"
  10. #include "eudcedit.h"
  11. #include "registry.h"
  12. #include "util.h"
  13. static TCHAR subkey1[] = TEXT("EUDC");
  14. static TCHAR subkey2[] = TEXT("System\\CurrentControlSet\\control\\Nls\\Codepage\\EUDCCodeRange");
  15. static TCHAR SubKey[MAX_PATH];
  16. #ifdef IN_FONTS_DIR // IsFileUnderWindowsRoot()
  17. LPTSTR
  18. IsFileUnderWindowsRoot(
  19. LPTSTR TargetPath)
  20. {
  21. TCHAR WindowsRoot[MAX_PATH+1];
  22. UINT WindowsRootLength;
  23. WindowsRootLength = GetSystemWindowsDirectory(WindowsRoot,MAX_PATH);
  24. if( lstrcmpi(WindowsRoot,TargetPath) == 0)
  25. return (TargetPath + WindowsRootLength);
  26. return NULL;
  27. }
  28. void AdjustTypeFace(WCHAR *orgName, WCHAR *newName)
  29. {
  30. if (!lstrcmpW(orgName, L"\x5b8b\x4f53"))
  31. lstrcpy(newName, TEXT("Simsun"));
  32. else if (!lstrcmpW(orgName, L"\x65b0\x7d30\x660e\x9ad4"))
  33. lstrcpy(newName, TEXT("PMingLiU"));
  34. else if (!lstrcmpW(orgName, L"\xFF2d\xFF33\x0020\xFF30\x30b4\x30b7\x30c3\x30af"))
  35. lstrcpy(newName, TEXT("MS PGothic"));
  36. else if (!lstrcmpW(orgName, L"\xad74\xb9bc"))
  37. lstrcpy(newName, TEXT("Gulim"));
  38. else
  39. lstrcpy(newName, orgName);
  40. }
  41. #endif // IN_FONTS_DIR
  42. /****************************************/
  43. /* */
  44. /* Inquiry EUDC registry */
  45. /* */
  46. /****************************************/
  47. BOOL
  48. InqTypeFace(
  49. TCHAR *typeface,
  50. TCHAR *filename,
  51. INT bufsiz)
  52. {
  53. HKEY phkey;
  54. DWORD cb, dwType;
  55. LONG rc;
  56. TCHAR FaceName[LF_FACESIZE];
  57. TCHAR SysName[LF_FACESIZE];
  58. #ifdef BUILD_ON_WINNT // InqTypeFace()
  59. TCHAR FileName[MAX_PATH];
  60. #endif // BUILD_ON_WINNT
  61. GetStringRes(SysName, IDS_SYSTEMEUDCFONT_STR);
  62. if( !lstrcmp(typeface, SysName)){
  63. lstrcpy(FaceName,TEXT("SystemDefaultEUDCFont"));
  64. }else {
  65. #ifdef IN_FONTS_DIR
  66. AdjustTypeFace(typeface, FaceName);
  67. #else
  68. lstrcpy(FaceName, typeface);
  69. #endif
  70. }
  71. if( RegOpenKeyEx( HKEY_CURRENT_USER, (LPCTSTR)SubKey, 0,
  72. KEY_ALL_ACCESS, &phkey) != ERROR_SUCCESS){
  73. return FALSE;
  74. }
  75. #ifdef IN_FONTS_DIR // InqTypeFace()
  76. cb = (DWORD)MAX_PATH*sizeof(WORD)/sizeof(BYTE);
  77. rc = RegQueryValueEx(phkey, FaceName, 0, &dwType,
  78. (LPBYTE)FileName, &cb);
  79. RegCloseKey(phkey);
  80. /*
  81. * if there is some error or no data, just return false.
  82. */
  83. if ((rc != ERROR_SUCCESS) || (FileName[0] == '\0')) {
  84. return (FALSE);
  85. }
  86. /*
  87. * expand %SystemRoot% to Windows direcotry.
  88. */
  89. ExpandEnvironmentStrings((LPCTSTR)FileName,(LPTSTR)filename,bufsiz);
  90. #else
  91. cb = (DWORD)bufsiz*sizeof(WORD)/sizeof(BYTE);
  92. rc = RegQueryValueEx(phkey, (TCHAR *)FaceName, 0, &dwType,
  93. (LPBYTE)filename, &cb);
  94. RegCloseKey(phkey);
  95. if ((rc != ERROR_SUCCESS) || (filename[0] == '\0')) {
  96. return (FALSE);
  97. }
  98. #endif // IN_FONTS_DIR
  99. #ifdef BUILD_ON_WINNT // InqTypeFace()
  100. /*
  101. * if this is not 'full path'. Build 'full path'.
  102. *
  103. * EUDC.TTE -> C:\WINNT40\FONTS\EUDC.TTE
  104. * 0123456...
  105. *
  106. * 1. filename should have drive letter.
  107. * 2. filename should have one '\\' ,at least, for root.
  108. */
  109. if ((filename[1] != ':') || (Mytcsstr((const TCHAR *)filename,TEXT("\\")) == NULL)) {
  110. /* backup original.. */
  111. lstrcpy(FileName, (const TCHAR *)filename);
  112. /* Get windows directory */
  113. GetSystemWindowsDirectory((TCHAR *)filename, MAX_PATH);
  114. #ifdef IN_FONTS_DIR // InqTypeFace()
  115. lstrcat((TCHAR *)filename, TEXT("\\FONTS\\"));
  116. #else
  117. strcat((char *)filename, "\\");
  118. #endif // IN_FONTS_DIR
  119. lstrcat((TCHAR *) filename, FileName);
  120. }
  121. #endif // BUILD_ON_WINNT
  122. #ifdef IN_FONTS_DIR // InqTypeFace()
  123. return (TRUE);
  124. #else
  125. return rc == ERROR_SUCCESS && filename[0] != '\0' ? TRUE : FALSE;
  126. #endif
  127. }
  128. /****************************************/
  129. /* */
  130. /* Registry EUDC font and file */
  131. /* */
  132. /****************************************/
  133. BOOL
  134. RegistTypeFace(
  135. TCHAR *typeface,
  136. TCHAR *filename)
  137. {
  138. HKEY phkey;
  139. LONG rc;
  140. TCHAR FaceName[LF_FACESIZE];
  141. TCHAR SysName[LF_FACESIZE];
  142. #ifdef IN_FONTS_DIR // RegistTypeFace()
  143. LPTSTR SaveFileName;
  144. TCHAR FileName[MAX_PATH];
  145. #endif // IN_FONTS_DIR
  146. GetStringRes((TCHAR *)SysName, IDS_SYSTEMEUDCFONT_STR);
  147. if( !lstrcmp((const TCHAR *)typeface, (const TCHAR *)SysName)){
  148. lstrcpy(FaceName, TEXT("SystemDefaultEUDCFont"));
  149. }else{
  150. #ifdef IN_FONTS_DIR
  151. AdjustTypeFace(typeface, FaceName);
  152. #else
  153. lstrcpy(FaceName, (const TCHAR *)typeface);
  154. #endif
  155. }
  156. if( RegOpenKeyEx( HKEY_CURRENT_USER, (LPCTSTR)SubKey, 0,
  157. KEY_ALL_ACCESS, &phkey) != ERROR_SUCCESS){
  158. return FALSE;
  159. }
  160. #ifdef IN_FONTS_DIR // RegistTypeFace()
  161. /*
  162. * if registry data contains full path, and the file is under windows
  163. * directory, replace the hardcodeed path with %SystemRoot%....
  164. */
  165. if( (SaveFileName = IsFileUnderWindowsRoot((LPTSTR)filename)) != NULL) {
  166. lstrcpy(FileName, TEXT("%SystemRoot%"));
  167. if( *SaveFileName != '\\' ) lstrcat(FileName, TEXT("\\"));
  168. lstrcat(FileName, SaveFileName );
  169. } else {
  170. lstrcpy(FileName, (TCHAR *)filename );
  171. }
  172. rc = RegSetValueEx( phkey, (LPCTSTR)FaceName, 0,
  173. REG_SZ, (const BYTE *)FileName, (lstrlen((LPCTSTR)FileName)+1)*sizeof(WORD)/sizeof(BYTE));
  174. #else
  175. rc = RegSetValueEx( phkey, (LPCTSTR)FaceName, 0,
  176. REG_SZ, (const BYTE *)filename, (lstrlen((LPCTSTR)filename)+1)*sizeof(WORD)/sizeof(BYTE));
  177. #endif // IN_FONTS_DIR
  178. RegCloseKey(phkey);
  179. return rc == ERROR_SUCCESS ? TRUE : FALSE;
  180. }
  181. /****************************************/
  182. /* */
  183. /* Delete Registry string */
  184. /* */
  185. /****************************************/
  186. BOOL
  187. DeleteReg(
  188. TCHAR *typeface)
  189. {
  190. HKEY phkey;
  191. LONG rc;
  192. TCHAR FaceName[LF_FACESIZE];
  193. TCHAR SysName[LF_FACESIZE];
  194. GetStringRes((TCHAR *)SysName, IDS_SYSTEMEUDCFONT_STR);
  195. if( !lstrcmp((const TCHAR *)typeface, (const TCHAR *)SysName)){
  196. lstrcpy((TCHAR *)FaceName, TEXT("SystemDefaultEUDCFont"));
  197. }else{
  198. #ifdef IN_FONTS_DIR
  199. AdjustTypeFace(typeface, FaceName);
  200. #else
  201. lstrcpy((TCHAR *)FaceName, (const TCHAR *)typeface);
  202. #endif
  203. }
  204. if( RegOpenKeyEx(HKEY_CURRENT_USER, (LPCTSTR)SubKey, 0,
  205. KEY_ALL_ACCESS, &phkey) != ERROR_SUCCESS){
  206. return FALSE;
  207. }
  208. rc = RegDeleteValue( phkey, (LPTSTR)FaceName);
  209. RegCloseKey(phkey);
  210. return rc == ERROR_SUCCESS ? TRUE : FALSE;
  211. }
  212. /****************************************/
  213. /* */
  214. /* Create Registry Subkey */
  215. /* */
  216. /****************************************/
  217. BOOL
  218. CreateRegistrySubkey()
  219. {
  220. HKEY phkey;
  221. DWORD dwdisp;
  222. int LocalCP;
  223. TCHAR CodePage[10];
  224. int result;
  225. /* New Registry */
  226. LocalCP = GetACP();
  227. wsprintf( CodePage, TEXT("%d"), LocalCP);
  228. lstrcpy(SubKey, subkey1);
  229. lstrcat(SubKey, TEXT("\\"));
  230. lstrcat(SubKey, CodePage);
  231. if( RegOpenKeyEx( HKEY_CURRENT_USER, (LPCTSTR)SubKey, 0,
  232. KEY_ALL_ACCESS, &phkey) != ERROR_SUCCESS){
  233. result = RegCreateKeyEx(HKEY_CURRENT_USER,
  234. (LPCTSTR)SubKey, 0, TEXT(""),
  235. REG_OPTION_NON_VOLATILE,
  236. KEY_ALL_ACCESS, NULL, &phkey, &dwdisp);
  237. if( result == ERROR_SUCCESS)
  238. RegCloseKey( phkey);
  239. else return FALSE;
  240. }else RegCloseKey(phkey);
  241. return TRUE;
  242. }
  243. /****************************************/
  244. /* */
  245. /* Inquiry Code range registry */
  246. /* */
  247. /****************************************/
  248. BOOL
  249. InqCodeRange(
  250. TCHAR *Codepage,
  251. BYTE *Coderange,
  252. INT bufsiz)
  253. {
  254. HKEY phkey;
  255. DWORD cb, dwType;
  256. LONG rc;
  257. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCTSTR)subkey2, 0,
  258. KEY_READ, &phkey) != ERROR_SUCCESS) {
  259. return FALSE;
  260. }
  261. cb = (DWORD)bufsiz * sizeof(WORD)/sizeof(BYTE);
  262. rc = RegQueryValueEx(phkey, (TCHAR *)Codepage, 0, &dwType,
  263. (LPBYTE)Coderange, &cb);
  264. RegCloseKey(phkey);
  265. return rc == ERROR_SUCCESS && Coderange[0] != '\0' ? TRUE : FALSE;
  266. }
  267. BOOL
  268. DeleteRegistrySubkey()
  269. {
  270. HKEY phkey;
  271. if( RegOpenKeyEx( HKEY_CURRENT_USER, (LPCTSTR)SubKey, 0,
  272. KEY_ALL_ACCESS, &phkey) == ERROR_SUCCESS){
  273. RegCloseKey(phkey);
  274. return RegDeleteKey(HKEY_CURRENT_USER, (LPCTSTR)SubKey);
  275. }
  276. return TRUE;
  277. }
  278. BOOL
  279. FindFontSubstitute(TCHAR *orgFontName, TCHAR *sbstFontName)
  280. {
  281. static TCHAR fsKey[] = TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\FontSubstitutes");
  282. *sbstFontName = 0;
  283. lstrcpy(sbstFontName, orgFontName);
  284. HKEY phkey;
  285. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, (LPCTSTR)fsKey, 0,
  286. KEY_QUERY_VALUE, &phkey) != ERROR_SUCCESS) {
  287. return FALSE;
  288. }
  289. DWORD valueNameSize = LF_FACESIZE + 50; //should be facename + ',' + codepage
  290. TCHAR valueName[LF_FACESIZE + 50];
  291. DWORD valueType;
  292. DWORD valueDataSize = (LF_FACESIZE + 50) * sizeof(TCHAR); //should be facename + ',' + codepage
  293. BYTE valueData[(LF_FACESIZE + 50) * sizeof(TCHAR)];
  294. LONG ret;
  295. DWORD idx = 0;
  296. while ((ret = RegEnumValue(phkey, idx, valueName, &valueNameSize, 0,
  297. &valueType, valueData, &valueDataSize)) != ERROR_NO_MORE_ITEMS)
  298. {
  299. if (ret != ERROR_SUCCESS)
  300. {
  301. RegCloseKey(phkey);
  302. return FALSE;
  303. }
  304. Truncate(valueName, _T(','));
  305. if (!lstrcmpi(valueName, orgFontName))
  306. {
  307. Truncate((TCHAR *)valueData, _T(','));
  308. lstrcpy(sbstFontName, (TCHAR *)valueData);
  309. break;
  310. }
  311. idx ++;
  312. valueNameSize = LF_FACESIZE + 50;
  313. valueDataSize = (LF_FACESIZE + 50) * sizeof(TCHAR);
  314. }
  315. RegCloseKey(phkey);
  316. return TRUE;
  317. }
  318. void Truncate(TCHAR *str, TCHAR delim)
  319. {
  320. TCHAR *pchr = _tcschr(str, delim);
  321. if (pchr)
  322. *pchr = 0;
  323. }