From b1140dbfb41d22b5b0790bc8da676f6610cc45b0 Mon Sep 17 00:00:00 2001 From: frog Date: Mon, 2 Aug 2004 14:06:57 +0000 Subject: [PATCH] * gdcmPython/CMakeLists.txt: SWIG_FLAGS doesn't declare includeall to avoid inclusion recursion until STL is reached. * src/gdcmDocument.[h|cxx]: exceptions substituted to errno C-style mecanism. errno.h is not included in gdcm anymore. * src/gdcmException.h: introduced new gdcmFormatUnexpected class (gdcmFormatError now inherits from gdcmFormatUnexpected). * TODO updated --- ChangeLog | 9 ++++ TODO | 15 ------ gdcmPython/CMakeLists.txt | 11 +++- src/gdcmDocument.cxx | 103 +++++++++++++++++++++++--------------- src/gdcmDocument.h | 14 +++--- src/gdcmException.h | 41 +++++++++------ 6 files changed, 115 insertions(+), 78 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3a66c168..84ed0664 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2004-08-02 Eric Boix + * gdcmPython/CMakeLists.txt: SWIG_FLAGS doesn't declare includeall + to avoid inclusion recursion until STL is reached. + * src/gdcmDocument.[h|cxx]: exceptions substituted to errno C-style + mecanism. errno.h is not included in gdcm anymore. + * src/gdcmException.h: introduced new gdcmFormatUnexpected class + (gdcmFormatError now inherits from gdcmFormatUnexpected). + * TODO updated + 2004-07-06 Eric Boix * src/gdcmDicomDir.cxx, gdcmDocEntrySet.cxx: removed inclusion of errno.h * src/gdcmDocument.[h|cxx], gdcmFile.[h|cxx], gdcmHeader.[h|cxx]: diff --git a/TODO b/TODO index 31b1fe98..86c7f7cb 100644 --- a/TODO +++ b/TODO @@ -16,21 +16,6 @@ Convert the C-like IO to C++ IO: binary IO are available at http://www.angelfire.com/country/aldev0/cpphowto/cpp_BinaryFileIO.html ----------------------------------------------------------------------------- -Switch from errno C-style to exceptions: - Goal: remove all the C-oriented errno (#include ) and switch - to C++ exceptions. - Note: - only src/gdcmDocument.cxx includes errno.h - - when exceptions were first introduced within gdcm, it caused - gdcmPython to be uncompatible within the corresponding version - of wxPython (refer to the first lines of - Test/ExceptionAndPython/README). Hence we reverted to the old - errno mecanisme. Since wxPython seems not to be problem anymore - (check it with Test/ExceptionAndPython) we can now move back to C++ - exceptions. ------------------------------------------------------------------------------ -CLEANUP: - - Remove ignore_shadow from gdcmDocument(). ------------------------------------------------------------------------------ * vtk/vtkGdcmHeader.cxx: if speed becomes a concern some changes can be made at the cost of memory consumption (refer to header of vtk/vtkGdcmHeader.cxx) diff --git a/gdcmPython/CMakeLists.txt b/gdcmPython/CMakeLists.txt index 2b094c63..4c9ebd0e 100644 --- a/gdcmPython/CMakeLists.txt +++ b/gdcmPython/CMakeLists.txt @@ -22,7 +22,16 @@ IF(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 1.9) #SET_SOURCE_FILES_PROPERTIES(gdcm.i PROPERTIES PYTHON ON) SET_SOURCE_FILES_PROPERTIES(gdcm.i PROPERTIES CPLUSPLUS ON) - SET_SOURCE_FILES_PROPERTIES(gdcm.i PROPERTIES SWIG_FLAGS "-includeall") + # Setting SWIG_FLAGS to "-includeall" at the following line looks like + # a bad idea since swig tries to reculsively include all the referenced + # files, "including" the C++ , , ... Of course swig + # has no way of finding the path to those files (unless it is told by + # cmake) since they are related to the compiler installation layout. + # Anyhow, since this inclusion recursion is not necessary, just don't + # do it. + # Note: apparently the line is required in order to avoid a NOTFOUND + # as argument of swig on invocation of make. + SET_SOURCE_FILES_PROPERTIES(gdcm.i PROPERTIES SWIG_FLAGS "") SWIG_ADD_MODULE(gdcm python gdcm.i) #gdcm_wrap.cxx SWIG_LINK_LIBRARIES(gdcm gdcm ${PYTHON_LIBRARIES}) diff --git a/src/gdcmDocument.cxx b/src/gdcmDocument.cxx index 36d1703c..22b09570 100644 --- a/src/gdcmDocument.cxx +++ b/src/gdcmDocument.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDocument.cxx,v $ Language: C++ - Date: $Date: 2004/08/01 03:20:23 $ - Version: $Revision: 1.63 $ + Date: $Date: 2004/08/02 14:06:57 $ + Version: $Revision: 1.64 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -25,7 +25,6 @@ #include "gdcmUtil.h" #include "gdcmDebug.h" -#include #include // For nthos: @@ -1499,8 +1498,12 @@ void gdcmDocument::LoadDocEntry(gdcmDocEntry* entry) } // to be sure we are at the end of the value ... fseek(Fp, (long)entry->GetOffset()+(long)entry->GetLength(), SEEK_SET); - - return; //FIXME FIXME FIXME FIXME ???? + // Following return introduced by JPR on version 1.25. Since the + // treatement of a ValEntry is never executed (doh!) this means + // we were lucky up to now because we NEVER encountered a ValEntry + // whose length was bigger thant MaxSizeLoadEntry !? I can't believe + // this could ever work... + return; //FIXME FIXME FIXME FIXME JPR ???? // Be carefull : a BinEntry IS_A ValEntry ... if (gdcmValEntry* valEntryPtr = dynamic_cast< gdcmValEntry* >(entry) ) @@ -1618,7 +1621,8 @@ void gdcmDocument::LoadDocEntry(gdcmDocEntry* entry) * \brief Find the value Length of the passed Header Entry * @param Entry Header Entry whose length of the value shall be loaded. */ -void gdcmDocument::FindDocEntryLength (gdcmDocEntry *entry) +void gdcmDocument::FindDocEntryLength( gdcmDocEntry *entry ) + throw ( gdcmFormatError ) { uint16_t element = entry->GetElement(); std::string vr = entry->GetVR(); @@ -1637,8 +1641,12 @@ void gdcmDocument::FindDocEntryLength (gdcmDocEntry *entry) if ( vr == "OB" && length32 == 0xffffffff ) { - uint32_t lengthOB = FindDocEntryLengthOB(); - if ( errno == 1 ) + uint32_t lengthOB; + try + { + lengthOB = FindDocEntryLengthOB(); + } + catch ( gdcmFormatUnexpected ) { // Computing the length failed (this happens with broken // files like gdcm-JPEG-LossLess3a.dcm). We still have a @@ -1650,7 +1658,6 @@ void gdcmDocument::FindDocEntryLength (gdcmDocEntry *entry) long lengthUntilEOF = ftell(Fp) - currentPosition; fseek(Fp, currentPosition, SEEK_SET); entry->SetLength(lengthUntilEOF); - errno = 0; return; } entry->SetLength(lengthOB); @@ -1694,8 +1701,8 @@ void gdcmDocument::FindDocEntryLength (gdcmDocEntry *entry) { if ( !IsExplicitVRBigEndianTransferSyntax() ) { - dbg.Verbose(0, "gdcmDocument::FindLength", "not explicit VR"); - errno = 1; + throw gdcmFormatError( "gdcmDocument::FindDocEntryLength()", + " not explicit VR." ); return; } length16 = 4; @@ -1719,11 +1726,10 @@ void gdcmDocument::FindDocEntryLength (gdcmDocEntry *entry) // Heuristic: well, some files are really ill-formed. if ( length16 == 0xffff) { + // 0xffff means that we deal with 'Unknown Length' Sequence length16 = 0; - // Length16= 0xffff means that we deal with - // 'Unknown Length' Sequence } - FixDocEntryFoundLength(entry, (uint32_t)length16); + FixDocEntryFoundLength( entry, (uint32_t)length16 ); return; } else @@ -1735,7 +1741,7 @@ void gdcmDocument::FindDocEntryLength (gdcmDocEntry *entry) // not coexist in a Data Set and Data Sets nested within it".] // Length is on 4 bytes. - FixDocEntryFoundLength(entry, ReadInt32()); + FixDocEntryFoundLength( entry, ReadInt32() ); return; } } @@ -2150,6 +2156,7 @@ bool gdcmDocument::IsDocEntryAnInteger(gdcmDocEntry *entry) */ uint32_t gdcmDocument::FindDocEntryLengthOB() + throw( gdcmFormatUnexpected ) { // See PS 3.5-2001, section A.4 p. 49 on encapsulation of encoded pixel data. long positionOnEntry = ftell(Fp); @@ -2158,26 +2165,33 @@ uint32_t gdcmDocument::FindDocEntryLengthOB() while ( !foundSequenceDelimiter ) { - uint16_t g = ReadInt16(); - uint16_t n = ReadInt16(); - if ( errno == 1 ) + uint16_t group; + uint16_t elem; + try { - return 0; + group = ReadInt16(); + elem = ReadInt16(); + } + catch ( gdcmFormatError ) + { + throw gdcmFormatError("gdcmDocument::FindDocEntryLengthOB()", + " group or element not present."); } // We have to decount the group and element we just read totalLength += 4; - if ( g != 0xfffe || ( n != 0xe0dd && n != 0xe000 ) ) + if ( group != 0xfffe || ( ( elem != 0xe0dd ) && ( elem != 0xe000 ) ) ) { - dbg.Verbose(1, "gdcmDocument::FindLengthOB: neither an Item tag " - "nor a Sequence delimiter tag."); + dbg.Verbose(1, "gdcmDocument::FindDocEntryLengthOB: neither an Item " + "tag nor a Sequence delimiter tag."); fseek(Fp, positionOnEntry, SEEK_SET); - errno = 1; - return 0; + throw gdcmFormatUnexpected("gdcmDocument::FindDocEntryLengthOB()", + "Neither an Item tag nor a Sequence " + "delimiter tag."); } - if ( n == 0xe0dd ) + if ( elem == 0xe0dd ) { foundSequenceDelimiter = true; } @@ -2202,6 +2216,7 @@ uint32_t gdcmDocument::FindDocEntryLengthOB() * @return read value */ uint16_t gdcmDocument::ReadInt16() + throw( gdcmFormatError ) { uint16_t g; size_t item_read = fread (&g, (size_t)2,(size_t)1, Fp); @@ -2209,12 +2224,10 @@ uint16_t gdcmDocument::ReadInt16() { if( ferror(Fp) ) { - dbg.Verbose(0, "gdcmDocument::ReadInt16", " File Error"); + throw gdcmFormatError( "gdcmDocument::ReadInt16()", " file error." ); } - errno = 1; - return 0; + throw gdcmFormatError( "gdcmDocument::ReadInt16()", "EOF." ); } - errno = 0; g = SwapShort(g); return g; } @@ -2225,6 +2238,7 @@ uint16_t gdcmDocument::ReadInt16() * @return read value */ uint32_t gdcmDocument::ReadInt32() + throw( gdcmFormatError ) { uint32_t g; size_t item_read = fread (&g, (size_t)4,(size_t)1, Fp); @@ -2232,12 +2246,10 @@ uint32_t gdcmDocument::ReadInt32() { if( ferror(Fp) ) { - dbg.Verbose(0, "gdcmDocument::ReadInt32", " File Error"); + throw gdcmFormatError( "gdcmDocument::ReadInt16()", " file error." ); } - errno = 1; - return 0; + throw gdcmFormatError( "gdcmDocument::ReadInt32()", "EOF." ); } - errno = 0; g = SwapLong(g); return g; } @@ -2524,28 +2536,39 @@ void gdcmDocument::SetMaxSizePrintEntry(long newSize) * gets the VR, gets the length, gets the offset value) * @return On succes the newly created DocEntry, NULL on failure. */ -gdcmDocEntry *gdcmDocument::ReadNextDocEntry() +gdcmDocEntry* gdcmDocument::ReadNextDocEntry() { - uint16_t g = ReadInt16(); - uint16_t n = ReadInt16(); + uint16_t group; + uint16_t elem; - if (errno == 1) + try + { + group = ReadInt16(); + elem = ReadInt16(); + } + catch ( gdcmFormatError e ) { // We reached the EOF (or an error occured) therefore // header parsing has to be considered as finished. + std::cout << e; return 0; } - gdcmDocEntry *newEntry = NewDocEntryByNumber(g, n); + gdcmDocEntry *newEntry = NewDocEntryByNumber(group, elem); FindDocEntryVR(newEntry); - FindDocEntryLength(newEntry); - if (errno == 1) + try + { + FindDocEntryLength(newEntry); + } + catch ( gdcmFormatError e ) { // Call it quits + std::cout << e; delete newEntry; return 0; } + newEntry->SetOffset(ftell(Fp)); return newEntry; diff --git a/src/gdcmDocument.h b/src/gdcmDocument.h index b5d15237..0923cfb6 100644 --- a/src/gdcmDocument.h +++ b/src/gdcmDocument.h @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDocument.h,v $ Language: C++ - Date: $Date: 2004/08/01 03:20:23 $ - Version: $Revision: 1.28 $ + Date: $Date: 2004/08/02 14:06:58 $ + Version: $Revision: 1.29 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -131,7 +131,7 @@ public: FileType GetFileType(); - FILE* OpenFile(bool exception_on_error = false) throw(gdcmFileError); + FILE* OpenFile(bool exception_on_error = false) throw( gdcmFileError ); bool CloseFile(); void Write(FILE* fp, FileType type); @@ -202,7 +202,7 @@ private: long ParseSQ (gdcmSeqEntry *seq, long offset, long l_max, bool delim_mode); void LoadDocEntry (gdcmDocEntry *); - void FindDocEntryLength(gdcmDocEntry *); + void FindDocEntryLength(gdcmDocEntry *) throw ( gdcmFormatError ); void FindDocEntryVR (gdcmDocEntry *); bool CheckDocEntryVR (gdcmDocEntry *, gdcmVRKey); @@ -215,10 +215,10 @@ private: void FixDocEntryFoundLength(gdcmDocEntry *, uint32_t); bool IsDocEntryAnInteger (gdcmDocEntry *); - uint32_t FindDocEntryLengthOB(); + uint32_t FindDocEntryLengthOB() throw( gdcmFormatUnexpected ); - uint16_t ReadInt16(); - uint32_t ReadInt32(); + uint16_t ReadInt16() throw ( gdcmFormatError ); + uint32_t ReadInt32() throw ( gdcmFormatError ); void SkipBytes(uint32_t); bool ReadTag(uint16_t, uint16_t); uint32_t ReadTagLength(uint16_t, uint16_t); diff --git a/src/gdcmException.h b/src/gdcmException.h index 95d9a3c5..7d486158 100644 --- a/src/gdcmException.h +++ b/src/gdcmException.h @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmException.h,v $ Language: C++ - Date: $Date: 2004/06/20 18:08:47 $ - Version: $Revision: 1.13 $ + Date: $Date: 2004/08/02 14:06:58 $ + Version: $Revision: 1.14 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -95,22 +95,33 @@ public: //----------------------------------------------------------------------------- -/* - * Invalid file format exception +/** + * \brief Unexpected file format exception */ -class GDCM_EXPORT gdcmFormatError : public gdcmException { +class GDCM_EXPORT gdcmFormatUnexpected : public gdcmException { public: - /* - * Builds an file-related exception with minimal information: name of - * the thrower method and error message - * - * @param from name of the thrower - * @param error error description string - */ + /// \brief Builds a file-related exception with minimal information: + /// name of the thrower method and error message + /// @param from name of the thrower + /// @param error error description string + explicit gdcmFormatUnexpected(const std::string &from, + const std::string &error = "Unexpected file format") + throw() : gdcmException( from, error ) { } +}; + +//----------------------------------------------------------------------------- +/** + * \brief Invalid file format exception + */ +class GDCM_EXPORT gdcmFormatError : public gdcmFormatUnexpected { +public: + /// \brief Builds a file-related exception with minimal information: + /// name of the thrower method and error message + /// @param from name of the thrower + /// @param error error description string explicit gdcmFormatError(const std::string &from, - const std::string &error = "Invalid file format error") - throw() : gdcmException(from, error) { - } + const std::string &error = "Invalid file format") + throw() : gdcmFormatUnexpected( from, error ) { } }; //----------------------------------------------------------------------------- -- 2.45.1