1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
|
/*
* Profile functions
*
* Copyright 1993 Miguel de Icaza
* Copyright 1996 Alexandre Julliard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include "wine/port.h"
#include <string.h>
#include <stdarg.h>
#include "windef.h"
#include "winbase.h"
#include "winnls.h"
#include "winerror.h"
#include "winternl.h"
#include "wine/winbase16.h"
#include "wine/unicode.h"
#include "wine/library.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(profile);
static const char bom_utf8[] = {0xEF,0xBB,0xBF};
typedef enum
{
ENCODING_ANSI = 1,
ENCODING_UTF8,
ENCODING_UTF16LE,
ENCODING_UTF16BE
} ENCODING;
typedef struct tagPROFILEKEY
{
WCHAR *value;
struct tagPROFILEKEY *next;
WCHAR name[1];
} PROFILEKEY;
typedef struct tagPROFILESECTION
{
struct tagPROFILEKEY *key;
struct tagPROFILESECTION *next;
WCHAR name[1];
} PROFILESECTION;
typedef struct
{
BOOL changed;
PROFILESECTION *section;
WCHAR *filename;
FILETIME LastWriteTime;
ENCODING encoding;
} PROFILE;
#define N_CACHED_PROFILES 10
/* Cached profile files */
static PROFILE *MRUProfile[N_CACHED_PROFILES]={NULL};
#define CurProfile (MRUProfile[0])
/* Check for comments in profile */
#define IS_ENTRY_COMMENT(str) ((str)[0] == ';')
static const WCHAR emptystringW[] = {0};
static const WCHAR wininiW[] = { 'w','i','n','.','i','n','i',0 };
static CRITICAL_SECTION PROFILE_CritSect;
static CRITICAL_SECTION_DEBUG critsect_debug =
{
0, 0, &PROFILE_CritSect,
{ &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
0, 0, { (DWORD_PTR)(__FILE__ ": PROFILE_CritSect") }
};
static CRITICAL_SECTION PROFILE_CritSect = { &critsect_debug, -1, 0, 0, 0, 0 };
static const char hex[16] = "0123456789ABCDEF";
/***********************************************************************
* PROFILE_CopyEntry
*
* Copy the content of an entry into a buffer, removing quotes, and possibly
* translating environment variables.
*/
static void PROFILE_CopyEntry( LPWSTR buffer, LPCWSTR value, int len,
BOOL strip_quote )
{
WCHAR quote = '\0';
if(!buffer) return;
if (strip_quote && ((*value == '\'') || (*value == '\"')))
{
if (value[1] && (value[strlenW(value)-1] == *value)) quote = *value++;
}
lstrcpynW( buffer, value, len );
if (quote && (len >= strlenW(value))) buffer[strlenW(buffer)-1] = '\0';
}
/* byte-swaps shorts in-place in a buffer. len is in WCHARs */
static inline void PROFILE_ByteSwapShortBuffer(WCHAR * buffer, int len)
{
int i;
USHORT * shortbuffer = (USHORT *)buffer;
for (i = 0; i < len; i++)
shortbuffer[i] = RtlUshortByteSwap(shortbuffer[i]);
}
/* writes any necessary encoding marker to the file */
static inline void PROFILE_WriteMarker(HANDLE hFile, ENCODING encoding)
{
DWORD dwBytesWritten;
WCHAR bom;
switch (encoding)
{
case ENCODING_ANSI:
break;
case ENCODING_UTF8:
WriteFile(hFile, bom_utf8, sizeof(bom_utf8), &dwBytesWritten, NULL);
break;
case ENCODING_UTF16LE:
bom = 0xFEFF;
WriteFile(hFile, &bom, sizeof(bom), &dwBytesWritten, NULL);
break;
case ENCODING_UTF16BE:
bom = 0xFFFE;
WriteFile(hFile, &bom, sizeof(bom), &dwBytesWritten, NULL);
break;
}
}
static void PROFILE_WriteLine( HANDLE hFile, WCHAR * szLine, int len, ENCODING encoding)
{
char * write_buffer;
int write_buffer_len;
DWORD dwBytesWritten;
TRACE("writing: %s\n", debugstr_wn(szLine, len));
switch (encoding)
{
case ENCODING_ANSI:
write_buffer_len = WideCharToMultiByte(CP_ACP, 0, szLine, len, NULL, 0, NULL, NULL);
write_buffer = HeapAlloc(GetProcessHeap(), 0, write_buffer_len);
if (!write_buffer) return;
len = WideCharToMultiByte(CP_ACP, 0, szLine, len, write_buffer, write_buffer_len, NULL, NULL);
WriteFile(hFile, write_buffer, len, &dwBytesWritten, NULL);
HeapFree(GetProcessHeap(), 0, write_buffer);
break;
case ENCODING_UTF8:
write_buffer_len = WideCharToMultiByte(CP_UTF8, 0, szLine, len, NULL, 0, NULL, NULL);
write_buffer = HeapAlloc(GetProcessHeap(), 0, write_buffer_len);
if (!write_buffer) return;
len = WideCharToMultiByte(CP_UTF8, 0, szLine, len, write_buffer, write_buffer_len, NULL, NULL);
WriteFile(hFile, write_buffer, len, &dwBytesWritten, NULL);
HeapFree(GetProcessHeap(), 0, write_buffer);
break;
case ENCODING_UTF16LE:
WriteFile(hFile, szLine, len * sizeof(WCHAR), &dwBytesWritten, NULL);
break;
case ENCODING_UTF16BE:
PROFILE_ByteSwapShortBuffer(szLine, len);
WriteFile(hFile, szLine, len * sizeof(WCHAR), &dwBytesWritten, NULL);
break;
default:
FIXME("encoding type %d not implemented\n", encoding);
}
}
/***********************************************************************
* PROFILE_Save
*
* Save a profile tree to a file.
*/
static void PROFILE_Save( HANDLE hFile, const PROFILESECTION *section, ENCODING encoding )
{
PROFILEKEY *key;
WCHAR *buffer, *p;
PROFILE_WriteMarker(hFile, encoding);
for ( ; section; section = section->next)
{
int len = 0;
if (section->name[0]) len += strlenW(section->name) + 6;
for (key = section->key; key; key = key->next)
{
len += strlenW(key->name) + 2;
if (key->value) len += strlenW(key->value) + 1;
}
buffer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
if (!buffer) return;
p = buffer;
if (section->name[0])
{
*p++ = '\r';
*p++ = '\n';
*p++ = '[';
strcpyW( p, section->name );
p += strlenW(p);
*p++ = ']';
*p++ = '\r';
*p++ = '\n';
}
for (key = section->key; key; key = key->next)
{
strcpyW( p, key->name );
p += strlenW(p);
if (key->value)
{
*p++ = '=';
strcpyW( p, key->value );
p += strlenW(p);
}
*p++ = '\r';
*p++ = '\n';
}
PROFILE_WriteLine( hFile, buffer, len, encoding );
HeapFree(GetProcessHeap(), 0, buffer);
}
}
/***********************************************************************
* PROFILE_Free
*
* Free a profile tree.
*/
static void PROFILE_Free( PROFILESECTION *section )
{
PROFILESECTION *next_section;
PROFILEKEY *key, *next_key;
for ( ; section; section = next_section)
{
for (key = section->key; key; key = next_key)
{
next_key = key->next;
HeapFree( GetProcessHeap(), 0, key->value );
HeapFree( GetProcessHeap(), 0, key );
}
next_section = section->next;
HeapFree( GetProcessHeap(), 0, section );
}
}
/* returns 1 if a character white space else 0 */
static inline int PROFILE_isspaceW(WCHAR c)
{
if (isspaceW(c)) return 1;
if (c=='\r' || c==0x1a) return 1;
/* CR and ^Z (DOS EOF) are spaces too (found on CD-ROMs) */
return 0;
}
static inline ENCODING PROFILE_DetectTextEncoding(const void * buffer, int * len)
{
int flags = IS_TEXT_UNICODE_SIGNATURE |
IS_TEXT_UNICODE_REVERSE_SIGNATURE |
IS_TEXT_UNICODE_ODD_LENGTH;
if (*len >= sizeof(bom_utf8) && !memcmp(buffer, bom_utf8, sizeof(bom_utf8)))
{
*len = sizeof(bom_utf8);
return ENCODING_UTF8;
}
RtlIsTextUnicode(buffer, *len, &flags);
if (flags & IS_TEXT_UNICODE_SIGNATURE)
{
*len = sizeof(WCHAR);
return ENCODING_UTF16LE;
}
if (flags & IS_TEXT_UNICODE_REVERSE_SIGNATURE)
{
*len = sizeof(WCHAR);
return ENCODING_UTF16BE;
}
*len = 0;
return ENCODING_ANSI;
}
/***********************************************************************
* PROFILE_Load
*
* Load a profile tree from a file.
*/
static PROFILESECTION *PROFILE_Load(HANDLE hFile, ENCODING * pEncoding)
{
void *buffer_base, *pBuffer;
WCHAR * szFile;
const WCHAR *szLineStart, *szLineEnd;
const WCHAR *szValueStart, *szEnd, *next_line;
int line = 0, len;
PROFILESECTION *section, *first_section;
PROFILESECTION **next_section;
PROFILEKEY *key, *prev_key, **next_key;
DWORD dwFileSize;
TRACE("%p\n", hFile);
dwFileSize = GetFileSize(hFile, NULL);
if (dwFileSize == INVALID_FILE_SIZE)
return NULL;
buffer_base = HeapAlloc(GetProcessHeap(), 0 , dwFileSize);
if (!buffer_base) return NULL;
if (!ReadFile(hFile, buffer_base, dwFileSize, &dwFileSize, NULL))
{
HeapFree(GetProcessHeap(), 0, buffer_base);
WARN("Error %d reading file\n", GetLastError());
return NULL;
}
len = dwFileSize;
*pEncoding = PROFILE_DetectTextEncoding(buffer_base, &len);
/* len is set to the number of bytes in the character marker.
* we want to skip these bytes */
pBuffer = (char *)buffer_base + len;
dwFileSize -= len;
switch (*pEncoding)
{
case ENCODING_ANSI:
TRACE("ANSI encoding\n");
len = MultiByteToWideChar(CP_ACP, 0, (char *)pBuffer, dwFileSize, NULL, 0);
szFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
if (!szFile)
{
HeapFree(GetProcessHeap(), 0, buffer_base);
return NULL;
}
MultiByteToWideChar(CP_ACP, 0, (char *)pBuffer, dwFileSize, szFile, len);
szEnd = szFile + len;
break;
case ENCODING_UTF8:
TRACE("UTF8 encoding\n");
len = MultiByteToWideChar(CP_UTF8, 0, (char *)pBuffer, dwFileSize, NULL, 0);
szFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
if (!szFile)
{
HeapFree(GetProcessHeap(), 0, buffer_base);
return NULL;
}
MultiByteToWideChar(CP_UTF8, 0, (char *)pBuffer, dwFileSize, szFile, len);
szEnd = szFile + len;
break;
case ENCODING_UTF16LE:
TRACE("UTF16 Little Endian encoding\n");
szFile = (WCHAR *)pBuffer;
szEnd = (WCHAR *)((char *)pBuffer + dwFileSize);
break;
case ENCODING_UTF16BE:
TRACE("UTF16 Big Endian encoding\n");
szFile = (WCHAR *)pBuffer;
szEnd = (WCHAR *)((char *)pBuffer + dwFileSize);
PROFILE_ByteSwapShortBuffer(szFile, dwFileSize / sizeof(WCHAR));
break;
default:
FIXME("encoding type %d not implemented\n", *pEncoding);
HeapFree(GetProcessHeap(), 0, buffer_base);
return NULL;
}
first_section = HeapAlloc( GetProcessHeap(), 0, sizeof(*section) );
if(first_section == NULL)
{
if (szFile != pBuffer)
HeapFree(GetProcessHeap(), 0, szFile);
HeapFree(GetProcessHeap(), 0, buffer_base);
return NULL;
}
first_section->name[0] = 0;
first_section->key = NULL;
first_section->next = NULL;
next_section = &first_section->next;
next_key = &first_section->key;
prev_key = NULL;
next_line = szFile;
while (next_line < szEnd)
{
szLineStart = next_line;
next_line = memchrW(szLineStart, '\n', szEnd - szLineStart);
if (!next_line) next_line = szEnd;
else next_line++;
szLineEnd = next_line;
line++;
/* get rid of white space */
while (szLineStart < szLineEnd && PROFILE_isspaceW(*szLineStart)) szLineStart++;
while ((szLineEnd > szLineStart) && ((szLineEnd[-1] == '\n') || PROFILE_isspaceW(szLineEnd[-1]))) szLineEnd--;
if (szLineStart >= szLineEnd) continue;
if (*szLineStart == '[') /* section start */
{
const WCHAR * szSectionEnd;
if (!(szSectionEnd = memrchrW( szLineStart, ']', szLineEnd - szLineStart )))
{
WARN("Invalid section header at line %d: %s\n",
line, debugstr_wn(szLineStart, (int)(szLineEnd - szLineStart)) );
}
else
{
szLineStart++;
len = (int)(szSectionEnd - szLineStart);
/* no need to allocate +1 for NULL terminating character as
* already included in structure */
if (!(section = HeapAlloc( GetProcessHeap(), 0, sizeof(*section) + len * sizeof(WCHAR) )))
break;
memcpy(section->name, szLineStart, len * sizeof(WCHAR));
section->name[len] = '\0';
section->key = NULL;
section->next = NULL;
*next_section = section;
next_section = §ion->next;
next_key = §ion->key;
prev_key = NULL;
TRACE("New section: %s\n", debugstr_w(section->name));
continue;
}
}
/* get rid of white space after the name and before the start
* of the value */
len = szLineEnd - szLineStart;
if ((szValueStart = memchrW( szLineStart, '=', szLineEnd - szLineStart )) != NULL)
{
const WCHAR *szNameEnd = szValueStart;
while ((szNameEnd > szLineStart) && PROFILE_isspaceW(szNameEnd[-1])) szNameEnd--;
len = szNameEnd - szLineStart;
szValueStart++;
while (szValueStart < szLineEnd && PROFILE_isspaceW(*szValueStart)) szValueStart++;
}
if (len || !prev_key || *prev_key->name)
{
/* no need to allocate +1 for NULL terminating character as
* already included in structure */
if (!(key = HeapAlloc( GetProcessHeap(), 0, sizeof(*key) + len * sizeof(WCHAR) ))) break;
memcpy(key->name, szLineStart, len * sizeof(WCHAR));
key->name[len] = '\0';
if (szValueStart)
{
len = (int)(szLineEnd - szValueStart);
key->value = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
memcpy(key->value, szValueStart, len * sizeof(WCHAR));
key->value[len] = '\0';
}
else key->value = NULL;
key->next = NULL;
*next_key = key;
next_key = &key->next;
prev_key = key;
TRACE("New key: name=%s, value=%s\n",
debugstr_w(key->name), key->value ? debugstr_w(key->value) : "(none)");
}
}
if (szFile != pBuffer)
HeapFree(GetProcessHeap(), 0, szFile);
HeapFree(GetProcessHeap(), 0, buffer_base);
return first_section;
}
/***********************************************************************
* PROFILE_DeleteSection
*
* Delete a section from a profile tree.
*/
static BOOL PROFILE_DeleteSection( PROFILESECTION **section, LPCWSTR name )
{
while (*section)
{
if ((*section)->name[0] && !strcmpiW( (*section)->name, name ))
{
PROFILESECTION *to_del = *section;
*section = to_del->next;
to_del->next = NULL;
PROFILE_Free( to_del );
return TRUE;
}
section = &(*section)->next;
}
return FALSE;
}
/***********************************************************************
* PROFILE_DeleteKey
*
* Delete a key from a profile tree.
*/
static BOOL PROFILE_DeleteKey( PROFILESECTION **section,
LPCWSTR section_name, LPCWSTR key_name )
{
while (*section)
{
if ((*section)->name[0] && !strcmpiW( (*section)->name, section_name ))
{
PROFILEKEY **key = &(*section)->key;
while (*key)
{
if (!strcmpiW( (*key)->name, key_name ))
{
PROFILEKEY *to_del = *key;
*key = to_del->next;
HeapFree( GetProcessHeap(), 0, to_del->value);
HeapFree( GetProcessHeap(), 0, to_del );
return TRUE;
}
key = &(*key)->next;
}
}
section = &(*section)->next;
}
return FALSE;
}
/***********************************************************************
* PROFILE_DeleteAllKeys
*
* Delete all keys from a profile tree.
*/
static void PROFILE_DeleteAllKeys( LPCWSTR section_name)
{
PROFILESECTION **section= &CurProfile->section;
while (*section)
{
if ((*section)->name[0] && !strcmpiW( (*section)->name, section_name ))
{
PROFILEKEY **key = &(*section)->key;
while (*key)
{
PROFILEKEY *to_del = *key;
*key = to_del->next;
HeapFree( GetProcessHeap(), 0, to_del->value);
HeapFree( GetProcessHeap(), 0, to_del );
CurProfile->changed =TRUE;
}
}
section = &(*section)->next;
}
}
/***********************************************************************
* PROFILE_Find
*
* Find a key in a profile tree, optionally creating it.
*/
static PROFILEKEY *PROFILE_Find( PROFILESECTION **section, LPCWSTR section_name,
LPCWSTR key_name, BOOL create, BOOL create_always )
{
LPCWSTR p;
int seclen, keylen;
while (PROFILE_isspaceW(*section_name)) section_name++;
p = section_name + strlenW(section_name) - 1;
while ((p > section_name) && PROFILE_isspaceW(*p)) p--;
seclen = p - section_name + 1;
while (PROFILE_isspaceW(*key_name)) key_name++;
p = key_name + strlenW(key_name) - 1;
while ((p > key_name) && PROFILE_isspaceW(*p)) p--;
keylen = p - key_name + 1;
while (*section)
{
if ( ((*section)->name[0])
&& (!(strncmpiW( (*section)->name, section_name, seclen )))
&& (((*section)->name)[seclen] == '\0') )
{
PROFILEKEY **key = &(*section)->key;
while (*key)
{
/* If create_always is FALSE then we check if the keyname
* already exists. Otherwise we add it regardless of its
* existence, to allow keys to be added more than once in
* some cases.
*/
if(!create_always)
{
if ( (!(strncmpiW( (*key)->name, key_name, keylen )))
&& (((*key)->name)[keylen] == '\0') )
return *key;
}
key = &(*key)->next;
}
if (!create) return NULL;
if (!(*key = HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILEKEY) + strlenW(key_name) * sizeof(WCHAR) )))
return NULL;
strcpyW( (*key)->name, key_name );
(*key)->value = NULL;
(*key)->next = NULL;
return *key;
}
section = &(*section)->next;
}
if (!create) return NULL;
*section = HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILESECTION) + strlenW(section_name) * sizeof(WCHAR) );
if(*section == NULL) return NULL;
strcpyW( (*section)->name, section_name );
(*section)->next = NULL;
if (!((*section)->key = HeapAlloc( GetProcessHeap(), 0,
sizeof(PROFILEKEY) + strlenW(key_name) * sizeof(WCHAR) )))
{
HeapFree(GetProcessHeap(), 0, *section);
return NULL;
}
strcpyW( (*section)->key->name, key_name );
(*section)->key->value = NULL;
(*section)->key->next = NULL;
return (*section)->key;
}
/***********************************************************************
* PROFILE_FlushFile
*
* Flush the current profile to disk if changed.
*/
static BOOL PROFILE_FlushFile(void)
{
HANDLE hFile = NULL;
FILETIME LastWriteTime;
if(!CurProfile)
{
WARN("No current profile!\n");
return FALSE;
}
if (!CurProfile->changed) return TRUE;
hFile = CreateFileW(CurProfile->filename, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
WARN("could not save profile file %s (error was %d)\n", debugstr_w(CurProfile->filename), GetLastError());
return FALSE;
}
TRACE("Saving %s\n", debugstr_w(CurProfile->filename));
PROFILE_Save( hFile, CurProfile->section, CurProfile->encoding );
if(GetFileTime(hFile, NULL, NULL, &LastWriteTime))
CurProfile->LastWriteTime=LastWriteTime;
CloseHandle( hFile );
CurProfile->changed = FALSE;
return TRUE;
}
/***********************************************************************
* PROFILE_ReleaseFile
*
* Flush the current profile to disk and remove it from the cache.
*/
static void PROFILE_ReleaseFile(void)
{
PROFILE_FlushFile();
PROFILE_Free( CurProfile->section );
HeapFree( GetProcessHeap(), 0, CurProfile->filename );
CurProfile->changed = FALSE;
CurProfile->section = NULL;
CurProfile->filename = NULL;
CurProfile->encoding = ENCODING_ANSI;
ZeroMemory(&CurProfile->LastWriteTime, sizeof(CurProfile->LastWriteTime));
}
/***********************************************************************
* PROFILE_Open
*
* Open a profile file, checking the cached file first.
*/
static BOOL PROFILE_Open( LPCWSTR filename, BOOL write_access )
{
WCHAR windirW[MAX_PATH];
WCHAR buffer[MAX_PATH];
HANDLE hFile = INVALID_HANDLE_VALUE;
FILETIME LastWriteTime;
int i,j;
PROFILE *tempProfile;
ZeroMemory(&LastWriteTime, sizeof(LastWriteTime));
/* First time around */
if(!CurProfile)
for(i=0;i<N_CACHED_PROFILES;i++)
{
MRUProfile[i]=HeapAlloc( GetProcessHeap(), 0, sizeof(PROFILE) );
if(MRUProfile[i] == NULL) break;
MRUProfile[i]->changed=FALSE;
MRUProfile[i]->section=NULL;
MRUProfile[i]->filename=NULL;
MRUProfile[i]->encoding=ENCODING_ANSI;
ZeroMemory(&MRUProfile[i]->LastWriteTime, sizeof(FILETIME));
}
GetWindowsDirectoryW( windirW, MAX_PATH );
if (!filename)
filename = wininiW;
if ((RtlDetermineDosPathNameType_U(filename) == RELATIVE_PATH) &&
!strchrW(filename, '\\') && !strchrW(filename, '/'))
{
static const WCHAR wszSeparator[] = {'\\', 0};
strcpyW(buffer, windirW);
strcatW(buffer, wszSeparator);
strcatW(buffer, filename);
}
else
{
LPWSTR dummy;
GetFullPathNameW(filename, sizeof(buffer)/sizeof(buffer[0]), buffer, &dummy);
}
TRACE("path: %s\n", debugstr_w(buffer));
hFile = CreateFileW(buffer, GENERIC_READ | (write_access ? GENERIC_WRITE : 0),
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if ((hFile == INVALID_HANDLE_VALUE) && (GetLastError() != ERROR_FILE_NOT_FOUND))
{
WARN("Error %d opening file %s\n", GetLastError(), debugstr_w(buffer));
return FALSE;
}
for(i=0;i<N_CACHED_PROFILES;i++)
{
if ((MRUProfile[i]->filename && !strcmpiW( buffer, MRUProfile[i]->filename )))
{
TRACE("MRU Filename: %s, new filename: %s\n", debugstr_w(MRUProfile[i]->filename), debugstr_w(buffer));
if(i)
{
PROFILE_FlushFile();
tempProfile=MRUProfile[i];
for(j=i;j>0;j--)
MRUProfile[j]=MRUProfile[j-1];
CurProfile=tempProfile;
}
if (hFile != INVALID_HANDLE_VALUE)
{
if (TRACE_ON(profile))
{
GetFileTime(hFile, NULL, NULL, &LastWriteTime);
if (memcmp(&CurProfile->LastWriteTime, &LastWriteTime, sizeof(FILETIME)))
TRACE("(%s): already opened (mru=%d)\n",
debugstr_w(buffer), i);
else
TRACE("(%s): already opened, needs refreshing (mru=%d)\n",
debugstr_w(buffer), i);
}
CloseHandle(hFile);
}
else TRACE("(%s): already opened, not yet created (mru=%d)\n",
debugstr_w(buffer), i);
return TRUE;
}
}
/* Flush the old current profile */
PROFILE_FlushFile();
/* Make the oldest profile the current one only in order to get rid of it */
if(i==N_CACHED_PROFILES)
{
tempProfile=MRUProfile[N_CACHED_PROFILES-1];
for(i=N_CACHED_PROFILES-1;i>0;i--)
MRUProfile[i]=MRUProfile[i-1];
CurProfile=tempProfile;
}
if(CurProfile->filename) PROFILE_ReleaseFile();
/* OK, now that CurProfile is definitely free we assign it our new file */
CurProfile->filename = HeapAlloc( GetProcessHeap(), 0, (strlenW(buffer)+1) * sizeof(WCHAR) );
strcpyW( CurProfile->filename, buffer );
if (hFile != INVALID_HANDLE_VALUE)
{
CurProfile->section = PROFILE_Load(hFile, &CurProfile->encoding);
GetFileTime(hFile, NULL, NULL, &CurProfile->LastWriteTime);
CloseHandle(hFile);
}
else
{
/* Does not exist yet, we will create it in PROFILE_FlushFile */
WARN("profile file %s not found\n", debugstr_w(buffer) );
}
return TRUE;
}
/***********************************************************************
* PROFILE_GetSection
*
* Returns all keys of a section.
* If return_values is TRUE, also include the corresponding values.
*/
static INT PROFILE_GetSection( PROFILESECTION *section, LPCWSTR section_name,
LPWSTR buffer, UINT len, BOOL return_values, BOOL return_noequalkeys )
{
PROFILEKEY *key;
if(!buffer) return 0;
TRACE("%s,%p,%u\n", debugstr_w(section_name), buffer, len);
while (section)
{
if (section->name[0] && !strcmpiW( section->name, section_name ))
{
UINT oldlen = len;
for (key = section->key; key; key = key->next)
{
if (len <= 2) break;
if (!*key->name) continue; /* Skip empty lines */
if (IS_ENTRY_COMMENT(key->name)) continue; /* Skip comments */
if (!return_noequalkeys && !return_values && !key->value) continue; /* Skip lines w.o. '=' */
PROFILE_CopyEntry( buffer, key->name, len - 1, 0 );
len -= strlenW(buffer) + 1;
buffer += strlenW(buffer) + 1;
if (len < 2)
break;
if (return_values && key->value) {
buffer[-1] = '=';
PROFILE_CopyEntry ( buffer, key->value, len - 1, 0 );
len -= strlenW(buffer) + 1;
buffer += strlenW(buffer) + 1;
}
}
*buffer = '\0';
if (len <= 1)
/*If either lpszSection or lpszKey is NULL and the supplied
destination buffer is too small to hold all the strings,
the last string is truncated and followed by two null characters.
In this case, the return value is equal to cchReturnBuffer
minus two. */
{
buffer[-1] = '\0';
return oldlen - 2;
}
return oldlen - len;
}
section = section->next;
}
buffer[0] = buffer[1] = '\0';
return 0;
}
/* See GetPrivateProfileSectionNamesA for documentation */
static INT PROFILE_GetSectionNames( LPWSTR buffer, UINT len )
{
LPWSTR buf;
UINT buflen,tmplen;
PROFILESECTION *section;
TRACE("(%p, %d)\n", buffer, len);
if (!buffer || !len)
return 0;
if (len==1) {
*buffer='\0';
return 0;
}
buflen=len-1;
buf=buffer;
section = CurProfile->section;
while ((section!=NULL)) {
if (section->name[0]) {
tmplen = strlenW(section->name)+1;
if (tmplen >= buflen) {
if (buflen > 0) {
memcpy(buf, section->name, (buflen-1) * sizeof(WCHAR));
buf += buflen-1;
*buf++='\0';
}
*buf='\0';
return len-2;
}
memcpy(buf, section->name, tmplen * sizeof(WCHAR));
buf += tmplen;
buflen -= tmplen;
}
section = section->next;
}
*buf='\0';
return buf-buffer;
}
/***********************************************************************
* PROFILE_GetString
*
* Get a profile string.
*
* Tests with GetPrivateProfileString16, W95a,
* with filled buffer ("****...") and section "set1" and key_name "1" valid:
* section key_name def_val res buffer
* "set1" "1" "x" 43 [data]
* "set1" "1 " "x" 43 [data] (!)
* "set1" " 1 "' "x" 43 [data] (!)
* "set1" "" "x" 1 "x"
* "set1" "" "x " 1 "x" (!)
* "set1" "" " x " 3 " x" (!)
* "set1" NULL "x" 6 "1\02\03\0\0"
* "set1" "" "x" 1 "x"
* NULL "1" "x" 0 "" (!)
* "" "1" "x" 1 "x"
* NULL NULL "" 0 ""
*
*
*/
static INT PROFILE_GetString( LPCWSTR section, LPCWSTR key_name,
LPCWSTR def_val, LPWSTR buffer, UINT len, BOOL win32 )
{
PROFILEKEY *key = NULL;
static const WCHAR empty_strW[] = { 0 };
if(!buffer) return 0;
if (!def_val) def_val = empty_strW;
if (key_name)
{
if (!key_name[0])
{
/* Win95 returns 0 on keyname "". Tested with Likse32 bon 000227 */
return 0;
}
key = PROFILE_Find( &CurProfile->section, section, key_name, FALSE, FALSE);
PROFILE_CopyEntry( buffer, (key && key->value) ? key->value : def_val,
len, TRUE );
TRACE("(%s,%s,%s): returning %s\n",
debugstr_w(section), debugstr_w(key_name),
debugstr_w(def_val), debugstr_w(buffer) );
return strlenW( buffer );
}
/* no "else" here ! */
if (section && section[0])
{
INT ret = PROFILE_GetSection(CurProfile->section, section, buffer, len, FALSE, !win32);
if (!buffer[0]) /* no luck -> def_val */
{
PROFILE_CopyEntry(buffer, def_val, len, TRUE);
ret = strlenW(buffer);
}
return ret;
}
buffer[0] = '\0';
return 0;
}
/***********************************************************************
* PROFILE_SetString
*
* Set a profile string.
*/
static BOOL PROFILE_SetString( LPCWSTR section_name, LPCWSTR key_name,
LPCWSTR value, BOOL create_always )
{
if (!key_name) /* Delete a whole section */
{
TRACE("(%s)\n", debugstr_w(section_name));
CurProfile->changed |= PROFILE_DeleteSection( &CurProfile->section,
section_name );
return TRUE; /* Even if PROFILE_DeleteSection() has failed,
this is not an error on application's level.*/
}
else if (!value) /* Delete a key */
{
TRACE("(%s,%s)\n", debugstr_w(section_name), debugstr_w(key_name) );
CurProfile->changed |= PROFILE_DeleteKey( &CurProfile->section,
section_name, key_name );
return TRUE; /* same error handling as above */
}
else /* Set the key value */
{
PROFILEKEY *key = PROFILE_Find(&CurProfile->section, section_name,
key_name, TRUE, create_always );
TRACE("(%s,%s,%s):\n",
debugstr_w(section_name), debugstr_w(key_name), debugstr_w(value) );
if (!key) return FALSE;
/* strip the leading spaces. We can safely strip \n\r and
* friends too, they should not happen here anyway. */
while (PROFILE_isspaceW(*value)) value++;
if (key->value)
{
if (!strcmpW( key->value, value ))
{
TRACE(" no change needed\n" );
return TRUE; /* No change needed */
}
TRACE(" replacing %s\n", debugstr_w(key->value) );
HeapFree( GetProcessHeap(), 0, key->value );
}
else TRACE(" creating key\n" );
key->value = HeapAlloc( GetProcessHeap(), 0, (strlenW(value)+1) * sizeof(WCHAR) );
strcpyW( key->value, value );
CurProfile->changed = TRUE;
}
return TRUE;
}
/********************* API functions **********************************/
/***********************************************************************
* GetProfileIntA (KERNEL32.@)
*/
UINT WINAPI GetProfileIntA( LPCSTR section, LPCSTR entry, INT def_val )
{
return GetPrivateProfileIntA( section, entry, def_val, "win.ini" );
}
/***********************************************************************
* GetProfileIntW (KERNEL32.@)
*/
UINT WINAPI GetProfileIntW( LPCWSTR section, LPCWSTR entry, INT def_val )
{
return GetPrivateProfileIntW( section, entry, def_val, wininiW );
}
/*
* if win32, copy:
* - Section names if 'section' is NULL
* - Keys in a Section if 'entry' is NULL
* (see MSDN doc for GetPrivateProfileString)
*/
static int PROFILE_GetPrivateProfileString( LPCWSTR section, LPCWSTR entry,
LPCWSTR def_val, LPWSTR buffer,
UINT len, LPCWSTR filename,
BOOL win32 )
{
int ret;
LPWSTR defval_tmp = NULL;
TRACE("%s,%s,%s,%p,%u,%s\n", debugstr_w(section), debugstr_w(entry),
debugstr_w(def_val), buffer, len, debugstr_w(filename));
/* strip any trailing ' ' of def_val. */
if (def_val)
{
LPCWSTR p = &def_val[strlenW(def_val)]; /* even "" works ! */
while (p > def_val)
{
p--;
if ((*p) != ' ')
break;
}
if (*p == ' ') /* ouch, contained trailing ' ' */
{
int len = (int)(p - def_val);
defval_tmp = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
memcpy(defval_tmp, def_val, len * sizeof(WCHAR));
defval_tmp[len] = '\0';
def_val = defval_tmp;
}
}
RtlEnterCriticalSection( &PROFILE_CritSect );
if (PROFILE_Open( filename, FALSE )) {
if (win32 && (section == NULL))
ret = PROFILE_GetSectionNames(buffer, len);
else
/* PROFILE_GetString can handle the 'entry == NULL' case */
ret = PROFILE_GetString( section, entry, def_val, buffer, len, win32 );
} else if (buffer && def_val) {
lstrcpynW( buffer, def_val, len );
ret = strlenW( buffer );
}
else
ret = 0;
RtlLeaveCriticalSection( &PROFILE_CritSect );
HeapFree(GetProcessHeap(), 0, defval_tmp);
TRACE("returning %s, %d\n", debugstr_w(buffer), ret);
return ret;
}
/***********************************************************************
* GetPrivateProfileString (KERNEL.128)
*/
INT16 WINAPI GetPrivateProfileString16( LPCSTR section, LPCSTR entry,
LPCSTR def_val, LPSTR buffer,
UINT16 len, LPCSTR filename )
{
UNICODE_STRING sectionW, entryW, def_valW, filenameW;
LPWSTR bufferW;
INT16 retW, ret = 0;
bufferW = buffer ? HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)) : NULL;
if (section) RtlCreateUnicodeStringFromAsciiz(§ionW, section);
else sectionW.Buffer = NULL;
if (entry) RtlCreateUnicodeStringFromAsciiz(&entryW, entry);
else entryW.Buffer = NULL;
if (def_val) RtlCreateUnicodeStringFromAsciiz(&def_valW, def_val);
else def_valW.Buffer = NULL;
if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
else filenameW.Buffer = NULL;
retW = PROFILE_GetPrivateProfileString( sectionW.Buffer, entryW.Buffer,
def_valW.Buffer, bufferW, len,
filenameW.Buffer, FALSE );
if (len)
{
ret = WideCharToMultiByte(CP_ACP, 0, bufferW, retW + 1, buffer, len, NULL, NULL);
if (!ret)
{
ret = len - 1;
buffer[ret] = 0;
}
else
ret--; /* strip terminating 0 */
}
RtlFreeUnicodeString(§ionW);
RtlFreeUnicodeString(&entryW);
RtlFreeUnicodeString(&def_valW);
RtlFreeUnicodeString(&filenameW);
HeapFree(GetProcessHeap(), 0, bufferW);
return ret;
}
/***********************************************************************
* GetPrivateProfileStringA (KERNEL32.@)
*/
INT WINAPI GetPrivateProfileStringA( LPCSTR section, LPCSTR entry,
LPCSTR def_val, LPSTR buffer,
UINT len, LPCSTR filename )
{
UNICODE_STRING sectionW, entryW, def_valW, filenameW;
LPWSTR bufferW;
INT retW, ret = 0;
bufferW = buffer ? HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)) : NULL;
if (section) RtlCreateUnicodeStringFromAsciiz(§ionW, section);
else sectionW.Buffer = NULL;
if (entry) RtlCreateUnicodeStringFromAsciiz(&entryW, entry);
else entryW.Buffer = NULL;
if (def_val) RtlCreateUnicodeStringFromAsciiz(&def_valW, def_val);
else def_valW.Buffer = NULL;
if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
else filenameW.Buffer = NULL;
retW = GetPrivateProfileStringW( sectionW.Buffer, entryW.Buffer,
def_valW.Buffer, bufferW, len,
filenameW.Buffer);
if (len)
{
ret = WideCharToMultiByte(CP_ACP, 0, bufferW, retW + 1, buffer, len, NULL, NULL);
if (!ret)
{
ret = len - 1;
buffer[ret] = 0;
}
else
ret--; /* strip terminating 0 */
}
RtlFreeUnicodeString(§ionW);
RtlFreeUnicodeString(&entryW);
RtlFreeUnicodeString(&def_valW);
RtlFreeUnicodeString(&filenameW);
HeapFree(GetProcessHeap(), 0, bufferW);
return ret;
}
/***********************************************************************
* GetPrivateProfileStringW (KERNEL32.@)
*/
INT WINAPI GetPrivateProfileStringW( LPCWSTR section, LPCWSTR entry,
LPCWSTR def_val, LPWSTR buffer,
UINT len, LPCWSTR filename )
{
TRACE("(%s, %s, %s, %p, %d, %s)\n", debugstr_w(section), debugstr_w(entry), debugstr_w(def_val), buffer, len, debugstr_w(filename));
return PROFILE_GetPrivateProfileString( section, entry, def_val,
buffer, len, filename, TRUE );
}
/***********************************************************************
* GetProfileStringA (KERNEL32.@)
*/
INT WINAPI GetProfileStringA( LPCSTR section, LPCSTR entry, LPCSTR def_val,
LPSTR buffer, UINT len )
{
return GetPrivateProfileStringA( section, entry, def_val,
buffer, len, "win.ini" );
}
/***********************************************************************
* GetProfileStringW (KERNEL32.@)
*/
INT WINAPI GetProfileStringW( LPCWSTR section, LPCWSTR entry,
LPCWSTR def_val, LPWSTR buffer, UINT len )
{
return GetPrivateProfileStringW( section, entry, def_val,
buffer, len, wininiW );
}
/***********************************************************************
* WriteProfileStringA (KERNEL32.@)
*/
BOOL WINAPI WriteProfileStringA( LPCSTR section, LPCSTR entry,
LPCSTR string )
{
return WritePrivateProfileStringA( section, entry, string, "win.ini" );
}
/***********************************************************************
* WriteProfileStringW (KERNEL32.@)
*/
BOOL WINAPI WriteProfileStringW( LPCWSTR section, LPCWSTR entry,
LPCWSTR string )
{
return WritePrivateProfileStringW( section, entry, string, wininiW );
}
/***********************************************************************
* GetPrivateProfileIntW (KERNEL32.@)
*/
UINT WINAPI GetPrivateProfileIntW( LPCWSTR section, LPCWSTR entry,
INT def_val, LPCWSTR filename )
{
WCHAR buffer[30];
UNICODE_STRING bufferW;
INT len;
ULONG result;
if (!(len = GetPrivateProfileStringW( section, entry, emptystringW,
buffer, sizeof(buffer)/sizeof(WCHAR),
filename )))
return def_val;
/* FIXME: if entry can be found but it's empty, then Win16 is
* supposed to return 0 instead of def_val ! Difficult/problematic
* to implement (every other failure also returns zero buffer),
* thus wait until testing framework avail for making sure nothing
* else gets broken that way. */
if (!buffer[0]) return (UINT)def_val;
RtlInitUnicodeString( &bufferW, buffer );
RtlUnicodeStringToInteger( &bufferW, 0, &result);
return result;
}
/***********************************************************************
* GetPrivateProfileIntA (KERNEL32.@)
*
* FIXME: rewrite using unicode
*/
UINT WINAPI GetPrivateProfileIntA( LPCSTR section, LPCSTR entry,
INT def_val, LPCSTR filename )
{
UNICODE_STRING entryW, filenameW, sectionW;
UINT res;
if(entry) RtlCreateUnicodeStringFromAsciiz(&entryW, entry);
else entryW.Buffer = NULL;
if(filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
else filenameW.Buffer = NULL;
if(section) RtlCreateUnicodeStringFromAsciiz(§ionW, section);
else sectionW.Buffer = NULL;
res = GetPrivateProfileIntW(sectionW.Buffer, entryW.Buffer, def_val,
filenameW.Buffer);
RtlFreeUnicodeString(§ionW);
RtlFreeUnicodeString(&filenameW);
RtlFreeUnicodeString(&entryW);
return res;
}
/***********************************************************************
* GetPrivateProfileSectionW (KERNEL32.@)
*/
INT WINAPI GetPrivateProfileSectionW( LPCWSTR section, LPWSTR buffer,
DWORD len, LPCWSTR filename )
{
int ret = 0;
if (!section || !buffer)
{
SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
TRACE("(%s, %p, %d, %s)\n", debugstr_w(section), buffer, len, debugstr_w(filename));
RtlEnterCriticalSection( &PROFILE_CritSect );
if (PROFILE_Open( filename, FALSE ))
ret = PROFILE_GetSection(CurProfile->section, section, buffer, len, TRUE, FALSE);
RtlLeaveCriticalSection( &PROFILE_CritSect );
return ret;
}
/***********************************************************************
* GetPrivateProfileSectionA (KERNEL32.@)
*/
INT WINAPI GetPrivateProfileSectionA( LPCSTR section, LPSTR buffer,
DWORD len, LPCSTR filename )
{
UNICODE_STRING sectionW, filenameW;
LPWSTR bufferW;
INT retW, ret = 0;
if (!section || !buffer)
{
SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}
bufferW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
RtlCreateUnicodeStringFromAsciiz(§ionW, section);
if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
else filenameW.Buffer = NULL;
retW = GetPrivateProfileSectionW(sectionW.Buffer, bufferW, len, filenameW.Buffer);
if (len > 2)
{
ret = WideCharToMultiByte(CP_ACP, 0, bufferW, retW + 1, buffer, len, NULL, NULL);
if (ret > 2)
ret -= 1;
else
{
ret = 0;
buffer[len-2] = 0;
buffer[len-1] = 0;
}
}
else
{
buffer[0] = 0;
buffer[1] = 0;
}
RtlFreeUnicodeString(§ionW);
RtlFreeUnicodeString(&filenameW);
HeapFree(GetProcessHeap(), 0, bufferW);
return ret;
}
/***********************************************************************
* GetProfileSectionA (KERNEL32.@)
*/
INT WINAPI GetProfileSectionA( LPCSTR section, LPSTR buffer, DWORD len )
{
return GetPrivateProfileSectionA( section, buffer, len, "win.ini" );
}
/***********************************************************************
* GetProfileSectionW (KERNEL32.@)
*/
INT WINAPI GetProfileSectionW( LPCWSTR section, LPWSTR buffer, DWORD len )
{
return GetPrivateProfileSectionW( section, buffer, len, wininiW );
}
/***********************************************************************
* WritePrivateProfileStringW (KERNEL32.@)
*/
BOOL WINAPI WritePrivateProfileStringW( LPCWSTR section, LPCWSTR entry,
LPCWSTR string, LPCWSTR filename )
{
BOOL ret = FALSE;
RtlEnterCriticalSection( &PROFILE_CritSect );
if (!section && !entry && !string) /* documented "file flush" case */
{
if (!filename || PROFILE_Open( filename, TRUE ))
{
if (CurProfile) PROFILE_ReleaseFile(); /* always return FALSE in this case */
}
}
else if (PROFILE_Open( filename, TRUE ))
{
if (!section) {
FIXME("(NULL?,%s,%s,%s)?\n",
debugstr_w(entry), debugstr_w(string), debugstr_w(filename));
} else {
ret = PROFILE_SetString( section, entry, string, FALSE);
PROFILE_FlushFile();
}
}
RtlLeaveCriticalSection( &PROFILE_CritSect );
return ret;
}
/***********************************************************************
* WritePrivateProfileStringA (KERNEL32.@)
*/
BOOL WINAPI WritePrivateProfileStringA( LPCSTR section, LPCSTR entry,
LPCSTR string, LPCSTR filename )
{
UNICODE_STRING sectionW, entryW, stringW, filenameW;
BOOL ret;
if (section) RtlCreateUnicodeStringFromAsciiz(§ionW, section);
else sectionW.Buffer = NULL;
if (entry) RtlCreateUnicodeStringFromAsciiz(&entryW, entry);
else entryW.Buffer = NULL;
if (string) RtlCreateUnicodeStringFromAsciiz(&stringW, string);
else stringW.Buffer = NULL;
if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
else filenameW.Buffer = NULL;
ret = WritePrivateProfileStringW(sectionW.Buffer, entryW.Buffer,
stringW.Buffer, filenameW.Buffer);
RtlFreeUnicodeString(§ionW);
RtlFreeUnicodeString(&entryW);
RtlFreeUnicodeString(&stringW);
RtlFreeUnicodeString(&filenameW);
return ret;
}
/***********************************************************************
* WritePrivateProfileSectionW (KERNEL32.@)
*/
BOOL WINAPI WritePrivateProfileSectionW( LPCWSTR section,
LPCWSTR string, LPCWSTR filename )
{
BOOL ret = FALSE;
LPWSTR p;
RtlEnterCriticalSection( &PROFILE_CritSect );
if (!section && !string)
{
if (!filename || PROFILE_Open( filename, TRUE ))
{
if (CurProfile) PROFILE_ReleaseFile(); /* always return FALSE in this case */
}
}
else if (PROFILE_Open( filename, TRUE )) {
if (!string) {/* delete the named section*/
ret = PROFILE_SetString(section,NULL,NULL, FALSE);
PROFILE_FlushFile();
} else {
PROFILE_DeleteAllKeys(section);
ret = TRUE;
while(*string) {
LPWSTR buf = HeapAlloc( GetProcessHeap(), 0, (strlenW(string)+1) * sizeof(WCHAR) );
strcpyW( buf, string );
if((p = strchrW( buf, '='))) {
*p='\0';
ret = PROFILE_SetString( section, buf, p+1, TRUE);
}
HeapFree( GetProcessHeap(), 0, buf );
string += strlenW(string)+1;
}
PROFILE_FlushFile();
}
}
RtlLeaveCriticalSection( &PROFILE_CritSect );
return ret;
}
/***********************************************************************
* WritePrivateProfileSectionA (KERNEL32.@)
*/
BOOL WINAPI WritePrivateProfileSectionA( LPCSTR section,
LPCSTR string, LPCSTR filename)
{
UNICODE_STRING sectionW, filenameW;
LPWSTR stringW;
BOOL ret;
if (string)
{
INT lenA, lenW;
LPCSTR p = string;
while(*p) p += strlen(p) + 1;
lenA = p - string + 1;
lenW = MultiByteToWideChar(CP_ACP, 0, string, lenA, NULL, 0);
if ((stringW = HeapAlloc(GetProcessHeap(), 0, lenW * sizeof(WCHAR))))
MultiByteToWideChar(CP_ACP, 0, string, lenA, stringW, lenW);
}
else stringW = NULL;
if (section) RtlCreateUnicodeStringFromAsciiz(§ionW, section);
else sectionW.Buffer = NULL;
if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
else filenameW.Buffer = NULL;
ret = WritePrivateProfileSectionW(sectionW.Buffer, stringW, filenameW.Buffer);
HeapFree(GetProcessHeap(), 0, stringW);
RtlFreeUnicodeString(§ionW);
RtlFreeUnicodeString(&filenameW);
return ret;
}
/***********************************************************************
* WriteProfileSectionA (KERNEL32.@)
*/
BOOL WINAPI WriteProfileSectionA( LPCSTR section, LPCSTR keys_n_values)
{
return WritePrivateProfileSectionA( section, keys_n_values, "win.ini");
}
/***********************************************************************
* WriteProfileSectionW (KERNEL32.@)
*/
BOOL WINAPI WriteProfileSectionW( LPCWSTR section, LPCWSTR keys_n_values)
{
return WritePrivateProfileSectionW(section, keys_n_values, wininiW);
}
/***********************************************************************
* GetPrivateProfileSectionNamesW (KERNEL32.@)
*
* Returns the section names contained in the specified file.
* FIXME: Where do we find this file when the path is relative?
* The section names are returned as a list of strings with an extra
* '\0' to mark the end of the list. Except for that the behavior
* depends on the Windows version.
*
* Win95:
* - if the buffer is 0 or 1 character long then it is as if it was of
* infinite length.
* - otherwise, if the buffer is too small only the section names that fit
* are returned.
* - note that this means if the buffer was too small to return even just
* the first section name then a single '\0' will be returned.
* - the return value is the number of characters written in the buffer,
* except if the buffer was too small in which case len-2 is returned
*
* Win2000:
* - if the buffer is 0, 1 or 2 characters long then it is filled with
* '\0' and the return value is 0
* - otherwise if the buffer is too small then the first section name that
* does not fit is truncated so that the string list can be terminated
* correctly (double '\0')
* - the return value is the number of characters written in the buffer
* except for the trailing '\0'. If the buffer is too small, then the
* return value is len-2
* - Win2000 has a bug that triggers when the section names and the
* trailing '\0' fit exactly in the buffer. In that case the trailing
* '\0' is missing.
*
* Wine implements the observed Win2000 behavior (except for the bug).
*
* Note that when the buffer is big enough then the return value may be any
* value between 1 and len-1 (or len in Win95), including len-2.
*/
DWORD WINAPI GetPrivateProfileSectionNamesW( LPWSTR buffer, DWORD size,
LPCWSTR filename)
{
DWORD ret = 0;
RtlEnterCriticalSection( &PROFILE_CritSect );
if (PROFILE_Open( filename, FALSE ))
ret = PROFILE_GetSectionNames(buffer, size);
RtlLeaveCriticalSection( &PROFILE_CritSect );
return ret;
}
/***********************************************************************
* GetPrivateProfileSectionNamesA (KERNEL32.@)
*/
DWORD WINAPI GetPrivateProfileSectionNamesA( LPSTR buffer, DWORD size,
LPCSTR filename)
{
UNICODE_STRING filenameW;
LPWSTR bufferW;
INT retW, ret = 0;
bufferW = buffer ? HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR)) : NULL;
if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
else filenameW.Buffer = NULL;
retW = GetPrivateProfileSectionNamesW(bufferW, size, filenameW.Buffer);
if (retW && size)
{
ret = WideCharToMultiByte(CP_ACP, 0, bufferW, retW+1, buffer, size-1, NULL, NULL);
if (!ret)
{
ret = size-2;
buffer[size-1] = 0;
}
else
ret = ret-1;
}
else if(size)
buffer[0] = '\0';
RtlFreeUnicodeString(&filenameW);
HeapFree(GetProcessHeap(), 0, bufferW);
return ret;
}
/***********************************************************************
* GetPrivateProfileStructW (KERNEL32.@)
*
* Should match Win95's behaviour pretty much
*/
BOOL WINAPI GetPrivateProfileStructW (LPCWSTR section, LPCWSTR key,
LPVOID buf, UINT len, LPCWSTR filename)
{
BOOL ret = FALSE;
RtlEnterCriticalSection( &PROFILE_CritSect );
if (PROFILE_Open( filename, FALSE )) {
PROFILEKEY *k = PROFILE_Find ( &CurProfile->section, section, key, FALSE, FALSE);
if (k) {
TRACE("value (at %p): %s\n", k->value, debugstr_w(k->value));
if (((strlenW(k->value) - 2) / 2) == len)
{
LPWSTR end, p;
BOOL valid = TRUE;
WCHAR c;
DWORD chksum = 0;
end = k->value + strlenW(k->value); /* -> '\0' */
/* check for invalid chars in ASCII coded hex string */
for (p=k->value; p < end; p++)
{
if (!isxdigitW(*p))
{
WARN("invalid char '%x' in file %s->[%s]->%s !\n",
*p, debugstr_w(filename), debugstr_w(section), debugstr_w(key));
valid = FALSE;
break;
}
}
if (valid)
{
BOOL highnibble = TRUE;
BYTE b = 0, val;
LPBYTE binbuf = (LPBYTE)buf;
end -= 2; /* don't include checksum in output data */
/* translate ASCII hex format into binary data */
for (p=k->value; p < end; p++)
{
c = toupperW(*p);
val = (c > '9') ?
(c - 'A' + 10) : (c - '0');
if (highnibble)
b = val << 4;
else
{
b += val;
*binbuf++ = b; /* feed binary data into output */
chksum += b; /* calculate checksum */
}
highnibble ^= 1; /* toggle */
}
/* retrieve stored checksum value */
c = toupperW(*p++);
b = ( (c > '9') ? (c - 'A' + 10) : (c - '0') ) << 4;
c = toupperW(*p);
b += (c > '9') ? (c - 'A' + 10) : (c - '0');
if (b == (chksum & 0xff)) /* checksums match ? */
ret = TRUE;
}
}
}
}
RtlLeaveCriticalSection( &PROFILE_CritSect );
return ret;
}
/***********************************************************************
* GetPrivateProfileStructA (KERNEL32.@)
*/
BOOL WINAPI GetPrivateProfileStructA (LPCSTR section, LPCSTR key,
LPVOID buffer, UINT len, LPCSTR filename)
{
UNICODE_STRING sectionW, keyW, filenameW;
INT ret;
if (section) RtlCreateUnicodeStringFromAsciiz(§ionW, section);
else sectionW.Buffer = NULL;
if (key) RtlCreateUnicodeStringFromAsciiz(&keyW, key);
else keyW.Buffer = NULL;
if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
else filenameW.Buffer = NULL;
ret = GetPrivateProfileStructW(sectionW.Buffer, keyW.Buffer, buffer, len,
filenameW.Buffer);
/* Do not translate binary data. */
RtlFreeUnicodeString(§ionW);
RtlFreeUnicodeString(&keyW);
RtlFreeUnicodeString(&filenameW);
return ret;
}
/***********************************************************************
* WritePrivateProfileStructW (KERNEL32.@)
*/
BOOL WINAPI WritePrivateProfileStructW (LPCWSTR section, LPCWSTR key,
LPVOID buf, UINT bufsize, LPCWSTR filename)
{
BOOL ret = FALSE;
LPBYTE binbuf;
LPWSTR outstring, p;
DWORD sum = 0;
if (!section && !key && !buf) /* flush the cache */
return WritePrivateProfileStringW( NULL, NULL, NULL, filename );
/* allocate string buffer for hex chars + checksum hex char + '\0' */
outstring = HeapAlloc( GetProcessHeap(), 0, (bufsize*2 + 2 + 1) * sizeof(WCHAR) );
p = outstring;
for (binbuf = (LPBYTE)buf; binbuf < (LPBYTE)buf+bufsize; binbuf++) {
*p++ = hex[*binbuf >> 4];
*p++ = hex[*binbuf & 0xf];
sum += *binbuf;
}
/* checksum is sum & 0xff */
*p++ = hex[(sum & 0xf0) >> 4];
*p++ = hex[sum & 0xf];
*p++ = '\0';
RtlEnterCriticalSection( &PROFILE_CritSect );
if (PROFILE_Open( filename, TRUE )) {
ret = PROFILE_SetString( section, key, outstring, FALSE);
PROFILE_FlushFile();
}
RtlLeaveCriticalSection( &PROFILE_CritSect );
HeapFree( GetProcessHeap(), 0, outstring );
return ret;
}
/***********************************************************************
* WritePrivateProfileStructA (KERNEL32.@)
*/
BOOL WINAPI WritePrivateProfileStructA (LPCSTR section, LPCSTR key,
LPVOID buf, UINT bufsize, LPCSTR filename)
{
UNICODE_STRING sectionW, keyW, filenameW;
INT ret;
if (section) RtlCreateUnicodeStringFromAsciiz(§ionW, section);
else sectionW.Buffer = NULL;
if (key) RtlCreateUnicodeStringFromAsciiz(&keyW, key);
else keyW.Buffer = NULL;
if (filename) RtlCreateUnicodeStringFromAsciiz(&filenameW, filename);
else filenameW.Buffer = NULL;
/* Do not translate binary data. */
ret = WritePrivateProfileStructW(sectionW.Buffer, keyW.Buffer, buf, bufsize,
filenameW.Buffer);
RtlFreeUnicodeString(§ionW);
RtlFreeUnicodeString(&keyW);
RtlFreeUnicodeString(&filenameW);
return ret;
}
/***********************************************************************
* WriteOutProfiles (KERNEL.315)
*/
void WINAPI WriteOutProfiles16(void)
{
RtlEnterCriticalSection( &PROFILE_CritSect );
PROFILE_FlushFile();
RtlLeaveCriticalSection( &PROFILE_CritSect );
}
/***********************************************************************
* CloseProfileUserMapping (KERNEL32.@)
*/
BOOL WINAPI CloseProfileUserMapping(void) {
FIXME("(), stub!\n");
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return FALSE;
}
|