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.
 
 
 
 

113 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. //////////////////////////////////////////////////////////////////////
  29. // Splits: a util class to divide a line into space sep pieces
  30. //////////////////////////////////////////////////////////////////////
  31. // TODO: implement begin() + end() to make "for( : )" work
  32. // TODO: implement field enclosing & escaping chars
  33. struct Splits {
  34. /// CONFIG ///
  35. enum { FieldMax=256, LineMax=1024 };
  36. /// properties ///
  37. char line[LineMax]; // Line buffer
  38. int len; // Length of line (after split())
  39. char sep; // Separator character.
  40. bool combine; // Treat multiple consecutive seps as one (combine)
  41. char *fields[FieldMax]; // pointers to fields in line
  42. int count; // How many fields there were
  43. // construct
  44. Splits(): count(0), len(0), sep(' '), combine(true) { line[LineMax-1] = 0; }
  45. Splits(const std::string &_line) { *this=_line; }
  46. // Assign from string
  47. inline Splits &operator=(const std::string &_line) { return *this=_line.c_str(); }
  48. Splits &operator=(const char *_line);
  49. // Convert field[] to string
  50. inline std::string operator[](int i) const { std::string s(fields[i]); return s; }
  51. // split line. Returns count.
  52. int split();
  53. };
  54. // istream >> operator: getline() + .split()
  55. std::istream &operator>>(std::istream &in, Splits &sp);
  56. //////////////////////////////////////////////////////////////////////
  57. // TSV version of Splits
  58. //////////////////////////////////////////////////////////////////////
  59. struct TSV: public Splits {
  60. TSV() { sep='\t'; combine=false; }
  61. // Need some weird casties to make C++ remember its base class
  62. TSV(const std::string &_line) { sep='\t'; combine=false; *this=_line; }
  63. inline TSV &operator=(const std::string &_line) { return *this=_line.c_str(); }
  64. inline TSV &operator=(const char *_line) { *((Splits *)this)=_line; return *this; }
  65. };
  66. //////////////////////////////////////////////////////////////////////
  67. // Function to match a list of prefixes against a string
  68. //
  69. // Since C++ < 11 doesn't support constant vector initialization we'll
  70. // do this the old fashioned way with a null terminated char*[].
  71. //////////////////////////////////////////////////////////////////////
  72. bool pre_match(const char **list, const std::string &s);
  73. // And if vectors can be used...
  74. bool pre_match(const StringList &list, const std::string &s);
  75. //////////////////////////////////////////////////////////////////////
  76. // Strip leading and trailing space from a string
  77. //////////////////////////////////////////////////////////////////////
  78. std::string strip(const std::string &s);
  79. #endif