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.
 
 
 
 

120 lines
3.0 KiB

  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. //////////////////////////////////////////////////////////////////////
  30. // Splits
  31. //////////////////////////////////////////////////////////////////////
  32. Splits &Splits::operator=(const char *_line) {
  33. if(strlen(_line)>=LineMax)
  34. throw std::runtime_error("Splits::Splits(char*): string is longer than buffer");
  35. strncpy(line, _line, LineMax-1);
  36. split();
  37. return *this;
  38. }
  39. int Splits::split() {
  40. len = count = 0;
  41. if(!*line) return count;
  42. fields[0] = line;
  43. while(len<LineMax && line[len]) {
  44. if(line[len]==sep) {
  45. line[len++]=0;
  46. if(combine) while(len<LineMax && line[len]==sep) len++;
  47. if(++count<FieldMax) {
  48. // this shouldn't happen
  49. if(len>=LineMax) throw
  50. std::runtime_error("Splits::split: end of buffer null missing!");
  51. fields[count] = line+len;
  52. } else
  53. throw std::runtime_error("Splits::split: Too many fields in the line");
  54. } else
  55. len++;
  56. }
  57. return count++;
  58. }
  59. std::istream &operator>>(std::istream &in, Splits &sp) {
  60. if(in.getline(sp.line, sp.LineMax-1)) sp.split();
  61. return in;
  62. }
  63. //////////////////////////////////////////////////////////////////////
  64. // pre_match()
  65. //////////////////////////////////////////////////////////////////////
  66. bool pre_match(const char **list, const std::string &s) {
  67. const char *p = s.c_str();
  68. for(; *list; list++)
  69. if(!strncmp(*list, p, strlen(*list))) return true;
  70. return false;
  71. }
  72. // And if vectors can be used...
  73. bool pre_match(const StringList &list, const std::string &s) {
  74. for(
  75. StringList::const_iterator p=list.begin();
  76. p!=list.end();
  77. p++
  78. )
  79. if(s.substr(0, p->size())==*p) return true;
  80. return false;
  81. }
  82. //////////////////////////////////////////////////////////////////////
  83. // misc
  84. //////////////////////////////////////////////////////////////////////
  85. std::string strip(const std::string &s) {
  86. int x, y;
  87. for(x=0; x<s.size() && s[x]<=' '; x++);
  88. for(y=s.size()-1; y>=x && s[y]<=' '; y--);
  89. if(y<x) return "";
  90. return s.substr(x, y-x+1);
  91. }