Settings.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 ** Copyright (c) 2001-2014
00003 **
00004 ** This file is part of the QuickFIX FIX Engine
00005 **
00006 ** This file may be distributed under the terms of the quickfixengine.org
00007 ** license as defined by quickfixengine.org and appearing in the file
00008 ** LICENSE included in the packaging of this file.
00009 **
00010 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00011 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00012 **
00013 ** See http://www.quickfixengine.org/LICENSE for licensing information.
00014 **
00015 ** Contact ask@quickfixengine.org if any conditions of this licensing are
00016 ** not clear to you.
00017 **
00018 ****************************************************************************/
00019 
00020 #ifdef _MSC_VER
00021 #include "stdafx.h"
00022 #else
00023 #include "config.h"
00024 #endif
00025 
00026 #include "Settings.h"
00027 
00028 namespace FIX
00029 {
00030 bool isComment( const std::string& line )
00031 {
00032   if( line.size() == 0 )
00033     return false;
00034 
00035   return line[0] == '#';
00036 }
00037 
00038 bool isSection( const std::string& line )
00039 {
00040   if( line.size() == 0 )
00041     return false;
00042 
00043   return line[0] == '[' && line[line.size()-1] == ']';
00044 }
00045 
00046 std::string splitSection( const std::string& line )
00047 {
00048   return string_strip(std::string( line, 1, line.size() - 2 ));
00049 }
00050 
00051 bool isKeyValue( const std::string& line )
00052 {
00053   return line.find( '=' ) != std::string::npos;
00054 }
00055 
00056 std::pair<std::string, std::string> splitKeyValue( const std::string& line )
00057 {
00058   size_t equals = line.find( '=' );
00059   std::string key = std::string( line, 0, equals );
00060   std::string value = std::string( line, equals + 1, std::string::npos );
00061   return std::pair<std::string, std::string>( key, value );
00062 }
00063 
00064 std::istream& operator>>( std::istream& stream, Settings& s )
00065 {
00066   char buffer[1024];
00067   std::string line;
00068   Settings::Sections::iterator section = s.m_sections.end();;
00069 
00070   while( stream.getline(buffer, 1024) )
00071   {
00072     line = string_strip( buffer );
00073     if( isComment(line) )
00074     {
00075       continue;
00076     }
00077     else if( isSection(line) )
00078     {
00079       section = s.m_sections.insert( s.m_sections.end(), Dictionary(splitSection(line)) );
00080     }
00081     else if( isKeyValue(line) )
00082     {
00083       std::pair<std::string, std::string> keyValue = splitKeyValue( line );
00084       if( section == s.m_sections.end() )
00085         continue;
00086       (*section).setString( keyValue.first, keyValue.second );
00087     }
00088   }
00089   return stream;
00090 }
00091 
00092 Settings::Sections Settings::get( const std::string& name ) const
00093 {
00094   Sections sections;
00095   for ( Sections::size_type i = 0; i < m_sections.size(); ++i )
00096     if ( m_sections[ i ].getName() == name )
00097       sections.push_back( m_sections[ i ] );
00098   return sections;
00099 }
00100 }

Generated on Mon Sep 15 2014 01:23:54 for QuickFIX by doxygen 1.7.6.1 written by Dimitri van Heesch, © 1997-2001