USBGuard
Software framework that protects your computer against rogue USB devices by implementing basic whitelisting and blacklisting capabilities.
RuleParser.hpp
1 //
2 // Copyright (C) 2015 Red Hat, Inc.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // Authors: Daniel Kopecek <dkopecek@redhat.com>
18 //
19 #pragma once
20 
21 #include "Typedefs.hpp"
22 #include "Rule.hpp"
23 
24 #include <stdexcept>
25 #include <string>
26 
27 #include <cstddef>
28 
29 namespace usbguard
30 {
34  class RuleParserError : public std::exception
35  {
36  public:
37 
47  RuleParserError(const std::string& rule_spec, const std::string& hint = "",
48  const std::string& file = "", size_t error_line = 0, unsigned int error_offset = 0)
49  : _rule_spec(rule_spec),
50  _hint(hint),
51  _offset(error_offset),
52  _file(file),
53  _line(error_line)
54  {
55  }
56 
62  void setHint(const std::string& hint)
63  {
64  _hint = hint;
65  }
66 
72  void setOffset(size_t offset)
73  {
74  _offset = offset;
75  }
76 
84  void setFileInfo(const std::string& file, size_t error_line, size_t error_offset = 0)
85  {
86  _file = file;
87  _line = error_line;
88  _offset = error_offset;
89  }
90 
96  const char* what() const noexcept
97  {
98  return "RuleParserError";
99  }
100 
106  const std::string& rule() const
107  {
108  return _rule_spec;
109  }
110 
116  const std::string& hint() const
117  {
118  return _hint;
119  }
120 
127  bool hasFileInfo() const
128  {
129  return !_file.empty();
130  }
131 
138  const std::string fileInfo() const
139  {
140  return _file + ": line=" + std::to_string(_line) + ": offset=" + std::to_string(_offset);
141  }
142 
148  const std::string& file() const
149  {
150  return _file;
151  }
152 
158  size_t line() const
159  {
160  return _line;
161  }
162 
168  size_t offset() const
169  {
170  return _offset;
171  }
172 
173  protected:
174  const std::string _rule_spec;
175  std::string _hint;
176  size_t _offset;
177  std::string _file;
178  size_t _line;
179  };
180 
192  DLL_PUBLIC Rule parseRuleFromString(const std::string& rule_spec, const std::string& file = std::string(), size_t line = 0,
193  bool trace = false);
194 } /* namespace usbguard */
195 
196 /* vim: set ts=2 sw=2 et */
void setOffset(size_t offset)
Sets the offset where the error occured.
Definition: RuleParser.hpp:72
const char * what() const noexcept
Describes the exception.
Definition: RuleParser.hpp:96
const std::string & rule() const
Returns string representation of the rule.
Definition: RuleParser.hpp:106
const std::string & hint() const
Returns error hint.
Definition: RuleParser.hpp:116
Represents error during the rule parsing.
Definition: RuleParser.hpp:34
const std::string fileInfo() const
Returns string containing file, line and offset where the error occured.
Definition: RuleParser.hpp:138
bool hasFileInfo() const
Decides whether information about error location is set.
Definition: RuleParser.hpp:127
void setHint(const std::string &hint)
Sets the describtion of the error.
Definition: RuleParser.hpp:62
const std::string & file() const
File where the rule is located.
Definition: RuleParser.hpp:148
size_t offset() const
Offset where the error occured.
Definition: RuleParser.hpp:168
size_t line() const
Line number where the rule is located.
Definition: RuleParser.hpp:158
RuleParserError(const std::string &rule_spec, const std::string &hint="", const std::string &file="", size_t error_line=0, unsigned int error_offset=0)
Constructs RuleParserError.
Definition: RuleParser.hpp:47
void setFileInfo(const std::string &file, size_t error_line, size_t error_offset=0)
Sets exact location where the error occured.
Definition: RuleParser.hpp:84