Atlas-C++
Packed.h
00001 // This file may be redistributed and modified only under the terms of
00002 // the GNU Lesser General Public License (See COPYING for details).
00003 // Copyright (C) 2000-2001 Stefanus Du Toit, Michael Day
00004 
00005 // $Id$
00006 
00007 #ifndef ATLAS_CODECS_PACKED_H
00008 #define ATLAS_CODECS_PACKED_H
00009 
00010 #include <Atlas/Codecs/Utility.h>
00011 #include <Atlas/Codec.h>
00012 
00013 #include <iosfwd>
00014 #include <stack>
00015 
00016 namespace Atlas { namespace Codecs {
00017 
00018 /*
00019 
00020 The form for each element of this codec is as follows:
00021 
00022 [type][name=][data][|endtype]
00023   
00024 ( ) for lists
00025 [ ] for maps
00026 $ for string
00027 @ for int
00028 # for float
00029 
00030 Sample output for this codec: (whitespace added for clarity)
00031 
00032 [@id=17$name=Fred +28the +2b great+29#weight=1.5(args=@1@2@3)]
00033 
00034 The complete specification is located in cvs at:
00035     forge/protocols/atlas/spec/packed_syntax.html
00036     
00037 */
00038   
00039 class Packed : public Codec
00040 {
00041 public:
00042     
00043     Packed(std::iostream& s, Atlas::Bridge & b);
00044 
00045     virtual void poll(bool can_read = true);
00046 
00047     virtual void streamBegin();
00048     virtual void streamMessage();
00049     virtual void streamEnd();
00050 
00051     virtual void mapMapItem(const std::string& name);
00052     virtual void mapListItem(const std::string& name);
00053     virtual void mapIntItem(const std::string& name, long);
00054     virtual void mapFloatItem(const std::string& name, double);
00055     virtual void mapStringItem(const std::string& name, const std::string&);
00056     virtual void mapEnd();
00057     
00058     virtual void listMapItem();
00059     virtual void listListItem();
00060     virtual void listIntItem(long);
00061     virtual void listFloatItem(double);
00062     virtual void listStringItem(const std::string&);
00063     virtual void listEnd();
00064 
00065 protected:
00066     
00067     std::iostream& m_socket;
00068     Bridge & m_bridge;
00069 
00070     enum State
00071     {
00072         PARSE_STREAM,
00073         PARSE_MAP,
00074         PARSE_LIST,
00075         PARSE_MAP_BEGIN,
00076         PARSE_LIST_BEGIN,
00077         PARSE_INT,
00078         PARSE_FLOAT,
00079         PARSE_STRING,
00080         PARSE_NAME
00081     };
00082     
00083     std::stack<State> m_state;
00084 
00085     std::string m_name;
00086     std::string m_data;
00087 
00088     inline void parseStream(char);
00089     inline void parseMap(char);
00090     inline void parseList(char);
00091     inline void parseMapBegin(char);
00092     inline void parseListBegin(char);
00093     inline void parseInt(char);
00094     inline void parseFloat(char);
00095     inline void parseString(char);
00096     inline void parseName(char);
00097 
00098     inline const std::string hexEncode(const std::string& data)
00099     {
00100         return hexEncodeWithPrefix("+", "+[]()@#$=", data);
00101     }
00102 
00103     inline const std::string hexDecode(const std::string& data)
00104     {
00105         return hexDecodeWithPrefix("+", data);
00106     }
00107 };
00108 
00109 } } // namespace Atlas::Codecs
00110 
00111 #endif

Copyright 2000-2004 the respective authors.

This document can be licensed under the terms of the GNU Free Documentation License or the GNU General Public License and may be freely distributed under the terms given by one of these licenses.