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