The Poor Man's (or Woman's) Intrusion Detection System
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

strutil.cpp 3.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //////////////////////////////////////////////////////////////////////
  2. // String splitter & other useful string tools
  3. // Written by Jonathan A. Foster <ChipMaster@YeOlPiShack.net>
  4. // Started April 23rd, 2021
  5. // Copyright JF Possibilities, Inc. All rights reserved.
  6. // Copied with permission from JF Possibilities's C++ lib.
  7. //////////////////////////////////////////////////////////////////////
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdexcept>
  11. // Sounds an awful lot like a German pastry
  12. #include "strutil.h"
  13. //////////////////////////////////////////////////////////////////////
  14. // Generic string transformations
  15. //////////////////////////////////////////////////////////////////////
  16. std::string trim(const std::string &s) {
  17. int x, y;
  18. for(x=0; x<s.size() && s[x]<=' '; x++);
  19. for(y=s.size()-1; y>=x && s[y]<=' '; y--);
  20. if(y<x) return "";
  21. return s.substr(x, y-x+1);
  22. }
  23. // You have to have C++11+ to get to_string()
  24. std::string str(long long n) {
  25. char s[24]; s[23]=0;
  26. snprintf(s, 23, "%lld", n);
  27. return std::string(s);
  28. }
  29. // TODO: more optimal way to handle?
  30. std::string qesc(const std::string &s) {
  31. std::string r="'";
  32. int i;
  33. for(i=0; i<s.size(); i++) {
  34. if(s[i]=='\'') r+= "''"; else r+=s[i];
  35. }
  36. r+='\'';
  37. return r;
  38. }
  39. //////////////////////////////////////////////////////////////////////
  40. // Splits
  41. //////////////////////////////////////////////////////////////////////
  42. Splits &Splits::operator=(const char *_line) {
  43. if(strlen(_line)>=LineMax)
  44. throw std::runtime_error("Splits::Splits(char*): string is longer than buffer");
  45. strncpy(line, _line, LineMax-1);
  46. split();
  47. return *this;
  48. }
  49. int Splits::split() {
  50. len = count = 0;
  51. if(!*line) return count;
  52. fields[0] = line;
  53. while(len<LineMax && line[len]) {
  54. if(line[len]==sep) {
  55. line[len++]=0;
  56. if(combine) while(len<LineMax && line[len]==sep) len++;
  57. if(++count<FieldMax) {
  58. // this shouldn't happen
  59. if(len>=LineMax) throw
  60. std::runtime_error("Splits::split: end of buffer null missing!");
  61. fields[count] = line+len;
  62. } else
  63. throw std::runtime_error("Splits::split: Too many fields in the line");
  64. } else
  65. len++;
  66. }
  67. return count++;
  68. }
  69. std::istream &operator>>(std::istream &in, Splits &sp) {
  70. if(in.getline(sp.line, sp.LineMax-1)) sp.split();
  71. return in;
  72. }
  73. //////////////////////////////////////////////////////////////////////
  74. // pre_match()
  75. //////////////////////////////////////////////////////////////////////
  76. bool pre_match(const char **list, const std::string &s) {
  77. const char *p = s.c_str();
  78. for(; *list; list++)
  79. if(!strncmp(*list, p, strlen(*list))) return true;
  80. return false;
  81. }
  82. // And if vectors can be used...
  83. bool pre_match(const StringList &list, const std::string &s) {
  84. for(
  85. StringList::const_iterator p=list.begin();
  86. p!=list.end();
  87. p++
  88. )
  89. if(s.substr(0, p->size())==*p) return true;
  90. return false;
  91. }
  92. //////////////////////////////////////////////////////////////////////
  93. // misc
  94. //////////////////////////////////////////////////////////////////////
  95. std::string strip(const std::string &s) {
  96. int x, y;
  97. for(x=0; x<s.size() && s[x]<=' '; x++);
  98. for(y=s.size()-1; y>=x && s[y]<=' '; y--);
  99. if(y<x) return "";
  100. return s.substr(x, y-x+1);
  101. }