encode.c

FIX Adapted for STreaming API encoder example

00001 // $Id: encode_8c-example.html,v 1.2 2006/02/09 19:12:40 Daniel.May Exp $
00002 //
00003 // FIX Adapted for STreaming (sm) (FAST Protocol (sm)) 
00004 //
00005 // Copyright (c) 2005-2006, Pantor Engineering AB (http://www.pantor.com)
00006 // Copyright (c) 2005-2006, SpryWare LLC (http://www.spryware.com)
00007 // Copyright (c) 2005-2006, FIX Protocol Ltd (http://www.fixprotocol.org)
00008 // All Rights Reserved.
00009 //
00010 // This work is distributed under the W3CŪ Software License [1] in the
00011 // hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
00012 // implied warranty of MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS 
00013 // FOR A PARTICULAR PURPOSE.
00014 //
00015 // [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
00016 // [FPL's Intellectual Property details] http://www.fixprotocol.org/ip
00017 // [FAST Protocol details] http://www.fixprotocol.org/fast
00018 // [FAST Protocol credits] http://fixprotocol.org/fastcredits
00019 //
00020 // The example application 'encode' will encode a sample data feed into the FAST format.
00021 // The data feed is a very simple feed containing only 6 fields, SeqNum, Time, Symbol,Price, Size, Exch
00022 // The data is read from stdin a line at a time, parsed, then encoded into fast messages.  The sample input
00023 // file "example.dat" contains a small sample data set.  Ouptut is written to stdout.  See the sample test
00024 // scripts example.sh (Linux) or example.bat (Windows) for example usage.
00025 //
00026 
00032 #include "fastapi.h"
00033 #include "common.h"
00034 
00035 // define the template ID
00036 enum test_tids
00037 {
00038    TEST_BASE_TID = 0,
00039 };
00040 
00041 // define the fields
00042 enum test_fields
00043 {
00044    TID          = MAKE_TAG (FAST_TYPE_U32,  FAST_OP_COPY,  TEST_BASE_TID,  0),
00045    SEQ_NUM      = MAKE_TAG (FAST_TYPE_U32,  FAST_OP_INCR,  TEST_BASE_TID,  1),
00046    TIME         = MAKE_TAG (FAST_TYPE_U32,  FAST_OP_COPY,  TEST_BASE_TID,  2),
00047    SYMBOL       = MAKE_TAG (FAST_TYPE_STR,  FAST_OP_COPY,  TEST_BASE_TID,  3),
00048    PRICE        = MAKE_TAG (FAST_TYPE_U32,  FAST_OP_COPY,  TEST_BASE_TID,  4),
00049    SHARES       = MAKE_TAG (FAST_TYPE_U32,  FAST_OP_COPY,  TEST_BASE_TID,  5),
00050    EXCH         = MAKE_TAG (FAST_TYPE_STR,  FAST_OP_COPY,  TEST_BASE_TID,  6),
00051 };
00052 
00053 
00058 int main(int argc, char* argv[])
00059 {
00060    char data [128];
00061    fast_codec_t *codec;
00062 
00063     // We want binary input from stdio and stdout 
00064     init_platform_io();
00065 
00066     // Create the Codec
00067     codec = fast_create_codec ();
00068     assert(NULL!=codec);
00069 
00070     // Inside some IDE's, it may be ease debugging to set the input file here ...
00071     //freopen( "../example.dat", "r", stdin );
00072 
00073     // Encode the data
00074     while(fgets(data,sizeof(data)-1,stdin) != NULL)
00075     {
00076         // ; SeqNum, Time, Symbol,Price, Size, Exch
00077         // skip comment header
00078         if(';' == data[0])
00079             continue;
00080         
00081         // encode the data, we always start off with a template ID
00082         fast_encode_new_msg (codec, TID);
00083         fast_encode_u32 (codec, TID, 1);
00084         // SeqNum is first 6 chars in input file, starting at pos 0
00085         fast_encode_u32 (codec, SEQ_NUM, fast_ascii_to_u32((u8 *)&data[0],6));              
00086         // Time is next 6 chars in input file, starting at pos 7
00087         fast_encode_u32 (codec, TIME, fast_ascii_to_u32((u8 *)&data[7],6));             
00088         // Symbol is next 3 chars in input file, starting at pos 14
00089         fast_encode_str (codec, SYMBOL, &data[14],3);               
00090         // Price is next 5 chars in input file, starting at pos 18
00091         fast_encode_u32 (codec, PRICE, fast_ascii_to_u32((u8 *)&data[18],5));               
00092         // Shares are next 3 chars in input file, starting at pos 24
00093         fast_encode_u32 (codec, SHARES, fast_ascii_to_u32((u8 *)&data[24],3));              
00094         // Exchange is next char in input file, starting at pos 28
00095         fast_encode_str (codec, EXCH, &data[28],1);
00096 
00097         // we are done with this message
00098         if(fast_encode_end_msg (codec,0) != FAST_ERR_NONE)
00099             fast_print_error (codec, stderr);
00100    }
00101 
00102     // clean up
00103     fast_destroy_codec (codec);
00104     return 0;
00105 }
00106 
00107 

Generated on Thu Feb 9 13:11:32 2006 for fastapi by doxygen 1.4.6-NO    FAST ProtocolSM