/* parse1.h Provides a basic parsing interface for ebstrans Copyright (C) 1993/94 C.Taegert-Kilger, Institut fuer Physiologie und Biokybernetik (IPB), Universitaet Erlangen, Deutschland Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and related publications and that both that copyright notice and this permission notice appear in supporting documentation. The authors make no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty. Send your comments, suggestions or bug reports to ftpebs@uni-erlangen.de Or mail to Institut fuer Physiologie und Biokybernetik Markus Prosch Universitaetsstrasse 17 D-91054 Erlangen Germany $Id: parse1.h,v 1.1 1994/12/08 11:50:12 hospitan Exp $ */ #include #include #include // Error messages #define ERR_OBJECT_ALREADY_DELETED "Object already deleted" #define ERR_PARSER_SINGLE_USE "Parsing object was single use" #define ERR_INV_FUNARGS "Invalid arguments to function" //#define TRACE #undef TRACE #include "trace.h" /* Lists & their construction */ class List { protected: void* arg; int is_deleted; public: // Construction functions List(void* arg_,List* next_=NULL); // Construct with next element == List List(void* arg1_,void* arg2_); virtual ~List(); // Append a new element virtual class List* InsertAfter( class List*); /* Access Fns */ private: class List* search_pointer; public: class List* SetNext(class List*); // Searching class List* GetFirst(); class List* GetContinued(); // Get next pointer class List* GetNext(); void* GetContent(); int GetLength(); protected: class List* next; }; List* operator +(void* el, List l); /* Basic functions */ char* remove_leading_blanks(char** input); char* CreateArgString(int argc, char **argv); /* Basic parsing object: Does not do anything, but only returns true if parsing */ class Parser { protected: int is_deleted; // Avoid multiple deleting void* buf; // Where do we want to have the result? int consume_all; // Input has to be consumed completely int usage; // 1 Obligatory, use once // -1 Facultative, use once // 2 1 Is Obligatory, more possible // -2 May be used 0 or more times int usage_o; // Store original usage int user_disposal; // Does the user do the disposal of this object? // (Only important if in ParserChoice) public: /* Parsing method to call: !!! This one is NOT to be modified in derived object classes !!! */ virtual int Parse( // Input a pointer to a string which is modified char** input, // by the number of chars read, write result to buf, void* buf_ = NULL); // use special parameters if necessary, // return number of relevant bytes in buf // buf_: User may provide a buffer, otherwise // buffer from construction will be used // user_disposal_: If 0 then this object will // be deleted if a super object is deleted virtual void // Set usage to original value ResetUsage(); /* Internal parsing call; this one may be modified. - Modify *input by number of chars parsed */ virtual int Parse1( char** input, void* buf_); int UserDisposal(); // Ask for user disposal flag int Usable(); // Ask if this object may be used virtual void Use(); // Use this object int Usage(); // Ask for usage flag void SetUsage(int); // Modify usage public: virtual int IsSatisfied(); // Ask if this object is ok with the data it // has got public: Parser(void* buf, int usage_=-1, // Usage: Default single use, but not obligatory int consume_all_=0, // No need of consuming the whole input int user_disposal_=0); // Disposal; Disposed by choice object if in there virtual ~Parser(); }; // ParsingChoice may affect internal structures /* Parser for a constant string */ class ParserConstant : public Parser { public: ParserConstant( const char* str_, int* buf_ =NULL, int return_value_=1, int remove_white_=0, int usage_=-1, int consume_all_=0, int user_disposal_=0); virtual int Parse1( char** input,void* buf); protected: const char* str; int remove_white; int return_value; }; /* Basic parsing object: Can be used to scan quite e.g. numbers, strings, anything which can be expressed by a format string */ class ParserFormat : public Parser { /* Internal parsing call; this one may be modified. - Modify *input by number of chars parsed */ virtual int Parse1( char** input, void* buf_); protected: int nread; // How many bytes have we read? void* special; // Are there any extra parameters? public: ParserFormat(void* buf_, // Construct with format specifier const char* format_, int usage_=-1, // Usage: Default single use, but not obligatory int consume_all_=0, // No need of consuming the whole input int user_disposal_=0);// Disposal; Disposed by choice object if in there virtual ~ParserFormat(); protected: const char* format; }; // ParsingChoice may affect internal structures /* Parsing object which parses nothing */ class ParserNothing : public Parser { public: ParserNothing( int consume_all_=0, int user_disposal_=0); virtual int Parse1( char** input, void* buf_); }; /* Parsing object which tries to parse different parameters */ class ParserChoice : public Parser { protected: Parser** objects; // Array of pointers to Parser objects int nobjects; // Size of array int dispose_array; // Flag which decides on disposal // of the objects array public: /* Construct from array */ ParserChoice(int nobjects_, // Initialize with number of objects, Parser** objects_, // Pointer to array of objects, int usage_=-1, // Usage, int consume_all_=0, // No need of consuming the whole input int user_disposal_=0); // Disposal flag /* Construct from List */ ParserChoice(List* objects_, int usage_=-1, int consume_all_=0, int user_disposal_=0); virtual void ResetUsage(); ~ParserChoice(); virtual int Parse1( // Input a pointer to a string which is modified char** input, // by the number of chars read, write result to buf, void* buf_ = NULL); // use special parameters if necessary, modify // usage, return number of relevant bytes in buf // buf_: User may provide a buffer // user_disposal_: If 0 then the object will // be deleted if a super object is deleted virtual int AllElementsSatisfied();// Ask if all elements are ok with the data they // have got }; /* Parsing object which tries to parse different parameters */ class ParserBag : public ParserChoice { public: ParserBag(int nobjects_, // Initialize with number of objects, Parser** objects_, // Pointer to array of objects, int usage_=-1, // Usage, int consume_all_=0, // No need of consuming the whole input int user_disposal_=0); // Disposal flag /* Construct from List */ ParserBag(List* objects_, int usage_=-1, int consume_all_=0, int user_disposal_=0); virtual int Parse1( // Input a pointer to a string which is modified char** input, // by the number of chars read, write result to buf, void* buf_ = NULL); // use special parameters if necessary, modify // usage, return number of relevant bytes in buf // buf_: User may provide a buffer // user_disposal_: If 0 then the object will // be deleted if a super object is deleted }; /* Special parser for tags */ class ParserTag : public ParserBag { public: ParserTag( const char* tag, // Tag to find Parser* p_contents_, // Parser for contents int usage_=-1, // Usage: Default single use int consume_all_=0, // No need of consuming the whole input int user_disposal_=0);// Disposal; Disposed by choice object if in there }; /* A selective parser */ class ParserSelection: public ParserChoice { public: ParserSelection(Parser* parser1,Parser* parser2,int usage=-1, int consume_all_=0, int user_disposal_=0); ParserSelection(List* parsers,int usage=-1, int consume_all_=0,int user_disposal_=0); protected: virtual int Parse1( char** input, void* buf); }; /* A list parser */ class ParserList: public Parser { public: virtual int InputFn(); // InputFn is called each time a list element // is successfully parsed; only if InputFn // says ok success is returned typedef int UseInputFn(); ParserList( Parser* parser_el_, Parser* parser_separator_, UseInputFn* input_fn_=NULL, int usage_=-1, int consume_all_=0, int user_disposal_=0); virtual ~ParserList(); protected: Parser* parser_el; Parser* parser_separator; char separator; UseInputFn* input_fn; virtual int Parse1( // Input a pointer to a string which is modified char** input, // by the number of chars read, write result to buf, void* buf_ = NULL); // use special parameters if necessary, modify // usage, return number of relevant bytes in buf // buf_: User may provide a buffer // user_disposal_: If 0 then the object will // be deleted if a super object is deleted }; /* A general parsing procedur which uses a parse_struct */ int parse(char** input,struct parse_struct* parse_struct); /* Some special type parsing routines */ int parse_int(char** input, void* buf, void* special = NULL);