00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef StreamProtocol_h
00022 #define StreamProtocol_h
00023
00024 #include "StreamBuffer.h"
00025 #include <stdio.h>
00026
00027 enum FormatType {NoFormat, ScanFormat, PrintFormat};
00028
00029 class StreamProtocolParser
00030 {
00031 public:
00032
00033 enum Codes
00034 {
00035 eos = 0, skip, whitespace, format, format_field, last_function_code
00036 };
00037
00038 class Client;
00039
00040 class Protocol
00041 {
00042 class Variable;
00043
00044 friend class StreamProtocolParser;
00045
00046 private:
00047 Protocol* next;
00048 Variable* variables;
00049 const StreamBuffer protocolname;
00050 StreamBuffer* commands;
00051 int line;
00052 const char* parameter[10];
00053
00054 Protocol(const char* filename);
00055 Protocol(const Protocol& p, StreamBuffer& name, int line);
00056 StreamBuffer* createVariable(const char* name, int line);
00057 bool compileFormat(StreamBuffer&, const char*& source,
00058 FormatType, Client*);
00059 bool compileCommands(StreamBuffer&, const char*& source, Client*);
00060 bool replaceVariable(StreamBuffer&, const char* varname);
00061 const Variable* getVariable(const char* name);
00062
00063 public:
00064
00065 const StreamBuffer filename;
00066
00067 bool getNumberVariable(const char* varname, unsigned long& value,
00068 unsigned long max = 0xFFFFFFFF);
00069 bool getEnumVariable(const char* varname, unsigned short& value,
00070 const char ** enumstrings);
00071 bool getStringVariable(const char* varname,StreamBuffer& value, bool* defined = NULL);
00072 bool getCommands(const char* handlername, StreamBuffer& code, Client*);
00073 bool compileNumber(unsigned long& number, const char*& source,
00074 unsigned long max = 0xFFFFFFFF);
00075 bool compileString(StreamBuffer& buffer, const char*& source,
00076 FormatType formatType = NoFormat, Client* = NULL, int quoted = false);
00077 bool checkUnused();
00078 ~Protocol();
00079 void report();
00080 };
00081
00082 class Client
00083 {
00084 friend class StreamProtocolParser::Protocol;
00085 virtual bool compileCommand(Protocol*, StreamBuffer& buffer,
00086 const char* command, const char*& args) = 0;
00087 virtual bool getFieldAddress(const char* fieldname,
00088 StreamBuffer& address) = 0;
00089 virtual const char* name() = 0;
00090 public:
00091 virtual ~Client();
00092 };
00093
00094 private:
00095 StreamBuffer filename;
00096 FILE* file;
00097 int line;
00098 int quote;
00099 Protocol globalSettings;
00100 Protocol* protocols;
00101 StreamProtocolParser* next;
00102 static StreamProtocolParser* parsers;
00103 bool valid;
00104
00105 StreamProtocolParser(FILE* file, const char* filename);
00106 Protocol* getProtocol(const StreamBuffer& protocolAndParams);
00107 bool isGlobalContext(const StreamBuffer* commands);
00108 bool isHandlerContext(Protocol&, const StreamBuffer* commands);
00109 static StreamProtocolParser* readFile(const char* file);
00110 bool parseProtocol(Protocol&, StreamBuffer* commands);
00111 int readChar();
00112 bool readToken(StreamBuffer& buffer,
00113 const char* specialchars = NULL, bool eofAllowed = false);
00114 bool parseAssignment(const char* variable, Protocol&);
00115 bool parseValue(StreamBuffer& buffer, bool lazy = false);
00116
00117 protected:
00118 ~StreamProtocolParser();
00119
00120 public:
00121 static Protocol* getProtocol(const char* file,
00122 const StreamBuffer& protocolAndParams);
00123 static void free();
00124 static const char* path;
00125 static const char* printString(StreamBuffer&, const char* string);
00126 void report();
00127 };
00128
00129 inline int getLineNumber(const char* s)
00130 {
00131 int l;
00132 memcpy (&l, s+strlen(s)+1, sizeof(int));
00133 return l;
00134 }
00135
00136 template<class T>
00137 inline const T extract(const char*& string)
00138 {
00139 T p;
00140 memcpy (&p, string, sizeof(T));
00141 string += sizeof(T);
00142 return p;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 #endif