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.
 
 
 
 

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