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.
 
 
 
 

115 lines
3.7 KiB

  1. //////////////////////////////////////////////////////////////////////
  2. // String Utilities
  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. // 2021-05-14 <ChipMaster@YeOlPiShack.net>
  9. // Restructure: broke out of monolithic iptraffic.cpp and made its
  10. // own module.
  11. //////////////////////////////////////////////////////////////////////
  12. #ifndef __JFP_STRUTIL_H__
  13. #define __JFP_STRUTIL_H__
  14. #include <string>
  15. #include <vector>
  16. #include <map>
  17. #include <istream>
  18. //////////////////////////////////////////////////////////////////////
  19. // Useful typedefs
  20. //////////////////////////////////////////////////////////////////////
  21. typedef std::vector<std::string> StringList;
  22. typedef std::map<std::string,std::string> NameVal;
  23. //////////////////////////////////////////////////////////////////////
  24. // Generic string transformations
  25. //////////////////////////////////////////////////////////////////////
  26. std::string trim(const std::string &s);
  27. std::string str(long long n);
  28. // "query" escape: ' -> ''
  29. std::string qesc(const std::string &s);
  30. //////////////////////////////////////////////////////////////////////
  31. // Splits: a util class to divide a line into space sep pieces
  32. //////////////////////////////////////////////////////////////////////
  33. // TODO: implement begin() + end() to make "for( : )" work
  34. // TODO: implement field enclosing & escaping chars
  35. struct Splits {
  36. /// CONFIG ///
  37. enum { FieldMax=256, LineMax=1024 };
  38. /// properties ///
  39. char line[LineMax]; // Line buffer
  40. int len; // Length of line (after split())
  41. char sep; // Separator character.
  42. bool combine; // Treat multiple consecutive seps as one (combine)
  43. char *fields[FieldMax]; // pointers to fields in line
  44. int count; // How many fields there were
  45. // construct
  46. Splits(): count(0), len(0), sep(' '), combine(true) { line[LineMax-1] = 0; }
  47. Splits(const std::string &_line) { *this=_line; }
  48. // Assign from string
  49. inline Splits &operator=(const std::string &_line) { return *this=_line.c_str(); }
  50. Splits &operator=(const char *_line);
  51. // Convert field[] to string
  52. inline std::string operator[](int i) const { std::string s(fields[i]); return s; }
  53. // split line. Returns count.
  54. int split();
  55. };
  56. // istream >> operator: getline() + .split()
  57. std::istream &operator>>(std::istream &in, Splits &sp);
  58. //////////////////////////////////////////////////////////////////////
  59. // TSV version of Splits
  60. //////////////////////////////////////////////////////////////////////
  61. struct TSV: public Splits {
  62. TSV() { sep='\t'; combine=false; }
  63. // Need some weird casties to make C++ remember its base class
  64. TSV(const std::string &_line) { sep='\t'; combine=false; *this=_line; }
  65. inline TSV &operator=(const std::string &_line) { return *this=_line.c_str(); }
  66. inline TSV &operator=(const char *_line) { *((Splits *)this)=_line; return *this; }
  67. };
  68. //////////////////////////////////////////////////////////////////////
  69. // Function to match a list of prefixes against a string
  70. //
  71. // Since C++ < 11 doesn't support constant vector initialization we'll
  72. // do this the old fashioned way with a null terminated char*[].
  73. //////////////////////////////////////////////////////////////////////
  74. bool pre_match(const char **list, const std::string &s);
  75. // And if vectors can be used...
  76. bool pre_match(const StringList &list, const std::string &s);
  77. //////////////////////////////////////////////////////////////////////
  78. // Strip leading and trailing space from a string
  79. //////////////////////////////////////////////////////////////////////
  80. std::string strip(const std::string &s);
  81. #endif