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.

241 lines
5.3 KiB

  1. #include <windows.h>
  2. #include "common.h"
  3. #define BLOCKLEN 100
  4. #ifndef DBCS
  5. #define AnsiNext(x) ((x)+1)
  6. #endif
  7. LPSTR lpMerge;
  8. char szDotClasses[] = "\\.classes\\";
  9. char szHkeyClassesRoot[] = "HKEY_CLASSES_ROOT\\";
  10. extern HWND hWndHelp;
  11. extern unsigned long NEAR PASCAL MySetValue(HKEY hKey, PSTR pSubKey, PSTR pVal);
  12. NEAR PASCAL
  13. ImportWin40RegFile(
  14. VOID
  15. );
  16. static PSTR NEAR PASCAL GetFileLine(void)
  17. {
  18. static HANDLE hFile = NULL;
  19. static PSTR pFile;
  20. static WORD wLen;
  21. LPSTR lpStart;
  22. HANDLE hTemp;
  23. WORD wLineLen;
  24. char cFile;
  25. /* We need a place to put the file line */
  26. if(!hFile) {
  27. if(!(hFile=LocalAlloc(LMEM_MOVEABLE, wLen=BLOCKLEN)))
  28. goto Error1;
  29. if(!(pFile=LocalLock(hFile)))
  30. goto Error2;
  31. }
  32. /* If we have read the whole file, then clean up and return */
  33. if(!*lpMerge)
  34. goto Error3;
  35. for(lpStart=lpMerge; ; ) {
  36. cFile = *lpMerge;
  37. lpMerge = AnsiNext(lpMerge);
  38. switch(cFile) {
  39. case('\n'):
  40. case('\r'):
  41. case('\0'):
  42. /* EOL so return */
  43. goto CopyLine;
  44. }
  45. }
  46. CopyLine:
  47. wLineLen = lpMerge - lpStart - 1;
  48. /* Make the buffer larger if necessary */
  49. if(wLineLen >= wLen) {
  50. LocalUnlock(hFile);
  51. wLen = wLineLen + BLOCKLEN;
  52. if(!(hTemp=LocalReAlloc(hFile, wLen, LMEM_MOVEABLE)))
  53. goto Error2;
  54. if(!(pFile=LocalLock(hFile=hTemp)))
  55. goto Error2;
  56. }
  57. RepeatMove(pFile, lpStart, wLineLen);
  58. pFile[wLineLen] = '\0';
  59. return(pFile);
  60. Error3:
  61. LocalUnlock(hFile);
  62. Error2:
  63. LocalFree(hFile);
  64. hFile = NULL;
  65. Error1:
  66. return(NULL);
  67. }
  68. static VOID NEAR PASCAL MergeFileData(void)
  69. {
  70. static struct tagKEYANDROOT
  71. {
  72. PSTR szKey;
  73. HKEY hKeyRoot;
  74. } krClasses[] =
  75. {
  76. szHkeyClassesRoot, HKEY_CLASSES_ROOT,
  77. szDotClasses, HKEY_CLASSES_ROOT
  78. } ;
  79. #define NUM_KEYWORDS (sizeof(krClasses)/sizeof(krClasses[0]))
  80. PSTR pLine;
  81. PSTR pLast;
  82. HKEY hKeyRoot, hSubKey;
  83. int i;
  84. /* Get the initial line if this is the first time through */
  85. if(!(pLine=GetFileLine()))
  86. return;
  87. /* Otherwise, open a key so that we only do one write to the disk */
  88. if(RegCreateKey(HKEY_CLASSES_ROOT, NULL, &hSubKey) != ERROR_SUCCESS)
  89. return;
  90. do
  91. {
  92. for (i=0; i<NUM_KEYWORDS; ++i)
  93. {
  94. char cTemp;
  95. int nCmp, nLen;
  96. cTemp = pLine[nLen=lstrlen(krClasses[i].szKey)];
  97. pLine[nLen] = '\0';
  98. nCmp = lstrcmp(krClasses[i].szKey, pLine);
  99. pLine[nLen] = cTemp;
  100. if (!nCmp)
  101. {
  102. pLine += nLen;
  103. hKeyRoot = krClasses[i].hKeyRoot;
  104. goto MergeTheData;
  105. }
  106. }
  107. continue;
  108. MergeTheData:
  109. /* This is a line that needs to get merged.
  110. * Find a space (and then skip spaces) or "= " */
  111. for(pLast=pLine; *pLast; pLast=AnsiNext(pLast))
  112. {
  113. if(*pLast == ' ')
  114. {
  115. *pLast = '\0';
  116. while(*(++pLast) == ' ')
  117. /* find the first non-space */ ;
  118. break;
  119. }
  120. }
  121. /* There is an error if we don't have "= " */
  122. if(*pLast=='=' && *(++pLast)==' ')
  123. ++pLast;
  124. /* Set the value */
  125. MySetValue(hKeyRoot, pLine, pLast);
  126. } while(pLine=GetFileLine()) ;
  127. RegCloseKey(hSubKey);
  128. }
  129. VOID NEAR PASCAL ProcessFiles(HWND hDlg, HANDLE hCmdLine, WORD wFlags)
  130. {
  131. HANDLE hMerge, hHeader;
  132. PSTR pHeader;
  133. int hFile;
  134. LONG lSize;
  135. LPSTR lpFileName, lpTemp;
  136. OFSTRUCT of;
  137. WORD wErrMsg;
  138. lpFileName = GlobalLock(hCmdLine);
  139. /* We need to process all file names on the command line */
  140. while(lpTemp=MyStrTok(lpFileName, ' ')) {
  141. /* Open the file */
  142. wErrMsg = IDS_CANTOPENFILE;
  143. if((hFile=OpenFile(lpFileName, &of, OF_READ)) == -1)
  144. goto Error2;
  145. /* Determine the file size; limit it to just less than 64K */
  146. wErrMsg = IDS_CANTREADFILE;
  147. if((lSize=_llseek(hFile, 0L, 2))==-1 || lSize>0xfff0)
  148. goto Error3;
  149. _llseek(hFile, 0L, 0);
  150. /* Allocate a block of memory for the file */
  151. wErrMsg = IDS_OUTOFMEMORY;
  152. if(!(hMerge=GlobalAlloc(GHND, lSize+2)))
  153. goto Error3;
  154. if(!(lpMerge=GlobalLock(hMerge)))
  155. goto Error4;
  156. /* Read in the file */
  157. wErrMsg = IDS_CANTREADFILE;
  158. if(_lread(hFile, lpMerge, LOWORD(lSize)) != LOWORD(lSize))
  159. goto Error5;
  160. /* Look for the header */
  161. wErrMsg = IDS_OUTOFMEMORY;
  162. if(!(hHeader=MyLoadString(IDS_REGHEADER, NULL, LMEM_MOVEABLE)))
  163. goto Error5;
  164. pHeader = LocalLock(hHeader);
  165. wErrMsg = IDS_BADFORMAT;
  166. while(*lpMerge == ' ')
  167. ++lpMerge;
  168. while(*pHeader)
  169. if(*lpMerge++ != *pHeader++)
  170. goto Error6;
  171. if(*lpMerge=='4')
  172. {
  173. ImportWin40RegFile();
  174. wErrMsg = IDS_SUCCESSREAD;
  175. goto Error6;
  176. }
  177. while(*lpMerge == ' ')
  178. ++lpMerge;
  179. if(*lpMerge!='\r' && *lpMerge!='\n')
  180. goto Error6;
  181. /* Merge the data */
  182. MergeFileData(); /* This makes the changes */
  183. wErrMsg = IDS_SUCCESSREAD;
  184. Error6:
  185. LocalUnlock(hHeader);
  186. LocalFree(hHeader);
  187. Error5:
  188. GlobalUnlock(hMerge);
  189. Error4:
  190. GlobalFree(hMerge);
  191. Error3:
  192. _lclose(hFile);
  193. Error2:
  194. /* Display the status message */
  195. if(!(wFlags&FLAG_SILENT) || wErrMsg!=IDS_SUCCESSREAD)
  196. MyMessageBox(hDlg, wErrMsg, MB_OK | MB_ICONEXCLAMATION,
  197. lstrlen(lpFileName), lpFileName);
  198. lpFileName = lpTemp;
  199. while(*lpFileName == ' ')
  200. ++lpFileName;
  201. }
  202. GlobalUnlock(hCmdLine);
  203. GlobalFree(hCmdLine);
  204. }