00001 
00002 
00003 #ifndef GDCMPARSER_H
00004 #define GDCMPARSER_H
00005 
00006 #include "gdcmCommon.h"
00007 #include "gdcmVR.h"
00008 #include "gdcmTS.h"
00009 #include "gdcmException.h"
00010 #include "gdcmDictSet.h"
00011 #include "gdcmHeaderEntry.h"
00012 
00013 #include <map>
00014 #include <list>       
00015 
00016 
00017 typedef std::string VRKey;
00018 typedef std::string VRAtr;
00019 typedef std::map<VRKey, VRAtr> VRHT;    
00020 
00021 typedef std::multimap<TagKey, gdcmHeaderEntry *> TagHeaderEntryHT;
00022 typedef std::pair<TagKey, gdcmHeaderEntry *> PairHT;
00023 typedef std::pair<TagHeaderEntryHT::iterator,TagHeaderEntryHT::iterator> IterHT; 
00024 
00025 typedef std::list<gdcmHeaderEntry *> ListTag; 
00026 
00027 typedef std::string GroupKey;
00028 typedef std::map<GroupKey, int> GroupHT;
00029 
00030 
00031 
00032 
00033 
00034 
00035 class GDCM_EXPORT gdcmParser
00036 {
00037 public:
00038    gdcmParser(bool exception_on_error  = false);
00039    gdcmParser(const char *filename, 
00040               bool  exception_on_error = false, 
00041               bool  enable_sequences   = false,
00042               bool  ignore_shadow      = false);
00043    virtual ~gdcmParser(void);
00044 
00045 
00051    void SetPrintLevel(int level) { printLevel = level; };
00052    virtual void Print        (std::ostream &os = std::cout) {PrintEntry(os);};
00053    virtual void PrintEntry   (std::ostream &os = std::cout);
00054    virtual void PrintPubDict (std::ostream &os = std::cout);
00055    virtual void PrintShaDict (std::ostream &os = std::cout);
00056 
00057 
00058    inline std::string GetFileName(void) {return filename;}
00059 
00060 
00061    gdcmDict *GetPubDict(void);
00062    gdcmDict *GetShaDict(void);
00063    bool SetShaDict(gdcmDict *dict);
00064    bool SetShaDict(DictKey dictName);
00065 
00066 
00067    virtual bool IsReadable(void);
00068    bool IsImplicitVRLittleEndianTransferSyntax(void);
00069    bool IsExplicitVRLittleEndianTransferSyntax(void);
00070    bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
00071    bool IsExplicitVRBigEndianTransferSyntax(void);
00072    FileType GetFileType(void);
00073 
00074 
00080    inline TagHeaderEntryHT &GetEntry(void) { return tagHT; };
00081 
00087    inline ListTag &GetListEntry(void) { return listEntries; };
00088 
00089 
00090    FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
00091    bool CloseFile(void);
00092 
00093 
00094    virtual bool Write(FILE *, FileType);
00095 
00096    bool ReplaceOrCreateByNumber(std::string Value, guint16 Group, guint16 Elem);
00097    bool ReplaceOrCreateByNumber(     char  *Value, guint16 Group, guint16 Elem);
00098    bool ReplaceIfExistByNumber (     char  *Value, guint16 Group, guint16 Elem);
00099 
00100 
00101    inline int GetSwapCode(void) { return sw; }
00102    guint16 GetGrPixel(void)  {return GrPixel;}
00103    guint16 GetNumPixel(void) {return NumPixel;}
00104    
00105    guint16 SwapShort(guint16);   
00106    guint32 SwapLong(guint32);    
00107    guint16 UnswapShort(guint16); 
00108    guint32 UnswapLong(guint32);  
00109 
00110 protected:
00111 
00112    int CheckIfEntryExistByNumber(guint16 Group, guint16 Elem ); 
00113    virtual std::string GetEntryByName    (std::string tagName);
00114    virtual std::string GetEntryVRByName  (std::string tagName);
00115    virtual std::string GetEntryByNumber  (guint16 group, guint16 element);
00116    virtual std::string GetEntryVRByNumber(guint16 group, guint16 element);
00117 
00118    virtual bool SetEntryByName  (std::string content, std::string tagName);
00119    virtual bool SetEntryByNumber(std::string content, guint16 group, guint16 element);
00120    virtual bool SetEntryLengthByNumber(guint32 length, guint16 group, guint16 element);
00121 
00122    virtual size_t GetEntryOffsetByNumber  (guint16 Group, guint16 Elem);
00123    virtual void  *GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem);   
00124    virtual void  *LoadEntryVoidArea       (guint16 Group, guint16 Element);
00125    virtual bool   SetEntryVoidAreaByNumber(void *a, guint16 Group, guint16 Elem);
00126 
00127    virtual void UpdateShaEntries(void);
00128 
00129 
00130    gdcmHeaderEntry *GetHeaderEntryByNumber  (guint16 group, guint16 element); 
00131    gdcmHeaderEntry *GetHeaderEntryByName    (std::string Name);
00132    IterHT           GetHeaderEntrySameNumber(guint16 group, guint16 element); 
00133 
00134 
00135    void LoadHeaderEntrySafe(gdcmHeaderEntry *);
00136 
00137    void UpdateGroupLength(bool SkipSequence = false, FileType type = ImplicitVR);
00138    void WriteEntries(FILE *_fp,FileType type);
00139 
00140 
00141    FILE *fp;
00142    FileType filetype; 
00143 
00144    static const unsigned int HEADER_LENGTH_TO_READ; 
00145    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
00146    static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
00147 
00148 protected:
00149    TagHeaderEntryHT tagHT; 
00150    ListTag listEntries;    
00151     
00152    int enableSequences;
00153    int printLevel;
00154    
00155    
00156    guint16 GrPixel;
00157    guint16 NumPixel;
00158    
00159    
00160    int countGrPixel;
00161       
00162 private:
00163    
00164    bool ParseHeader(bool exception_on_error = false) throw(gdcmFormatError);
00165 
00166    void LoadHeaderEntries    (void);
00167    void LoadHeaderEntry      (gdcmHeaderEntry *);
00168    void AddHeaderEntry       (gdcmHeaderEntry *);
00169    void FindHeaderEntryLength(gdcmHeaderEntry *);
00170    void FindHeaderEntryVR    (gdcmHeaderEntry *);
00171    bool CheckHeaderEntryVR   (gdcmHeaderEntry *, VRKey);
00172 
00173    std::string GetHeaderEntryValue  (gdcmHeaderEntry *);
00174    std::string GetHeaderEntryUnvalue(gdcmHeaderEntry *);
00175 
00176    void SkipHeaderEntry          (gdcmHeaderEntry *);
00177    void FixHeaderEntryFoundLength(gdcmHeaderEntry *, guint32);
00178    bool IsHeaderEntryAnInteger   (gdcmHeaderEntry *);
00179 
00180    guint32 FindHeaderEntryLengthOB(void);
00181 
00182    guint16 ReadInt16(void);
00183    guint32 ReadInt32(void);
00184    void    SkipBytes(guint32);
00185 
00186    void Initialise(void);
00187    bool CheckSwap(void);
00188    void SwitchSwapToBigEndian(void);
00189    void SetMaxSizeLoadEntry(long);
00190    void SetMaxSizePrintEntry(long);
00191 
00192    
00193    gdcmDictEntry *GetDictEntryByName  (std::string Name);
00194    gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
00195    gdcmDictEntry *NewVirtualDictEntry(guint16 group, 
00196                                       guint16 element,
00197                                       std::string vr     = "unkn",
00198                                       std::string fourth = "unkn",
00199                                       std::string name   = "unkn");
00200    gdcmDictEntry *NewVirtualDictEntry(gdcmHeaderEntry *);
00201    
00202    
00203    gdcmHeaderEntry *ReadNextHeaderEntry   (void);
00204    gdcmHeaderEntry *NewHeaderEntryByNumber(guint16 group, 
00205                                            guint16 element);
00206    gdcmHeaderEntry *NewHeaderEntryByName  (std::string Name);
00207 
00208 
00209    
00210    
00211    
00212    guint32 GenerateFreeTagKeyInGroup(guint16 group);
00213 
00214    
00215    std::string filename; 
00216 
00217    
00218    gdcmDict *RefPubDict;
00219    
00220    gdcmDict *RefShaDict;
00221 
00222    
00223    int wasUpdated;
00224    
00225    
00226    int ignoreShadow;
00227 
00228    
00229    
00230    int sw;
00231 
00232    
00233    
00234    
00235    
00236    guint32 MaxSizeLoadEntry;
00237    
00238    
00239    
00240    guint32 MaxSizePrintEntry;
00241    
00242 };
00243 
00244 
00245 #endif