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