GDCM 2.2.1

- Main Page
- Related Pages
- Namespaces
- Classes
- Files
- Examples

- File List
- File Members

gdcmParser.h
Go to the documentation of this file.
1 /*=========================================================================
2
3 Program: GDCM (Grassroots DICOM). A DICOM library
4
5 Copyright (c) 2006-2011 Mathieu Malaterre
6 All rights reserved.
7 See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
8
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notice for more information.
12
13 =========================================================================*/
14
15 #ifndef GDCMPARSER_H
16 #define GDCMPARSER_H
17
18 #include "gdcmTag.h"
19 #error do not use
20 #include "gdcmByteBuffer.h"
21
22 #include <fstream> // std::ifstream
23
24 namespace gdcm
25 {
32 class GDCM_EXPORT Parser /*: private IStream*/
33 {
34 public:
35 typedef enum {
36 NoError,
37 NoMemoryError,
38 SyntaxError,
39 NoElementsError,
40 TagMismatchError,
41 DuplicateAttributeError,
42 JunkAfterDocElementError,
43 UndefinedEntityError,
44 UnexpectedStateError
45 } ErrorType;
46
47 Parser() : UserData(0),Buffer(),ErrorCode(NoError) {}
48 ~Parser() {}
49
50 // Parse some more of the document. The string s is a buffer containing
51 // part (or perhaps all) of the document. The number of bytes of s that
52 // are part of the document is indicated by len. This means that s
53 // doesn't have to be null terminated. It also means that if len is
54 // larger than the number of bytes in the block of memory that s points
55 // at, then a memory fault is likely. The isFinal parameter informs the
56 // parser that this is the last piece of the document. Frequently, the
57 // last piece is empty (i.e. len is zero.) If a parse error occurred,
58 // it returns 0. Otherwise it returns a non-zero value.
59 bool Parse(const char* s, int len, bool isFinal);
60
61 // Set handlers for start and end tags. Attributes are passed to the
62 // start handler as a pointer to a vector of char pointers. Each
63 // attribute seen in a start (or empty) tag occupies 2 consecutive places
64 // in this vector: the attribute name followed by the attribute value.
65 // These pairs are terminated by a null pointer.
66 typedef void (*StartElementHandler) (void *userData,
67 const Tag &tag,
68 const char *atts[]);
69 typedef void (*EndElementHandler) (void *userData, const Tag &name);
70 void SetElementHandler(StartElementHandler start, EndElementHandler end);
71
72 // Return what type of error has occurred.
73 ErrorType GetErrorCode() const;
74
75 // Return a string describing the error corresponding to code.
76 // The code should be one of the enums that can be returned from
77 // GetErrorCode.
78 static const char *GetErrorString(ErrorType const &err);
79
80 // Return the byte offset of the position.
81 unsigned long GetCurrentByteIndex() const;
82
83 // Miscellaneous functions
84
85 // The functions in this section either obtain state information from
86 // the parser or can be used to dynamically set parser options.
87
88 // This sets the user data pointer that gets passed to handlers.
89 void SetUserData(void *userData);
90
91 // This returns the user data pointer that gets passed to handlers.
92 void * GetUserData() const;
93
94 protected:
95
96 // This is just like Parse, except in this case expat provides the buffer.
97 // By obtaining the buffer from expat with the GetBuffer function,
98 // the application can avoid double copying of the input.
99 bool ParseBuffer(int len, bool isFinal);
100
101 // Obtain a buffer of size len to read a piece of the document into.
102 // A NULL value is returned if expat can't allocate enough memory for
103 // this buffer. This has to be called prior to every call to ParseBuffer.
104 char *GetBuffer(int len);
105
106 ErrorType Process();
107
108 private:
109 std::ifstream Stream;
110 void* UserData;
111 ByteBuffer Buffer;
112 ErrorType ErrorCode;
113
114 StartElementHandler StartElement;
115 EndElementHandler EndElement;
116 };
117
118 } // end namespace gdcm
119
120 #endif //GDCMPARSER_H