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 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224
|
#pragma once
#include <atlcoll.h>
#include <atlutil.h>
#ifndef ATL_EPSILON
#define ATL_EPSILON .0001
#endif
#ifndef VALIDATION_S_OK
#define VALIDATION_S_OK 0x00000000
#endif
#ifndef VALIDATION_S_EMPTY
#define VALIDATION_S_EMPTY 0x00000001
#endif
#ifndef VALIDATION_E_PARAMNOTFOUND
#define VALIDATION_E_PARAMNOTFOUND 0x00000002
#endif
#ifndef VALIDATION_E_LENGTHMIN
#define VALIDATION_E_LENGTHMIN 0x80000083
#endif
#ifndef VALIDATION_E_LENGTHMAX
#define VALIDATION_E_LENGTHMAX 0x80000084
#endif
#ifndef VALIDATION_E_INVALIDLENGTH
#define VALIDATION_E_INVALIDLENGTH 0x80000080
#endif
#ifndef VALIDATION_E_INVALIDPARAM
#define VALIDATION_E_INVALIDPARAM 0x80000005
#endif
#ifndef VALIDATION_E_FAIL
#define VALIDATION_E_FAIL 0x80000006
#endif
#ifndef VALIDATION_SUCCEEDED
#define VALIDATION_SUCCEEDED(x) (((x == VALIDATION_S_OK) || (x == VALIDATION_S_EMPTY )))
#endif
namespace AKS
{
/*
CAksDataSet dispose des méthodes de stockage des données instantanées telles que les AddValue().
et propose plusieurs méthodes surchargées de validation de données instantanées telles que les Validate().
Elle comprend également des méthodes d'échanges des données
dans leurs types primitifs sans validation telles que les Exchange().
CAksDataSet propose aussi plusieurs méthodes de conversion
des données en nombre telles que les ConvertNumber().
CAksDataSet fournit des opérations élémentaires sur les ensembles de données
prise dans leurs globalité en surchargeant les opérateurs +, -, +=, -=, &, &=, ^, ^=.
Ce qui permet de faire l'union ou l'intersection de deux dataset ou retrancher un dataset d'un autre.
Ou bien CAksDataSet permet de cumuler plusieurs ensembles de données...
*/
// This class represents a collection of validation failures.
// Use this class in combination with CValidateObject to validate
// forms, cookies, or query strings and build up a collection of
// failures. If appropriate, use the information in the collection
// to return detailed responses to the client to help them correct the failures.
class CAksValidateContext
{
public:
enum { ATL_EMPTY_PARAMS_ARE_FAILURES = 0x00000001 };
class CAksValidateContext (__in DWORD dwFlags=0) throw()
{
m_bFailures = false;
m_dwFlags = dwFlags;
}
bool SetResultAt(__in LPCSTR szName, __in DWORD type)
{
_ATLTRY
{
if (!VALIDATION_SUCCEEDED(type) ||
(type == VALIDATION_S_EMPTY && (m_dwFlags & ATL_EMPTY_PARAMS_ARE_FAILURES)))
m_bFailures = true;
return TRUE == m_results.SetAt(szName,type);
}
_ATLCATCHALL()
{
}
return false;
}
// Call this function to add a validation result to the collection managed by this object.
// Each result is identified by a name and the type of result that occurred.
// The result codes are the VALIDATION_ codes defined at the top of this file.
// The bOnlyFailure parameter below is used to only allow failure results to
// be added to the list of failures. The reason you'd want to do this is that
// success codes should be the common case in validation routines so you can
// use bOnlyFailures to limit the number of allocations by this class's base
// map for mapping success results if you don't care about iterating successes.
bool AddResult(__in LPCSTR szName, __in DWORD type, __in bool bOnlyFailures = true) throw()
{
_ATLTRY
{
if (!VALIDATION_SUCCEEDED(type) ||
(type == VALIDATION_S_EMPTY && (m_dwFlags & ATL_EMPTY_PARAMS_ARE_FAILURES)))
m_bFailures = true;
if (!bOnlyFailures)
return TRUE == m_results.Add(szName, type); // add everything
else if (bOnlyFailures &&
(!VALIDATION_SUCCEEDED(type) ||
(type == VALIDATION_S_EMPTY && (m_dwFlags & ATL_EMPTY_PARAMS_ARE_FAILURES))))
return TRUE == m_results.Add(szName, type); // only add failures
}
_ATLCATCHALL()
{
}
return false;
}
// Returns true if there are no validation failures in the collection,
// returns false otherwise.
__checkReturn bool ParamsOK() throw()
{
return !m_bFailures;
}
// Returns the number of validation results in the collection.
__checkReturn int GetResultCount() throw()
{
return m_results.GetSize();
}
// Call this function to retrieve the name and type of a
// validation result based on its index in the collection.
// Returns true on success, false on failure.
//
// i The index of a result managed by this collection.
//
// strName On success, the name of the result with index i.
//
// type On success, the type of the result with index i.
__checkReturn bool GetResultAt(__in int i, __out CStringA& strName, __out DWORD& type) throw()
{
if ( i >= 0 && i < m_results.GetSize())
{
_ATLTRY
{
strName = m_results.GetKeyAt(i);
type = m_results.GetValueAt(i);
}
_ATLCATCHALL()
{
return false;
}
return true;
}
return false;
}
DWORD m_dwFlags;
protected:
CSimpleMap<CStringA, DWORD> m_results;
bool m_bFailures;
}; // CAksValidateContext
class CAksValidator
{
public:
template <class T, class TCompType>
static DWORD Validate(
__in T value,
__in TCompType nMinValue,
__in TCompType nMaxValue) throw()
{
DWORD dwRet = VALIDATION_S_OK;
if (value < static_cast<T>(nMinValue))
dwRet = VALIDATION_E_LENGTHMIN;
else if (value > static_cast<T>(nMaxValue))
dwRet = VALIDATION_E_LENGTHMAX;
return dwRet;
}
static DWORD Validate( __in LPCSTR pszValue, __in int nMinChars, __in int nMaxChars) throw()
{
DWORD dwRet = VALIDATION_S_OK;
if(!pszValue)
{
return VALIDATION_E_FAIL;
}
int nChars = (int) strlen(pszValue);
if (nChars < nMinChars)
dwRet = VALIDATION_E_LENGTHMIN;
else if (nChars > nMaxChars)
dwRet = VALIDATION_E_LENGTHMAX;
return dwRet;
}
static DWORD Validate( __in double dblValue, __in double dblMinValue, __in double dblMaxValue) throw()
{
DWORD dwRet = VALIDATION_S_OK;
if ( dblValue < (dblMinValue - ATL_EPSILON) )
dwRet = VALIDATION_E_LENGTHMIN;
else if ( dblValue > (dblMaxValue + ATL_EPSILON) )
dwRet = VALIDATION_E_LENGTHMAX;
return dwRet;
}
}; // CAksValidator
// This class provides functions for retrieving and validating named values.
//
// The named values are expected to be provided in string form by the class used as
// the template parameter. CValidateObject provides the means of
// retrieving these values converted to data types chosen by you. You can validate the values
// by specifying a range for numeric values or by specifying a minimum and maximum length
// for string values.
//
// Call one of the Exchange overloads to retrieve a named value converted to your chosen data type.
// Call one of the Validate overloads to retrieve a named value converted to your chosen data type
// and validated against a minimum and maximum value or length supplied by you.
//
// To add validation functionality to the class TLookupClass, derive that class from CValidateObject<TLookupClass>
// and provide a Lookup function that takes a name as a string and returns the corresponding value
// also as a string:
// LPCSTR Lookup(LPCSTR szName);
template <class TLookupClass, class TValidator = CAksValidator>
class CAksValidateObject
{
public:
// Exchange Routines
// Call this function to retrieve a named value converted to your chosen data type.
// Returns one of the following validation status codes:
// VALIDATION_S_OK The named value was found and could be converted successfully
// VALIDATION_S_EMPTY The name was present, but the value was empty
// VALIDATION_E_PARAMNOTFOUND The named value was not found
// VALIDATION_E_INVALIDPARAM The name was present, but the value could not be converted to the requested data type
// VALIDATION_E_FAIL An unspecified error occurred
// Pass a pointer to a validation context object if you want to add
// failures to the collection managed by that object.
template <class T>
ATL_NOINLINE __checkReturn DWORD Exchange(
__in LPCSTR szParam,
__out T* pValue,
__inout_opt CAksValidateContext *pContext = NULL) const throw()
{
DWORD dwRet = VALIDATION_E_PARAMNOTFOUND;
if (pValue)
{
_ATLTRY
{
const TLookupClass *pT = static_cast<const TLookupClass*>(this);
LPCSTR szValue = pT->Lookup(szParam);
if (szValue)
{
if (*szValue=='\0')
dwRet = VALIDATION_S_EMPTY;
else
{
dwRet = ConvertNumber(szValue, pValue);
}
}
}
_ATLCATCHALL()
{
return VALIDATION_E_FAIL;
}
}
else
dwRet = VALIDATION_E_FAIL; // invalid input
if (pContext)
pContext->AddResult(szParam, dwRet);
return dwRet;
}
template<>
ATL_NOINLINE __checkReturn DWORD Exchange(
__in LPCSTR szParam,
__out_opt CStringA* pstrValue,
__in_opt CAksValidateContext *pContext) const throw()
{
_ATLTRY
{
LPCSTR pszValue = NULL;
DWORD dwRet = VALIDATION_E_PARAMNOTFOUND;
if (pstrValue)
{
dwRet = Exchange(szParam, &pszValue, pContext);
if (VALIDATION_SUCCEEDED(dwRet) && pstrValue != NULL)
*pstrValue = CA2T(pszValue);
}
else
{
dwRet = VALIDATION_E_FAIL; // invalid input
if (pContext)
pContext->AddResult(szParam, dwRet);
}
return dwRet;
}
_ATLCATCHALL()
{
return VALIDATION_E_FAIL;
}
}
template<>
ATL_NOINLINE __checkReturn DWORD Exchange(
__in LPCSTR szParam,
__deref_out_opt LPCSTR* ppszValue,
__inout_opt CAksValidateContext *pContext) const throw()
{
DWORD dwRet = VALIDATION_E_PARAMNOTFOUND;
if (ppszValue)
{
_ATLTRY
{
*ppszValue = NULL;
const TLookupClass *pT = static_cast<const TLookupClass*>(this);
LPCSTR szValue = pT->Lookup(szParam);
if (szValue)
{
if (*szValue=='\0')
dwRet = VALIDATION_S_EMPTY;
else
{
*ppszValue = szValue;
dwRet = VALIDATION_S_OK;
}
}
}
_ATLCATCHALL()
{
return VALIDATION_E_FAIL;
}
}
else
dwRet = VALIDATION_E_FAIL; // invalid input
if (pContext)
pContext->AddResult(szParam, dwRet);
return dwRet;
}
template<>
ATL_NOINLINE __checkReturn DWORD Exchange(
__in LPCSTR szParam,
__out GUID* pValue,
__inout_opt CAksValidateContext *pContext) const throw()
{
DWORD dwRet = VALIDATION_E_PARAMNOTFOUND;
if (pValue)
{
_ATLTRY
{
const TLookupClass *pT = static_cast<const TLookupClass*>(this);
LPCSTR szValue = pT->Lookup(szParam);
if (szValue)
{
if (*szValue=='\0')
dwRet = VALIDATION_S_EMPTY;
else
{
if (S_OK != CLSIDFromString(CA2W(szValue), pValue))
{
dwRet = VALIDATION_E_INVALIDPARAM;
}
else
dwRet = VALIDATION_S_OK;
}
}
}
_ATLCATCHALL()
{
return VALIDATION_E_FAIL;
}
}
else
dwRet = VALIDATION_E_FAIL; // invalid input
if (pContext)
pContext->AddResult(szParam, dwRet);
return dwRet;
}
template<>
ATL_NOINLINE __checkReturn DWORD Exchange(
__in LPCSTR szParam,
__out bool* pbValue,
__inout_opt CAksValidateContext *pContext) const throw()
{
DWORD dwRet = VALIDATION_S_OK;
if (pbValue)
{
_ATLTRY
{
const TLookupClass *pT = static_cast<const TLookupClass*>(this);
LPCSTR szValue = pT->Lookup(szParam);
*pbValue = false;
if (szValue)
{
if (*szValue != '\0')
*pbValue = true;
}
}
_ATLCATCHALL()
{
return VALIDATION_E_FAIL;
}
}
else
dwRet = VALIDATION_E_FAIL; // invalid input
if (pContext)
pContext->AddResult(szParam, dwRet);
return dwRet;
}
__checkReturn DWORD ConvertNumber(__in LPCSTR szVal, __out ULONGLONG *pnVal) const throw()
{
if (!szVal)
return VALIDATION_E_FAIL;
ATLASSERT(pnVal);
if (!pnVal)
return VALIDATION_E_FAIL;
char *pEnd = NULL;
ULONGLONG n = 0;
errno_t errnoValue = AtlStrToNum(&n, szVal, &pEnd, 10);
if (pEnd == szVal || errnoValue == ERANGE)
{
return VALIDATION_E_INVALIDPARAM;
}
*pnVal = n;
return VALIDATION_S_OK;
}
__checkReturn DWORD ConvertNumber(__in LPCSTR szVal, __out LONGLONG *pnVal) const throw()
{
if (!szVal)
return VALIDATION_E_FAIL;
ATLASSERT(pnVal);
if (!pnVal)
return VALIDATION_E_FAIL;
char *pEnd = NULL;
LONGLONG n = 0;
errno_t errnoValue = AtlStrToNum(&n, szVal, &pEnd, 10);
if (pEnd == szVal || errnoValue == ERANGE)
{
return VALIDATION_E_INVALIDPARAM;
}
*pnVal = n;
return VALIDATION_S_OK;
}
__checkReturn DWORD ConvertNumber(__in LPCSTR szVal, __out double *pdblVal) const throw()
{
if (!szVal)
return VALIDATION_E_FAIL;
ATLASSERT(pdblVal);
if (!pdblVal)
return VALIDATION_E_FAIL;
char *pEnd = NULL;
double d = 0.0;
errno_t errnoValue = AtlStrToNum(&d, szVal, &pEnd);
if (pEnd == szVal || errnoValue == ERANGE)
{
return VALIDATION_E_INVALIDPARAM;
}
*pdblVal = d;
return VALIDATION_S_OK;
}
__checkReturn DWORD ConvertNumber(__in LPCSTR szVal, __out int *pnVal) const throw()
{
return ConvertNumber(szVal, (long*)pnVal);
}
__checkReturn DWORD ConvertNumber(__in LPCSTR szVal, __out unsigned int *pnVal) const throw()
{
return ConvertNumber(szVal, (unsigned long*)pnVal);
}
__checkReturn DWORD ConvertNumber(__in LPCSTR szVal, __out long *pnVal) const throw()
{
if (!szVal)
return VALIDATION_E_FAIL;
ATLASSERT(pnVal);
if (!pnVal)
return VALIDATION_E_FAIL;
char *pEnd = NULL;
long n = 0;
errno_t errnoValue = AtlStrToNum(&n, szVal, &pEnd, 10);
if (pEnd == szVal || errnoValue == ERANGE)
{
return VALIDATION_E_INVALIDPARAM;
}
*pnVal = n;
return VALIDATION_S_OK;
}
__checkReturn DWORD ConvertNumber(__in LPCSTR szVal, __out unsigned long *pnVal) const throw()
{
if (!szVal)
return VALIDATION_E_FAIL;
ATLASSERT(pnVal);
if (!pnVal)
return VALIDATION_E_FAIL;
char *pEnd = NULL;
unsigned long n = 0;
errno_t errnoValue = AtlStrToNum(&n, szVal, &pEnd, 10);
if (pEnd == szVal || errnoValue == ERANGE)
{
return VALIDATION_E_INVALIDPARAM;
}
*pnVal = n;
return VALIDATION_S_OK;
}
__checkReturn DWORD ConvertNumber(__in LPCSTR szVal, __out short *pnVal) const throw()
{
if (!szVal)
return VALIDATION_E_FAIL;
ATLASSERT(pnVal);
if (!pnVal)
return VALIDATION_E_FAIL;
long nVal = 0;
DWORD dwRet = ConvertNumber(szVal, &nVal);
if (dwRet == VALIDATION_S_OK)
{
// clamp to the size of a short
if(nVal <= SHRT_MAX &&
nVal >= SHRT_MIN)
{
*pnVal = (short)nVal;
}
else
{
dwRet = VALIDATION_E_INVALIDPARAM;
}
}
return dwRet;
};
__checkReturn DWORD ConvertNumber(__in LPCSTR szVal, __out unsigned short *pnVal) const throw()
{
if (!szVal)
return VALIDATION_E_FAIL;
ATLASSERT(pnVal);
if (!pnVal)
return VALIDATION_E_FAIL;
unsigned long nVal = 0;
DWORD dwRet = ConvertNumber(szVal, &nVal);
if (dwRet == VALIDATION_S_OK)
{
// clamp to the size of a short
if(nVal <= USHRT_MAX &&
nVal >= 0)
{
*pnVal = (unsigned short)nVal;
}
else
{
dwRet = VALIDATION_E_INVALIDPARAM;
}
}
return dwRet;
};
// Call this function to retrieve a named value converted to your chosen data type
// and validated against a minimum and maximum value or length supplied by you.
//
// Returns one of the following validation status codes:
// VALIDATION_S_OK The named value was found and could be converted successfully
// VALIDATION_S_EMPTY The name was present, but the value was empty
// VALIDATION_E_PARAMNOTFOUND The named value was not found
// VALIDATION_E_INVALIDPARAM The name was present, but the value could not be converted to the requested data type
// VALIDATION_E_LENGTHMIN The name was present and could be converted to the requested data type, but the value was too small
// VALIDATION_E_LENGTHMAX The name was present and could be converted to the requested data type, but the value was too large
// VALIDATION_E_FAIL An unspecified error occurred
//
// Validate can be used to convert and validate name-value pairs
// such as those associated with HTTP requests (query string, form fields, or cookie values).
// The numeric specializations validate the minimum and maximum value.
// The string specializations validate the minimum and maximum length.
//
// Pass a pointer to a validation context object if you want to add
// failures to the collection managed by that object.
//
// Note that you can validate the value of a parameter without
// storing its value by passing NULL for the second parameter. However
// if you pass NULL for the second parameter, make sure you cast the NULL to a
// type so that the compiler will call the correct specialization of Validate.
template <class T, class TCompType>
ATL_NOINLINE __checkReturn DWORD Validate(
__in LPCSTR Param,
__out_opt T *pValue,
__in TCompType nMinValue,
__in TCompType nMaxValue,
__inout_opt CAksValidateContext *pContext = NULL) const throw()
{
T value;
DWORD dwRet = Exchange(Param, &value, pContext);
if ( dwRet == VALIDATION_S_OK )
{
if (pValue)
*pValue = value;
dwRet = TValidator::Validate(value, nMinValue, nMaxValue);
if (pContext && dwRet != VALIDATION_S_OK)
pContext->AddResult(Param, dwRet);
}
else if (dwRet == VALIDATION_S_EMPTY &&
!IsNullByType(nMinValue))
{
dwRet = VALIDATION_E_LENGTHMIN;
if (pContext)
{
pContext->SetResultAt(Param, VALIDATION_E_LENGTHMIN);
}
}
return dwRet;
}
// Specialization for strings. Comparison is for number of characters.
template<>
ATL_NOINLINE __checkReturn DWORD Validate(
__in LPCSTR Param,
__deref_opt_out LPCSTR* ppszValue,
__in int nMinChars,
__in int nMaxChars,
__inout_opt CAksValidateContext *pContext) const throw()
{
LPCSTR pszValue = NULL;
DWORD dwRet = Exchange(Param, &pszValue, pContext);
if (dwRet == VALIDATION_S_OK )
{
if (ppszValue)
*ppszValue = pszValue;
dwRet = TValidator::Validate(pszValue, nMinChars, nMaxChars);
if (pContext && dwRet != VALIDATION_S_OK)
pContext->AddResult(Param, dwRet);
}
else if (dwRet == VALIDATION_S_EMPTY &&
nMinChars > 0)
{
dwRet = VALIDATION_E_LENGTHMIN;
if (pContext)
{
pContext->SetResultAt(Param, VALIDATION_E_LENGTHMIN);
}
}
return dwRet;
}
// Specialization for CStringA so caller doesn't have to cast CStringA
template<>
ATL_NOINLINE __checkReturn DWORD Validate(
__in LPCSTR Param,
__out_opt CStringA* pstrValue,
__in int nMinChars,
__in int nMaxChars,
__inout_opt CAksValidateContext *pContext) const throw()
{
_ATLTRY
{
LPCSTR szValue;
DWORD dwRet = Validate(Param, &szValue, nMinChars, nMaxChars, pContext);
if (pstrValue && dwRet == VALIDATION_S_OK )
*pstrValue = szValue;
return dwRet;
}
_ATLCATCHALL()
{
return VALIDATION_E_FAIL;
}
}
// Specialization for doubles, uses a different comparison.
template<>
ATL_NOINLINE __checkReturn DWORD Validate(
__in LPCSTR Param,
__out_opt double* pdblValue,
__in double dblMinValue,
__in double dblMaxValue,
__inout_opt CAksValidateContext *pContext) const throw()
{
double dblValue;
DWORD dwRet = Exchange(Param, &dblValue, pContext);
if (dwRet == VALIDATION_S_OK)
{
if (pdblValue)
*pdblValue = dblValue;
dwRet = TValidator::Validate(dblValue, dblMinValue, dblMaxValue);
if (pContext && dwRet != VALIDATION_S_OK)
pContext->AddResult(Param, dwRet);
}
else if (dwRet == VALIDATION_S_EMPTY &&
(dblMinValue < -ATL_EPSILON ||
dblMinValue > ATL_EPSILON))
{
dwRet = VALIDATION_E_LENGTHMIN;
if (pContext)
{
pContext->SetResultAt(Param, VALIDATION_E_LENGTHMIN);
}
}
return dwRet;
}
}; // CAksValidateObject
class CAksDataSet : public CAksValidateObject<CAksDataSet>
{
typedef CAksValidateObject<CAksDataSet> baseType;
typedef CAtlMap<CStringA, CStringA, CStringElementTraits<CStringA>, CStringElementTraits<CStringA> > mapType;
// cette classe permet de considérer ou d'ignorer
// si ses valeurs chaînes sont sensibles à la case
// la clé entière commence à 1
class CAksSimpleMap : public CSimpleMap<ULONG, CStringA>
{
public:
CAksSimpleMap()
{
m_bMatchCase = false; // no case sensitive by default
}
CStringA Lookup(const ULONG& key) const // cette méthode surcharge celle de la classe de base
{
int nIndex = FindKey(key);
if(nIndex == -1)
return CStringA(); // à cause de la conversion qui doit être fournit ici
return GetValueAt(nIndex);
}
ULONG ReverseLookup(const CStringA& val) const
{
if(val.IsEmpty())
return 0; // bad key
if(m_bMatchCase)
return CSimpleMap<ULONG, CStringA>::ReverseLookup(val);
// ignoring case sentive of val and lookup its key
CStringA strNewValue(val);
int nCount = GetSize();
for(int nIndex = 0; nIndex < nCount; nIndex++)
{
CStringA strOldValue(GetValueAt(nIndex));
if(strOldValue.CompareNoCase(strNewValue) == 0)
return GetKeyAt(nIndex); // or return nIndex + 1
}
return 0; // bad key
}
public:
bool m_bMatchCase;
};
typedef CAksSimpleMap simpleMapType;
public:
CAksDataSet()
{
MatchCase(false);
}
CAksDataSet(const CAksDataSet& aDataSet)
{
*this = aDataSet;
}
~CAksDataSet()
{
}
bool MatchCase(bool bMatchCase = false)
{
bool bValue = m_Indexes.m_bMatchCase;
m_Indexes.m_bMatchCase = bMatchCase;
return bValue;
}
ATL_NOINLINE bool AddValue(CStringA strName, CStringA strValue)
{
if(strName.IsEmpty())
return false;
CStringA strOriginalName(strName);
if( !m_Indexes.m_bMatchCase )
strName.MakeUpper();
const mapType::CPair* pPair = m_Values.Lookup(strName);
if( !pPair )
{ // this is the first time !!!
if(m_Values.SetAt(strName, strValue))
{
ULONG newKey = (ULONG) m_Values.GetCount();
ATLASSERT(newKey > 0);
if(m_Indexes.Add(newKey, CStringA(strOriginalName))) // newKey begin by 1
return true;
}
}
else if(m_Values.SetAt(strName, strValue)) // we replace the existing value
{ // ReverseLookup() hold case sensitive value
ULONG Key = m_Indexes.ReverseLookup(CStringA(strOriginalName));
ATLASSERT(Key > 0);
if(m_Indexes.SetAt(Key, CStringA(strOriginalName))) // we replace the existing key
return true;
}
return false;
}
bool AddValue(CStringA strName, LPCSTR szValue)
{
if(szValue == NULL) // test l'existence mais pas le contenu du buffer szValue
return false;
return AddValue(strName, CStringA(szValue));
}
bool AddValue(CStringA strName, LPCWSTR szValue)
{
if(szValue == NULL) // test l'existence mais pas le contenu du buffer szValue
return false;
return AddValue(strName, CStringA(szValue));
}
bool AddValue(CStringA strName, BYTE* szValue)
{
if(szValue == NULL) // test l'existence mais pas le contenu du buffer szValue
return false;
return AddValue(strName, CStringA(szValue));
}
bool AddValue(CStringA strName, BSTR szValue)
{
if(szValue == NULL) // test l'existence mais pas le contenu du buffer szValue
return false;
return AddValue(strName, CStringA(szValue));
}
bool AddValue(CStringA strName, double dValue)
{
CStringA strValue;
strValue.Format("%lf", dValue);
return AddValue(strName, strValue);
}
bool AddValue(CStringA strName, float fValue)
{
CStringA strValue;
strValue.Format("%f", fValue);
return AddValue(strName, strValue);
}
bool ATL_NOINLINE AddValue(CStringA strName, ULONGLONG ui64Value)
{
CStringA strValue;
strValue.Format("%I64u", ui64Value);
return AddValue(strName, strValue);
}
bool ATL_NOINLINE AddValue(CStringA strName, LONGLONG i64Value)
{
CStringA strValue;
strValue.Format("%I64d", i64Value);
return AddValue(strName, strValue);
}
bool AddValue(CStringA strName, long unsigned int luiValue)
{
CStringA strValue;
strValue.Format("%lu", luiValue);
return AddValue(strName, strValue);
}
bool ATL_NOINLINE AddValue(CStringA strName, long lValue)
{
CStringA strValue;
strValue.Format("%ld", lValue);
return AddValue(strName, strValue);
}
bool AddValue(CStringA strName, unsigned int uiValue)
{
CStringA strValue;
strValue.Format("%u", uiValue);
return AddValue(strName, strValue);
}
bool AddValue(CStringA strName, int iValue)
{
CStringA strValue;
strValue.Format("%d", iValue);
return AddValue(strName, strValue);
}
bool AddValue(CStringA strName, short unsigned int suiValue)
{
CStringA strValue;
strValue.Format("%hu", suiValue);
return AddValue(strName, strValue);
}
bool AddValue(CStringA strName, short sValue)
{
CStringA strValue;
strValue.Format("%hd", sValue);
return AddValue(strName, strValue);
}
bool ATL_NOINLINE AddValue(CStringA strName, bool bValue)
{
CHAR szValue[2];
szValue[0] = (bValue? '1' : '\0');
szValue[1] = '\0';
return AddValue(strName, szValue);
}
bool AddValue(CStringA strName, GUID guid)
{
CComBSTR bstr(guid);
CStringA strGuid(bstr);
return AddValue(strName, strGuid);
}
bool AddValue(CStringA strName, COleDateTime oleDate)
{
DATE date = oleDate.m_dt;
return AddValue(strName, date);
}
bool AddValue(CStringA strName, char ch)
{
return AddValue(strName, CStringA(ch));
}
bool AddValue(long nName, CStringA strValue)
{
CStringA strName;
strName.Format("%ld", nName);
return AddValue(strName, strValue);
}
bool AddValue(long nName, long lValue)
{
CStringA strValue;
strValue.Format("%ld", lValue);
return AddValue(nName, strValue);
}
bool AddValue(long nName, double dValue)
{
CStringA strValue;
strValue.Format("%lf", dValue);
return AddValue(nName, strValue);
}
bool AddValue(long nName, GUID guidValue)
{
CComBSTR bstr(guidValue);
CStringA strValue(bstr);
return AddValue(nName, strValue);
}
bool AddValue(double dName, CStringA strValue)
{
CStringA strName;
strName.Format("%lf", dName);
return AddValue(strName, strValue);
}
bool AddValue(double dName, long lValue)
{
CStringA strValue;
strValue.Format("%ld", lValue);
return AddValue(dName, strValue);
}
bool AddValue(double dName, double dValue)
{
CStringA strValue;
strValue.Format("%lf", dValue);
return AddValue(dName, strValue);
}
bool AddValue(double dName, GUID guidValue)
{
CComBSTR bstr(guidValue);
CStringA strValue(bstr);
return AddValue(dName, strValue);
}
bool AddValue(GUID guidName, CStringA strValue)
{
CComBSTR bstr(guidName);
CStringA strName(bstr);
return AddValue(strName, strValue);
}
bool AddValue(GUID guidName, long lValue)
{
CStringA strValue;
strValue.Format("%ld", lValue);
return AddValue(guidName, strValue);
}
bool AddValue(GUID guidName, double dValue)
{
CStringA strValue;
strValue.Format("%lf", dValue);
return AddValue(guidName, strValue);
}
bool AddValue(GUID guidName, GUID guidValue)
{
CComBSTR bstr(guidValue);
CStringA strValue(bstr);
return AddValue(guidName, strValue);
}
bool ATL_NOINLINE AddVariant(CStringA strName, CAksVariant varValue)
{
switch(varValue.m_vtOperation)
{
case VT_I1: return AddValue(strName, CStringA((char)varValue));
case VT_I2: return AddValue(strName, (short)varValue);
case VT_I4: return AddValue(strName, (long)varValue);
case VT_CY: return AddValue(strName, ((CY)varValue).int64);
case VT_DECIMAL: return AddValue(strName, (LONGLONG)((DECIMAL)varValue).Lo64);
case VT_UI1: return AddValue(strName, (long)(BYTE)varValue);
case VT_UI2: return AddValue(strName, (unsigned short)varValue);
case VT_UI4: return AddValue(strName, (unsigned long)varValue);
case VT_INT: return AddValue(strName, (int)varValue);
case VT_UINT: return AddValue(strName, (unsigned int)varValue);
#if (_WIN32_WINNT >= 0x0501)
case VT_I8: return AddValue(strName, (__int64)varValue);
case VT_UI8: return AddValue(strName, (unsigned __int64)varValue);
#endif
case VT_R4: return AddValue(strName, (float)varValue);
case VT_R8: return AddValue(strName, (double)varValue);
case VT_DATE: return AddValue(strName, COleDateTime((DATE)varValue));
case VT_BSTR: return AddValue(strName, CStringA(((_bstr_t)varValue).GetBSTR()));
default:
return false;
}
}
bool AddVariant(long nName, CAksVariant varValue)
{
CStringA strName;
strName.Format("%ld", nName);
return AddVariant(strName, varValue);
}
bool AddVariant(double dName, CAksVariant varValue)
{
CStringA strName;
strName.Format("%lf", dName);
return AddVariant(strName, varValue);
}
bool AddVariant(GUID guidName, CAksVariant varValue)
{
CComBSTR bstr(guidName);
CStringA strName(bstr);
return AddVariant(strName, varValue);
}
ULONG ATL_NOINLINE GetCount() const throw()
{
size_t nValuesSize = m_Values.GetCount();
size_t nIndexSize = m_Indexes.GetSize();
ATLASSERT(nValuesSize == nIndexSize);
return (nValuesSize == nIndexSize)? (ULONG)nValuesSize : 0;
}
bool ATL_NOINLINE IsDataPresent(CStringA strName) const
{
if ( !GetCount() )
return false;
CStringA strOriginalName = strName;
if( !m_Indexes.m_bMatchCase )
strName.MakeUpper();
const mapType::CPair* pPair = m_Values.Lookup(strName);
if( !pPair )
return false;
// here data is present
ULONG iItem = m_Indexes.ReverseLookup(strOriginalName);
ATLASSERT(iItem != 0); // ensure that it is present
return (iItem == 0)? false: true;
}
bool IsEmpty() const
{
return (m_Values.IsEmpty() && !m_Indexes.GetSize())? true: false;
}
// toute classe dérivée de CValidateObject doit fournir ce prototype de fonction membre
LPCSTR ATL_NOINLINE Lookup(LPCSTR szName) const throw()
{
_ATLTRY
{
if (!szName)
return NULL;
if ( !m_Values.GetCount() )
return NULL;
CStringA strName = szName;
if( !m_Indexes.m_bMatchCase )
strName.MakeUpper();
const mapType::CPair* pPair = m_Values.Lookup(strName);
if (pPair)
return (LPCSTR) pPair->m_value;
}
_ATLCATCHALL()
{
}
return NULL;
}
LPCSTR Lookup(CStringA strName) const throw()
{
return Lookup(strName.GetString());
}
bool ATL_NOINLINE LookupAt(ULONG iBasedZeroIndex, CStringA& strName, CStringA& strValue) const throw()
{
_ATLTRY
{
ULONG nCount = (ULONG) m_Indexes.GetSize();
if(iBasedZeroIndex >= nCount)
return false;
strName = m_Indexes.GetValueAt(iBasedZeroIndex); // return the orignal name
if(strName.IsEmpty())
return false;
LPCSTR szValue = Lookup(strName);
if( !szValue )
return false;
strValue = szValue;
return true;
}
_ATLCATCHALL()
{
return false;
}
}
ATL_NOINLINE POSITION GetFirstPosition()
{
if(!IsEmpty())
{
m_Pos = 0;
return (POSITION) &m_Pos;
}
return NULL;
}
ATL_NOINLINE bool GetNext(POSITION& pos, CStringA& strName, CStringA& strValue) const
{
strName.Empty();
strValue.Empty();
if( !pos )
return false;
ULONG* piItem = (ULONG*) pos;
ATLASSERT(piItem);
if( !piItem )
return false;
ULONG nCount = GetCount();
if( *piItem < nCount )
{
bool bRet = LookupAt(*piItem, strName, strValue);
ATLASSERT(bRet);
if(bRet)
{
(*piItem)++;
}
}
if(*piItem >= nCount)
pos = NULL;
return true;
}
bool ATL_NOINLINE RemoveData(CStringA strName)
{
if(strName.IsEmpty())
return false;
CStringA strOriginalName = strName;
if( !m_Indexes.m_bMatchCase )
strName.MakeUpper();
ULONG Key = m_Indexes.ReverseLookup(strOriginalName);
if(Key == 0)
return false;
// On rédéfinit les valeurs associées aux indexes en mappant les valeurs
// vers leurs indexes respectifs diminués d'une unité à partir de l'élément à supprimer.
if(m_Values.RemoveKey(strName)) // on suprime l'élément de l'ensemble des valeurs
{
ULONG nCount = (ULONG) m_Indexes.GetSize();
if(nCount == 1) // s'il n'y avait qu'un seul élément
return (m_Indexes.Remove(nCount)? true: false); // on retourne en le supprimant de la table des indexes
ULONG i = Key+1; // on définit l'indexe supérieur juste après l'élément supprimé.
while(i <= nCount)
{
strName = m_Indexes.Lookup(i); // on récupère le nom d'indexe supérieur
if(strName.IsEmpty())
return false;
if( !m_Indexes.SetAt(i-1, strName) ) // qu'on place à l'indexe inférieur
return false;
i++;
}
// En fin de compte c'est le dernier élément qui est effectivement supprimé!!!
return (m_Indexes.Remove(nCount)? true: false); // on supprime le dernier élément qui cause un doublon.
}
return false;
}
void Clear()
{
CStringA strEmpty;
strEmpty.Empty();
Fill(strEmpty);
}
void RemoveAll()
{
m_Values.RemoveAll();
m_Indexes.RemoveAll();
}
void Reset()
{
RemoveAll();
MatchCase(false);
}
void ATL_NOINLINE Fill(CStringA strSameValue)
{
ULONG nItem = GetCount();
for(ULONG iItem = 0; iItem < nItem; iItem++)
{
CStringA strName;
CStringA strValue;
bool bRet = LookupAt(iItem, strName, strValue);
if(bRet)
AddValue(strName, strSameValue);
}
}
void Fill(double dValue)
{
CStringA strValue;
strValue.Format("%lf", dValue);
Fill(strValue);
}
void Fill(float fValue)
{
Fill((double) fValue);
}
void Fill(long nValue)
{
CStringA strValue;
strValue.Format("%ld", nValue);
Fill(strValue);
}
void Fill(int nValue)
{
Fill((long) nValue);
}
void ATL_NOINLINE Fill(bool bValue)
{
CHAR szValue[2];
szValue[0] = (bValue? '1' : '\0');
szValue[1] = '\0';
return Fill(CStringA(szValue));
}
bool ATL_NOINLINE Fill(GUID guid)
{
CComBSTR bstrGuid(guid);
CStringA strGuid(bstrGuid);
Fill(strGuid);
return true;
}
void Fill(COleDateTime oleDate)
{
DATE date = oleDate;
Fill(date);
}
bool ReverseKeyValue()
{
AKS::CAksDataSet aDataSet;
aDataSet.m_Indexes.m_bMatchCase = this->m_Indexes.m_bMatchCase;
ULONG nItem = GetCount();
for(ULONG iItem = 0; iItem < nItem; iItem++)
{
CStringA strName;
CStringA strValue;
bool bRet = LookupAt(iItem, strName, strValue);
ATLASSERT(bRet);
if(bRet)
aDataSet.AddValue(strValue, strName);
}
if(nItem)
*this = aDataSet;
return nItem? true: false;
}
// Exchange methods
public:
template <class T>
DWORD Exchange(LPCSTR szParam, T* pValue, CAksValidateContext *pContext = NULL) const throw()
{
CStringA strName = szParam;
if( !m_Indexes.m_bMatchCase )
strName.MakeUpper();
return baseType::Exchange<T>(strName.GetString(), pValue, pContext);
}
template <>
DWORD Exchange(LPCSTR szParam, CStringA* pstrValue, CAksValidateContext *pContext) const throw()
{
CStringA strName = szParam;
if( !m_Indexes.m_bMatchCase )
strName.MakeUpper();
return baseType::Exchange<CStringA>(strName.GetString(), pstrValue, pContext);
}
template<>
DWORD Exchange(LPCSTR szParam, LPCSTR* ppszValue, CAksValidateContext *pContext) const throw()
{
CStringA strName = szParam;
if( !m_Indexes.m_bMatchCase )
strName.MakeUpper();
return baseType::Exchange<LPCSTR>(strName.GetString(), ppszValue, pContext);
}
template<>
DWORD Exchange(LPCSTR szParam, GUID* pguidValue, CAksValidateContext *pContext) const throw()
{
CStringA strName = szParam;
if( !m_Indexes.m_bMatchCase )
strName.MakeUpper();
return baseType::Exchange<GUID>(strName.GetString(), pguidValue, pContext);
}
template<>
DWORD Exchange(LPCSTR szParam, bool* pbValue, CAksValidateContext *pContext) const throw()
{
CStringA strName = szParam;
if( !m_Indexes.m_bMatchCase )
strName.MakeUpper();
if(IsDataPresent(strName))
return baseType::Exchange<bool>(strName.GetString(), pbValue, pContext);
else
return VALIDATION_E_INVALIDPARAM;
}
template <>
DWORD ATL_NOINLINE Exchange(LPCSTR szParam, float* pValue, CAksValidateContext *pContext) const throw()
{
_ATLTRY
{
if( !pValue )
return VALIDATION_E_FAIL;
double dValue;
DWORD dwRet = Exchange(szParam, &dValue, pContext);
if (dwRet == VALIDATION_S_OK)
*pValue = (float)dValue;
return dwRet;
}
_ATLCATCHALL()
{
return VALIDATION_E_FAIL;
}
}
template <>
DWORD Exchange(LPCSTR szParam, CComBSTR* pbstrValue, CAksValidateContext *pContext) const throw()
{
_ATLTRY
{
if( !pbstrValue )
return VALIDATION_E_FAIL;
CStringA strValue;
DWORD dwRet = Exchange(szParam, &strValue, pContext);
if(dwRet == VALIDATION_S_OK)
*pbstrValue = strValue;
return dwRet;
}
_ATLCATCHALL()
{
return VALIDATION_E_FAIL;
}
}
template<>
DWORD Exchange(LPCSTR szParam, BSTR* pbstrValue, CAksValidateContext *pContext) const throw()
{
_ATLTRY
{
if( !pbstrValue )
return VALIDATION_E_FAIL;
CStringA strValue;
DWORD dwRet = Exchange(szParam, &strValue, pContext);
if(dwRet == VALIDATION_S_OK)
{
CComBSTR bstrValue(strValue);
HRESULT hr = bstrValue.CopyTo(pbstrValue);
ATLASSERT(hr == S_OK);
}
return dwRet;
}
_ATLCATCHALL()
{
return VALIDATION_E_FAIL;
}
}
template<>
DWORD Exchange(LPCSTR szParam, char* pchValue, CAksValidateContext *pContext) const throw()
{
_ATLTRY
{
if( !pchValue )
return VALIDATION_E_FAIL;
CStringA strValue;
DWORD dwRet = Exchange(szParam, &strValue, pContext);
if(dwRet == VALIDATION_S_OK)
{
*pchValue = strValue.GetAt(0);
}
return dwRet;
}
_ATLCATCHALL()
{
return VALIDATION_E_FAIL;
}
}
ATL_NOINLINE DWORD ExchangeStringT(
LPCSTR szParam,
LPSTR pszValue,
const ULONG nSize,
bool bTrunc = false,
CAksValidateContext *pContext = NULL) const throw()
{
if( pszValue == NULL || nSize == 0)
return VALIDATION_E_FAIL;
_ATLTRY
{
pszValue[0] = '\0';
CStringA strValue;
DWORD dwRet = Exchange(szParam, &strValue, pContext);
if(dwRet == VALIDATION_S_OK)
{
if(AssignString(pszValue, nSize, strValue, bTrunc) != S_OK)
dwRet = VALIDATION_E_FAIL;
}
return dwRet;
}
_ATLCATCHALL()
{
return VALIDATION_E_FAIL;
}
}
DWORD ExchangeStringT(
LPCSTR szParam,
BYTE* pbyteValue,
const ULONG nSize,
bool bTrunc = false,
CAksValidateContext *pContext = NULL) const throw()
{
return ExchangeStringT(szParam, (LPSTR) pbyteValue, nSize, bTrunc, pContext);
}
DWORD ExchangeStringT(
LPCSTR szParam,
LPWSTR pszValue,
const ULONG nSize,
bool bTrunc = false,
CAksValidateContext *pContext = NULL) const throw()
{
CStringA strValue(pszValue);
return ExchangeStringT(szParam, (LPSTR)(LPCSTR) strValue, nSize, bTrunc, pContext);
}
ATL_NOINLINE DWORD Exchange(
LPCSTR szParam,
COleDateTime* pValue,
CAksValidateContext *pContext = NULL) const throw()
{
_ATLTRY
{
if( !pValue )
return VALIDATION_E_FAIL;
DATE dt;
DWORD dwRet = Exchange(szParam, &dt, pContext);
if (dwRet == VALIDATION_S_OK)
*pValue = dt;
return dwRet;
}
_ATLCATCHALL()
{
return VALIDATION_E_FAIL;
}
}
ATL_NOINLINE DWORD ExchangeVariant(LPCSTR szParam, CAksVariant* pvarValue) const throw()
{
if( !pvarValue )
return VALIDATION_E_FAIL;
_ATLTRY
{
CStringA strValue;
DWORD dwRet = Exchange(szParam, &strValue, NULL);
if(dwRet == VALIDATION_S_OK)
*pvarValue = strValue;
return dwRet;
}
_ATLCATCHALL()
{
return VALIDATION_E_FAIL;
}
}
// Validate methods
public:
template <class T, class TCompType>
DWORD Validate(
LPCSTR szParam,
T *pValue,
TCompType nMinValue,
TCompType nMaxValue,
CAksValidateContext *pContext = NULL) const throw()
{
CStringA strName = szParam;
if( !m_Indexes.m_bMatchCase )
strName.MakeUpper();
return baseType::Validate<T, TCompType>(strName.GetString(), pValue, nMinValue, nMaxValue, pContext);
}
template<>
DWORD Validate(
LPCSTR szParam,
LPCSTR* ppszValue,
int nMinChars,
int nMaxChars,
CAksValidateContext *pContext) const throw()
{
CStringA strName = szParam;
if( !m_Indexes.m_bMatchCase )
strName.MakeUpper();
return baseType::Validate<LPCSTR, int>(strName.GetString(), ppszValue, nMinChars, nMaxChars, pContext);
}
template<>
DWORD Validate(
LPCSTR szParam,
CStringA* pstrValue,
int nMinChars,
int nMaxChars,
CAksValidateContext *pContext) const throw()
{
CStringA strName = szParam;
if( !m_Indexes.m_bMatchCase )
strName.MakeUpper();
return baseType::Validate<CStringA, int>(strName.GetString(), pstrValue, nMinChars, nMaxChars, pContext);
}
template<>
DWORD Validate(
LPCSTR szParam,
double* pdblValue,
double dblMinValue,
double dblMaxValue,
CAksValidateContext *pContext) const throw()
{
CStringA strName = szParam;
if( !m_Indexes.m_bMatchCase )
strName.MakeUpper();
return baseType::Validate<double, double>(strName.GetString(), pdblValue, dblMinValue, dblMaxValue, pContext);
}
ATL_NOINLINE DWORD Validate(
LPCSTR szParam,
LPSTR* ppszValue,
const ULONG nSize,
int nMinChars,
int nMaxChars,
CAksValidateContext* pContext = NULL) const throw()
{
if( !ppszValue || *ppszValue == NULL || nSize == 0)
return VALIDATION_E_FAIL;
CStringA strValue;
DWORD dwRet = Validate(szParam, &strValue, nMinChars, nMaxChars, pContext);
if(dwRet == VALIDATION_S_OK)
{
HRESULT hr = AssignString(*ppszValue, nSize, strValue, false);
if(hr == S_OK)
return VALIDATION_S_OK;
dwRet = VALIDATION_E_FAIL;
}
return dwRet;
}
ATL_NOINLINE DWORD ValidateString(
CStringA strParam,
int nMinChars,
int nMaxChars,
bool bAllowAlpha,
bool bAllowNumeric,
LPCSTR lpcszAdditional,
int nFirstIsAlpha,
CAksDataSet& dsFailureFlags,
CStringA strGenFailureParam,
CStringA strFailureParam) const throw()
{
CStringA strData;
DWORD dwRet = Validate(strParam, &strData, nMinChars, nMaxChars);
if ( dwRet != VALIDATION_S_OK ||
!IsParamValid(strParam, bAllowAlpha, bAllowNumeric, lpcszAdditional, nFirstIsAlpha))
{
if(dwRet == VALIDATION_S_OK)
dwRet = VALIDATION_E_INVALIDPARAM;
dsFailureFlags.AddValue(strGenFailureParam, true);
dsFailureFlags.AddValue(strFailureParam, true);
return dwRet;
}
dsFailureFlags.AddValue(strFailureParam, false);
return dwRet;
}
ATL_NOINLINE DWORD ValidateString(
CStringA strParam,
int nMinChars,
int nMaxChars,
CAksDataSet& dsFailureFlags,
CStringA strGenFailureParam,
CStringA strFailureParam) const throw()
{
CStringA strData;
DWORD dwRet = Validate(strParam, &strData, nMinChars, nMaxChars);
if ( dwRet != VALIDATION_S_OK)
{
dsFailureFlags.AddValue(strGenFailureParam, true);
dsFailureFlags.AddValue(strFailureParam, true);
return dwRet;
}
dsFailureFlags.AddValue(strFailureParam, false);
return dwRet;
}
ATL_NOINLINE DWORD ValidateEmail(
CStringA strParam,
LPCSTR lpcszAdditional,
CAksDataSet& dsFailureFlags,
CStringA strGenFailureParam,
CStringA strFailureParam) const throw()
{
DWORD dwRet = ValidateString(
strParam,
5,
50,
true,
true,
lpcszAdditional,
1,
dsFailureFlags,
strGenFailureParam,
strFailureParam);
if(dwRet != VALIDATION_S_OK)
return dwRet;
CStringA strEmail;
dwRet = Exchange(strParam, &strEmail);
ATLASSERT(dwRet == VALIDATION_S_OK);
if(dwRet != VALIDATION_S_OK)
{
dsFailureFlags.AddValue(strGenFailureParam, true);
dsFailureFlags.AddValue(strFailureParam, true);
return dwRet;
}
int nIndex = -1;
if( ( (nIndex = strEmail.Find("@", 0)) == -1 ) ||
( ++nIndex == strEmail.GetLength() ) ||
( (strEmail.Find("@", nIndex)) != -1 ) ||
( (nIndex = strEmail.Find(".", nIndex)) == -1 ) ||
( (strEmail.Find("@", 0) + 1) == nIndex) ||
( ++nIndex == strEmail.GetLength() ) ||
( (strEmail.Find(".", nIndex)) != -1 ) )
{
dsFailureFlags.AddValue(strGenFailureParam, true);
dsFailureFlags.AddValue(strFailureParam, true);
return VALIDATION_E_FAIL;
}
dsFailureFlags.AddValue(strFailureParam, false);
return VALIDATION_S_OK;
}
ATL_NOINLINE bool ValidateEmail(
CStringA strEmailParam,
LPCSTR lpcszAdditional = "@._") const throw()
{
if(strEmailParam.IsEmpty())
return false;
AKS::CAksDataSet dsFailureFlags;
DWORD dwRet = ValidateEmail(
strEmailParam,
lpcszAdditional,
dsFailureFlags,
"Email_failure",
"failure");
return (dwRet == VALIDATION_S_OK)? true: false;
}
ATL_NOINLINE DWORD ValidateBool(
CStringA strParam,
CAksDataSet& dsFailureFlags,
CStringA strGenFailureParam,
CStringA strFailureParam) const throw()
{
LPCSTR szValue = Lookup(strParam);
if(szValue == NULL)
{
dsFailureFlags.AddValue(strGenFailureParam, true);
dsFailureFlags.AddValue(strFailureParam, true);
return VALIDATION_E_INVALIDPARAM;
}
dsFailureFlags.AddValue(strFailureParam, false);
return VALIDATION_S_OK;
}
ATL_NOINLINE DWORD ValidateDate(
CStringA strDateParam,
CStringA strDayParam,
CStringA strMonthParam,
CStringA strYearParam,
int nMinYear,
int nMaxYear,
CAksDataSet& dsFailureFlags,
CStringA strGenFailureParam,
CStringA strFailureParam) const throw()
{
int day = -1, month = -1, year = -1;
DWORD dwRet = VALIDATION_E_INVALIDPARAM;
if( (dwRet = Exchange(strDayParam, &day)) != VALIDATION_S_OK ||
(dwRet = Exchange(strMonthParam, &month)) != VALIDATION_S_OK ||
(dwRet = Exchange(strYearParam, &year)) != VALIDATION_S_OK)
{
dsFailureFlags.AddValue(strGenFailureParam, true);
dsFailureFlags.AddValue(strFailureParam, true);
return dwRet;
}
if(day <= 0 || day > 31 || month <= 0 || month > 12 || year < nMinYear || year > nMaxYear)
{
dsFailureFlags.AddValue(strGenFailureParam, true);
dsFailureFlags.AddValue(strFailureParam, true);
return VALIDATION_E_INVALIDPARAM;
}
if(month == 2)
{
if(year%4 != 0)
{
if(day > 28)
{
dsFailureFlags.AddValue(strGenFailureParam, true);
dsFailureFlags.AddValue(strFailureParam, true);
return VALIDATION_E_INVALIDPARAM;
}
}
else if(day > 29)
{
dsFailureFlags.AddValue(strGenFailureParam, true);
dsFailureFlags.AddValue(strFailureParam, true);
return VALIDATION_E_INVALIDPARAM;
}
}
else if(month == 4 || month == 6 || month == 9 || month == 11)
{
if(day > 30)
{
dsFailureFlags.AddValue(strGenFailureParam, true);
dsFailureFlags.AddValue(strFailureParam, true);
return VALIDATION_E_INVALIDPARAM;
}
}
dsFailureFlags.AddValue(strFailureParam, false);
return VALIDATION_S_OK;
}
template <class T>
ATL_NOINLINE DWORD ValidateNumber(
CStringA strParam,
T nMinValue,
T nMaxValue,
CAksDataSet& dsFailureFlags,
CStringA strGenFailureParam,
CStringA strFailureParam) const throw()
{
T Value;
DWORD dwRet = Validate(strParam, &Value, nMinValue, nMaxValue);
if(dwRet != VALIDATION_S_OK)
{
dsFailureFlags.AddValue(strGenFailureParam, true);
dsFailureFlags.AddValue(strFailureParam, true);
return dwRet;
}
dsFailureFlags.AddValue(strFailureParam, false);
return dwRet;
}
// operation in CAksDataSet
public:
CAksDataSet& operator=(const CAksDataSet& aDataSet) throw() // affectation globale des ensembles de données
{
RemoveAll();
this->m_Indexes.m_bMatchCase = aDataSet.m_Indexes.m_bMatchCase;
return ((*this) += aDataSet);
}
ATL_NOINLINE CAksDataSet& operator+=(const CAksDataSet& aDataSet) throw() // union des ensembles de données
{
ULONG nItem = aDataSet.GetCount();
for(ULONG iItem = 0; iItem < nItem; iItem++)
{
CStringA strName;
CStringA strValue;
bool bRet = aDataSet.LookupAt(iItem, strName, strValue);
ATLASSERT(bRet);
if(bRet)
AddValue(strName, strValue);
}
return (*this);
}
ATL_NOINLINE CAksDataSet& operator-=(const CAksDataSet& aDataSet) throw() // soustraction des ensembles de données
{
ULONG nItem = aDataSet.GetCount();
for(ULONG iItem = 0; iItem < nItem; iItem++)
{
CStringA strName;
CStringA strValue;
bool bRet = aDataSet.LookupAt(iItem, strName, strValue);
ATLASSERT(bRet);
if(bRet)
RemoveData(strName);
}
return (*this);
}
ATL_NOINLINE CAksDataSet& operator&=(const CAksDataSet& aDataSet) throw() // intersection des ensembles de données
{
CAksDataSet dsResult;
bool bSaveMatchValue = this->MatchCase();
dsResult.MatchCase(bSaveMatchValue);
this->MatchCase(bSaveMatchValue);
ULONG nItem = aDataSet.GetCount();
for(ULONG iItem = 0; iItem < nItem; iItem++)
{
CStringA strName;
CStringA strValue;
bool bRet = aDataSet.LookupAt(iItem, strName, strValue);
ATLASSERT(bRet);
if(bRet)
{
bRet = this->IsDataPresent(strName);
if(bRet)
dsResult.AddValue(strName, strValue);
}
}
*this = dsResult;
return (*this);
}
ATL_NOINLINE CAksDataSet& operator^=(const CAksDataSet& aDataSet) throw() // l'union exclusif des ensembles de données
{
CAksDataSet dsUnion(*this);
CAksDataSet dsIntersection(*this);
dsUnion += aDataSet;
dsIntersection &= aDataSet;
*this = dsUnion - dsIntersection;
return (*this);
}
friend CAksDataSet operator+(const CAksDataSet& aDataSet1, const CAksDataSet& aDataSet2) // union des ensembles de données
{
CAksDataSet aDataSetResult = aDataSet1;
aDataSetResult += aDataSet2;
return aDataSetResult;
}
friend CAksDataSet operator-(const CAksDataSet& aDataSet1, const CAksDataSet& aDataSet2) // soustraction des ensembles de données
{
CAksDataSet aDataSetResult = aDataSet1;
aDataSetResult -= aDataSet2;
return aDataSetResult;
}
friend CAksDataSet operator&(const CAksDataSet& aDataSet1, const CAksDataSet& aDataSet2) // intersection des ensembles de données
{
CAksDataSet aDataSetResult = aDataSet1;
aDataSetResult &= aDataSet2;
return aDataSetResult;
}
friend CAksDataSet operator^(const CAksDataSet& aDataSet1, const CAksDataSet& aDataSet2) // l'union exclusif des ensembles de données
{
CAksDataSet aDataSetResult = aDataSet1;
aDataSetResult ^= aDataSet2;
return aDataSetResult;
}
ATL_NOINLINE bool operator==(const CAksDataSet& aDataSet) const throw() // équivalence des ensembles de données
{
ULONG nItem = aDataSet.GetCount();
if(this->GetCount() != nItem)
return false;
for(ULONG iItem = 0; iItem < nItem; iItem++)
{
CStringA strName;
CStringA strValue;
bool bRet = aDataSet.LookupAt(iItem, strName, strValue);
ATLASSERT(bRet);
if(bRet)
{
if( !this->IsDataPresent(strName) )
return false;
CStringA strThisValue = this->Lookup(strName);
if(strThisValue.CompareNoCase(strValue))
return false;
}
}
return true;
}
ATL_NOINLINE bool operator!=(const CAksDataSet& aDataSet) const throw() // différence des ensembles de données
{
return !(*this == aDataSet);
}
// helpers
public:
static CAksDataSet* CastToAksDataSet(VARIANT varDataSet)
{
return CCastingVariant<CAksDataSet>::CastFromVariant(varDataSet);
}
static VARIANT CastToVariant(CAksDataSet* pAksDataSet)
{
return CCastingVariant<CAksDataSet>::CastToVariant(pAksDataSet);
}
ATL_NOINLINE bool IsParamValid(
CStringA strParam,
bool bAllowAlpha = true,
bool bAllowNumeric = true,
LPCSTR lpcszAdditional = NULL,
int nFirstIsAlpha = 1) const throw()
{
_ATLTRY
{
CStringA strValue;
if(Exchange(strParam, &strValue) != VALIDATION_S_OK)
return false;
return IsStringValid(strValue.GetString(), bAllowAlpha, bAllowNumeric, lpcszAdditional, nFirstIsAlpha);
}
_ATLCATCHALL()
{
return false;
}
}
ATL_NOINLINE static bool IsStringValid(
LPCSTR lpcszString,
bool bAllowAlpha = true,
bool bAllowNumeric = true,
LPCSTR lpcszAdditional = NULL,
int nFirstIsAlpha = 1)
{
if (lpcszString == NULL)
return false;
int len = lstrlenA(lpcszString);
int len_additional = (lpcszAdditional == NULL)? 0: lstrlenA(lpcszAdditional);
int i = -1, j = -1;
if(bAllowAlpha && bAllowNumeric)
{
if(nFirstIsAlpha == 1)
{
if( (lpcszString[0] < 'A' || lpcszString[0] > 'Z') && (lpcszString[0] < 'a' || lpcszString[0] > 'z') )
return false;
}
else if(nFirstIsAlpha == 0)
{
if(lpcszString[0] < '0' || lpcszString[0] > '9')
return false;
}
}
// le premier et le dernier caractère ne doivent pas être un espace
if(lpcszString[0] == ' ' || lpcszString[len-1] == ' ')
return false;
for (i = 0; i < len; i++)
{
if(bAllowAlpha)
{
if(lpcszString[i] >= 'A' && lpcszString[i] <= 'Z')
continue;
if(lpcszString[i] >= 'a' && lpcszString[i] <= 'z')
continue;
}
if(bAllowNumeric)
{
if(lpcszString[i] >= '0' && lpcszString[i] <= '9')
continue;
}
for (j = 0; j < len_additional; j++)
{
if (lpcszAdditional[j] == lpcszString[i])
break;
}
if (j >= len_additional)
break;
}
return (i < len) ? false : true;
}
ATL_NOINLINE static HRESULT AssignString(LPSTR pDataDest, const ULONG nSizeDest, LPCSTR pDataSrc, bool bTrunc)
{
if(pDataDest == NULL || pDataSrc == NULL || nSizeDest == 0)
return E_POINTER;
pDataDest[0] = '\0';
ULONG nLength = (ULONG) lstrlenA(pDataSrc);
if(nLength == 0)
return S_OK; // pDataSrc was empty so pDataDest will be empty!!!
if(nLength >= nSizeDest)
{
if( bTrunc )
nLength = nSizeDest - 1;
else
return E_FAIL;
}
if(nLength == 0)
return S_OK;
if(lstrcpynA(pDataDest, pDataSrc, nLength+1))
{
pDataDest[nLength] = '\0';
return S_OK;
}
return E_FAIL;
}
static HRESULT AssignString(BSTR* pbstrDataDest, LPCSTR pDataSrc)
{
if( !pbstrDataDest || !pDataSrc )
return E_POINTER;
CComBSTR bstr(pDataSrc);
return bstr.CopyTo(pbstrDataDest);
}
ATL_NOINLINE static HRESULT ConvertClsidToString(GUID guid, CStringA& strGuid)
{
CComBSTR bstrGuid(guid);
CStringA strTGuid(bstrGuid);
strGuid = strTGuid;
return S_OK;
}
ATL_NOINLINE static HRESULT ConvertClsidToString(GUID guid, BSTR* pbstrGuid)
{
if( !pbstrGuid )
return E_POINTER;
CComBSTR bstr(guid);
return bstr.CopyTo(pbstrGuid);
}
ATL_NOINLINE static HRESULT ConvertStringToClsid(CStringA strGuid, GUID* pGuid)
{
if(strGuid.IsEmpty() || pGuid == NULL)
return E_POINTER;
CStringW wstrGUID(strGuid);
if (FAILED(::CLSIDFromString(wstrGUID.GetBuffer(), pGuid)))
return E_FAIL;
wstrGUID.ReleaseBuffer();
return S_OK;
}
ATL_NOINLINE static HRESULT ConvertStringToClsid(BSTR bstrGuid, GUID* pGuid)
{
return ConvertStringToClsid(CStringA(bstrGuid), pGuid);
}
ATL_NOINLINE static bool TestResults(bool* pbResults, int nItem)
{
if( !pbResults || nItem <= 0)
return false;
for(int iItem = 0; iItem < nItem; iItem++)
{
if( !pbResults[iItem] )
return false;
}
return true;
}
ATL_NOINLINE static bool TestResults(CAtlArray<bool>& aResultArray)
{
ULONG nCount = (ULONG) aResultArray.GetCount();
if(nCount == 0)
return false;
for(ULONG i = 0; i < nCount; i++)
{
if( !aResultArray[i] )
return false;
}
return true;
}
template<typename T> // T = HRESULT or T = DWORD
ATL_NOINLINE static bool TestResults(T* pdwResults, int nItem, int nSuccessCode = 0)
{
if( !pdwResults || nItem <= 0)
return false;
for(int iItem = 0; iItem < nItem; iItem++)
{
if( pdwResults[iItem] != nSuccessCode)
return false;
}
return true;
}
private:
mapType m_Values;
simpleMapType m_Indexes;
ULONG m_Pos;
}; // class CAksDataSet
} // namespace AKS |
Partager