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.
 
 
 
 

94 lines
2.4 KiB

  1. //////////////////////////////////////////////////////////////////////
  2. // String splitter
  3. // Written by Jonathan A. Foster <ChipMaster@YeOlPiShack.net>
  4. // Started April 23rd, 2021
  5. // Copyright JF Possibilities, Inc. All rights reserved.
  6. //////////////////////////////////////////////////////////////////////
  7. #include <string.h>
  8. #include <stdexcept>
  9. // Sounds an awful lot like a German pastry
  10. #include "strutil.h"
  11. //////////////////////////////////////////////////////////////////////
  12. // Splits
  13. //////////////////////////////////////////////////////////////////////
  14. Splits &Splits::operator=(const char *_line) {
  15. if(strlen(_line)>=LineMax)
  16. throw std::runtime_error("Splits::Splits(char*): string is longer than buffer");
  17. strncpy(line, _line, LineMax-1);
  18. split();
  19. return *this;
  20. }
  21. int Splits::split() {
  22. len = count = 0;
  23. if(!*line) return count;
  24. fields[0] = line;
  25. while(len<LineMax && line[len]) {
  26. if(line[len]==sep) {
  27. line[len++]=0;
  28. if(combine) while(len<LineMax && line[len]==sep) len++;
  29. if(++count<FieldMax) {
  30. // this shouldn't happen
  31. if(len>=LineMax) throw
  32. std::runtime_error("Splits::split: end of buffer null missing!");
  33. fields[count] = line+len;
  34. } else
  35. throw std::runtime_error("Splits::split: Too many fields in the line");
  36. } else
  37. len++;
  38. }
  39. return count++;
  40. }
  41. std::istream &operator>>(std::istream &in, Splits &sp) {
  42. if(in.getline(sp.line, sp.LineMax-1)) sp.split();
  43. return in;
  44. }
  45. //////////////////////////////////////////////////////////////////////
  46. // pre_match()
  47. //////////////////////////////////////////////////////////////////////
  48. bool pre_match(const char **list, const std::string &s) {
  49. const char *p = s.c_str();
  50. for(; *list; list++)
  51. if(!strncmp(*list, p, strlen(*list))) return true;
  52. return false;
  53. }
  54. // And if vectors can be used...
  55. bool pre_match(const StringList &list, const std::string &s) {
  56. for(
  57. StringList::const_iterator p=list.begin();
  58. p!=list.end();
  59. p++
  60. )
  61. if(s.substr(0, p->size())==*p) return true;
  62. return false;
  63. }
  64. //////////////////////////////////////////////////////////////////////
  65. // misc
  66. //////////////////////////////////////////////////////////////////////
  67. std::string strip(const std::string &s) {
  68. int x, y;
  69. for(x=0; x<s.size() && s[x]<=' '; x++);
  70. for(y=s.size()-1; y>=x && s[y]<=' '; y--);
  71. if(y<x) return "";
  72. return s.substr(x, y-x+1);
  73. }