Parser.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 "Parser.h"
00028 #include "Utility.h"
00029 #include "FieldConvertors.h"
00030 #include <algorithm>
00031
00032 namespace FIX
00033 {
00034 bool Parser::extractLength( int& length, std::string::size_type& pos,
00035 const std::string& buffer )
00036 throw( MessageParseError )
00037 { QF_STACK_PUSH(Parser::extractLength)
00038
00039 if( !buffer.size() ) return false;
00040
00041 std::string::size_type startPos = buffer.find( "\0019=", 0 );
00042 if( startPos == std::string::npos ) return false;
00043 startPos += 3;
00044 std::string::size_type endPos = buffer.find( "\001", startPos );
00045 if( endPos == std::string::npos ) return false;
00046
00047 std::string strLength( buffer, startPos, endPos - startPos );
00048
00049 try
00050 {
00051 length = IntConvertor::convert( strLength );
00052 if( length < 0 ) throw MessageParseError();
00053 }
00054 catch( FieldConvertError& )
00055 { throw MessageParseError(); }
00056
00057 pos = endPos + 1;
00058 return true;
00059
00060 QF_STACK_POP
00061 }
00062
00063 bool Parser::readFixMessage( std::string& str )
00064 throw( MessageParseError )
00065 { QF_STACK_PUSH(Parser::readFixMessage)
00066
00067 std::string::size_type pos = 0;
00068
00069 if( m_buffer.length() < 2 ) return false;
00070 pos = m_buffer.find( "8=" );
00071 if( pos == std::string::npos ) return false;
00072 m_buffer.erase( 0, pos );
00073
00074 int length = 0;
00075
00076 try
00077 {
00078 if( extractLength(length, pos, m_buffer) )
00079 {
00080 pos += length;
00081 if( m_buffer.size() < pos )
00082 return false;
00083
00084 pos = m_buffer.find( "\00110=", pos-1 );
00085 if( pos == std::string::npos ) return false;
00086 pos += 4;
00087 pos = m_buffer.find( "\001", pos );
00088 if( pos == std::string::npos ) return false;
00089 pos += 1;
00090
00091 str = m_buffer.substr( 0, pos );
00092 m_buffer.erase( 0, pos );
00093 return true;
00094 }
00095 }
00096 catch( MessageParseError& e )
00097 {
00098 if( length > 0 )
00099 m_buffer.erase( 0, pos + length );
00100 else
00101 m_buffer.erase();
00102
00103 throw e;
00104 }
00105
00106 return false;
00107
00108 QF_STACK_POP
00109 }
00110 }