Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members   Related Pages  

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 #ifndef ATLAS_CODECS_PACKED_H
00006 #define ATLAS_CODECS_PACKED_H
00007 
00008 #include <iostream>
00009 #include <stack>
00010 
00011 #include <Atlas/Codecs/Utility.h>
00012 #include <Atlas/Codec.h>
00013 
00014 namespace Atlas { namespace Codecs {
00015 
00016 /*
00017 
00018 The form for each element of this codec is as follows:
00019 
00020 [type][name=][data][|endtype]
00021   
00022 ( ) for lists
00023 [ ] for maps
00024 $ for string
00025 @ for int
00026 # for float
00027 
00028 Sample output for this codec: (whitespace added for clarity)
00029 
00030 [@id=17$name=Fred +28the +2b great+29#weight=1.5(args=@1@2@3)]
00031 
00032 The complete specification is located in cvs at:
00033     forge/protocols/atlas/spec/packed_syntax.html
00034     
00035 */
00036   
00037 class Packed : public Codec<std::iostream>
00038 {
00039 public:
00040     
00041     Packed(std::iostream& s, Atlas::Bridge* b);
00042 
00043     virtual void Poll(bool can_read = true);
00044 
00045     virtual void StreamBegin();
00046     virtual void StreamMessage(const Map&);
00047     virtual void StreamEnd();
00048 
00049     virtual void MapItem(const std::string& name, const Map&);
00050     virtual void MapItem(const std::string& name, const List&);
00051     virtual void MapItem(const std::string& name, long);
00052     virtual void MapItem(const std::string& name, double);
00053     virtual void MapItem(const std::string& name, const std::string&);
00054     virtual void MapEnd();
00055     
00056     virtual void ListItem(const Map&);
00057     virtual void ListItem(const List&);
00058     virtual void ListItem(long);
00059     virtual void ListItem(double);
00060     virtual void ListItem(const std::string&);
00061     virtual void ListEnd();
00062 
00063 protected:
00064     
00065     std::iostream& socket;
00066     Bridge* bridge;
00067 
00068     enum State
00069     {
00070         PARSE_STREAM,
00071         PARSE_MAP,
00072         PARSE_LIST,
00073         PARSE_MAP_BEGIN,
00074         PARSE_LIST_BEGIN,
00075         PARSE_INT,
00076         PARSE_FLOAT,
00077         PARSE_STRING,
00078         PARSE_NAME
00079     };
00080     
00081     std::stack<State> state;
00082 
00083     std::string name;
00084     std::string data;
00085 
00086     inline void ParseStream(char);
00087     inline void ParseMap(char);
00088     inline void ParseList(char);
00089     inline void ParseMapBegin(char);
00090     inline void ParseListBegin(char);
00091     inline void ParseInt(char);
00092     inline void ParseFloat(char);
00093     inline void ParseString(char);
00094     inline void ParseName(char);
00095 
00096     inline const std::string HexEncode(const std::string& data)
00097     {
00098         return hexEncode("+", "+[]()@#$=", data);
00099     }
00100 
00101     inline const std::string HexDecode(const std::string& data)
00102     {
00103         return hexDecode("+", data);
00104     }
00105 };
00106 
00107 } } // namespace Atlas::Codecs
00108 
00109 #endif

Copyright 2000 the respective authors.

This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.