hstring.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "hstring.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdarg.h>
  5. #include <iostream>
  6. #include <sstream>
  7. int vscprintf(const char* fmt, va_list ap) {
  8. return vsnprintf(NULL, 0, fmt, ap);
  9. }
  10. std::string asprintf(const char* fmt, ...) {
  11. va_list ap;
  12. va_start(ap, fmt);
  13. int len = vscprintf(fmt, ap) + 1;
  14. va_end(ap);
  15. // must recall va_start in linux
  16. va_start(ap, fmt);
  17. char* buf = (char*)malloc(len);
  18. vsnprintf(buf, len, fmt, ap);
  19. va_end(ap);
  20. buf[len-1] = '\0';
  21. std::string res(buf);
  22. free(buf);
  23. return res;
  24. }
  25. StringList split(std::string& str, char delim){
  26. std::stringstream ss;
  27. ss << str;
  28. std::string item;
  29. StringList res;
  30. while (std::getline(ss, item, delim)){
  31. res.push_back(item);
  32. }
  33. return res;
  34. }
  35. std::string trim(std::string& str){
  36. std::string::size_type pos1 = str.find_first_not_of(" \t\r\n");
  37. if (pos1 == std::string::npos) return "";
  38. std::string::size_type pos2 = str.find_last_not_of(" \t\r\n");
  39. return str.substr(pos1, pos2-pos1+1);
  40. }
  41. std::string replace(std::string& str, std::string& find, std::string& rep){
  42. std::string::size_type pos = 0;
  43. std::string::size_type a = find.size();
  44. std::string::size_type b = rep.size();
  45. while ((pos = str.find(find,pos)) != std::string::npos){
  46. str.replace(pos, a, rep);
  47. pos += b;
  48. }
  49. return str;
  50. }
  51. string basename(string& str){
  52. string::size_type pos1 = str.find_last_not_of("/\\");
  53. if (pos1 == string::npos){
  54. return "/";
  55. }
  56. string::size_type pos2 = str.find_last_of("/\\", pos1);
  57. if (pos2 == string::npos){
  58. pos2 = 0;
  59. }else{
  60. pos2++;
  61. }
  62. return str.substr(pos2, pos1-pos2+1);
  63. }
  64. string dirname(string& str){
  65. string::size_type pos1 = str.find_last_not_of("/\\");
  66. if (pos1 == string::npos){
  67. return "/";
  68. }
  69. string::size_type pos2 = str.find_last_of("/\\", pos1);
  70. if (pos2 == string::npos){
  71. return ".";
  72. }else if (pos2 == 0){
  73. pos2 = 1;
  74. }
  75. return str.substr(0, pos2);
  76. }
  77. string filename(string& str){
  78. string::size_type pos1 = str.find_last_of("/\\");
  79. if (pos1 == string::npos){
  80. pos1 = 0;
  81. }else{
  82. pos1++;
  83. }
  84. string file = str.substr(pos1, -1);
  85. string::size_type pos2 = file.find_last_of(".");
  86. if (pos2 == string::npos){
  87. return file;
  88. }
  89. return file.substr(0, pos2);
  90. }
  91. string suffixname(string& str){
  92. string::size_type pos1 = str.find_last_of("/\\");
  93. if (pos1 == string::npos){
  94. pos1 = 0;
  95. }else{
  96. pos1++;
  97. }
  98. string file = str.substr(pos1, -1);
  99. string::size_type pos2 = file.find_last_of(".");
  100. if (pos2 == string::npos){
  101. return "";
  102. }
  103. return file.substr(pos2+1, -1);
  104. }