BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
Core.cxx
Go to the documentation of this file.
1#include <ctype.h>
2#include <string.h>
3
4#include "ers/Core.h"
5#include <assert.h>
6#include <cstdlib>
7#include <iostream>
8#include <sstream>
9#include <string.h>
10
11const char* const ers::Core::SEVERITY_NAMES[] = {
12 "none", "debug_0", "debug_1", "debug_2", "debug_3", "information",
13 "notification", "warning", "error", "fatal", "maximum (illegal)" };
14const char* const ers::Core::BOOLEAN_NAMES[] = { "false", "true" };
15const char* const ers::Core::RESPONSIBILITY_NAMES[] = { "precondition", "internal",
16 "subSystem" };
17const std::string ers::Core::empty_string = "";
18
19/**
20 * \brief Transforms a severity_t type into the corresponding string
21 * \param s severity
22 * \return pointer to string with associated text
23 */
24
25const char* ers::Core::to_string( ers::severity_t s ) throw() {
26 const unsigned int index = (unsigned int)s;
27 assert( index <= severity_max );
28 return ers::Core::SEVERITY_NAMES[index];
29} // getSeverityText
30
31/** Parses a string and extracts a severity_t
32 * \param s the string to parse
33 * \return a severity_t value
34 */
35
37 for ( int i = 0; i < severity_max; i++ )
38 {
39 if ( strcmp( s, ers::Core::SEVERITY_NAMES[i] ) == 0 ) return (ers::severity_t)i;
40 } // for
41 return severity_none;
42} // parse_severity
43
44/** Parses a string and extracts a severity_t
45 * \param s the string to parse
46 * \return a severity_t value
47 */
48
49ers::severity_t ers::Core::parse_severity( const std::string& s ) throw() {
50 return parse_severity( s.c_str() );
51} // parse_severity
52
53/** Transforms a responsibility value into a string
54 * \param r the responsibility
55 * \return string with text for responsibility
56 */
57
59 const unsigned int index = (unsigned int)r;
60 assert( index <= resp_max );
61 return RESPONSIBILITY_NAMES[index];
62} // to_string
63
64/** Parses a string and extracts a responsibility
65 * \param s the string to parse
66 * \return a responsibility value
67 */
68
70 for ( int i = 0; i < resp_max; i++ )
71 {
72 if ( strcmp( s, RESPONSIBILITY_NAMES[i] ) == 0 ) return (ers::responsibility_t)i;
73 } // for
74 return resp_unknown;
75} // parse_responsability
76
77/** Parses a string and extracts a responsibility
78 * \param s the string to parse
79 * \return a responsibility value
80 */
81
83 return parse_responsibility( s.c_str() );
84} // parse_responsability
85
86/** Parse a string and extract a boolean
87 * \param s the string to parse
88 * \return 1 if true
89 * \return 0 if false
90 * \return -1 if undefined
91 */
92
93int ers::Core::parse_boolean( const char* s ) throw() {
94 if ( !s ) return -1;
95 if ( !*s ) return -1;
96 if ( strcasecmp( s, BOOLEAN_NAMES[1] ) == 0 ) return 1;
97 if ( strcasecmp( s, BOOLEAN_NAMES[0] ) == 0 ) return 0;
98 return -1;
99} // parse_boolean
100
101/** Convert a boolean to a string
102 * \param b the boolean
103 * \return either the string "true" or "false"
104 */
105
106const char* ers::Core::to_string( bool b ) throw() {
107 int index = b ? 1 : 0;
108 return BOOLEAN_NAMES[index];
109} // to_string
110
111/** This method parses a string in the format used by gcc class names
112 * The string begins with the length of the string expressed in ascii encoded integer,
113 * followed by the character data (no 0 character at the end).
114 * \param ptr pointer to the character data pointer, this pointer is incremented by the
115 * parsing. \return a string containing the string data
116 */
117
118std::string ers::Core::parse_prefix_string( const char** ptr ) throw() {
119 if ( ptr == 0 || *ptr == 0 || **ptr == '\0' ) return ers::Core::empty_string;
120 int l = 0;
121 while ( isdigit( **ptr ) )
122 { // we parse the integer
123 l *= 10;
124 l += ( **ptr ) - '0';
125 ( *ptr )++;
126 } //
127 std::string s( *ptr, l ); // we create the string
128 ( *ptr ) += l;
129 return s;
130} // parse_gcc_string
131
132/** This method tries to unmangle GCC class names given by the RTTI system
133 * This works for GCC 3.2 and 3.4.
134 * It should not be used for anything else than human display or fallback mechanism.
135 * \param name the mangled name of the object
136 * \return an unmangled name
137 */
138
139std::string ers::Core::umangle_gcc_class_name( const char* name ) throw() {
140 if ( name == 0 || strlen( name ) == 0 ) return ers::Core::empty_string;
141 const char* ptr = name;
142 std::ostringstream stream;
143 while ( *ptr == 'P' )
144 { // pointers are denoted with P
145 stream << "*";
146 ptr++;
147 } // if
148 while ( *ptr == 'N' )
149 { // namespace are denoted by N+string
150 ptr++;
151 stream << parse_prefix_string( &ptr ) << "::";
152 } //
153 stream << parse_prefix_string( &ptr );
154 return stream.str();
155} // umangle_gcc_class_name
156
157std::vector<std::string> ers::Core::tokenize( const std::string& text,
158 const std::string& separators ) {
159 std::vector<std::string> result_vector;
160 std::string::size_type start_p, end_p;
161 start_p = text.find_first_not_of( separators );
162 while ( start_p != std::string::npos )
163 {
164 end_p = text.find_first_of( separators, start_p );
165 if ( end_p == std::string::npos ) { end_p = text.length(); }
166 const std::string sub_str = text.substr( start_p, end_p - start_p );
167 result_vector.push_back( sub_str );
168 start_p = text.find_first_not_of( separators, end_p );
169 } // while
170 return result_vector;
171} // tokenize
XmlRpcServer s
static int parse_boolean(const char *s)
string to boolean
Definition Core.cxx:93
static std::string parse_prefix_string(const char **ptr)
prefix string data to string
Definition Core.cxx:118
static std::string umangle_gcc_class_name(const char *name)
unmangles gcc RTTI names
Definition Core.cxx:139
static responsibility_t parse_responsibility(const char *s)
string to responsibility
Definition Core.cxx:69
static std::vector< std::string > tokenize(const std::string &text, const std::string &separators)
Definition Core.cxx:157
static const char * to_string(severity_t s)
severity_t to string
Definition Core.cxx:25
static severity_t parse_severity(const char *s)
string to severity_t
Definition Core.cxx:36
static const std::string empty_string
enum ers::_responsibility_t responsibility_t
enum ers::_severity_t severity_t