Settings.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (c) 2001-2014
3 **
4 ** This file is part of the QuickFIX FIX Engine
5 **
6 ** This file may be distributed under the terms of the quickfixengine.org
7 ** license as defined by quickfixengine.org and appearing in the file
8 ** LICENSE included in the packaging of this file.
9 **
10 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
11 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
12 **
13 ** See http://www.quickfixengine.org/LICENSE for licensing information.
14 **
15 ** Contact ask@quickfixengine.org if any conditions of this licensing are
16 ** not clear to you.
17 **
18 ****************************************************************************/
19 
20 #ifdef _MSC_VER
21 #include "stdafx.h"
22 #else
23 #include "config.h"
24 #endif
25 
26 #include <cstring>
27 #include "Settings.h"
28 
29 namespace FIX
30 {
31 bool isComment( const std::string& line )
32 {
33  if( line.size() == 0 )
34  return false;
35 
36  return line[0] == '#';
37 }
38 
39 bool isSection( const std::string& line )
40 {
41  if( line.size() == 0 )
42  return false;
43 
44  return line[0] == '[' && line[line.size()-1] == ']';
45 }
46 
47 std::string splitSection( const std::string& line )
48 {
49  return string_strip(std::string( line, 1, line.size() - 2 ));
50 }
51 
52 bool isKeyValue( const std::string& line )
53 {
54  return line.find( '=' ) != std::string::npos;
55 }
56 
57 std::pair<std::string, std::string> splitKeyValue( const std::string& line )
58 {
59  size_t equals = line.find( '=' );
60  std::string key = std::string( line, 0, equals );
61  std::string value = std::string( line, equals + 1, std::string::npos );
62  return std::pair<std::string, std::string>( key, value );
63 }
64 
65 std::string resolveEnvVars(const std::string& str)
66 {
67  std::string resultStr;
68  size_t actPos = 0;
69  size_t sourceLen = str.length();
70 
71  while (actPos < sourceLen)
72  {
73  char c = str[actPos++];
74  if (actPos < sourceLen)
75  {
76  // escape character
77  if (c == '\\')
78  {
79  c = str[actPos++];
80  switch (c)
81  {
82  case 't' : resultStr.append(1, '\t'); break;
83  case 'r' : resultStr.append(1, '\r'); break;
84  case 'n' : resultStr.append(1, '\n'); break;
85  default :
86  resultStr.append(1, c);
87  break;
88  }
89  continue;
90  }
91 
92  // variable substitution
93  if (c == '$')
94  {
95  bool inBraces = false;
96  c = str[actPos++];
97  if ((c == '(') || (c == '{'))
98  {
99  c = str[actPos++];
100  inBraces = true;
101  }
102 
103  // actPos now points at start of var name
104  if (actPos >= sourceLen)
105  break;
106  std::string varName;
107  while ( (actPos <= sourceLen) )
108  {
109  varName.append(1, c); // this must be done before overwriting c
110  c = str[actPos++];
111  if (std::strchr(" /:;,.=\"'?#+*()[]{}$&%\t\n", c))
112  break;
113  }
114  if (inBraces && (actPos <= sourceLen) && ((c == ')') || (c == '}')))
115  ;
116  else
117  --actPos;
118  // varName contains the name of the variable,
119  // actPos points to first char _after_ variable
120  const char *varValue = 0;
121  if (!varName.empty() && (0 != (varValue = getenv(varName.c_str()))))
122  resultStr.append(varValue);
123  continue;
124  }
125  }
126 
127  // nothing special, just copy
128  resultStr.append(1, c);
129  }
130 
131  return resultStr;
132 }
133 
134 std::istream& operator>>( std::istream& stream, Settings& s )
135 {
136  char buffer[1024];
137  std::string line;
138  Settings::Sections::iterator section = s.m_sections.end();;
139 
140  while( stream.getline(buffer, sizeof(buffer)) )
141  {
142  line = string_strip( buffer );
143  if( isComment(line) )
144  {
145  continue;
146  }
147  else if( isSection(line) )
148  {
149  section = s.m_sections.insert( s.m_sections.end(), Dictionary(splitSection(line)) );
150  }
151  else if( isKeyValue(line) )
152  {
153  std::pair<std::string, std::string> keyValue = splitKeyValue( line );
154  if( section == s.m_sections.end() )
155  continue;
156  (*section).setString( keyValue.first, s.m_resolveEnvVars ? resolveEnvVars(keyValue.second) : keyValue.second );
157  }
158  }
159  return stream;
160 }
161 
162 Settings::Sections Settings::get( const std::string& name ) const
163 {
164  Sections sections;
165  for ( Sections::size_type i = 0; i < m_sections.size(); ++i )
166  if ( m_sections[ i ].getName() == name )
167  sections.push_back( m_sections[ i ] );
168  return sections;
169 }
170 }
FIX::Settings::Sections
std::vector< Dictionary > Sections
Definition: Settings.h:77
FIX::Settings::get
Sections get(const std::string &name) const
Definition: Settings.cpp:179
FIX::string_strip
std::string string_strip(const std::string &value)
Definition: Utility.cpp:84
FIX::isSection
bool isSection(const std::string &line)
Definition: Settings.cpp:56
FIX::resolveEnvVars
std::string resolveEnvVars(const std::string &str)
Definition: Settings.cpp:82
FIX::operator>>
std::istream & operator>>(std::istream &stream, SessionID &sessionID)
Definition: SessionID.h:177
FIX::isComment
bool isComment(const std::string &line)
Definition: Settings.cpp:48
Settings.h
FIX::splitKeyValue
std::pair< std::string, std::string > splitKeyValue(const std::string &line)
Definition: Settings.cpp:74
FIX::splitSection
std::string splitSection(const std::string &line)
Definition: Settings.cpp:64
FIX
Definition: Acceptor.cpp:34
FIX::Settings::m_sections
Sections m_sections
Definition: Settings.h:83
FIX::isKeyValue
bool isKeyValue(const std::string &line)
Definition: Settings.cpp:69

Generated on Wed Apr 29 2020 19:41:30 for QuickFIX by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2001