Settings.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifdef _MSC_VER
00021 #include "stdafx.h"
00022 #else
00023 #include "config.h"
00024 #endif
00025 #include "CallStack.h"
00026
00027 #include "Settings.h"
00028
00029 namespace FIX
00030 {
00031 bool isComment( const std::string& line )
00032 {
00033 if( line.size() == 0 )
00034 return false;
00035
00036 return line[0] == '#';
00037 }
00038
00039 bool isSection( const std::string& line )
00040 {
00041 if( line.size() == 0 )
00042 return false;
00043
00044 return line[0] == '[' && line[line.size()-1] == ']';
00045 }
00046
00047 std::string splitSection( const std::string& line )
00048 {
00049 return string_strip(std::string( line, 1, line.size() - 2 ));
00050 }
00051
00052 bool isKeyValue( const std::string& line )
00053 {
00054 return line.find( '=' ) != std::string::npos;
00055 }
00056
00057 std::pair<std::string, std::string> splitKeyValue( const std::string& line )
00058 {
00059 int equals = line.find( '=' );
00060 std::string key = std::string( line, 0, equals );
00061 std::string value = std::string( line, equals + 1, std::string::npos );
00062 return std::pair<std::string, std::string>( key, value );
00063 }
00064
00065 std::istream& operator>>( std::istream& stream, Settings& s )
00066 {
00067 char buffer[1024];
00068 std::string line;
00069 Settings::Sections::iterator section = s.m_sections.end();;
00070
00071 while( stream.getline(buffer, 1024) )
00072 {
00073 line = string_strip( buffer );
00074 if( isComment(line) )
00075 {
00076 continue;
00077 }
00078 else if( isSection(line) )
00079 {
00080 section = s.m_sections.insert( s.m_sections.end(), Dictionary(splitSection(line)) );
00081 }
00082 else if( isKeyValue(line) )
00083 {
00084 std::pair<std::string, std::string> keyValue = splitKeyValue( line );
00085 if( section == s.m_sections.end() )
00086 continue;
00087 (*section).setString( keyValue.first, keyValue.second );
00088 }
00089 }
00090 return stream;
00091 }
00092
00093 Settings::Sections Settings::get( std::string name ) const
00094 { QF_STACK_PUSH(Settings::get)
00095
00096 Sections sections;
00097 for ( Sections::size_type i = 0; i < m_sections.size(); ++i )
00098 if ( m_sections[ i ].getName() == name )
00099 sections.push_back( m_sections[ i ] );
00100 return sections;
00101
00102 QF_STACK_POP
00103 }
00104 }