rambrain
regexmatcher.cpp
Go to the documentation of this file.
1 /* rambrain - a dynamical physical memory extender
2  * Copyright (C) 2015 M. Imgrund, A. Arth
3  * mimgrund (at) mpifr-bonn.mpg.de
4  * arth (at) usm.uni-muenchen.de
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "regexmatcher.h"
21 #include <sstream>
22 
23 #ifdef USE_XPRESSIVE
24 
25 #include <boost/xpressive/xpressive.hpp>
26 #define COMPILE_RGX(name, rx) sregex name = sregex::compile ( rx )
27 using namespace boost::xpressive;
28 
29 #else
30 
31 #include <regex>
32 #define COMPILE_RGX(name, rx) regex name( rx )
33 
34 #endif
35 
36 
37 namespace rambrain
38 {
39 
40 regexMatcher::regexMatcher()
41 {
42 }
43 
44 bool regexMatcher::matchConfigBlock ( const string &str, const string &blockname ) const
45 {
46  const COMPILE_RGX ( rgx, "\\s*\\[" + blockname + "\\]\\s*" );
47  return regex_match ( str, rgx );
48 }
49 
50 pair<string, string> regexMatcher::matchKeyEqualsValue ( const string &str, int valueType ) const
51 {
52  return matchKeyEqualsValue ( str, "[a-zA-Z]+", valueType );
53 }
54 
55 pair<string, string> regexMatcher::matchKeyEqualsValue ( const string &str, const string &key, int valueType ) const
56 {
57  pair<string, string> res;
58  smatch match;
59  const COMPILE_RGX ( rgx, "\\s*(" + key + ")\\s*=\\s*(" + createRegexMatching ( valueType ) + ")\\s*" );
60 
61  if ( regex_match ( str, match, rgx ) ) {
62  res.first = match[1];
63  res.second = match[2];
64  }
65 
66  return res;
67 }
68 
69 string regexMatcher::createRegexMatching ( int type ) const
70 {
71  stringstream ss;
72 
73  if ( type & swapfilename ) {
74  string ant = "[\\/\\.0-9a-zA-Z-_\\\\]";
75  ss << "~?" << ant << "+\\%d" << ant << "*\\%d" << ant << "*";
76  } else {
77  if ( type & boolean ) {
78  ss << "true|True|TRUE|false|False|FALSE";
79  if ( type & integer ) {
80  ss << "|\\d+";
81  }
82  } else {
83  ss << "[";
84  if ( type & integer || type & floating ) {
85  ss << "0-9";
86  }
87  if ( type & text ) {
88  ss << "a-zA-Z";
89  }
90  if ( type & alphanumtext ) {
91  ss << "\\/\\.0-9a-zA-Z-_\\%\\\\";
92  }
93  ss << "]+";
94  if ( type & floating ) {
95  ss << "\\.?\\d*f?";
96  }
97  if ( type & units ) {
98  ss << "\\s*[a-zA-Z]*";
99  }
100  }
101  }
102 
103  return ss.str();
104 }
105 
106 pair<double, string> regexMatcher::splitDoubleValueUnit ( const string &str ) const
107 {
108  pair<double, string> res;
109  const COMPILE_RGX ( rgx, "([0-9]+\\.?\\d*f?)\\s*([a-zA-Z]*)" );
110  smatch match;
111 
112  if ( regex_match ( str, match, rgx ) ) {
113  res.first = atof ( static_cast<string> ( match[1] ).c_str() );
114  if ( match.size() > 2 ) {
115  res.second = match[2];
116  }
117  }
118 
119  return res;
120 }
121 
122 pair<long long, string> regexMatcher::splitIntegerValueUnit ( const string &str ) const
123 {
124  pair<unsigned long long, string> res;
125  const COMPILE_RGX ( rgx, "([0-9]+)\\s*([a-zA-Z]*)" );
126  smatch match;
127 
128  if ( regex_match ( str, match, rgx ) ) {
129  res.first = atoll ( static_cast<string> ( match[1] ).c_str() );
130  if ( match.length() > 2 ) {
131  res.second = match[2];
132  }
133  }
134 
135  return res;
136 }
137 
138 string regexMatcher::substituteHomeDir ( const string &source, const string &homedir ) const
139 {
140  const COMPILE_RGX ( rgx, "~" );
141 
142  string res = regex_replace ( source, rgx, homedir );
143 
144  return res;
145 }
146 
147 }
148