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.

488 lines
14 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. tprof.c
  5. Abstract:
  6. Win32 Base API Test Program for Profile File Management calls
  7. Author:
  8. Steve Wood (stevewo) 26-Oct-1990
  9. Revision History:
  10. --*/
  11. #include <stdio.h>
  12. #include <string.h>
  13. #define WIN32_CONSOLE_APP
  14. #include <windows.h>
  15. void
  16. DumpProfile(
  17. LPTSTR ProfileFileName
  18. )
  19. {
  20. LPTSTR Sections, Section;
  21. LPTSTR Keywords, Keyword;
  22. LPTSTR KeyValue;
  23. Sections = LocalAlloc( 0, 4096 * sizeof( *Sections ) );
  24. Keywords = LocalAlloc( 0, 4096 * sizeof( *Keywords ) );
  25. KeyValue = LocalAlloc( 0, 1024 * sizeof( *KeyValue ) );
  26. #ifdef UNICODE
  27. printf( "\nDump of %ws\n",
  28. #else
  29. printf( "\nDump of %s\n",
  30. #endif
  31. ProfileFileName ? ProfileFileName : TEXT("win.ini")
  32. );
  33. *Sections = TEXT('\0');
  34. if (!GetPrivateProfileString( NULL, NULL, NULL,
  35. Sections, 4096 * sizeof( *Sections ),
  36. ProfileFileName
  37. )
  38. ) {
  39. printf( "*** Unable to read - rc == %d\n", GetLastError() );
  40. }
  41. Section = Sections;
  42. while (*Section) {
  43. #ifdef UNICODE
  44. printf( "[%ws]\n",
  45. #else
  46. printf( "[%s]\n",
  47. #endif
  48. Section
  49. );
  50. *Keywords = TEXT('\0');
  51. GetPrivateProfileString( Section, NULL, NULL,
  52. Keywords, 4096 * sizeof( *Keywords ),
  53. ProfileFileName
  54. );
  55. Keyword = Keywords;
  56. while (*Keyword) {
  57. GetPrivateProfileString( Section, Keyword, NULL,
  58. KeyValue, 1024 * sizeof( *KeyValue ),
  59. ProfileFileName
  60. );
  61. #ifdef UNICODE
  62. printf( " %ws=%ws\n",
  63. #else
  64. printf( " %s=%s\n",
  65. #endif
  66. Keyword, KeyValue
  67. );
  68. while (*Keyword++) {
  69. }
  70. }
  71. while (*Section++) {
  72. }
  73. }
  74. LocalFree( Sections );
  75. LocalFree( Keywords );
  76. LocalFree( KeyValue );
  77. return;
  78. }
  79. void
  80. DumpSection(
  81. LPTSTR ProfileFileName,
  82. LPTSTR SectionName
  83. )
  84. {
  85. LPTSTR SectionValue;
  86. LPTSTR s;
  87. SectionValue = LocalAlloc( 0, 4096 * sizeof( TCHAR ) );
  88. #ifdef UNICODE
  89. printf( "\nDump of Section %ws in %ws\n",
  90. #else
  91. printf( "\nDump of Section %s in %s\n",
  92. #endif
  93. SectionName,
  94. ProfileFileName ? ProfileFileName : TEXT("win.ini")
  95. );
  96. *SectionValue = TEXT('\0');
  97. GetPrivateProfileSection( SectionName,
  98. SectionValue, 4096 * sizeof( TCHAR ),
  99. ProfileFileName
  100. );
  101. #ifdef UNICODE
  102. printf( "[%ws]\n",
  103. #else
  104. printf( "[%s]\n",
  105. #endif
  106. SectionName
  107. );
  108. s = SectionValue;
  109. while (*s) {
  110. #ifdef UNICODE
  111. printf( " %ws\n", s );
  112. #else
  113. printf( " %s\n", s );
  114. #endif
  115. while (*s++) {
  116. }
  117. }
  118. LocalFree( SectionValue );
  119. return;
  120. }
  121. #define MAX_SECTIONS 32
  122. #define MAX_KEYWORDS 32
  123. DWORD
  124. main(
  125. int argc,
  126. char *argv[],
  127. char *envp[]
  128. )
  129. {
  130. ULONG n;
  131. int SectionNumber, KeyNumber;
  132. LPTSTR SectionValue, KeyValue, Keyword;
  133. TCHAR SectionName[ 32 ], KeyName[ 32 ], KeyValueBuffer[ 32 ], Buffer[ 32 ];
  134. FILE *fh;
  135. printf( "TPROF: Entering Test Program\n" );
  136. SectionValue = LocalAlloc( 0, 4096 * sizeof( TCHAR ) );
  137. KeyValue = LocalAlloc( 0, 1024 * sizeof( TCHAR ) );
  138. #if 0
  139. for (SectionNumber=0; SectionNumber<MAX_SECTIONS; SectionNumber++) {
  140. sprintf( SectionName, "Section%02u", SectionNumber );
  141. for (KeyNumber=0; KeyNumber<MAX_KEYWORDS; KeyNumber++) {
  142. sprintf( KeyName, "KeyName%02u", KeyNumber );
  143. sprintf( KeyValueBuffer, "KeyValue%02u_%02u", SectionNumber, KeyNumber );
  144. if (!WritePrivateProfileString( SectionName, KeyName, KeyValueBuffer, "foo.ini" )) {
  145. fprintf( stderr, "WriteProfileString( test.ini, [%s].%s=%s ) errno == %u\n",
  146. SectionName, KeyName, KeyValueBuffer, GetLastError()
  147. );
  148. }
  149. GetPrivateProfileString( SectionName, KeyName, "bogus", Buffer, sizeof( Buffer ), "foo.ini" );
  150. if (strcmp( Buffer, KeyValueBuffer )) {
  151. fprintf( stderr, "Write: %s != %s, errno == %u\n", KeyValueBuffer, Buffer, GetLastError() );
  152. }
  153. }
  154. }
  155. for (SectionNumber=0; SectionNumber<MAX_SECTIONS; SectionNumber++) {
  156. sprintf( SectionName, "Section%02u", SectionNumber );
  157. for (KeyNumber=0; KeyNumber<MAX_KEYWORDS; KeyNumber++) {
  158. sprintf( KeyName, "KeyName%02u", KeyNumber );
  159. sprintf( KeyValueBuffer, "KeyValue%02u_%02u", SectionNumber, KeyNumber );
  160. GetPrivateProfileString( SectionName, KeyName, "bogus", Buffer, sizeof( Buffer ), "foo.ini" );
  161. if (strcmp( Buffer, KeyValueBuffer )) {
  162. fprintf( stderr, "Read: %s != %s, errno == %u\n", KeyValueBuffer, Buffer, GetLastError() );
  163. }
  164. }
  165. }
  166. for (SectionNumber=0; SectionNumber<MAX_SECTIONS; SectionNumber++) {
  167. sprintf( SectionName, "Section%02u", SectionNumber );
  168. for (KeyNumber=0; KeyNumber<MAX_KEYWORDS; KeyNumber++) {
  169. sprintf( KeyName, "KeyName%02u", KeyNumber );
  170. sprintf( KeyValueBuffer, "KeyValue%02u_%02u", SectionNumber, KeyNumber );
  171. if (!WritePrivateProfileString( SectionName, KeyName, NULL, "foo.ini" )) {
  172. fprintf( stderr, "WriteProfileString( test.ini, [%s].%s (delete) ) errno == %u\n",
  173. SectionName, KeyName, GetLastError()
  174. );
  175. }
  176. GetPrivateProfileString( SectionName, KeyName, "bogus", Buffer, sizeof( Buffer ), "foo.ini" );
  177. if (strcmp( Buffer, "bogus" )) {
  178. fprintf( stderr, "Delete: bogus != %s, errno == %u\n", Buffer, GetLastError() );
  179. }
  180. }
  181. }
  182. exit( 0 );
  183. #endif
  184. WriteProfileString( TEXT("TESTINI"), TEXT("Key1"), TEXT("100abc") );
  185. n = GetProfileString( TEXT("ports"), NULL, TEXT(""), SectionValue, 4096 * sizeof( TCHAR ) );
  186. Keyword = SectionValue;
  187. printf( "Keywords in win.ini[ports]\n" );
  188. while (*Keyword) {
  189. #ifdef UNICODE
  190. printf( " %ws\n", Keyword );
  191. #else
  192. printf( " %s\n", Keyword );
  193. #endif
  194. while (*Keyword++) {
  195. }
  196. }
  197. n = GetProfileString( TEXT("ports"), NULL, NULL, SectionValue, 4096 );
  198. Keyword = SectionValue;
  199. printf( "Keywords in win.ini[ports]\n" );
  200. while (*Keyword) {
  201. #ifdef UNICODE
  202. printf( " %ws\n", Keyword );
  203. #else
  204. printf( " %s\n", Keyword );
  205. #endif
  206. while (*Keyword++) {
  207. }
  208. }
  209. DeleteFile( TEXT("\\nt\\windows\\test.ini") );
  210. fh = fopen( "\\nt\\windows\\test.ini", "w" );
  211. fclose( fh );
  212. DumpProfile( TEXT("test.ini") );
  213. WritePrivateProfileString( TEXT("StrApp"), TEXT("StrKey"), TEXT("StrVal"), TEXT("test.ini"));
  214. DumpProfile( TEXT("test.ini") );
  215. DeleteFile( TEXT("test.ini") );
  216. fh = fopen( "\\nt\\test.ini", "w" );
  217. fprintf( fh, "[IncompleteSectionWithoutTrailingBracket\n\n" );
  218. fprintf( fh, "[StrApp]\n" );
  219. fprintf( fh, "StrKey=xxxxxx\n" );
  220. fclose( fh );
  221. DumpProfile( TEXT("test.ini") );
  222. if (!WritePrivateProfileString( TEXT("StrApp"), TEXT("StrKey"), TEXT("StrVal"), TEXT("test.ini"))) {
  223. printf( "*** Write failed - rc == %d\n", GetLastError() );
  224. }
  225. else {
  226. DumpProfile( TEXT("test.ini") );
  227. }
  228. DeleteFile( "test.ini" );
  229. fh = fopen( "\\nt\\windows\\test.ini", "w" );
  230. fprintf( fh, "[a]\n" );
  231. fprintf( fh, "a1=b1\n" );
  232. fprintf( fh, "[b]\n" );
  233. fprintf( fh, "a2=b2\n" );
  234. fclose( fh );
  235. DumpProfile( TEXT("test.ini") );
  236. WritePrivateProfileSection( TEXT("Section2"),
  237. TEXT("Keyword21=Value21\0Keyword22=Value22\0Keyword23=Value23\0Keyword24=\0"),
  238. TEXT("test.ini")
  239. );
  240. DumpProfile( TEXT("test.ini") );
  241. n = GetPrivateProfileString( TEXT("a"), TEXT("\0"), TEXT("Default"),
  242. KeyValue, 1024 * sizeof( TCHAR ),
  243. TEXT("test.ini")
  244. );
  245. #ifdef UNICODE
  246. printf( "GetPrivateProfileString( a, \\0, Default ) == %ld '%ws'\n", n, KeyValue );
  247. #else
  248. printf( "GetPrivateProfileString( a, \\0, Default ) == %ld '%s'\n", n, KeyValue );
  249. #endif
  250. n = GetPrivateProfileInt( TEXT("a"), TEXT("\0"), 123, TEXT("test.ini") );
  251. printf( "GetPrivateProfileString( a, \\0, 123 ) == %ld\n", n );
  252. WritePrivateProfileString( TEXT("a"), NULL, NULL, TEXT("test.ini") );
  253. DumpProfile( TEXT("test.ini") );
  254. WritePrivateProfileString( TEXT("TESTINI"), TEXT("Key1"), TEXT("100abc"), TEXT("test.ini") );
  255. WritePrivateProfileString( TEXT(" TESTINI "),
  256. TEXT(" Key1 "),
  257. TEXT(" Val1 "),
  258. TEXT("test.ini")
  259. );
  260. DumpProfile( TEXT("test.ini") );
  261. printf( "GetProfileInt( 123 ) == %ld\n",
  262. GetProfileInt( TEXT("AAAAA"), TEXT("XXXXX"), 123 )
  263. );
  264. printf( "GetProfileInt( -123 ) == %ld\n",
  265. GetProfileInt( TEXT("AAAAA"), TEXT("XXXXX"), -123 )
  266. );
  267. WritePrivateProfileString( TEXT("TESTINI"),
  268. TEXT("Key1"),
  269. NULL,
  270. TEXT("test.ini")
  271. );
  272. DumpProfile( TEXT("test.ini") );
  273. WritePrivateProfileString( TEXT("TESTINI"),
  274. TEXT("Key2"),
  275. TEXT("Val2"),
  276. TEXT("test.ini")
  277. );
  278. DumpProfile( TEXT("test.ini") );
  279. WritePrivateProfileString( TEXT("TESTINI"),
  280. TEXT("Key2"),
  281. TEXT(""),
  282. TEXT("test.ini")
  283. );
  284. DumpProfile( TEXT("test.ini") );
  285. WritePrivateProfileString( TEXT("TESTINI"),
  286. TEXT("Key2"),
  287. NULL,
  288. TEXT("test.ini")
  289. );
  290. DumpProfile( TEXT("test.ini") );
  291. WritePrivateProfileString( TEXT("TESTINI"),
  292. TEXT("Key3"),
  293. TEXT("Val3"),
  294. TEXT("test.ini")
  295. );
  296. DumpProfile( TEXT("test.ini") );
  297. WritePrivateProfileString( TEXT("TESTINI"),
  298. NULL,
  299. TEXT("Something"),
  300. TEXT("test.ini")
  301. );
  302. DumpProfile( TEXT("test.ini") );
  303. WritePrivateProfileString( TEXT("Section1"),
  304. TEXT("Keyword11"),
  305. TEXT("Value11"),
  306. TEXT("test.ini")
  307. );
  308. DumpProfile( TEXT("test.ini") );
  309. WritePrivateProfileSection( TEXT("Section2"),
  310. TEXT("Keyword21=Value21\0Keyword22=Value22\0Keyword23=Value23\0Keyword24=\0"),
  311. TEXT("test.ini")
  312. );
  313. DumpProfile( TEXT("test.ini") );
  314. WritePrivateProfileString( TEXT("Section1"),
  315. TEXT("Keyword12"),
  316. TEXT("Value12"),
  317. TEXT("test.ini")
  318. );
  319. DumpProfile( TEXT("test.ini") );
  320. n = GetPrivateProfileSection( TEXT("Section1"),
  321. SectionValue, 4096 * sizeof( TCHAR ),
  322. TEXT("test.ini")
  323. );
  324. #if 0
  325. if (n != 36 ||
  326. strcmp( SectionValue, "Keyword11=Value11" ) ||
  327. strcmp( SectionValue+18, "Keyword12=Value12" )
  328. ) {
  329. printf( "*** test.ini[ Section1 ] is incorrect (Length == %d)\n", n );
  330. DumpSection( "test.ini", "Section1" );
  331. }
  332. n = GetPrivateProfileString( "Section2", "Keyword25", "Default25",
  333. KeyValue, 1024,
  334. "test.ini"
  335. );
  336. if (n != 9 || strcmp( KeyValue, "Default25" )) {
  337. printf( "*** test.ini[ Section2 ].Keyword25 is incorrect (Length == %d)\n", n );
  338. DumpSection( "test.ini", "Section2" );
  339. }
  340. n = GetPrivateProfileString( "Section2", "Keyword24", NULL,
  341. KeyValue, 1024,
  342. "test.ini"
  343. );
  344. if (n || strcmp( KeyValue, "" )) {
  345. printf( "*** test.ini[ Section2 ].Keyword24 is incorrect (Length == %d)\n", n );
  346. DumpSection( "test.ini", "Section2" );
  347. }
  348. n = GetPrivateProfileString( "Section2", "Keyword23", NULL,
  349. KeyValue, 1024,
  350. "test.ini"
  351. );
  352. if (n != 7 || strcmp( KeyValue, "Value23" )) {
  353. printf( "*** test.ini[ Section2 ].Keyword23 is incorrect (Length == %d)\n", n );
  354. DumpSection( "test.ini", "Section2" );
  355. }
  356. n = GetPrivateProfileString( "Section2", "Keyword22", NULL,
  357. KeyValue, 1024,
  358. "test.ini"
  359. );
  360. if (n != 7 || strcmp( KeyValue, "Value22" )) {
  361. printf( "*** test.ini[ Section2 ].Keyword22 is incorrect (Length == %d)\n", n );
  362. DumpSection( "test.ini", "Section2" );
  363. }
  364. n = GetPrivateProfileString( "Section2", "Keyword21", NULL,
  365. KeyValue, 1024,
  366. "test.ini"
  367. );
  368. if (n != 7 || strcmp( KeyValue, "Value21" )) {
  369. printf( "*** test.ini[ Section2 ].Keyword21 is incorrect (Length == %d)\n", n );
  370. DumpSection( "test.ini", "Section2" );
  371. }
  372. DumpProfile( "test.ini" );
  373. printf( "Deleting [Section1]Keyword11\n" );
  374. WritePrivateProfileString( "Section1",
  375. "Keyword11",
  376. NULL,
  377. "test.ini"
  378. );
  379. DumpProfile( "test.ini" );
  380. printf( "Deleting all keywords in [Section1]\n" );
  381. WritePrivateProfileSection( "Section1",
  382. "",
  383. "test.ini"
  384. );
  385. DumpProfile( "test.ini" );
  386. printf( "Deleting [Section1]\n" );
  387. WritePrivateProfileString( "Section1",
  388. NULL,
  389. NULL,
  390. "test.ini"
  391. );
  392. DumpProfile( "test.ini" );
  393. printf( "Setting [Section2]Keyword21=\n" );
  394. WritePrivateProfileString( "Section2",
  395. "Keyword21",
  396. "",
  397. "test.ini"
  398. );
  399. #endif
  400. DumpProfile( TEXT("test.ini") );
  401. DumpProfile( NULL );
  402. DumpSection( NULL, TEXT("Extensions") );
  403. printf( "TPROF: Exiting Test Program\n" );
  404. return 0;
  405. }