stringutils.H 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. * */
  13. /*
  14. * stringutils.H
  15. *
  16. * Created on: Apr 9, 2013
  17. * Author: xaxaxa
  18. */
  19. #ifndef URLPARSER_H_
  20. #define URLPARSER_H_
  21. #include <cpoll/cpoll.H>
  22. #include <string>
  23. namespace cppsp
  24. {
  25. #ifndef __CPPSP_TOLOWER
  26. #define __CPPSP_TOLOWER
  27. static inline char tolower(char c) {
  28. if (c <= 'Z' && c >= 'A') c = c - 'A' + 'a';
  29. return c;
  30. }
  31. #endif
  32. struct ci_less: std::binary_function<std::string, std::string, bool>
  33. {
  34. // case-independent (ci) compare_less binary function
  35. struct nocase_compare: public std::binary_function<unsigned char, unsigned char, bool>
  36. {
  37. bool operator()(const unsigned char& c1, const unsigned char& c2) const {
  38. return tolower(c1) < tolower(c2);
  39. }
  40. };
  41. bool operator()(const std::string & s1, const std::string & s2) const {
  42. return std::lexicographical_compare(s1.begin(), s1.end(), // source range
  43. s2.begin(), s2.end(), // dest range
  44. nocase_compare()); // comparison
  45. }
  46. };
  47. void urlDecode(const char* in, int inLen, CP::StreamWriter& sw);
  48. static inline void urlDecode(CP::String in, CP::StreamWriter& sw) {
  49. urlDecode(in.data(), in.length(), sw);
  50. }
  51. CP::String urlDecode(const char* in, int inLen, CP::StringPool& sp);
  52. void urlEncode(const char* in, int inLen, CP::StreamWriter& sw);
  53. static inline void urlEncode(CP::String in, CP::StreamWriter& sw) {
  54. urlEncode(in.data(), in.length(), sw);
  55. }
  56. std::string urlDecode(const char* in, int inLen);
  57. std::string urlEncode(const char* in, int inLen);
  58. static inline std::string urlDecode(const char* in) {
  59. return urlDecode(in, strlen(in));
  60. }
  61. static inline std::string urlEncode(const char* in) {
  62. return urlEncode(in, strlen(in));
  63. }
  64. static inline std::string urlDecode(std::string in) {
  65. return urlDecode(in.data(), in.length());
  66. }
  67. static inline std::string urlEncode(std::string in) {
  68. return urlEncode(in.data(), in.length());
  69. }
  70. typedef Delegate<void(const char* name, int nameLen, const char* value, int valueLen)> queryStringCallback;
  71. void parseQueryString(const char* in, int inLen, queryStringCallback cb, bool decode = true);
  72. void htmlEscape(const char* in, int inLen, CP::StreamWriter& sw);
  73. static inline void htmlEscape(CP::String in, CP::StreamWriter& sw) {
  74. htmlEscape(in.data(), in.length(), sw);
  75. }
  76. std::string htmlEscape(const char* in, int inLen);
  77. static inline std::string htmlEscape(const char* in) {
  78. return htmlEscape(in, strlen(in));
  79. }
  80. static inline std::string htmlEscape(std::string in) {
  81. return htmlEscape(in.data(), in.length());
  82. }
  83. void htmlAttributeEscape(const char* in, int inLen, CP::StreamWriter& sw);
  84. static inline void htmlAttributeEscape(CP::String in, CP::StreamWriter& sw) {
  85. htmlAttributeEscape(in.data(), in.length(), sw);
  86. }
  87. std::string htmlAttributeEscape(const char* in, int inLen);
  88. static inline std::string htmlAttributeEscape(const char* in) {
  89. return htmlAttributeEscape(in, strlen(in));
  90. }
  91. static inline std::string htmlAttributeEscape(std::string in) {
  92. return htmlAttributeEscape(in.data(), in.length());
  93. }
  94. int ci_compare(CP::String s1, CP::String s2);
  95. int rfctime(const tm& time, char* c);
  96. }
  97. #endif /* URLPARSER_H_ */