# gdcmParser.h

Go to the documentation of this file.

    00001 /*=========================================================================
    00002
    00003   Program: GDCM (Grassroots DICOM). A DICOM library
    00004   Module:  $URL$
    00005
    00006   Copyright (c) 2006-2009 Mathieu Malaterre
    00007   All rights reserved.
    00008   See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
    00009
    00010      This software is distributed WITHOUT ANY WARRANTY; without even
    00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
    00012      PURPOSE.  See the above copyright notice for more information.
    00013
    00014 =========================================================================*/
    00015
    00016 #ifndef __gdcmParser_h
    00017 #define __gdcmParser_h
    00018
    00019 #include "gdcmTag.h"
    00020 #error do not use
    00021 #include "gdcmByteBuffer.h"
    00022
    00023 #include <fstream> // std::ifstream
    00024
    00025 namespace gdcm
    00026 {
    00033 class GDCM_EXPORT Parser /*: private IStream*/
    00034 {
    00035 public:
    00036   typedef enum {
    00037     NoError,
    00038     NoMemoryError,
    00039     SyntaxError,
    00040     NoElementsError,
    00041     TagMismatchError,
    00042     DuplicateAttributeError,
    00043     JunkAfterDocElementError,
    00044     UndefinedEntityError,
    00045     UnexpectedStateError
    00046   } ErrorType;
    00047
    00048   Parser() : UserData(0),Buffer(),ErrorCode(NoError) {}
    00049   ~Parser() {}
    00050
    00051   // Parse some more of the document. The string s is a buffer containing
    00052   // part (or perhaps all) of the document. The number of bytes of s that
    00053   // are part of the document is indicated by len. This means that s
    00054   // doesn't have to be null terminated. It also means that if len is
    00055   // larger than the number of bytes in the block of memory that s points
    00056   // at, then a memory fault is likely. The isFinal parameter informs the
    00057   // parser that this is the last piece of the document. Frequently, the
    00058   // last piece is empty (i.e. len is zero.) If a parse error occurred,
    00059   // it returns 0. Otherwise it returns a non-zero value.
    00060   bool Parse(const char* s, int len, bool isFinal);
    00061
    00062   // Set handlers for start and end tags. Attributes are passed to the
    00063   // start handler as a pointer to a vector of char pointers. Each
    00064   // attribute seen in a start (or empty) tag occupies 2 consecutive places
    00065   // in this vector: the attribute name followed by the attribute value.
    00066   // These pairs are terminated by a null pointer.
    00067   typedef void (*StartElementHandler) (void *userData,
    00068                                        const Tag &tag,
    00069                                        const char *atts[]);
    00070   typedef void (*EndElementHandler) (void *userData, const Tag &name);
    00071   void SetElementHandler(StartElementHandler start, EndElementHandler end);
    00072
    00073   // Return what type of error has occurred.
    00074   ErrorType GetErrorCode() const;
    00075
    00076   // Return a string describing the error corresponding to code.
    00077   // The code should be one of the enums that can be returned from
    00078   // GetErrorCode.
    00079   static const char *GetErrorString(ErrorType const &err);
    00080
    00081   // Return the byte offset of the position.
    00082   unsigned long GetCurrentByteIndex() const;
    00083
    00084   // Miscellaneous functions
    00085
    00086   // The functions in this section either obtain state information from
    00087   // the parser or can be used to dynamically set parser options.
    00088
    00089   // This sets the user data pointer that gets passed to handlers.
    00090   void SetUserData(void *userData);
    00091
    00092   // This returns the user data pointer that gets passed to handlers.
    00093   void * GetUserData() const;
    00094
    00095 protected:
    00096
    00097   // This is just like Parse, except in this case expat provides the buffer.
    00098   // By obtaining the buffer from expat with the GetBuffer function,
    00099   // the application can avoid double copying of the input.
    00100   bool ParseBuffer(int len, bool isFinal);
    00101
    00102   // Obtain a buffer of size len to read a piece of the document into.
    00103   // A NULL value is returned if expat can't allocate enough memory for
    00104   // this buffer. This has to be called prior to every call to ParseBuffer.
    00105   char *GetBuffer(int len);
    00106
    00107   ErrorType Process();
    00108
    00109 private:
    00110   std::ifstream Stream;
    00111   void* UserData;
    00112   ByteBuffer Buffer;
    00113   ErrorType ErrorCode;
    00114
    00115   StartElementHandler StartElement;
    00116   EndElementHandler EndElement;
    00117 };
    00118
    00119 } // end namespace gdcm
    00120
    00121 #endif //__gdcmParser_h