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.

290 lines
9.0 KiB

  1. #include <windows.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <process.h> // for system()
  5. void ProcessGuidFile( char *pszFileName, DWORD*, BOOL );
  6. CHAR* ProcessChunk( CHAR*, CHAR*, int* );
  7. int main(void)
  8. {
  9. CHAR *pbuff = NULL, *pStart;
  10. HANDLE hFile, hChecksumFile, hExistingFile;
  11. DWORD dwSize, dw, i;
  12. DWORD dwOrigCheckSum, dwNewCheckSum = 0;
  13. CHAR szCurDir[512], szBuildDir[512], *pDir;
  14. // Change the current directory to the build directory
  15. dw = GetCurrentDirectory( 512, szCurDir );
  16. if( !dw ) return -1;
  17. strcpy( szBuildDir, szCurDir );
  18. strcat(szBuildDir, "\\..\\..\\build");
  19. /*
  20. // 1st try to find a "build" directory below the root and 1st level
  21. pDir = szBuildDir;
  22. i = 0;
  23. while( pDir < &szBuildDir[dw] )
  24. {
  25. if( *pDir++ == '\\' )
  26. {
  27. i++;
  28. if( i == 2 ) break;
  29. }
  30. }
  31. strcpy( pDir, "build" );
  32. *(pDir+5) = '\0';
  33. */
  34. if( !SetCurrentDirectory( szBuildDir ) )
  35. {
  36. // now search for a build directory under "sapi" or "sapi5"
  37. strcpy( szBuildDir, szCurDir );
  38. _strlwr( szBuildDir );
  39. pDir = strstr( szBuildDir, "sapi" );
  40. if( !pDir )
  41. {
  42. return -1;
  43. }
  44. else
  45. {
  46. if( *(pDir+4) == '5' )
  47. {
  48. strcpy( pDir+5, "\\build" );
  49. *(pDir+11) = '\0';
  50. }
  51. else
  52. {
  53. strcpy( pDir+4, "\\build" );
  54. *(pDir+10) = '\0';
  55. }
  56. if( !SetCurrentDirectory( szBuildDir ) )
  57. {
  58. return -1;
  59. }
  60. }
  61. }
  62. // read checksum information if available
  63. hChecksumFile = CreateFile( "checksum.bin", GENERIC_READ | GENERIC_WRITE,
  64. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_HIDDEN, NULL );
  65. if( hChecksumFile != INVALID_HANDLE_VALUE )
  66. {
  67. ReadFile( hChecksumFile, &dwOrigCheckSum, sizeof(dwOrigCheckSum), &dw, NULL );
  68. CloseHandle( hChecksumFile );
  69. // make sure sapiguid.lib exists - BUGBUG - change to i386 for intel
  70. hExistingFile = CreateFile( "..\\sdk\\lib\\i386\\sapiguid.lib", GENERIC_READ,
  71. FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
  72. if( hExistingFile == INVALID_HANDLE_VALUE )
  73. {
  74. dwOrigCheckSum = 0;
  75. }
  76. else
  77. {
  78. CloseHandle( hExistingFile );
  79. }
  80. }
  81. else
  82. {
  83. dwOrigCheckSum = 0;
  84. }
  85. // open file that contains the list of files to process
  86. hFile = CreateFile( "guidfiles.txt", GENERIC_READ,
  87. FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
  88. if( hFile != INVALID_HANDLE_VALUE )
  89. {
  90. dwSize = GetFileSize( hFile, NULL );
  91. if( dwSize != 0xFFFFFFFF )
  92. {
  93. pbuff = new CHAR [dwSize];
  94. }
  95. if( pbuff )
  96. {
  97. // read the names of the files we want to process
  98. ReadFile( hFile, pbuff, dwSize, &dw, NULL );
  99. pStart = pbuff;
  100. for( i = 0; i < dwSize; i++ )
  101. {
  102. // parse file names form the file and process each one
  103. if( *(pbuff+i) == 13 )
  104. {
  105. *(pbuff+i) = '\0';
  106. ProcessGuidFile( pStart, &dwNewCheckSum, true );
  107. pStart = pbuff + i + 2;
  108. }
  109. }
  110. if( dwOrigCheckSum != dwNewCheckSum )
  111. {
  112. pStart = pbuff;
  113. for( i = 0; i < dwSize; i++ )
  114. {
  115. // parse file names form the file and process each one
  116. if( *(pbuff+i) == '\0' )
  117. {
  118. ProcessGuidFile( pStart, 0, false );
  119. pStart = pbuff + i + 2;
  120. }
  121. }
  122. hChecksumFile = CreateFile( "checksum.bin", GENERIC_READ | GENERIC_WRITE,
  123. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL );
  124. if( hChecksumFile != INVALID_HANDLE_VALUE )
  125. {
  126. WriteFile( hChecksumFile, &dwNewCheckSum, sizeof(dwNewCheckSum), &dw, NULL );
  127. CloseHandle( hChecksumFile );
  128. }
  129. system( "cl /c /Ycprecomp.h GuidSep\\precomp.c" );
  130. system( "cl /c /Zl /Yuprecomp.h GuidSep\\*.c" );
  131. system( "link /lib *.obj /out:..\\sdk\\lib\\i386\\sapiguid.lib" ); // BUGBUG - change this to i386 for intel
  132. system( "del *.obj" );
  133. system( "del *.pch" );
  134. system( "del GuidSep\\SAPI5_guid_*.c" );
  135. }
  136. delete [] pbuff;
  137. }
  138. CloseHandle( hFile );
  139. }
  140. // reset working directory
  141. SetCurrentDirectory( szCurDir );
  142. return 0;
  143. }
  144. void ProcessGuidFile( char *pszFileName, DWORD *pdwCheckSum, BOOL bDoCheckSum )
  145. {
  146. static int count = 0;
  147. CHAR *pbuff = NULL, *pTmp, *pEndStream;
  148. HANDLE hFile;
  149. DWORD dwSize, dw, i;
  150. hFile = CreateFile( pszFileName, GENERIC_READ,
  151. FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
  152. if( hFile != INVALID_HANDLE_VALUE )
  153. {
  154. dwSize = GetFileSize( hFile, NULL );
  155. if( dwSize != 0xFFFFFFFF )
  156. {
  157. pbuff = new CHAR [dwSize];
  158. }
  159. if( pbuff )
  160. {
  161. pEndStream = pbuff + dwSize;
  162. ReadFile( hFile, pbuff, dwSize, &dw, NULL );
  163. if( bDoCheckSum )
  164. {
  165. for( i = 0; i < dwSize; i++ )
  166. {
  167. // checksum must skip over comments because midl places a time stamp
  168. // in generated files
  169. if( *(pbuff+i) == '/' )
  170. {
  171. if( *(pbuff+i+1) == '*' )
  172. {
  173. i+=2;
  174. if( i >= dwSize ) break;
  175. do
  176. {
  177. while( *(pbuff+i) != '*' )
  178. i++;
  179. if( i >= dwSize ) break;
  180. } while (*(pbuff+i+1) != '/' );
  181. i += 2;
  182. }
  183. }
  184. (*pdwCheckSum) += *(pbuff + i);
  185. }
  186. }
  187. else
  188. {
  189. pTmp = pbuff;
  190. while( pTmp = ProcessChunk( pTmp, pEndStream, &count ) )
  191. ;
  192. }
  193. delete [] pbuff;
  194. }
  195. CloseHandle( hFile );
  196. }
  197. }
  198. CHAR* ProcessChunk( CHAR* pStream, CHAR * pEndStream, int *pCount )
  199. {
  200. if( pStream >= pEndStream ) return NULL;
  201. CHAR *pTmp = pStream;
  202. CHAR szOutputName[50];
  203. HANDLE hOutputFile;
  204. DWORD dw;
  205. // remove leading or trailing white space
  206. while( *pTmp == ' ' || *pTmp == '\t' || *pTmp == 10 || *pTmp == 13 )
  207. pTmp++;
  208. if( pStream >= pEndStream ) return NULL;
  209. // search for the keyword "const"
  210. if( !strncmp( pTmp, "const ", 6 ) )
  211. {
  212. pTmp += 6;
  213. // remove additional white space
  214. while( *pTmp == ' ' || *pTmp == '\t' )
  215. pTmp++;
  216. // search for keywords "IID" or "CLSID" or "GUID"
  217. if( !strncmp( pTmp, "IID ", 4 ) || !strncmp( pTmp, "CLSID ", 6 ) || !strncmp( pTmp, "GUID ", 5 ) )
  218. {
  219. sprintf( szOutputName,"GuidSep\\SAPI5_guid_%03d.c", *pCount );
  220. (*pCount)++;
  221. hOutputFile = CreateFile( szOutputName, GENERIC_READ | GENERIC_WRITE,
  222. FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,
  223. FILE_ATTRIBUTE_NORMAL, NULL );
  224. if( hOutputFile != INVALID_HANDLE_VALUE )
  225. {
  226. WriteFile( hOutputFile, "#include \"precomp.h\"\n", strlen("#include \"precomp.h\"\n"), &dw, NULL );
  227. while( *pTmp != ';' )
  228. {
  229. pTmp++;
  230. if( pTmp > pEndStream ) return NULL;
  231. }
  232. WriteFile( hOutputFile, pStream, pTmp - pStream + 1, &dw, NULL );
  233. CloseHandle( hOutputFile );
  234. while( *pTmp != 10 )
  235. *pTmp++;
  236. pTmp++;
  237. }
  238. }
  239. else
  240. {
  241. // next line
  242. while( *pTmp != 10 )
  243. *pTmp++;
  244. pTmp++;
  245. }
  246. }
  247. else
  248. {
  249. // next line
  250. while( *pTmp != '\n' )
  251. {
  252. // eliminate any comment blocks
  253. if( *pTmp == '/' )
  254. {
  255. if( pTmp+1 <= pEndStream && (*(pTmp+1) == '*') )
  256. {
  257. pTmp += 2;
  258. do
  259. {
  260. while( *pTmp != '*' )
  261. pTmp++;
  262. if( pTmp > pEndStream ) return NULL;
  263. } while (*(pTmp+1) != '/' );
  264. pTmp+=2;
  265. continue;
  266. }
  267. }
  268. pTmp++;
  269. if( pTmp > pEndStream ) return NULL;
  270. }
  271. }
  272. return pTmp;
  273. }