/usr/include/ktutil.h is in libkyototycoon-dev 0.9.56-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*************************************************************************************************
* Utility functions
* Copyright (C) 2009-2012 FAL Labs
* This file is part of Kyoto Tycoon.
* This program is free software: you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation, either version
* 3 of the License, or any later version.
* This program 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 General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*************************************************************************************************/
#ifndef _KTUTIL_H // duplication check
#define _KTUTIL_H
#include <ktcommon.h>
namespace kyototycoon { // common namespace
/** The package version. */
extern const char* const VERSION;
/** The library version. */
extern const int32_t LIBVER;
/** The library revision. */
extern const int32_t LIBREV;
/** The extra feature list. */
extern const char* const FEATURES;
/** The default port number. */
const int32_t DEFPORT = 1978;
/**
* Set the signal handler for termination signals.
* @param handler the function pointer of the signal handler.
* @return true on success, or false on failure.
*/
bool setkillsignalhandler(void (*handler)(int));
/**
* Set the signal mask of the current to ignore all.
* @return true on success, or false on failure.
*/
bool maskthreadsignal();
/**
* Switch the process into the background.
* @return true on success, or false on failure.
*/
bool daemonize();
/**
* Execute a shell command.
* @param args an array of the command name and its arguments.
* @return the exit code of the command or `INT32_MIN' on failure.
* @note The command name and the arguments are quoted and meta characters are escaped.
*/
int32_t executecommand(const std::vector<std::string>& args);
/**
* Get the C-style string value of a record in a string map.
* @param map the target string map.
* @param key the key.
* @param sp the pointer to the variable into which the size of the region of the return value
* is assigned. If it is NULL, it is ignored.
* @return the C-style string value of the corresponding record, or NULL if there is no
* corresponding record.
*/
const char* strmapget(const std::map<std::string, std::string>& map, const char* key,
size_t* sp = NULL);
/**
* Print all records in a string vector.
* @param vec the target string vector.
* @param strm the output stream.
*/
void printstrvec(const std::vector<std::string>& vec, std::ostream& strm = std::cout);
/**
* Print all records in a string map.
* @param map the target string map.
* @param strm the output stream.
*/
void printstrmap(const std::map<std::string, std::string>& map, std::ostream& strm = std::cout);
/**
* Break up a URL into elements.
* @param url the URL string.
* @param elems the map object to contain the result elements. The key "self" indicates the URL
* itself. "scheme" indicates the scheme. "host" indicates the host of the server. "port"
* indicates the port number of the server. "authority" indicates the authority information.
* "path" indicates the path of the resource. "file" indicates the file name without the
* directory section. "query" indicates the query string. "fragment" indicates the fragment
* string.
* @note Supported schema are HTTP, HTTPS, FTP, and FILE. Both of absolute URL and relative URL
* are supported.
*/
void urlbreak(const char* url, std::map<std::string, std::string>* elems);
/**
* Escape meta characters in a string with the entity references of XML.
* @param str the string.
* @return the escaped string.
* @note Because the region of the return value is allocated with the the new[] operator, it
* should be released with the delete[] operator when it is no longer in use.
*/
char* xmlescape(const char* str);
/**
* Unescape meta characters in a string with the entity references of XML.
* @param str the string.
* @return the unescaped string.
* @note Because the region of the return value is allocated with the the new[] operator, it
* should be released with the delete[] operator when it is no longer in use.
*/
char* xmlunescape(const char* str);
/**
* Parse a www-form-urlencoded string and store each records into a map.
* @param str the source string.
* @param map the destination string map.
*/
void wwwformtomap(const std::string& str, std::map<std::string, std::string>* map);
/**
* Serialize a string map into a www-form-urlencoded string.
* @param map the source string map.
* @param str the destination string.
*/
void maptowwwform(const std::map<std::string, std::string>& map, std::string* str);
/**
* Parse a TSV string and store each records into a map.
* @param str the source string.
* @param map the destination string map.
*/
void tsvtomap(const std::string& str, std::map<std::string, std::string>* map);
/**
* Serialize a string map into a TSV string.
* @param map the source string map.
* @param str the destination string.
*/
void maptotsv(const std::map<std::string, std::string>& map, std::string* str);
/**
* Encode each record of a string map.
* @param map the string map.
* @param mode the encoding mode. 'B' for Base64 encoding, 'Q' for Quoted-printable encoding,
* 'U' for URL encoding.
*/
void tsvmapencode(std::map<std::string, std::string>* map, int32_t mode);
/**
* Decode each record of a string map.
* @param map the string map.
* @param mode the encoding mode. 'B' for Base64 encoding, 'Q' for Quoted-printable encoding,
* 'U' for URL encoding.
*/
void tsvmapdecode(std::map<std::string, std::string>* map, int32_t mode);
/**
* Check the best suited encoding of a string map.
* @param map the string map.
* @return the the best suited encoding. 0 for the raw format, 'B' for Base64 encoding, 'Q' for
* Quoted-printable encoding,
*/
int32_t checkmapenc(const std::map<std::string, std::string>& map);
/**
* Capitalize letters of a string.
* @param str the string to convert.
* @return the string itself.
*/
char* strcapitalize(char* str);
/**
* Check a string is composed of alphabets or numbers only.
* @return true if it is composed of alphabets or numbers only, or false if not.
*/
bool strisalnum(const char* str);
/**
* Tokenize a string separating by space characters.
* @param str the source string.
* @param tokens a string vector to contain the result tokens.
*/
void strtokenize(const char* str, std::vector<std::string>* tokens);
/**
* Get the Gregorian calendar of a time.
* @param t the source time in seconds from the epoch. If it is kyotocabinet::INT64MAX, the
* current time is specified.
* @param jl the jet lag of a location in seconds. If it is kyotocabinet::INT32MAX, the local
* jet lag is specified.
* @param yearp the pointer to a variable to which the year is assigned. If it is NULL, it is
* not used.
* @param monp the pointer to a variable to which the month is assigned. If it is NULL, it is
* not used. 1 means January and 12 means December.
* @param dayp the pointer to a variable to which the day of the month is assigned. If it is
* NULL, it is not used.
* @param hourp the pointer to a variable to which the hours is assigned. If it is NULL, it is
* not used.
* @param minp the pointer to a variable to which the minutes is assigned. If it is NULL, it is
* not used.
* @param secp the pointer to a variable to which the seconds is assigned. If it is NULL, it is
* not used.
*/
void getcalendar(int64_t t, int32_t jl,
int32_t* yearp = NULL, int32_t* monp = NULL, int32_t* dayp = NULL,
int32_t* hourp = NULL, int32_t* minp = NULL, int32_t* secp = NULL);
/**
* Format a date as a string in W3CDTF.
* @param t the source time in seconds from the epoch. If it is kyotocabinet::INT64MAX, the
* current time is specified.
* @param jl the jet lag of a location in seconds. If it is kyotocabinet::INT32MAX, the local
* jet lag is specified.
* @param buf the pointer to the region into which the result string is written. The size of
* the buffer should be equal to or more than 48 bytes.
*/
void datestrwww(int64_t t, int32_t jl, char* buf);
/**
* Format a date as a string in W3CDTF with the fraction part.
* @param t the source time in seconds from the epoch. If it is Not-a-Number, the current time
* is specified.
* @param jl the jet lag of a location in seconds. If it is kyotocabinet::INT32MAX, the local
* jet lag is specified.
* @param acr the accuracy of time by the number of columns of the fraction part.
* @param buf the pointer to the region into which the result string is written. The size of
* the buffer should be equal to or more than 48 bytes.
*/
void datestrwww(double t, int32_t jl, int32_t acr, char* buf);
/**
* Format a date as a string in RFC 1123 format.
* @param t the source time in seconds from the epoch. If it is kyotocabinet::INT64MAX, the
* current time is specified.
* @param jl the jet lag of a location in seconds. If it is kyotocabinet::INT32MAX, the local
* jet lag is specified.
* @param buf the pointer to the region into which the result string is written. The size of
* the buffer should be equal to or more than 48 bytes.
*/
void datestrhttp(int64_t t, int32_t jl, char* buf);
/**
* Get the time value of a date string.
* @param str the date string in decimal, hexadecimal, W3CDTF, or RFC 822 (1123). Decimal can
* be trailed by "s" for in seconds, "m" for in minutes, "h" for in hours, and "d" for in days.
* @return the time value of the date or INT64_MIN if the format is invalid.
*/
int64_t strmktime(const char* str);
/**
* Get the jet lag of the local time.
* @return the jet lag of the local time in seconds.
*/
int32_t jetlag();
/**
* Get the day of week of a date.
* @param year the year of a date.
* @param mon the month of the date.
* @param day the day of the date.
* @return the day of week of the date. 0 means Sunday and 6 means Saturday.
*/
int32_t dayofweek(int32_t year, int32_t mon, int32_t day);
/**
* Get the local time of a time.
* @param time the time.
* @param result the resulb buffer.
* @return true on success, or false on failure.
*/
bool getlocaltime(time_t time, struct std::tm* result);
/**
* Get the GMT local time of a time.
* @param time the time.
* @param result the resulb buffer.
* @return true on success, or false on failure.
*/
bool getgmtime(time_t time, struct std::tm* result);
/**
* Make the GMT from a time structure.
* @param tm the pointer to the time structure.
* @return the GMT.
*/
time_t mkgmtime(struct std::tm *tm);
/**
* Get the C-style string value of a record in a string map.
*/
inline const char* strmapget(const std::map<std::string, std::string>& map, const char* key,
size_t* sp) {
_assert_(key);
std::map<std::string, std::string>::const_iterator it = map.find(key);
if (it == map.end()) {
if (sp) *sp = 0;
return NULL;
}
if (sp) *sp = it->second.size();
return it->second.c_str();
}
/**
* Print all records in a string vector.
*/
inline void printstrvec(const std::vector<std::string>& vec, std::ostream& strm) {
_assert_(true);
std::vector<std::string>::const_iterator it = vec.begin();
std::vector<std::string>::const_iterator itend = vec.end();
while (it != itend) {
strm << *it << std::endl;
++it;
}
}
/**
* Print all records in a string map.
*/
inline void printstrmap(const std::map<std::string, std::string>& map, std::ostream& strm) {
_assert_(true);
std::map<std::string, std::string>::const_iterator it = map.begin();
std::map<std::string, std::string>::const_iterator itend = map.end();
while (it != itend) {
strm << it->first << '\t' << it->second << std::endl;
++it;
}
}
/**
* Break up a URL into elements.
*/
inline void urlbreak(const char* url, std::map<std::string, std::string>* elems) {
_assert_(url);
char* trim = kc::strdup(url);
kc::strtrim(trim);
char* rp = trim;
char* norm = new char[std::strlen(trim)*3+1];
char* wp = norm;
while (*rp != '\0') {
if (*rp > 0x20 && *rp < 0x7f) {
*(wp++) = *rp;
} else {
*(wp++) = '%';
int32_t num = *(unsigned char*)rp >> 4;
if (num < 10) {
*(wp++) = '0' + num;
} else {
*(wp++) = 'a' + num - 10;
}
num = *rp & 0x0f;
if (num < 10) {
*(wp++) = '0' + num;
} else {
*(wp++) = 'a' + num - 10;
}
}
rp++;
}
*wp = '\0';
rp = norm;
(*elems)["self"] = rp;
bool serv = false;
if (kc::strifwm(rp, "http://")) {
(*elems)["scheme"] = "http";
rp += 7;
serv = true;
} else if (kc::strifwm(rp, "https://")) {
(*elems)["scheme"] = "https";
rp += 8;
serv = true;
} else if (kc::strifwm(rp, "ftp://")) {
(*elems)["scheme"] = "ftp";
rp += 6;
serv = true;
} else if (kc::strifwm(rp, "sftp://")) {
(*elems)["scheme"] = "sftp";
rp += 7;
serv = true;
} else if (kc::strifwm(rp, "ftps://")) {
(*elems)["scheme"] = "ftps";
rp += 7;
serv = true;
} else if (kc::strifwm(rp, "tftp://")) {
(*elems)["scheme"] = "tftp";
rp += 7;
serv = true;
} else if (kc::strifwm(rp, "ldap://")) {
(*elems)["scheme"] = "ldap";
rp += 7;
serv = true;
} else if (kc::strifwm(rp, "ldaps://")) {
(*elems)["scheme"] = "ldaps";
rp += 8;
serv = true;
} else if (kc::strifwm(rp, "file://")) {
(*elems)["scheme"] = "file";
rp += 7;
serv = true;
}
char* ep;
if ((ep = std::strchr(rp, '#')) != NULL) {
(*elems)["fragment"] = ep + 1;
*ep = '\0';
}
if ((ep = std::strchr(rp, '?')) != NULL) {
(*elems)["query"] = ep + 1;
*ep = '\0';
}
if (serv) {
if ((ep = std::strchr(rp, '/')) != NULL) {
(*elems)["path"] = ep;
*ep = '\0';
} else {
(*elems)["path"] = "/";
}
if ((ep = std::strchr(rp, '@')) != NULL) {
*ep = '\0';
if (rp[0] != '\0') (*elems)["authority"] = rp;
rp = ep + 1;
}
if ((ep = std::strchr(rp, ':')) != NULL) {
if (ep[1] != '\0') (*elems)["port"] = ep + 1;
*ep = '\0';
}
if (rp[0] != '\0') (*elems)["host"] = rp;
} else {
(*elems)["path"] = rp;
}
delete[] norm;
delete[] trim;
const char* file = strmapget(*elems, "path");
if (file) {
const char* pv = std::strrchr(file, '/');
if (pv) file = pv + 1;
if (*file != '\0' && std::strcmp(file, ".") && std::strcmp(file, ".."))
(*elems)["file"] = file;
}
}
/**
* Escape meta characters in a string with the entity references of XML.
*/
inline char* xmlescape(const char* str) {
_assert_(str);
const char* rp = str;
size_t bsiz = 0;
while (*rp != '\0') {
switch (*rp) {
case '&': bsiz += 5; break;
case '<': bsiz += 4; break;
case '>': bsiz += 4; break;
case '"': bsiz += 6; break;
default: bsiz++; break;
}
rp++;
}
char* buf = new char[bsiz+1];
char* wp = buf;
while (*str != '\0') {
switch (*str) {
case '&': {
std::memcpy(wp, "&", 5);
wp += 5;
break;
}
case '<': {
std::memcpy(wp, "<", 4);
wp += 4;
break;
}
case '>': {
std::memcpy(wp, ">", 4);
wp += 4;
break;
}
case '"': {
std::memcpy(wp, """, 6);
wp += 6;
break;
}
default: {
*(wp++) = *str;
break;
}
}
str++;
}
*wp = '\0';
return buf;
}
/**
* Unescape meta characters in a string with the entity references of XML.
*/
inline char* xmlunescape(const char* str) {
_assert_(str);
char* buf = new char[std::strlen(str)+1];
char* wp = buf;
while (*str != '\0') {
if (*str == '&') {
if (kc::strfwm(str, "&")) {
*(wp++) = '&';
str += 5;
} else if (kc::strfwm(str, "<")) {
*(wp++) = '<';
str += 4;
} else if (kc::strfwm(str, ">")) {
*(wp++) = '>';
str += 4;
} else if (kc::strfwm(str, """)) {
*(wp++) = '"';
str += 6;
} else {
*(wp++) = *(str++);
}
} else {
*(wp++) = *(str++);
}
}
*wp = '\0';
return buf;
}
/**
* Parse a www-form-urlencoded string and store each records into a map.
*/
inline void wwwformtomap(const std::string& str, std::map<std::string, std::string>* map) {
_assert_(true);
const char* rp = str.data();
const char* pv = rp;
const char* ep = rp + str.size();
while (rp < ep) {
if (*rp == '&' || *rp == ';') {
while (pv < rp && *pv > '\0' && *pv <= ' ') {
pv++;
}
if (rp > pv) {
size_t len = rp - pv;
char* rbuf = new char[len+1];
std::memcpy(rbuf, pv, len);
rbuf[len] = '\0';
char* sep = std::strchr(rbuf, '=');
const char* vp = "";
if (sep) {
*(sep++) = '\0';
vp = sep;
}
size_t ksiz;
char* kbuf = kc::urldecode(rbuf, &ksiz);
size_t vsiz;
char* vbuf = kc::urldecode(vp, &vsiz);
std::string key(kbuf, ksiz);
std::string value(vbuf, vsiz);
(*map)[key] = value;
delete[] vbuf;
delete[] kbuf;
delete[] rbuf;
}
pv = rp + 1;
}
rp++;
}
while (pv < rp && *pv > '\0' && *pv <= ' ') {
pv++;
}
if (rp > pv) {
size_t len = rp - pv;
char* rbuf = new char[len+1];
std::memcpy(rbuf, pv, len);
rbuf[len] = '\0';
const char* vp = "";
char* sep = std::strchr(rbuf, '=');
if (sep) {
*(sep++) = '\0';
vp = sep;
}
size_t ksiz;
char* kbuf = kc::urldecode(rbuf, &ksiz);
size_t vsiz;
char* vbuf = kc::urldecode(vp, &vsiz);
std::string key(kbuf, ksiz);
std::string value(vbuf, vsiz);
(*map)[key] = value;
delete[] vbuf;
delete[] kbuf;
delete[] rbuf;
}
}
/**
* Serialize a string map into a www-form-urlencoded string.
*/
inline void maptowwwform(const std::map<std::string, std::string>& map, std::string* str) {
_assert_(true);
std::map<std::string, std::string>::const_iterator it = map.begin();
std::map<std::string, std::string>::const_iterator itend = map.end();
str->reserve(kc::UINT8MAX);
it = map.begin();
while (it != itend) {
if (str->size() > 0) str->append("&");
char* zstr = kc::urlencode(it->first.data(), it->first.size());
str->append(zstr);
delete[] zstr;
str->append("=");
zstr = kc::urlencode(it->second.data(), it->second.size());
str->append(zstr);
delete[] zstr;
++it;
}
}
/**
* Parse a TSV string and store each records into a map.
*/
inline void tsvtomap(const std::string& str, std::map<std::string, std::string>* map) {
_assert_(true);
std::string::const_iterator it = str.begin();
std::string::const_iterator pv = it;
std::string field;
while (it != str.end()) {
if (*it == '\n') {
if (it > pv) {
std::string::const_iterator ev = it;
if (ev[-1] == '\r') ev--;
std::string::const_iterator rv = pv;
while (rv < ev) {
if (*rv == '\t') {
std::string name(pv, rv);
rv++;
std::string value(rv, ev);
(*map)[name] = value;
break;
}
rv++;
}
}
pv = it + 1;
}
++it;
}
if (it > pv) {
std::string::const_iterator ev = it;
if (ev[-1] == '\r') ev--;
std::string::const_iterator rv = pv;
while (rv < ev) {
if (*rv == '\t') {
std::string name(pv, rv);
rv++;
std::string value(rv, ev);
(*map)[name] = value;
break;
}
rv++;
}
}
}
/**
* Serialize a string map into a TSV string.
*/
inline void maptotsv(const std::map<std::string, std::string>& map, std::string* str) {
_assert_(true);
std::map<std::string, std::string>::const_iterator it = map.begin();
std::map<std::string, std::string>::const_iterator itend = map.end();
size_t size = 0;
while (it != itend) {
size += it->first.size() + it->second.size() + 2;
++it;
}
str->reserve(size);
it = map.begin();
while (it != itend) {
str->append(it->first);
str->append("\t");
str->append(it->second);
str->append("\n");
++it;
}
}
/**
* Encode each record of a string map.
*/
inline void tsvmapencode(std::map<std::string, std::string>* map, int32_t mode) {
_assert_(map);
std::map<std::string, std::string> nmap;
std::map<std::string, std::string>::iterator it = map->begin();
std::map<std::string, std::string>::iterator itend = map->end();
while (it != itend) {
char* kstr, *vstr;
switch (mode) {
case 'B': case 'b': {
kstr = kc::baseencode(it->first.data(), it->first.size());
vstr = kc::baseencode(it->second.data(), it->second.size());
break;
}
case 'Q': case 'q': {
kstr = kc::quoteencode(it->first.data(), it->first.size());
vstr = kc::quoteencode(it->second.data(), it->second.size());
break;
}
case 'U': case 'u': {
kstr = kc::urlencode(it->first.data(), it->first.size());
vstr = kc::urlencode(it->second.data(), it->second.size());
break;
}
default: {
kstr = NULL;
vstr = NULL;
break;
}
}
if (kstr && vstr) {
std::string key(kstr);
std::string value(vstr);
nmap[key] = value;
}
delete[] vstr;
delete[] kstr;
++it;
}
map->swap(nmap);
}
/**
* Decode each record of a string map.
*/
inline void tsvmapdecode(std::map<std::string, std::string>* map, int32_t mode) {
_assert_(map);
std::map<std::string, std::string> nmap;
std::map<std::string, std::string>::iterator it = map->begin();
std::map<std::string, std::string>::iterator itend = map->end();
while (it != itend) {
char* kbuf, *vbuf;
size_t ksiz, vsiz;
switch (mode) {
case 'B': case 'b': {
kbuf = kc::basedecode(it->first.c_str(), &ksiz);
vbuf = kc::basedecode(it->second.c_str(), &vsiz);
break;
}
case 'Q': case 'q': {
kbuf = kc::quotedecode(it->first.c_str(), &ksiz);
vbuf = kc::quotedecode(it->second.c_str(), &vsiz);
break;
}
case 'U': case 'u': {
kbuf = kc::urldecode(it->first.c_str(), &ksiz);
vbuf = kc::urldecode(it->second.c_str(), &vsiz);
break;
}
default: {
kbuf = NULL;
vbuf = NULL;
break;
}
}
if (kbuf && vbuf) {
std::string key(kbuf, ksiz);
std::string value(vbuf, vsiz);
nmap[key] = value;
}
delete[] vbuf;
delete[] kbuf;
++it;
}
map->swap(nmap);
}
/**
* Check the best suited encoding of a string map.
*/
inline int32_t checkmapenc(const std::map<std::string, std::string>& map) {
_assert_(true);
bool bin = false;
size_t blen = 0;
size_t ulen = 0;
std::map<std::string, std::string>::const_iterator it = map.begin();
std::map<std::string, std::string>::const_iterator itend = map.end();
while (it != itend) {
const char* buf = it->first.data();
size_t size = it->first.size();
if (size > kc::UINT8MAX) {
size = kc::UINT8MAX;
bin = true;
}
blen += size * 6 / 4 + 3;
for (size_t i = 0; i < size; i++) {
int32_t c = ((unsigned char*)buf)[i];
if (c < ' ' || c == 0x7f) bin = true;
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') || (c != '\0' && std::strchr("_-.!~*'()", c))) {
ulen++;
} else {
ulen += 3;
}
}
buf = it->second.data();
size = it->second.size();
if (size > kc::UINT8MAX) {
size = kc::UINT8MAX;
bin = true;
}
blen += size * 6 / 4 + 3;
for (size_t i = 0; i < size; i++) {
int32_t c = ((unsigned char*)buf)[i];
if (c < ' ' || c == 0x7f) bin = true;
if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') || (c != '\0' && std::strchr("_-.!~*'()", c))) {
ulen++;
} else {
ulen += 3;
}
}
++it;
}
if (!bin) return 0;
return blen < ulen ? 'B' : 'U';
}
/**
* Capitalize letters of a string.
*/
inline char* strcapitalize(char* str) {
_assert_(str);
char* wp = str;
bool head = true;
while (*wp != '\0') {
if (head && *wp >= 'a' && *wp <= 'z') *wp -= 'a' - 'A';
head = *wp == '-' || *wp == ' ';
wp++;
}
return str;
}
/**
* Check a string is composed of alphabets or numbers only.
*/
inline bool strisalnum(const char* str) {
_assert_(str);
while (*str != '\0') {
if (!(*str >= 'a' && *str <= 'z') && !(*str >= 'A' && *str <= 'Z') &&
!(*str >= '0' && *str <= '9')) return false;
str++;
}
return true;
}
/**
* Tokenize a string separating by space characters.
*/
inline void strtokenize(const char* str, std::vector<std::string>* tokens) {
_assert_(str && tokens);
tokens->clear();
while (*str == ' ' || *str == '\t') {
str++;
}
const char* pv = str;
while (*str != '\0') {
if (*str > '\0' && *str <= ' ') {
if (str > pv) {
std::string elem(pv, str - pv);
tokens->push_back(elem);
}
while (*str > '\0' && *str <= ' ') {
str++;
}
pv = str;
} else {
str++;
}
}
if (str > pv) {
std::string elem(pv, str - pv);
tokens->push_back(elem);
}
}
} // common namespace
#endif // duplication check
// END OF FILE
|