message_reader.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. // $Id$
  15. #include <log/message_reader.h>
  16. #include <log/stringutil.h>
  17. using namespace std;
  18. namespace isc {
  19. namespace log {
  20. // Virtual destructor.
  21. MessageReader::~MessageReader() {
  22. }
  23. // Return error text
  24. string MessageReader::errorText(MessageReader::Status status) {
  25. switch (status) {
  26. case SUCCESS:
  27. return "Success";
  28. case DUPLPRFX:
  29. return "Error, duplicate $PREFIX directive found";
  30. case PRFXEXTRARG:
  31. return "Error, $PREFIX directive has extra arguments";
  32. case PRFXINVARG:
  33. return "Error, $PREFIX directive has an invalid argument";
  34. case PRFXNOARG:
  35. return "Error, $PREFIX directive has no arguments";
  36. case UNRECDIR:
  37. return "Error, unrecognised directive";
  38. default:
  39. return "Unknown message code";
  40. }
  41. }
  42. // Read the file
  43. MessageReader::Status MessageReader::readFile(const string&) {
  44. return OPENIN;
  45. }
  46. // Clear the Message Map
  47. void MessageReader::clear() {
  48. }
  49. // Parse a line of the file
  50. MessageReader::Status MessageReader::processLine(const string& line) {
  51. // Get rid of leading and trailing spaces
  52. string text = StringUtil::trim(line);
  53. if (text.empty()) {
  54. return SUCCESS; // Ignore blank lines
  55. } else if ((text[0] == '#') || (text[0] == '+')) {
  56. return SUCCESS; // Ignore comments or descriptions
  57. } else if (text[0] == '$') {
  58. return directive(text); // Process directives
  59. } else {
  60. return OPENIN;
  61. }
  62. }
  63. // Process directive
  64. MessageReader::Status MessageReader::directive(const std::string& text) {
  65. static string valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
  66. // Regardless of what happens, all prefixes will be uppercase (as will
  67. // all symbols).
  68. string line = text;
  69. StringUtil::uppercase(line);
  70. vector<string> tokens = StringUtil::tokens(line);
  71. // Only $PREFIX is recognised so far, so we'll handle it here.
  72. if (tokens[0] != string("$PREFIX")) {
  73. return UNRECDIR; // Directive is not prefix
  74. } else if (tokens.size() < 2) {
  75. return PRFXNOARG; // Does not have an argument
  76. } else if (tokens.size() > 2) {
  77. return PRFXEXTRARG; // Too many arguments
  78. }
  79. // Token is potentially valid providing it only contains alphabetic
  80. // and numeric characters (and underscores) and does not start with a
  81. // digit.
  82. if ((tokens[1].find_first_not_of(valid) != string::npos) ||
  83. (std::isdigit(tokens[1][0]))) {
  84. // Invalid character in string opr it starts with a digit.
  85. return PRFXINVARG;
  86. }
  87. // All OK - unless the prefix has already been set.
  88. if (prefix_.size() != 0) {
  89. return DUPLPRFX;
  90. }
  91. // Prefix has not been set, so set it and return success.
  92. prefix_ = tokens[1];
  93. return SUCCESS;
  94. }
  95. } // namespace log
  96. } // namespace isc