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 "Settings.h"
27 
28 namespace FIX
29 {
30 bool isComment( const std::string& line )
31 {
32  if( line.size() == 0 )
33  return false;
34 
35  return line[0] == '#';
36 }
37 
38 bool isSection( const std::string& line )
39 {
40  if( line.size() == 0 )
41  return false;
42 
43  return line[0] == '[' && line[line.size()-1] == ']';
44 }
45 
46 std::string splitSection( const std::string& line )
47 {
48  return string_strip(std::string( line, 1, line.size() - 2 ));
49 }
50 
51 bool isKeyValue( const std::string& line )
52 {
53  return line.find( '=' ) != std::string::npos;
54 }
55 
56 std::pair<std::string, std::string> splitKeyValue( const std::string& line )
57 {
58  size_t equals = line.find( '=' );
59  std::string key = std::string( line, 0, equals );
60  std::string value = std::string( line, equals + 1, std::string::npos );
61  return std::pair<std::string, std::string>( key, value );
62 }
63 
64 std::istream& operator>>( std::istream& stream, Settings& s )
65 {
66  char buffer[1024];
67  std::string line;
68  Settings::Sections::iterator section = s.m_sections.end();;
69 
70  while( stream.getline(buffer, 1024) )
71  {
72  line = string_strip( buffer );
73  if( isComment(line) )
74  {
75  continue;
76  }
77  else if( isSection(line) )
78  {
79  section = s.m_sections.insert( s.m_sections.end(), Dictionary(splitSection(line)) );
80  }
81  else if( isKeyValue(line) )
82  {
83  std::pair<std::string, std::string> keyValue = splitKeyValue( line );
84  if( section == s.m_sections.end() )
85  continue;
86  (*section).setString( keyValue.first, keyValue.second );
87  }
88  }
89  return stream;
90 }
91 
92 Settings::Sections Settings::get( const std::string& name ) const
93 {
94  Sections sections;
95  for ( Sections::size_type i = 0; i < m_sections.size(); ++i )
96  if ( m_sections[ i ].getName() == name )
97  sections.push_back( m_sections[ i ] );
98  return sections;
99 }
100 }
bool isSection(const std::string &line)
Definition: Settings.cpp:38
Sections m_sections
Definition: Settings.h:47
bool isComment(const std::string &line)
Definition: Settings.cpp:30
std::pair< std::string, std::string > splitKeyValue(const std::string &line)
Definition: Settings.cpp:56
std::string splitSection(const std::string &line)
Definition: Settings.cpp:46
Definition: Acceptor.cpp:34
std::istream & operator>>(std::istream &stream, SessionID &sessionID)
Definition: SessionID.h:160
std::string string_strip(const std::string &value)
Definition: Utility.cpp:67
bool isKeyValue(const std::string &line)
Definition: Settings.cpp:51
Sections get(const std::string &name) const
Definition: Settings.cpp:92
std::vector< Dictionary > Sections
Definition: Settings.h:41
For storage and retrieval of key/value pairs.
Definition: Dictionary.h:36
Internal representation of QuickFIX configuration settings.
Definition: Settings.h:38

Generated on Thu Sep 5 2019 11:07:58 for QuickFIX by doxygen 1.8.13 written by Dimitri van Heesch, © 1997-2001