0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TranslatorRole.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; version 3 of the
9  * License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
26 
27 #include <Common/Compat.h>
28 
29 #include "TranslatorRole.h"
30 
31 #include <Common/Error.h>
32 #include <Common/Logger.h>
33 #include <Common/String.h>
34 
35 #include <boost/algorithm/string.hpp>
36 
37 #include <cctype>
38 
39 using namespace Hypertable;
40 using namespace Hypertable::ClusterDefinitionFile;
41 using namespace std;
42 
44  string translated_text;
45  string translated_value;
46  size_t offset = m_text.find_first_of("role:");
47  if (offset == string::npos)
48  HT_THROWF(Error::SYNTAX_ERROR, "Bad role definition on line %d of '%s'",
49  (int)m_lineno, m_fname.c_str());
50  const char *ptr = m_text.c_str() + offset + 5;
51  // skip whitespace
52  while (*ptr && isspace(*ptr))
53  ptr++;
54 
55  const char *base = ptr;
56  if (*ptr == 0 || !isalpha(*ptr))
57  HT_THROWF(Error::SYNTAX_ERROR, "Bad role definition on line %d of '%s'",
58  (int)m_lineno, m_fname.c_str());
59  while (*ptr && (isalnum(*ptr) || *ptr == '_'))
60  ptr++;
61 
62  string name(base, ptr-base);
63 
64  if (name.compare("all") == 0)
65  HT_THROWF(Error::SYNTAX_ERROR, "Invalid role name 'all' on line %d of '%s'",
66  (int)m_lineno, m_fname.c_str());
67 
68  if (*ptr == 0 || !isspace(*ptr))
70  "Invalid role specification for '%s' on line %d of '%s'",
71  name.c_str(), (int)m_lineno, m_fname.c_str());
72 
73  while (*ptr && isspace(*ptr))
74  ptr++;
75 
76  base = 0;
77  while (*ptr) {
78  if (base == 0 && *ptr == '-') {
79  translated_value.append(1, *ptr);
80  }
81  else if (isalnum(*ptr) || *ptr == '_' || *ptr == '-' || *ptr == '.') {
82  if (base == 0)
83  base = ptr;
84  }
85  else if (isspace(*ptr) || *ptr == '+' ||
86  *ptr == ',' || *ptr == '(' || *ptr == ')') {
87  if (base) {
88  string name(base, ptr-base);
89  if (context.roles.count(name) > 0) {
90  translated_value.append("${ROLE_");
91  translated_value.append(name);
92  translated_value.append("}");
93  }
94  else
95  translated_value.append(name);
96  base = 0;
97  }
98  if (isspace(*ptr)) {
99  if (!isspace(translated_value[translated_value.length()-1]))
100  translated_value.append(1, ' ');
101  }
102  else
103  translated_value.append(1, *ptr);
104  }
105  else if (*ptr == '[' || *ptr == ']') {
106  if (base) {
107  translated_value.append(base, ptr-base);
108  base = 0;
109  }
110  translated_value.append(1, *ptr);
111  }
112  else
113  HT_THROWF(Error::SYNTAX_ERROR, "Invalid character '%c' found in role "
114  "definition on line %d of '%s'",
115  *ptr, (int)m_lineno, m_fname.c_str());
116  ptr++;
117  }
118 
119  boost::trim_right_if(translated_value, boost::is_any_of(" \t\n\r"));
120 
121  translated_text.append("ROLE_");
122  translated_text.append(name);
123  translated_text.append("=\"");
124  translated_text.append(translated_value);
125  translated_text.append("\"\n");
126 
127  context.roles.insert(name);
128  context.symbols["ROLE_"+name] = translated_value;
129 
130  return translated_text;
131 }
132 
const string translate(TranslationContext &context) override
Translates a role definition.
STL namespace.
Declarations for TranslatorRole.
Logging routines and macros.
Compatibility Macros for C/C++.
Hypertable definitions
map< string, string > symbols
Map of variable names to default values.
#define HT_THROWF(_code_, _fmt_,...)
Definition: Error.h:490
A String class based on std::string.
Context used to translate cluster definition statements.
Cluster definition file translation definitions.
Definition: Compiler.h:35
Error codes, Exception handling, error logging.