• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

epics/apps/crucible/stream/src/StreamProtocol.h (r4864/r2310)

Go to the documentation of this file.
00001 /***************************************************************
00002 * StreamDevice Support                                         *
00003 *                                                              *
00004 * (C) 1999 Dirk Zimoch (zimoch@delta.uni-dortmund.de)          *
00005 * (C) 2005 Dirk Zimoch (dirk.zimoch@psi.ch)                    *
00006 *                                                              *
00007 * This is the protocol parser of StreamDevice.                 *
00008 * Please refer to the HTML files in ../doc/ for a detailed     *
00009 * documentation.                                               *
00010 *                                                              *
00011 * If you do any changes in this file, you are not allowed to   *
00012 * redistribute it any more. If there is a bug or a missing     *
00013 * feature, send me an email and/or your patch. If I accept     *
00014 * your changes, they will go to the next release.              *
00015 *                                                              *
00016 * DISCLAIMER: If this software breaks something or harms       *
00017 * someone, it's your problem.                                  *
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(); // get rid of cygnus-2.7.2 compiler warning
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 API functions:
00148 
00149 NAME: getProtocol()
00150 PURPOSE: read protocol from file, create parser if necessary
00151 RETURNS: a copy of a protocol that must be deleted by the caller
00152 SIDEEFFECTS: file IO, memory allocation for parser
00153 
00154 NAME: free()
00155 PURPOSE: free all parser resources allocated by getProtocol()
00156 Call this function once after the last getProtocol() to clean up.
00157 
00158 */
00159 
00160 #endif

Generated on Tue Dec 10 2013 04:52:22 for ROCsoft by  doxygen 1.7.1