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

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