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 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418
|
/* Copyright (C) 2005 MySQL AB
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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "myx_util.h"
#include "myx_shared_util_functions.h"
#include "entities.h"
// Windows includes
#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#include <direct.h>
#else
// unix/linux includes
#include <sys/time.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/utsname.h> // uname()
#include <fcntl.h>
#define SIZE_T size_t
# include <sys/types.h>
# include <sys/stat.h>
# include <unistd.h>
#endif
// MacOS X
#if defined(__APPLE__) && defined(__MACH__)
#include <sys/sysctl.h>
#include <mach/machine.h>
#endif
#include "myx_util_functions.h"
//----------------------------------------------------------------------------------------------------------------------
/* Transforms a string consisting of
* 2byte hexadecimal characters into
* a binary string.
*
* If ret_str_len is not NULL it will be set
* to the length of the resulting string.
*
* The (newly allocated) returned string
* is exactly strlen(hex_str)/2 bytes long.
* The returned string should be considered
* binary. It may contain embedded 0's.
*/
char* hex_decode(const char *hex_str, int *ret_str_len)
{
int i;
int hex_str_len, result_len;
char *result;
g_return_val_if_fail(hex_str, NULL);
hex_str_len= (int) strlen(hex_str);
g_return_val_if_fail(hex_str_len%2 == 0, NULL);
result_len= hex_str_len/2;
result= (char*) g_malloc(result_len+1);
if (ret_str_len) *ret_str_len= result_len;
for (i= 0; i < result_len; i++)
{
unsigned int num;
sscanf(hex_str+i*2, "%2x", &num);
result[i]= num;
}
result[i]= 0;
return (char*)result;
}
//----------------------------------------------------------------------------------------------------------------------
/* This functions transforms an arbitrary sequence of bytes
* into a string consisting of 0-9 and A-F characters.
* Each byte of the input string is mapped to two bytes
* in the resulting-string.
*
* len is the length of the binary_string or -1 if the
* string is a normal null-terminated string.
*
* The returned string is a valid ASCII string and thus also
* a valid UTF-8 string.
*/
char *hex_encode(const char *binary_string, int len)
{
static char* hex_char= "0123456789ABCDEF";
int binary_string_len;
char *hex_str;
int j,k;
const char *from;
g_return_val_if_fail(binary_string,NULL);
g_return_val_if_fail(len, NULL);
if (len == -1)
binary_string_len= (int) strlen(binary_string);
else
binary_string_len= len;
hex_str= (char*) g_malloc(binary_string_len*2+1);
from= binary_string;
for (j=0,k=0; k < binary_string_len; j+=2, k++)
{
unsigned int data= (unsigned int) (unsigned char) from[k];
hex_str[j]= hex_char[data >> 4];
hex_str[j+1]= hex_char[data & 15];
}
hex_str[j]= 0;
return hex_str;
}
//----------------------------------------------------------------------------------------------------------------------
static char *find_name_with_value(const MYX_NAME_VALUE_PAIR *list, const char *value)
{
for (; list->name; list++)
{
if ( !g_utf8_casecollate(list->value,value) )
return list->name;
}
return "latin1";
}
//----------------------------------------------------------------------------------------------------------------------
/*
* Translates a mysql character-set name to a
* character-set name that is understood by iconv
*
* Return value: The corresponding iconv char name
* or "-" if there is no appropriate
* iconv character-set
* Don't free the result of this function!
*/
char *iconv_char_name(const char *mysql_character_set_name)
{
//attribute "name": iconv-name
//attribute "value": mysql-name
static MYX_NAME_VALUE_PAIR iconv_mysql[]=
{
/* different names */
{"HP-ROMAN8","hp8"},
{"KOI8-R", "koi8r"},
{"US-ASCII", "ascii"},
{"EUC-JP", "ujis"},
{"KOI8-U", "koi8u"},
{"ARMSCII-8", "armscii8"},
{"UCS-2", "ucs2"},
{"MACCENTRALEUROPE", "macce"},
/* same names */
{"big5", "big5"},
{"sjis", "sjis"},
{"cp850","cp850"},
{"cp866", "cp866"},
{"latin1", "latin1"},
{"latin2", "latin2"},
{"latin5", "latin5"},
{"latin7", "latin7"},
{"hebrew", "hebrew"},
{"tis620", "tis620"},
{"euckr", "euckr"},
{"gb2312", "gb2312"},
{"greek", "greek"},
{"cp1250", "cp1250"},
{"gbk", "gbk"},
{"utf8", "utf8"},
{"macroman", "macroman"},
{"cp1251", "cp1251"},
{"cp1256", "cp1256"},
{"cp1257", "cp1257"},
{NULL, NULL}
};
char *iconv_name;
iconv_name= find_name_with_value(iconv_mysql,mysql_character_set_name);
return iconv_name;
}
//----------------------------------------------------------------------------------------------------------------------
/*
* Normally a mysql identifier is quoted with ` (backtick).
* In Ansi Mode " is also a valid identifier.
* This function handles both.
*/
char* unquote_identifier(char *table_name)
{
char *beginning= table_name;
if (*table_name == '"' || *table_name == '`')
{
size_t len = strlen(table_name) - 2;
strncpy(beginning, table_name + 1, len);
beginning[len] = '\0';
};
return beginning;
}
//----------------------------------------------------------------------------------------------------------------------
/* This is a helper function
* that is only used by
* quote_identifier
*/
static char *quote_name(const char *name, char *buff, char quote_char)
{
char *to= buff;
*to++= quote_char;
while (*name)
{
if (*name == quote_char)
*to++= quote_char;
*to++= *name++;
}
to[0]= quote_char;
to[1]= 0;
return buff;
}
//----------------------------------------------------------------------------------------------------------------------
/*
* Returns a string allocated with gmalloc and surrounded by the given quote char.
*
* @param identifier The string to quote.
*
* @return A newly allocated string containing the quoted version of identifier.
*/
char *quote_identifier(const char *identifier, const char quote_char)
{
char *buff;
/* worst case: identifier consists of backticks only */
buff= (char*) g_malloc((gulong)(strlen(identifier) * 2 + 3));
return quote_name(identifier,buff, quote_char);
}
//----------------------------------------------------------------------------------------------------------------------
char *mask_out_string_re(char *str, const char *open_re,
const char open_trigger, const char close_trigger,
const char mask)
{
int maskout= 0;
unsigned int i;
pcre *pcre_exp;
const char *error_str;
int erroffset;
int o_vector[10];
int rc;
int start= -1;
size_t len= strlen(str);
pcre_exp= pcre_compile(open_re, PCRE_CASELESS, &error_str, &erroffset, NULL);
g_return_val_if_fail(pcre_exp!=NULL, str);
rc= pcre_exec(pcre_exp, NULL, str, (int)len, 0, 0, o_vector, 3);
if (rc > 0)
start= o_vector[0];
pcre_free(pcre_exp);
if (start < 0)
return str;
for (i= start; str[i]!=0; i++)
{
int c= str[i];
if((c==close_trigger)&&(maskout>0))
{
maskout--;
if(!maskout)
continue;
}
if(maskout>0)
{
str[i]= mask;
}
if(c==open_trigger)
{
maskout++;
}
}
return str;
}
//----------------------------------------------------------------------------------------------------------------------
char *mask_out_string(char *str, const char open_trigger,
const char close_trigger, const char mask)
{
int maskout= 0;
unsigned int i;
for(i=0;str[i]!=0;i++)
{
int c= str[i];
if((c==close_trigger)&&(maskout>0))
{
maskout--;
if(!maskout)
continue;
}
if(maskout>0)
{
str[i]= mask;
}
if(c==open_trigger)
{
maskout++;
}
}
return str;
}
//----------------------------------------------------------------------------------------------------------------------
static void __sappend(char **str, int *ressize, int *reslen, const char *sbegin, int count)
{
if (*reslen + count + 1 >= *ressize)
{
*ressize+= count + 100;
*str= (char*) g_realloc(*str, *ressize);
}
strncpy(*str+*reslen, sbegin, count);
*reslen+= count;
(*str)[*reslen]= 0;
}
//----------------------------------------------------------------------------------------------------------------------
char *str_g_subst(const char *str, const char *search, const char *replace)
{
int ressize, reslen;
int replsize= (int)strlen(replace);
int searchlen= (int)strlen(search);
char *res;
const char *bptr, *ptr;
g_return_val_if_fail(str != NULL, g_strdup(str));
if(str[0] == '\0')
{
return g_strdup(str);
}
g_return_val_if_fail(search != NULL && *search, g_strdup(str));
g_return_val_if_fail(replace != NULL, g_strdup(str));
ressize= (int)strlen(str)+1;
reslen= 0;
res= (char*) g_malloc(ressize);
bptr= str;
while ((ptr= strstr(bptr, search)) != NULL)
{
__sappend(&res, &ressize, &reslen, bptr, (int)(ptr-bptr));
bptr= ptr+searchlen;
__sappend(&res, &ressize, &reslen, replace, replsize);
}
__sappend(&res, &ressize, &reslen, bptr, (int)strlen(bptr));
return res;
}
//----------------------------------------------------------------------------------------------------------------------
/**
* Replaces the search string with the replace string
* in the given str
*
* @str becomes invalid after calling this
* function!
*
* Return value: Newly allocated result string
**/
char *str_g_replace(char *str, const char *search, const char *replace)
{
char *res;
if (!replace)
replace= "";
res= str_g_subst(str, search, replace);
g_free(str);
return res;
}
/**
* Appends the @addon string to the end of
* @base_str. @base_str is expected to have
* been allocated with a glib-memory allocatio
* function.
*
* @base_str becomes invalid after calling this
* function!
*
* Return value: The joined string
**/
char *str_g_append(char *base_str, const char *addon)
{
unsigned int addon_len;
char *tmp;
if(!base_str)
{
if(!addon)
base_str= g_strdup("");
else
base_str= g_strdup(addon);
}
else
{
if(addon)
{
addon_len= (unsigned int)strlen(addon);
tmp= (char*) g_realloc(base_str, (unsigned int)strlen(base_str)+addon_len+1);
base_str= strncat(tmp, addon,addon_len);
}
}
return base_str;
}
//----------------------------------------------------------------------------------------------------------------------
char *str_g_append_and_free(char *base_str, char *addon)
{
char *tmp= str_g_append(base_str, addon);
g_free(addon);
return tmp;
}
//----------------------------------------------------------------------------------------------------------------------
char *str_g_insert(const char *base_str, const char *addon, unsigned int index)
{
char *tmp= (char*) g_malloc(sizeof(char *)*(unsigned int)(strlen(base_str)+strlen(addon)+1));
g_stpcpy(tmp, base_str);
g_stpcpy(tmp+index, addon);
g_stpcpy(tmp+(index+(unsigned int)(strlen(addon))), base_str+index);
return tmp;
}
//----------------------------------------------------------------------------------------------------------------------
//from libmysql/strmov.c
char *strmov(register char *dst, register const char *src)
{
while ((*dst++ = *src++) != NULL)
;
return dst-1;
}
//----------------------------------------------------------------------------------------------------------------------
int strcmp2(const char *str1, const char *str2)
{
if((!str1)&&(!str2))
return 0;
if((!str1)||(!str2))
return -1;
else
return strcmp(str1, str2);
}
//----------------------------------------------------------------------------------------------------------------------
int strcmp3(const char *str1, const char *str2)
{
if((!str1)&&(!str2))
return 0;
else if(!str1)
{
if(strcmp(str2, "") == 0)
return 0;
else
return -1;
}
else if(!str2)
{
if(strcmp(str1, "") == 0)
return 0;
else
return -1;
}
else
return strcmp(str1, str2);
}
//----------------------------------------------------------------------------------------------------------------------
unsigned int strlen2(const char *str)
{
if(!str)
return 0;
else
return (unsigned int)strlen(str);
}
//----------------------------------------------------------------------------------------------------------------------
char *strcpy2(char *dst, const char *src)
{
if(!src)
{
*dst= 0;
}
else
{
strcpy(dst, src);
}
return dst;
}
//----------------------------------------------------------------------------------------------------------------------
char *utf8_str_trim(char *str)
{
size_t len= strlen(str);
size_t new_len;
gchar * start, * finish;
// Get length of leading spaces
gchar * pos= str;
gchar * end= str + len;
gunichar symbol;
for ( symbol= g_utf8_get_char(pos);
g_unichar_isspace(symbol);
symbol= g_utf8_get_char(pos))
{
pos= g_utf8_next_char(pos);
if (pos==end)
break;
}
start= pos;
//Get length of trailing spaces
pos= end;
//XXX
// the following line and (g_utf8_next_char()) won't do anything
// whoever wrote it, please check what's really intended and fix (or remove)
//g_utf8_prev_char(pos);
for ( symbol= g_utf8_get_char(pos);
g_unichar_isspace(symbol);
symbol= g_utf8_get_char(pos))
{
pos= g_utf8_find_prev_char(start,pos);
pos= g_utf8_prev_char(pos);
if (pos==start)
break;
}
//g_utf8_next_char(pos);
finish= pos;
new_len= finish-start;
if (start!=end || new_len!=len)
{
//shift chars to the begining of the string
memmove(str, start, new_len);
//terminate string
str[new_len]=0;
}
return str;
}
//----------------------------------------------------------------------------------------------------------------------
char *str_trim(char *str)
{
SIZE_T i, j;
SIZE_T length;
length= strlen(str);
//Get length of leading spaces
for (i=0; (i<length)&&isspace(str[i]); i++)
{}
//Get length of trailing spaces
for (j=length-1; (j>i)&&isspace(str[j]); j--)
{}
j++;
//shift chars to the begining of the string
memmove(str, str+i, j-i);
//terminate string
str[j-i]=0;
return str;
}
//----------------------------------------------------------------------------------------------------------------------
char *str_left(char *dest, char *src, unsigned int len)
{
unsigned int i;
unsigned int s_len= (int)strlen(src);
if(s_len<=len)
{
strncpy(dest, src, s_len);
dest[len]= 0;
}
else
{
for(i=0;i<len;i++)
{
dest[i]= src[i];
}
dest[len]= 0;
}
return dest;
}
//----------------------------------------------------------------------------------------------------------------------
char *str_right(char *dest, char *src, unsigned int len)
{
unsigned int i;
unsigned int s_len= (unsigned int)strlen(src);
if(s_len<=len)
{
strncpy(dest, src, s_len);
}
else
{
for(i=0;i<=len;i++)
{
dest[i]= src[i+s_len-len];
}
}
return dest;
}
//----------------------------------------------------------------------------------------------------------------------
char *str_align_left(const char *txt, unsigned int width, char linechar)
{
char *dst= (char*) g_malloc(width + 1);
unsigned int len= (unsigned int)strlen(txt);
unsigned int i;
if (len > width)
len= width;
memset(dst, linechar, width);
dst[width]= 0;
for (i= 0; i < len; i++)
dst[i]= txt[i];
return dst;
}
//----------------------------------------------------------------------------------------------------------------------
char *str_align_right(const char *txt, unsigned int width, char linechar)
{
char *dst= (char*) g_malloc(width + 1);
unsigned int len= (unsigned int)strlen(txt);
unsigned int i;
if (len > width)
len= width;
memset(dst, linechar, width);
dst[width]= 0;
for (i= 0; i < len; i++)
dst[i + width - len]= txt[i];
return dst;
}
//----------------------------------------------------------------------------------------------------------------------
char *str_align_center(const char *txt, unsigned int width, char linechar)
{
char *dst= (char*) g_malloc(width + 1);
unsigned int i;
unsigned int len= (unsigned int)strlen(txt);
unsigned int p= div(width, 2).quot - div(len, 2).quot;
if (len > width)
len= width;
memset(dst, linechar, width);
dst[width]= 0;
for (i= 0; i < len; i++)
dst[p + i]= txt[i];
return dst;
}
//----------------------------------------------------------------------------------------------------------------------
char *auto_line_break(const char *txt, unsigned int width, char sep)
{
char *dst= (char*) g_malloc((width + 2) * 80);
unsigned int i, o= 0, p= 0, w= 0, l= (unsigned int)strlen(txt);
for (i= 0; i < l;)
{
w++;
if (w > width)
{
#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
dst[p + o]= '\r';
dst[p + o + 1]= '\n';
o+= 1;
#else
dst[p + o]= '\n';
#endif
i= p + 1;
w= 0;
}
else
{
dst[i + o]= txt[i];
if (txt[i] == sep)
p= i;
i++;
}
}
dst[i + o]= 0;
return dst;
}
//----------------------------------------------------------------------------------------------------------------------
int sub_str_count(char *search_str, const char *src)
{
unsigned int i;
char *pdest;
char *psrc = (char *)src;
unsigned int ss_len= (unsigned int)strlen(search_str);
i= 0;
while((pdest= strstr(psrc, search_str)) != NULL)
{
i++;
psrc= pdest+ss_len;
}
return i;
}
//----------------------------------------------------------------------------------------------------------------------
int str_endswith(const char *str, const char *substr)
{
int slen= (int)strlen(str);
int sslen= (int)strlen(substr);
if (slen >= sslen && strcmp(str+slen-sslen, substr)==0)
return 1;
else
return 0;
}
//----------------------------------------------------------------------------------------------------------------------
int str_beginswith(const char *str, const char *substr)
{
if (str)
return strncmp(str, substr, strlen(substr))==0;
else
return 0;
}
//----------------------------------------------------------------------------------------------------------------------
int str_is_numeric(const char *str)
{
unsigned int len= (unsigned int)strlen(str);
unsigned int i;
for (i= 0; i < len; i++)
if(g_ascii_digit_value(str[i]) == -1)
return 0;
return 1;
}
//----------------------------------------------------------------------------------------------------------------------
MYX_PUBLIC_FUNC int g_utf8_casecollate(const char *str1, const char *str2)
{
char *s1= g_utf8_casefold(str1, (gssize)strlen(str1));
char *s2= g_utf8_casefold(str2, (gssize)strlen(str2));
int res;
res= g_utf8_collate(s1, s2);
g_free(s1);
g_free(s2);
return res;
}
//----------------------------------------------------------------------------------------------------------------------
char *str_toupper(char *str)
{
char *s= str;
while (*s)
{
*s= toupper(*s);
s++;
}
return str;
}
//----------------------------------------------------------------------------------------------------------------------
char *name_of_str(char *dst, const char *src)
{
char *dst_start= dst;
while (*src && *src!='=')
*dst++= *src++;
*dst= 0;
return dst_start;
}
//----------------------------------------------------------------------------------------------------------------------
char *value_of_str(char *dst, const char *src)
{
char *psrc= (char *)src;
psrc = strchr(psrc, '=');
if (psrc)
{
if (*psrc)
strcpy(dst, psrc+1);
else
*dst= 0;
}
else
*dst= 0;
return dst;
}
//----------------------------------------------------------------------------------------------------------------------
int set_value(char **string_list, unsigned int string_list_num, char *name, char *new_value)
{
unsigned int i;
unsigned int name_len= (unsigned int)strlen(name);
int rc= -1;
for(i=0; i<string_list_num; i++)
{
if(strncmp(string_list[i], name, name_len)==0 &&
(string_list[i][name_len]=='\0' || string_list[i][name_len]=='='))
{
if (!new_value)
{
g_free(string_list[i]);
if (string_list_num > 0)
memmove(string_list+i, string_list+i+1, string_list_num-1);
}
else
{
string_list[i]= (char*) g_realloc(string_list[i], (name_len+strlen(new_value)+2));
if (string_list[i][name_len]=='=')
strcpy(string_list[i]+name_len+1, new_value);
else
sprintf(string_list[i], "%s=%s", string_list[i], new_value);
}
rc= 0;
break;
}
}
return rc;
}
//----------------------------------------------------------------------------------------------------------------------
#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
int get_value_from_registry(HKEY root_key, const char *sub_key, const char *key, const char *def, char *value)
{
HKEY hSubKey;
DWORD dwType;
DWORD dwSize;
LONG retval;
unsigned char Buffer[512];
if((retval=RegOpenKeyEx(root_key, sub_key, 0, KEY_READ, &hSubKey))==ERROR_SUCCESS)
{
dwSize = 512;
if((RegQueryValueEx(hSubKey, key, NULL, &dwType, Buffer, &dwSize))==ERROR_SUCCESS)
{
if(dwType==REG_DWORD)
{
sprintf(value, "%d", Buffer[0] + Buffer[1] * 256 + Buffer[2] * 65536 + Buffer[3] * 16777216);
}
else
{
strcpy(value, (const char *)Buffer);
}
}
else
{
strcpy(value, def);
}
return 0;
}
else
{
strcpy(value, "");
return retval;
}
}
//----------------------------------------------------------------------------------------------------------------------
int set_value_to_registry(HKEY root_key, const char *sub_key, const char *key, const char *value)
{
HKEY hSubKey;
LONG retval;
DWORD dwDispo;
if((retval= RegCreateKeyEx(root_key, sub_key, 0, "",
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hSubKey, &dwDispo))==ERROR_SUCCESS)
{
retval = RegSetValueEx(hSubKey, key, 0, REG_SZ, value, (DWORD)strlen(value)+1);
if(retval != ERROR_SUCCESS)
{
return GetLastError();
}
else
{
return 0;
}
}
else
{
return -1;
}
}
//----------------------------------------------------------------------------------------------------------------------
char *get_local_os_name()
{
char *os_name = (char*) g_malloc(32);
OSVERSIONINFO info;
info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&info);
if (!os_name)
return NULL;
strcpy(os_name, "unknown");
switch (info.dwPlatformId)
{
case VER_PLATFORM_WIN32_WINDOWS:
if((info.dwMajorVersion>=4)&&(info.dwMinorVersion==0))
{
strcpy(os_name, "Windows 95");
}
else if((info.dwMajorVersion>=4)&&(info.dwMinorVersion>0)&&(info.dwMinorVersion>90))
{
strcpy(os_name, "Windows 98");
}
else if((info.dwMajorVersion>=4)&&(info.dwMinorVersion>0)&&(info.dwMinorVersion>=90))
{
strcpy(os_name, "Windows ME");
}
break;
case VER_PLATFORM_WIN32_NT:
if((info.dwMajorVersion<=4)&&(info.dwMinorVersion==0))
{
strcpy(os_name, "Windows NT");
}
else if((info.dwMajorVersion==5)&&(info.dwMinorVersion==0))
{
strcpy(os_name, "Windows 2k");
}
else if((info.dwMajorVersion==5)&&(info.dwMinorVersion==1))
{
strcpy(os_name, "Windows XP");
}
else if((info.dwMajorVersion==5)&&(info.dwMinorVersion==2))
{
strcpy(os_name, "Windows Server 2003/XP");
}
break;
}
return os_name;
}
//----------------------------------------------------------------------------------------------------------------------
char *get_local_hardware_info()
{
char *hardware_string;
SYSTEM_INFO sysinfo;
MEMORYSTATUSEX memstat;
char processor_name[100];
char processor_mhz[10];
char total_phys_ram[16];
DWORDLONG total_phys_ram_val;
GetSystemInfo(&sysinfo);
memstat.dwLength = sizeof(memstat);
GlobalMemoryStatusEx(&memstat);
strcpy(processor_mhz, "");
strcpy(processor_name, "");
get_value_from_registry(HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", "~MHz", "",
processor_mhz);
get_value_from_registry(HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", "ProcessorNameString", "",
processor_name);
if(!processor_name[0])
{
get_value_from_registry(HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", "Identifier", "",
processor_name);
sprintf(processor_name, "%s %s", processor_name, processor_mhz);
}
str_trim(processor_name);
total_phys_ram_val= memstat.ullTotalPhys;
if(total_phys_ram_val >= 1072410624 / 1.9)
{
sprintf(total_phys_ram, "%1.1f GB RAM", (double)total_phys_ram_val / 1072410624.0);
}
else if(total_phys_ram_val >= 1047276 / 1.9)
{
sprintf(total_phys_ram, "%1.0f MB RAM", (double)total_phys_ram_val / 1047276.0);
}
else
{
sprintf(total_phys_ram, "%d B RAM", total_phys_ram_val);
}
hardware_string = (char*) g_malloc(16 + (gulong) strlen(processor_name) + (gulong)strlen(total_phys_ram));
if(sysinfo.dwNumberOfProcessors>1)
{
sprintf(hardware_string, "%dx %s, %s", sysinfo.dwNumberOfProcessors, processor_name, total_phys_ram);
}
else
{
sprintf(hardware_string, "%s, %s", processor_name, total_phys_ram);
}
return hardware_string;
}
#else /* !__WIN__ */
//----------------------------------------------------------------------------------------------------------------------
// Linux/Unix version
char *get_local_os_name()
{
struct utsname info;
if (uname(&info) < 0)
{
return NULL;
}
return g_strdup_printf("%s %s", info.sysname, info.release);
}
//----------------------------------------------------------------------------------------------------------------------
#if defined(__APPLE__) && defined(__MACH__)
static const char *get_cpu_type_name(int cpu_type, int cpu_subtype)
{
switch (cpu_type)
{
case CPU_TYPE_I386:
switch (cpu_subtype)
{
case CPU_SUBTYPE_386:
return "80386";
case CPU_SUBTYPE_486:
return "80486";
case CPU_SUBTYPE_486SX:
return "80486SX";
case CPU_SUBTYPE_PENT:
return "Pentium";
case CPU_SUBTYPE_PENTPRO:
return "Pentium Pro";
case CPU_SUBTYPE_PENTII_M3:
return "Pentium II";
case CPU_SUBTYPE_PENTII_M5:
return "Pentium II";
#ifdef CPU_SUBTYPE_CELERON
case CPU_SUBTYPE_CELERON:
return "Celeron";
#endif
#ifdef CPU_SUBTYPE_CELERON_MOBILE
case CPU_SUBTYPE_CELERON_MOBILE:
return "Celeron Mobile";
#endif
#ifdef CPU_SUBTYPE_PENTIUM_3
case CPU_SUBTYPE_PENTIUM_3:
return "Pentium III";
#endif
#ifdef CPU_SUBTYPE_PENTIUM_3_M
case CPU_SUBTYPE_PENTIUM_3_M:
return "Pentium III M";
#endif
#ifdef CPU_SUBTYPE_PENTIUM_3_XEON
case CPU_SUBTYPE_PENTIUM_3_XEON:
return "Pentium III Xeon";
#endif
#ifdef CPU_SUBTYPE_PENTIUM_M
case CPU_SUBTYPE_PENTIUM_M:
return "Pentium M";
#endif
#ifdef CPU_SUBTYPE_PENTIUM_4
case CPU_SUBTYPE_PENTIUM_4:
return "Pentium 4";
#endif
#ifdef CPU_SUBTYPE_PENTIUM_4_M
case CPU_SUBTYPE_PENTIUM_4_M:
return "Pentium 4M";
#endif
#ifdef CPU_SUBTYPE_ITANIUM
case CPU_SUBTYPE_ITANIUM:
return "Itanium";
#endif
#ifdef CPU_SUBTYPE_ITANIUM_2
case CPU_SUBTYPE_ITANIUM_2:
return "Itanium 2";
#endif
#ifdef CPU_SUBTYPE_XEON
case CPU_SUBTYPE_XEON:
return "Xeon";
#endif
#ifdef CPU_SUBTYPE_XEON_MP
case CPU_SUBTYPE_XEON_MP:
return "Xeon MP";
#endif
default:
return "x86";
}
break;
case CPU_TYPE_POWERPC:
switch (cpu_subtype)
{
case CPU_SUBTYPE_POWERPC_601:
return "PowerPC 601";
case CPU_SUBTYPE_POWERPC_602:
return "PowerPC 602";
case CPU_SUBTYPE_POWERPC_603:
return "PowerPC 603";
case CPU_SUBTYPE_POWERPC_603e:
return "PowerPC 603e";
case CPU_SUBTYPE_POWERPC_603ev:
return "PowerPC 603ev";
case CPU_SUBTYPE_POWERPC_604:
return "PowerPC 604";
case CPU_SUBTYPE_POWERPC_604e:
return "PowerPC 604e";
case CPU_SUBTYPE_POWERPC_620:
return "PowerPC 620";
case CPU_SUBTYPE_POWERPC_750:
return "PowerPC G3";
case CPU_SUBTYPE_POWERPC_7400:
return "PowerPC G4";
case CPU_SUBTYPE_POWERPC_7450:
return "PowerPC G4";
case CPU_SUBTYPE_POWERPC_970:
return "PowerPC G5";
default:
return "PowerPC";
}
break;
}
return "Unknown";
}
//----------------------------------------------------------------------------------------------------------------------
// MacOS X
static int _get_hardware_info(char **cpu, char **clock, int *cpu_count, unsigned long *mem_kb)
{
int mib[2];
size_t length;
unsigned int tmp;
char *mclass;
int cpu_type, cpu_subtype;
// get machine class
mib[0]= CTL_HW;
mib[1]= HW_MACHINE;
sysctl(mib, 2, NULL, &length, NULL, 0);
mclass= g_malloc(length*sizeof(char*));
sysctl(mib, 2, mclass, &length, NULL, 0);
// get machine arch
length= sizeof(cpu_type);
sysctlbyname("hw.cputype", &cpu_type, &length, NULL, 0);
length= sizeof(cpu_subtype);
sysctlbyname("hw.cpusubtype", &cpu_subtype, &length, NULL, 0);
*cpu= g_strdup_printf("%s (%s)", mclass, get_cpu_type_name(cpu_type, cpu_subtype));
g_free(mclass);
// get cpu clock
mib[0]= CTL_HW;
mib[1]= HW_CPU_FREQ;
length= sizeof(tmp);
if (sysctl(mib, 2, &tmp, &length, NULL, 0) < 0)
*clock= g_strdup_printf("?");
else
*clock= g_strdup_printf("%.01f", (double)tmp/1000000.0);
// get cpu count
mib[0]= CTL_HW;
mib[1]= HW_NCPU;
length= sizeof(cpu_count);
if (sysctl(mib, 2, cpu_count, &length, NULL, 0) < 0)
*cpu_count= 1;
// get memory size
*mem_kb= get_physical_memory_size()/1024;
return 0;
}
//----------------------------------------------------------------------------------------------------------------------
#else
// Linux
static int _get_hardware_info(char **cpu, char **clock, int *cpu_count, unsigned long *mem_kb)
{
FILE *proc;
char line[256];
// fetch processor info from /proc/cpuinfo
proc= fopen("/proc/cpuinfo", "r");
if (!proc)
{
return -1;
}
*cpu_count= 0;
while (!feof(proc))
{
if (!fgets(line, sizeof(line), proc))
break;
if (str_beginswith(line,"model name"))
{
(*cpu_count)++;
*cpu= g_strdup(str_trim(strchr(line, ':')+1));
}
else if (str_beginswith(line,"cpu MHz"))
{
*clock= g_strdup(str_trim(strchr(line, ':')+1));
}
}
fclose(proc);
*mem_kb= get_physical_memory_size()/1024;
return 0;
}
#endif
//----------------------------------------------------------------------------------------------------------------------
char *get_local_hardware_info()
{
char *hardware_string;
int processor_count = 0;
char *processor_name= NULL;
char *processor_mhz= NULL;
char total_phys_ram[64];
unsigned long total_phys_ram_val = 0;
_get_hardware_info(&processor_name, &processor_mhz, &processor_count,
&total_phys_ram_val);
if (total_phys_ram_val>=(1024*1024)/1.9)
{
sprintf(total_phys_ram, "%1.1f GB RAM", (double)total_phys_ram_val/(1024*1024));
}
else if(total_phys_ram_val>=1024/1.9)
{
sprintf(total_phys_ram, "%1.0f MB RAM", (double)total_phys_ram_val/1024);
}
else
{
sprintf(total_phys_ram, "%ld KB RAM", total_phys_ram_val);
}
if(processor_count>1)
{
hardware_string= g_strdup_printf("%dx %s %s MHz, %s", processor_count, processor_name, processor_mhz, total_phys_ram);
}
else
{
hardware_string= g_strdup_printf("%s %s MHz, %s", processor_name, processor_mhz, total_phys_ram);
}
g_free(processor_name);
g_free(processor_mhz);
return hardware_string;
}
//----------------------------------------------------------------------------------------------------------------------
FILE *myx_popen(char *const args[], pid_t *pid_ret)
{
FILE *f;
int fd[2];
if (pipe(fd) >= 0)
{
*pid_ret = fork();
if (*pid_ret == 0)
{
close(1);
close(2);
dup2(fd[1], 1);
dup2(fd[1], 2);
close(fd[0]);
execvp(args[0], args);
exit(-1);
}
else if (*pid_ret > 0)
{
close(fd[1]);
f = fdopen(fd[0], "r");
if (!f)
{
int ret;
kill(*pid_ret, 9);
waitpid(*pid_ret, &ret, 0);
close(fd[0]);
}
return f;
}
}
return NULL;
}
//----------------------------------------------------------------------------------------------------------------------
int myx_read_timeout(FILE *f, int timeout, char *result, size_t result_len)
{
fd_set fds;
struct timeval tv;
int ret;
FD_ZERO(&fds);
FD_SET(fileno(f), &fds);
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000)*1000;
do {
ret= select(fileno(f)+1, &fds, NULL, NULL, timeout<0?NULL:&tv);
} while (ret < 0 && errno == EINTR);
if (ret > 0)
{
if (fgets(result, result_len, f))
return strlen(result);
else
return 0;
}
return -1;
}
//----------------------------------------------------------------------------------------------------------------------
int myx_pclose(FILE *f, pid_t pid)
{
int status = -1;
if (kill(pid, SIGKILL) == 0)
waitpid(pid, &status, 0);
fclose(f);
return status;
}
//----------------------------------------------------------------------------------------------------------------------
int copy_file(const char *orig_file, const char *new_file)
{
char buffer[4*1024];
int from_fd, to_fd;
int count;
from_fd= open(orig_file, O_RDONLY, 0666);
if (from_fd < 0)
return -1;
to_fd= open(new_file, O_WRONLY|O_CREAT, 0666);
if (to_fd < 0)
{
int err= errno;
close(from_fd);
errno= err;
return -1;
}
do {
count= read(from_fd, buffer, sizeof(buffer));
if (count < 0)
{
int err= errno;
close(from_fd);
close(to_fd);
errno= err;
return -1;
}
if (count > 0 && write(to_fd, buffer, count) < 0)
{
int err= errno;
close(from_fd);
close(to_fd);
errno= err;
return -1;
}
} while (count > 0);
close(from_fd);
close(to_fd);
return 0;
}
#endif
//----------------------------------------------------------------------------------------------------------------------
bigint get_physical_memory_size()
{
#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
MEMORYSTATUS memstat;
GlobalMemoryStatus(&memstat);
return memstat.dwTotalPhys;
#elif defined(__APPLE__)
uint64_t mem64;
int mib[2];
int mem32;
size_t length;
mib[0]= CTL_HW;
mib[1]= HW_MEMSIZE;
length= sizeof(mem64);
if (sysctl(mib, 2, &mem64, &length, NULL, 0) < 0)
{
mib[0]= CTL_HW;
mib[1]= HW_PHYSMEM;
length= sizeof(mem32);
sysctl(mib, 2, &mem32, &length, NULL, 0);
mem64= mem32;
}
return mem64;
#else
FILE *proc;
bigint mem64;
mem64= 0;
// fetch physical memory info from /proc/meminfo
proc= fopen("/proc/meminfo", "r");
if (proc)
{
char line[1024];
char *ptr, *end;
while (fgets(line, sizeof(line), proc))
{
if (strncasecmp(line, "MemTotal:", sizeof("MemTotal:")-1)==0)
{
char *line_end= line+strlen(line);
ptr= strchr(line, ':')+1;
while (*ptr && *ptr==' ') ptr++;
end= strchr(ptr, ' ');
if (end)
*end= 0;
if (end < line_end)
end++;
if (strstr(end, "gB") || strstr(end, "GB"))
mem64= strtoul(str_trim(ptr), NULL, 10)*1024*1024*1024LL;
else if (strstr(end, "mB") || strstr(end, "MB"))
mem64= strtoul(str_trim(ptr), NULL, 10)*1024*1024LL;
else if (strstr(end, "kB") || strstr(end, "KB"))
mem64= strtoul(str_trim(ptr), NULL, 10)*1024LL;
else
mem64= strtoul(str_trim(ptr), NULL, 10);
break;
}
}
fclose(proc);
}
else
{
g_warning("Memory stats retrieval not implemented for this system");
}
return mem64;
#endif
}
//----------------------------------------------------------------------------------------------------------------------
bigint get_file_size(const char *filename)
{
#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
DWORD dwSizeLow;
DWORD dwSizeHigh = 0;
HANDLE hfile;
char *local_filename;
if ((local_filename= g_filename_from_utf8(filename,-1,NULL,NULL,NULL)) == NULL)
return -1;
hfile = CreateFile(local_filename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hfile != INVALID_HANDLE_VALUE)
{
dwSizeLow= GetFileSize(hfile, &dwSizeHigh);
CloseHandle(hfile);
g_free(local_filename);
if((dwSizeLow==INVALID_FILE_SIZE)&&(GetLastError()) != NO_ERROR )
{
return -1;
}
else
{
return (((bigint) dwSizeHigh << 32) + dwSizeLow);
}
}
else
{
g_free(local_filename);
return -1;
}
#else //!WINDOWS
struct stat buf;
char *local_filename;
if (! (local_filename= g_filename_from_utf8(filename,-1,NULL,NULL,NULL)))
return -1;
if (stat(local_filename, &buf) < 0) {
g_free(local_filename);
return -1;
}
g_free(local_filename);
return buf.st_size;
#endif //!WINDOWS
}
// note, needle has to be ascii!
char *strcasestr_len(const char *haystack, int haystack_len, const char *needle)
{
gssize needle_len= (gssize)strlen(needle);
int i;
if (needle_len > haystack_len)
return NULL;
i= 0;
while (i <= haystack_len - needle_len)
{
if (g_ascii_strncasecmp(needle, haystack+i, needle_len)==0)
return (char *)haystack+i;
i++;
}
return NULL;
}
//----------------------------------------------------------------------------------------------------------------------
#define O_VECTOR_COUNT 64 // max # of ()*2+2
char * get_value_from_text_ex_opt(const char *txt, int txt_length,
const char *regexpr,
unsigned int substring_nr,
int options_for_exec)
{
pcre *pcre_exp;
const char *error_str;
int erroffset;
int o_vector[O_VECTOR_COUNT];
int rc;
const char *ret_val;
char *value= NULL;
if(txt && *txt)
{
pcre_exp= pcre_compile(regexpr, PCRE_CASELESS, &error_str, &erroffset, NULL);
if (pcre_exp)
{
if ((rc= pcre_exec(pcre_exp, NULL, txt, txt_length, 0,
options_for_exec, o_vector, O_VECTOR_COUNT) ) > 0)
{
if (o_vector[substring_nr * 2] != -1)
{
pcre_get_substring(txt, o_vector, rc, substring_nr, &ret_val);
value= g_strdup(ret_val);
pcre_free_substring((char*)ret_val);
}
}
pcre_free(pcre_exp);
}
}
return value;
}
//----------------------------------------------------------------------------------------------------------------------
char * get_value_from_text_ex(const char *txt, int txt_length,
const char *regexpr, unsigned int substring_nr)
{
return get_value_from_text_ex_opt(txt,txt_length,regexpr,substring_nr,0);
}
//----------------------------------------------------------------------------------------------------------------------
char * get_value_from_text(const char *txt, int txt_length, const char *regexpr)
{
return get_value_from_text_ex(txt, txt_length, regexpr, 1);
}
//----------------------------------------------------------------------------------------------------------------------
int strlist_g_indexof(const char **list, const char *value)
{
int i= 0;
while(list[i] != NULL)
{
if(strcmp(list[i], value) == 0)
{
return i;
}
}
return -1;
}
//----------------------------------------------------------------------------------------------------------------------
void strlist_g_append(char ***list, char *value)
{
unsigned int i;
if (*list)
{
for (i= 0; (*list)[i]; i++);
*list= (char**) g_realloc(*list, sizeof(char*)*(i+2));
(*list)[i]= value;
(*list)[i+1]= 0;
}
else
{
*list= (char**) g_malloc(sizeof(char*)*2);
(*list)[0]= value;
(*list)[1]= 0;
}
}
//----------------------------------------------------------------------------------------------------------------------
void strlist_g_append_or_replace(char ***list, char *value)
{
int i= strlist_g_indexof((const char**)*list, value);
if(i < 0)
{
strlist_g_append(list, value);
}
else
{
(*list)[i]= value;
}
}
//----------------------------------------------------------------------------------------------------------------------
const char *strfindword(const char *str, const char *word)
{
const char* result = NULL;
const char *ptr;
size_t wordlen= strlen(word);
ptr= str;
for (;;)
{
// find match
ptr= strcasestr_len(ptr, (int)strlen(ptr), word);
if (!ptr)
break;
// check if its acceptable
if ((ptr == str || !isalnum(*(ptr - 1))) && // space or any other non-alpha-numeric before
(!isalnum(*(ptr + wordlen)) || *(ptr + wordlen) == '\0')) // space or any other non-alpha-numeric after
{
result= ptr;
break;
};
ptr+= wordlen;
}
return result;
}
//----------------------------------------------------------------------------------------------------------------------
char *subst_pcre(const char *pattern, const char *repl,
int flags, int max_matches,
const char *string)
{
pcre *pcre_exp;
const char *error_str;
int erroffset;
char *res= NULL;
pcre_exp= pcre_compile(pattern, flags, &error_str, &erroffset, NULL);
if (pcre_exp)
{
int rc;
int *matches= (int*)g_malloc(sizeof(int)*(max_matches*3));
rc= pcre_exec(pcre_exp, NULL, string, (int)strlen(string), 0, 0, matches, max_matches);
if (rc > 0)
{
res= subst_pcre_matches(string, matches, rc, repl);
}
pcre_free(pcre_exp);
g_free(matches);
}
else
{
g_message("error compiling PCRE pattern: %s", error_str);
}
return res;
}
//----------------------------------------------------------------------------------------------------------------------
char *subst_pcre_matches(const char *src, int *matches, int matchcount,
const char *repl)
{
const char *p0, *pf;
int ressize= (int)strlen(repl);
int reslen= 0;
char *res= (char*) g_malloc(ressize);
char number[4];
int index;
p0= repl;
while (p0)
{
pf= strchr(p0, '\\');
if (pf)
{
__sappend(&res, &ressize, &reslen, p0, (int)(pf-p0));
pf++;
// no more than 99 groups!
if (isdigit(*pf) && isdigit(*(pf+1)))
{
number[0]=*(pf++);
number[1]=*(pf++);
number[2]=0;
index= atoi(number);
}
else if (isdigit(*pf))
{
number[0]=*(pf++);
number[1]=0;
index= atoi(number);
}
else
index= -1;
if (index > 0 && index <= matchcount)
__sappend(&res, &ressize, &reslen, src+matches[index*2],
matches[index*2+1]-matches[index*2]);
}
else
{
__sappend(&res, &ressize, &reslen, p0, (int)strlen(p0));
pf= NULL;
}
p0= pf;
}
return (char*) g_realloc(res, reslen+1);
}
//----------------------------------------------------------------------------------------------------------------------
#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
void timer_start(MYX_TIMER_VALUE *tval)
{
QueryPerformanceCounter(tval);
}
//----------------------------------------------------------------------------------------------------------------------
double timer_stop(MYX_TIMER_VALUE *tval)
{
MYX_TIMER_VALUE end;
LARGE_INTEGER freq;
QueryPerformanceCounter(&end);
QueryPerformanceFrequency(&freq);
return ((double)end.QuadPart - (double)tval->QuadPart)/((double)freq.QuadPart);
}
#else
//----------------------------------------------------------------------------------------------------------------------
void timer_start(MYX_TIMER_VALUE *tval)
{
gettimeofday(tval, NULL);
}
//----------------------------------------------------------------------------------------------------------------------
double timer_stop(MYX_TIMER_VALUE *tval)
{
MYX_TIMER_VALUE end;
gettimeofday(&end, NULL);
return (end.tv_sec - tval->tv_sec) + (end.tv_usec - tval->tv_usec)/1000000;
}
#endif
//----------------------------------------------------------------------------------------------------------------------
void *vec_insert_resize(void *vec, guint size, guint *vecsize, guint pos, void *data)
{
vec= g_realloc(vec, size*(*vecsize+1));
if (*vecsize > 0 && ((size_t)pos) < (*vecsize-1))
memmove((char*)vec+(pos+1)*size, (char*)vec+pos*size, size*(*vecsize-pos));
memcpy((char*)vec+pos*size, data, size);
(*vecsize)++;
return vec;
}
//----------------------------------------------------------------------------------------------------------------------
void *vec_remove(void *vec, guint size, guint *vecsize, guint pos)
{
if (*vecsize > 0 && ((size_t)pos) < (*vecsize-1))
memmove((char*)vec+pos*size, (char*)vec+(pos+1)*size, size*(*vecsize-pos-1));
(*vecsize)--;
return vec;
}
//----------------------------------------------------------------------------------------------------------------------
int split_schema_table(const char *ident, char **schema, char **table)
{
pcre *pcre_exp;
const char *error_str;
int erroffset;
int o_vector[32];
int rc;
pcre_exp= pcre_compile("(\\w+|`.+?`|\".+?\")(?:\\.(\\w+|`.+?`|\".+?\"))?",
PCRE_CASELESS|PCRE_UTF8|PCRE_DOTALL, &error_str, &erroffset, NULL);
*schema= NULL;
*table= NULL;
if (pcre_exp)
{
if ((rc= pcre_exec(pcre_exp, NULL, ident, (int)strlen(ident),
0, 0, o_vector, sizeof(o_vector)/sizeof(int))) > 0)
{
const char *a= NULL, *b= NULL;
pcre_get_substring(ident, o_vector, rc, 1, &a);
if (a)
{
*schema= unquote_identifier(g_strdup(a));
pcre_free_substring(a);
}
pcre_get_substring(ident, o_vector, rc, 2, &b);
if (b)
{
*table= unquote_identifier(g_strdup(b));
pcre_free_substring(b);
}
pcre_free(pcre_exp);
if (*schema && *table)
return 2;
else if (*schema && !*table)
{
*table= *schema;
*schema= NULL;
return 1;
}
}
pcre_free(pcre_exp);
}
return -1;
}
//----------------------------------------------------------------------------------------------------------------------
static int cmp_entity(const void *a, const void *b)
{
HTMLEntity *aa= (HTMLEntity*)a;
HTMLEntity *bb= (HTMLEntity*)b;
if (aa->code < bb->code)
return -1;
else if (aa->code > bb->code)
return 1;
else
return 0;
}
//----------------------------------------------------------------------------------------------------------------------
static char *escape_entities(const char *str, HTMLEntity *entities)
{
char *outstr= NULL;
int alloced= 0;
int length= 0;
gunichar uc;
int entity_num= 0;
while (entities[entity_num].name) entity_num++;
while (*str)
{
HTMLEntity key;
HTMLEntity *p;
uc= g_utf8_get_char(str);
key.code= uc;
p= (HTMLEntity*)bsearch(&key, entities, entity_num, sizeof(entities[0]), cmp_entity);
if (p)
{
char buffer[100];
buffer[0]= '&';
strcpy(buffer+1, p->name);
strcat(buffer, ";");
__sappend(&outstr, &alloced, &length, buffer, (int)strlen(buffer));
}
else
{
__sappend(&outstr, &alloced, &length, str, (int)(g_utf8_next_char(str)-str));
}
str= g_utf8_next_char(str);
}
return outstr;
}
//----------------------------------------------------------------------------------------------------------------------
char *escape_xml_entities(const char *str)
{
return escape_entities(str, XMLEntities);
}
//----------------------------------------------------------------------------------------------------------------------
char *escape_html_entities(const char *str)
{
return escape_entities(str, HTMLEntities);
}
//----------------------------------------------------------------------------------------------------------------------
char *unescape_html_entities(const char *str)
{
char *outstr= NULL;
int alloced= 0;
int length= 0;
int i;
const char *b, *e;
gchar utf8char[10];
b= str;
while (*b)
{
e= strchr(b, '&');
if (e)
{
const char *p;
if (e-b > 0) // copy until &
__sappend(&outstr, &alloced, &length, b, (int)(e-b));
// find end of &thing;
p= e+1;
while (*p && *p!='&' && *p!=';') p++;
if (*p == 0) // no ; found
__sappend(&outstr, &alloced, &length, e, (int)(p-e));
else if (*p == '&') // another & found, copy what we have and start over
__sappend(&outstr, &alloced, &length, e, (int)(p-e));
else if (*p == ';') // &ewq;
{
p++;
i= -1;
if (p-e-2 > 0)
{
for (i= 0; HTMLEntities[i].name; i++)
{
if (strncmp(HTMLEntities[i].name, e+1, p-e-2)==0)
break;
}
}
if (i >= 0 && HTMLEntities[i].name && p-e-2>0)
{
i= g_unichar_to_utf8(HTMLEntities[i].code, utf8char);
__sappend(&outstr, &alloced, &length, utf8char, i);
}
else // invalid &thing; just copy it over
__sappend(&outstr, &alloced, &length, e, (int)(p-e));
}
b= p;
}
else
{
// no &things; in the rest of the string
__sappend(&outstr, &alloced, &length, b, (int)strlen(b));
break;
}
}
return outstr;
}
//----------------------------------------------------------------------------------------------------------------------
//taken from glib
int myx_mkdir(const char *filename, int mode, int *error_no)
{
#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
wchar_t *wfilename = (wchar_t*) g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
int retval= _wmkdir (wfilename);
int save_errno= errno;
*error_no= errno;
g_free (wfilename);
errno= save_errno;
return retval;
#else
int retval= mkdir (filename, mode);
*error_no= errno;
return retval;
#endif
}
//----------------------------------------------------------------------------------------------------------------------
int myx_chdir(const char *path)
{
#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
wchar_t *wpath = (wchar_t*) g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
int retval;
int save_errno;
if (wpath == NULL)
{
errno = EINVAL;
return -1;
}
retval = _wchdir (wpath);
save_errno = errno;
g_free (wpath);
errno = save_errno;
return retval;
#else
return chdir(path);
#endif
}
//----------------------------------------------------------------------------------------------------------------------
int get_str_index(char **string_list, unsigned int string_list_num, const char *search)
{
unsigned int i= 0;
while (i<string_list_num)
{
if (strcmp2(string_list[i], search) == 0)
return i;
i++;
}
return -1;
}
//----------------------------------------------------------------------------------------------------------------------
/** @brief checks if filename really exists
@param filename path to file
@return 0 if the file doesn't exist else 1
\b vva_todo why don't it use access function?
*/
int check_file_exists(const char *filename)
{
FILE *f;
/*check if we can open the file */
if ((f= myx_fopen(filename, "r")) == NULL)
{
return 0;
}
else
{
fclose(f);
return 1;
}
}
//----------------------------------------------------------------------------------------------------------------------
/**
* Scans the given string and duplicates it to result while inserting a back slash character in front of each
* character that must be masked.
*
* @param source The source string to examine.
* @param special_chars All characters that must be masked.
*
* @return A new copy of source with all special characters escaped. This string must be freed by the caller (using g_free).
* @note covered by unit tests
*/
char* internal_escape_string(const char* source, const char* special_chars)
{
int i;
int j;
char* result= NULL;
if (source != NULL)
{
// Do a first loop to determine how many characters must be escaped.
int count= 0;
i= 0;
while (source[i] != '\0')
{
if (strchr(special_chars, source[i++]) != NULL)
count++;
};
// If no special char was found then just duplicate the string otherwise do a second round.
if (count == 0)
{
result= g_strdup(source);
}
else
{
result= (char*) g_malloc0(i + count + 1);
i= 0;
j= 0;
while (source[i] != '\0')
{
if (strchr(special_chars, source[i]) != NULL)
{
result[j++]= '\\';
switch (source[i])
{
case '\n':
result[j++]= 'n';
break;
case '\r':
result[j++]= 'r';
break;
case '\b':
result[j++]= 'b';
break;
case '\t':
result[j++]= 't';
break;
case '\x1a':
result[j++]= 'Z';
break;
default:
result[j++]= source[i];
};
}
else
result[j++]= source[i];
++i;
};
result[j]= '\0';
};
};
return result;
}
//----------------------------------------------------------------------------------------------------------------------
/**
* Scans the given string and duplicates it to result while inserting a back slash character in front of each
* character that must be masked.
*
* @param source The source string to examine.
*
* @return A new copy of source with all special characters escaped. This string must be freed by the caller (using g_free).
* @note This function does not escape % and _ as they are used only with pattern matching. Use escape_string_for_search
* if you need these characters escaped too.
* @note covered by unit tests
*/
char * escape_string(const char* source)
{
return internal_escape_string(source, "'\"\x8\n\r\t\x1a\\");
}
//----------------------------------------------------------------------------------------------------------------------
char * escape_string_for_search(const char* source)
{
return internal_escape_string(source, "'\"\x8\n\r\t\x1a\\%_");
}
//----------------------------------------------------------------------------------------------------------------------
/**
* Similar to the (non-portable) itoa function. Converts a number to a string using the given base, which can be
* between 2 and 16 (inclusive).
*
* @param num The number to convert.
* @param base The base to use for the output.
*
* @return A string containing the number in the given base. The string must be freed using g_free (caller is responsible).
*/
char* baseconv(ubigint num, int base)
{
char buffer[65];
char *p;
if (base < 2 || base > 16)
return NULL;
p = &buffer[sizeof(buffer) - 1];
*p = '\0';
do
{
*--p = "0123456789abcdef"[(int)(num % base)];
num /= base;
} while (num != 0);
return g_strdup(p);
}
//----------------------------------------------------------------------------------------------------------------------
/**
* Case insensitive variant of strstr. Use this only for ANSI text!
*
* @param haystack The string to search through.
* @param needle The string to search in haystack.
*
* @return The position of needle in haystack if there is a match or NULL if not.
*/
const char *stristr(const char *haystack, const char *needle)
{
if (!*needle)
{
return haystack;
};
for ( ; *haystack; ++haystack )
{
if (toupper(*haystack) == toupper(*needle))
{
// Matched starting char -- loop through remaining chars.
const char *h, *n;
for ( h = haystack, n = needle; *h && *n; ++h, ++n )
{
if ( toupper(*h) != toupper(*n) )
break;
};
if (!*n) // Matched all of 'needle' to null termination?
return haystack; // Return the start of the match.
}
}
return NULL;
}
//----------------------------------------------------------------------------------------------------------------------
|