]> Creatis software - gdcm.git/blobdiff - src/gdcmDocument.cxx
* gdcmPython/testSuite.py checks on CR-MONO1-10-chest.dcm moved to
[gdcm.git] / src / gdcmDocument.cxx
index 53b39e752917d3fcaa7e893166d28674bdc940a8..10fd52f39b03b6b8e7301947ab205a10aa755220 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmDocument.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/07/28 21:13:03 $
-  Version:   $Revision: 1.58 $
+  Date:      $Date: 2004/08/02 16:42:14 $
+  Version:   $Revision: 1.65 $
                                                                                 
   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 <errno.h>
 #include <vector>
 
 // For nthos:
@@ -83,32 +82,15 @@ const unsigned int gdcmDocument::MAX_SIZE_PRINT_ELEMENT_VALUE = 0x7fffffff;
 /**
  * \brief   constructor  
  * @param   inFilename file to be opened for parsing
- * @param   exception_on_error whether we throw an exception or not
- * @param   enable_sequences = true to allow the header 
- *          to be parsed *inside* the SeQuences,
- *          when they have an actual length 
- * \warning enable_sequences *has to be* true for reading PAPYRUS 3.0 files 
- * @param   ignore_shadow to allow skipping the shadow elements, 
- *          to save memory space.
- * \warning The TRUE value for this param has to be used 
- *          with a FALSE value for the 'enable_sequence' param.
- *          ('public elements' may be embedded in 'shadow Sequences')
- */
-gdcmDocument::gdcmDocument( std::string const & filename, 
-                            bool exception_on_error,
-                            bool enable_sequences,
-                            bool ignore_shadow) 
+ */
+gdcmDocument::gdcmDocument( std::string const & filename ) 
               : gdcmElementSet(-1)
 {
-   IgnoreShadow = ignore_shadow;
-   //EnableSequences=enable_sequences;
-   (void)enable_sequences;
-   EnableSequences = true; // JPR // TODO : remove params out of the constructor
    SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE); 
    Filename = filename;
    Initialise();
 
-   if ( !OpenFile(exception_on_error))
+   if ( !OpenFile()
    {
       return;
    }
@@ -125,7 +107,7 @@ gdcmDocument::gdcmDocument( std::string const & filename,
    long beg = ftell(Fp);
    lgt -= beg;
    
-   SQDepthLevel=0;
+   SQDepthLevel = 0;
    
    long l = ParseDES( this, beg, lgt, false); // le Load sera fait a la volee
    (void)l; //is l used anywhere ?
@@ -152,7 +134,11 @@ gdcmDocument::gdcmDocument( std::string const & filename,
    CloseFile(); 
   
    // --------------------------------------------------------------
-   // Special Patch to allow gdcm to read ACR-LibIDO formated images
+   // Specific code to allow gdcm to read ACR-LibIDO formated images
+   // Note: ACR-LibIDO is an extension of the ACR standard that was
+   //       used at CREATIS. For the time being (say a couple years)
+   //       we keep this kludge to allow a smooth move to gdcm for
+   //       CREATIS developpers (sorry folks).
    //
    // if recognition code tells us we deal with a LibIDO image
    // we switch lineNumber and columnNumber
@@ -169,24 +155,20 @@ gdcmDocument::gdcmDocument( std::string const & filename,
          SetEntryByNumber(columns, 0x0028, 0x0010);
          SetEntryByNumber(rows   , 0x0028, 0x0011);
    }
-   // ----------------- End of Special Patch ---------------- 
+   // ----------------- End of ACR-LibIDO kludge ------------------ 
 
    PrintLevel = 1;  // 'Medium' print level by default
 }
 
 /**
- * \brief  constructor 
- * @param   exception_on_error
+ * \brief This default constructor doesn't parse the file. You should
+ *        then invoke \ref gdcmDocument::SetFileName and then the parsing.
  */
-gdcmDocument::gdcmDocument(bool exception_on_error
+gdcmDocument::gdcmDocument() 
              :gdcmElementSet(-1)
 {
-   (void)exception_on_error;
-   //EnableSequences=0; // ?!? JPR
-
    SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE);
    Initialise();
-
    PrintLevel = 1;  // 'Medium' print level by default
 }
 
@@ -260,7 +242,7 @@ bool gdcmDocument::SetShaDict(gdcmDict *dict)
  * \brief   Set the shadow dictionary used
  * \param   dictName name of the dictionary to use in shadow
  */
-bool gdcmDocument::SetShaDict(DictKey dictName)
+bool gdcmDocument::SetShaDict(DictKey const & dictName)
 {
    RefShaDict = gdcmGlobal::GetDicts()->GetDict(dictName);
    return !RefShaDict;
@@ -479,58 +461,46 @@ FileType gdcmDocument::GetFileType()
 }
 
 /**
- * \brief   opens the file
- * @param   exception_on_error
- * @return  
+ * \brief  Tries to open the file \ref gdcmDocument::Filename and
+ *         checks the preamble when existing.
+ * @return The FILE pointer on success. 
  */
-FILE *gdcmDocument::OpenFile(bool exception_on_error)
-  throw(gdcmFileError) 
+FILE* gdcmDocument::OpenFile()
 {
-  Fp = fopen(Filename.c_str(),"rb");
-
-  if(!Fp)
-  {
-     if(exception_on_error)
-     {
-        throw gdcmFileError("gdcmDocument::gdcmDocument(const char *, bool)");
-     }
-     else
-     {
-        dbg.Verbose(0, "gdcmDocument::OpenFile cannot open file: ",
-                    Filename.c_str());
-        return NULL;
-     }
-  }
-
-  if ( Fp )
-  {
-     uint16_t zero;
-     fread(&zero,  (size_t)2, (size_t)1, Fp);
-
-    //ACR -- or DICOM with no Preamble --
-    if( zero == 0x0008 || zero == 0x0800 || zero == 0x0002 || zero == 0x0200 )
-    {
-       return Fp;
-    }
-
-    //DICOM
-    fseek(Fp, 126L, SEEK_CUR);
-    char dicm[4];
-    fread(dicm,  (size_t)4, (size_t)1, Fp);
-    if( memcmp(dicm, "DICM", 4) == 0 )
-    {
-       return Fp;
-    }
+   Fp = fopen(Filename.c_str(),"rb");
 
-    fclose(Fp);
-    dbg.Verbose(0, "gdcmDocument::OpenFile not DICOM/ACR", Filename.c_str());
-  }
-  else
-  {
-    dbg.Verbose(0, "gdcmDocument::OpenFile cannot open file", Filename.c_str());
-  }
-
-  return 0;
+   if(!Fp)
+   {
+      dbg.Verbose( 0,
+                   "gdcmDocument::OpenFile cannot open file: ",
+                   Filename.c_str());
+      return 0;
+   }
+   uint16_t zero;
+   fread(&zero,  (size_t)2, (size_t)1, Fp);
+   //ACR -- or DICOM with no Preamble --
+   if( zero == 0x0008 || zero == 0x0800 || zero == 0x0002 || zero == 0x0200 )
+   {
+      return Fp;
+   }
+   //DICOM
+   fseek(Fp, 126L, SEEK_CUR);
+   char dicm[4];
+   fread(dicm,  (size_t)4, (size_t)1, Fp);
+   if( memcmp(dicm, "DICM", 4) == 0 )
+   {
+      return Fp;
+   }
+   fclose(Fp);
+   dbg.Verbose( 0,
+                "gdcmDocument::OpenFile not DICOM/ACR (missing preamble)",
+                Filename.c_str());
+   return 0;
 }
 
 /**
@@ -540,7 +510,7 @@ FILE *gdcmDocument::OpenFile(bool exception_on_error)
 bool gdcmDocument::CloseFile()
 {
   int closed = fclose(Fp);
-  Fp = (FILE *)0;
+  Fp = 0;
 
   return closed;
 }
@@ -615,7 +585,7 @@ void gdcmDocument::Write(FILE* fp,FileType filetype)
  */
   
 gdcmValEntry * gdcmDocument::ReplaceOrCreateByNumber(
-                                         std::string value, 
+                                         std::string const & value, 
                                          uint16_t group, 
                                          uint16_t elem )
 {
@@ -709,11 +679,10 @@ gdcmBinEntry * gdcmDocument::ReplaceOrCreateByNumber(
  * @param Elem element number of the Entry
  * \return  boolean 
  */
-bool gdcmDocument::ReplaceIfExistByNumber(const char* value, uint16_t group,
-                                          uint16_t elem ) 
+bool gdcmDocument::ReplaceIfExistByNumber(std::string const & value, 
+                                          uint16_t group, uint16_t elem ) 
 {
-   const std::string v = value;
-   SetEntryByNumber(v, group, elem);
+   SetEntryByNumber(value, group, elem);
 
    return true;
 } 
@@ -729,7 +698,7 @@ bool gdcmDocument::ReplaceIfExistByNumber(const char* value, uint16_t group,
  */
 int gdcmDocument::CheckIfEntryExistByNumber(uint16_t group, uint16_t element )
 {
-   std::string key = gdcmDictEntry::TranslateToKey(group, element );
+   const std::string &key = gdcmDictEntry::TranslateToKey(group, element );
    return TagHT.count(key);
 }
 
@@ -742,7 +711,7 @@ int gdcmDocument::CheckIfEntryExistByNumber(uint16_t group, uint16_t element )
  * @return  Corresponding element value when it exists,
  *          and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
  */
-std::string gdcmDocument::GetEntryByName(TagName tagName)
+std::string gdcmDocument::GetEntryByName(TagName const & tagName)
 {
    gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName); 
    if( !dictEntry )
@@ -766,7 +735,7 @@ std::string gdcmDocument::GetEntryByName(TagName tagName)
  * @return  Corresponding element value representation when it exists,
  *          and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise.
  */
-std::string gdcmDocument::GetEntryVRByName(TagName tagName)
+std::string gdcmDocument::GetEntryVRByName(TagName const & tagName)
 {
    gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName); 
    if( dictEntry == NULL)
@@ -774,8 +743,8 @@ std::string gdcmDocument::GetEntryVRByName(TagName tagName)
       return GDCM_UNFOUND;
    }
 
-   gdcmDocEntry* elem =  GetDocEntryByNumber(dictEntry->GetGroup(),
-                                             dictEntry->GetElement());
+   gdcmDocEntry* elem = GetDocEntryByNumber(dictEntry->GetGroup(),
+                                            dictEntry->GetElement());
    return elem->GetVR();
 }
 
@@ -848,7 +817,7 @@ int gdcmDocument::GetEntryLengthByNumber(uint16_t group, uint16_t element)
  * @param   tagName name of the searched Dicom Element.
  * @return  true when found
  */
-bool gdcmDocument::SetEntryByName(std::string content,std::string tagName)
+bool gdcmDocument::SetEntryByName(std::string const & content,std::string const & tagName)
 {
    gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName); 
    if( !dictEntry )
@@ -868,7 +837,7 @@ bool gdcmDocument::SetEntryByName(std::string content,std::string tagName)
  * @param   group     group number of the Dicom Element to modify
  * @param   element element number of the Dicom Element to modify
  */
-bool gdcmDocument::SetEntryByNumber(std::string content, 
+bool gdcmDocument::SetEntryByNumber(std::string const & content, 
                                     uint16_t group,
                                     uint16_t element) 
 {
@@ -880,11 +849,12 @@ bool gdcmDocument::SetEntryByNumber(std::string content,
       return false;
    }
    // Non even content must be padded with a space (020H)...
-   if( content.length() % 2 )
+   std::string evenContent = content;
+   if( evenContent.length() % 2 )
    {
-      content += '\0';  // ... therefore we padd with (000H) .!?!
+      evenContent += '\0';  // ... therefore we padd with (000H) .!?!
    }      
-   valEntry->SetValue(content);
+   valEntry->SetValue(evenContent);
    
    // Integers have a special treatement for their length:
    gdcmVRKey vr = valEntry->GetVR();
@@ -898,7 +868,7 @@ bool gdcmDocument::SetEntryByNumber(std::string content,
    }
    else
    {
-      valEntry->SetLength(content.length());
+      valEntry->SetLength(evenContent.length());
    }
 
    return true;
@@ -932,10 +902,10 @@ bool gdcmDocument::SetEntryByNumber(void *content,
       //content = content + '\0'; // fing a trick to enlarge a binary field?
    }
 */      
-   gdcmBinEntry * a;
-   a = (gdcmBinEntry *)TagHT[key];           
+   gdcmBinEntry* a = (gdcmBinEntry *)TagHT[key];           
    a->SetVoidArea(content);  
    //a->SetLength(lgth);  // ???  
+
    return true;
 } 
 
@@ -1082,8 +1052,9 @@ bool gdcmDocument::SetEntryVoidAreaByNumber(void * area,
    {
       return false;
    }
-      // This was for multimap ?
-    (( gdcmBinEntry *)( ((TagHT.equal_range(key)).first)->second ))->SetVoidArea(area);
+
+   // This was for multimap ?
+   (( gdcmBinEntry *)( ((TagHT.equal_range(key)).first)->second ))->SetVoidArea(area);
       
    return true;
 }
@@ -1238,7 +1209,7 @@ uint32_t gdcmDocument::SwapLong(uint32_t a)
       default :
          std::cout << "swapCode= " << SwapCode << std::endl;
          dbg.Error(" gdcmDocument::SwapLong : unset swap code");
-         a=0;
+         a = 0;
    }
    return a;
 } 
@@ -1261,7 +1232,7 @@ uint16_t gdcmDocument::SwapShort(uint16_t a)
 {
    if ( SwapCode == 4321 || SwapCode == 2143 )
    {
-      a =((( a << 8 ) & 0x0ff00 ) | (( a >> 8 ) & 0x00ff ) );
+      a = ((( a << 8 ) & 0x0ff00 ) | (( a >> 8 ) & 0x00ff ) );
    }
    return a;
 }
@@ -1317,7 +1288,7 @@ long gdcmDocument::ParseDES(gdcmDocEntrySet *set,
             newValEntry->SetDepthLevel(depth);
             set->AddEntry(newValEntry);
             LoadDocEntry(newValEntry);
-            if (newValEntry->isItemDelimitor())
+            if (newValEntry->IsItemDelimitor())
             {
                break;
             }
@@ -1427,7 +1398,7 @@ long gdcmDocument::ParseSQ(gdcmSeqEntry *set,
       }
       if( delim_mode )
       {
-         if ( newDocEntry->isSequenceDelimitor() )
+         if ( newDocEntry->IsSequenceDelimitor() )
          {
             set->SetSequenceDelimitationItem( newDocEntry );
             break;
@@ -1514,8 +1485,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) )
@@ -1633,7 +1608,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();
@@ -1652,8 +1628,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
@@ -1665,7 +1645,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);
@@ -1705,12 +1684,12 @@ void gdcmDocument::FindDocEntryLength (gdcmDocEntry *entry)
       // endian encoding". When this is the case, chances are we have got our
       // hands on a big endian encoded file: we switch the swap code to
       // big endian and proceed...
-      if ( (element  == 0x0000) && (length16 == 0x0400) ) 
+      if ( element  == 0x0000 && length16 == 0x0400 ) 
       {
-         if ( ! IsExplicitVRBigEndianTransferSyntax() ) 
+         if ( !IsExplicitVRBigEndianTransferSyntax() ) 
          {
-            dbg.Verbose(0, "gdcmDocument::FindLength", "not explicit VR");
-            errno = 1;
+            throw gdcmFormatError( "gdcmDocument::FindDocEntryLength()",
+                                   " not explicit VR." );
             return;
          }
          length16 = 4;
@@ -1734,11 +1713,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
@@ -1750,7 +1728,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;
    }
 }
@@ -1818,7 +1796,7 @@ bool gdcmDocument::CheckDocEntryVR(gdcmDocEntry *entry, gdcmVRKey vr)
    // expected VR read happens to be non-ascii characters we consider
    // we hit falsely explicit VR tag.
 
-   if ( (!isalpha(vr[0])) && (!isalpha(vr[1])) )
+   if ( !isalpha(vr[0]) && !isalpha(vr[1]) )
    {
       realExplicit = false;
    }
@@ -2087,8 +2065,7 @@ void gdcmDocument::FixDocEntryFoundLength(gdcmDocEntry *entry,
       entry->SetReadLength(4); /// \todo a bug is to be fixed !?
    } 
  
-   //////// Deal with sequences, but only on users request:
-   else if ( entry->GetVR() == "SQ" && EnableSequences)
+   else if ( entry->GetVR() == "SQ" )
    {
       foundLength = 0;      // ReadLength is unchanged 
    } 
@@ -2166,6 +2143,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);
@@ -2174,26 +2152,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;
       }
@@ -2218,6 +2203,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);
@@ -2225,12 +2211,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;
 }
@@ -2241,6 +2225,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);
@@ -2248,12 +2233,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;
 }
@@ -2540,28 +2523,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()
+gdcmDocEntrygdcmDocument::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 NULL;
+      return 0;
    }
+
    newEntry->SetOffset(ftell(Fp));  
 
    return newEntry;
@@ -2643,8 +2637,8 @@ bool gdcmDocument::ReadTag(uint16_t testGroup, uint16_t testElement)
  */
 uint32_t gdcmDocument::ReadTagLength(uint16_t testGroup, uint16_t testElement)
 {
-   long PositionOnEntry = ftell(Fp);
-   (void)PositionOnEntry;
+   long positionOnEntry = ftell(Fp);
+   (void)positionOnEntry;
 
    if ( !ReadTag(testGroup, testElement) )
    {
@@ -2761,7 +2755,7 @@ void gdcmDocument::Parse7FE0 ()
 
       // Make sure that at the end of the item we encounter a 'Sequence
       // Delimiter Item':
-      if ( ! ReadTag(0xfffe, 0xe0dd) )
+      if ( !ReadTag(0xfffe, 0xe0dd) )
       {
          dbg.Verbose(0, "gdcmDocument::Parse7FE0: no sequence delimiter item");
          dbg.Verbose(0, "    at end of RLE item sequence");
@@ -2781,7 +2775,7 @@ void gdcmDocument::Parse7FE0 ()
 bool gdcmDocument::operator<(gdcmDocument &document)
 {
    // Patient Name
-   std::string s1 = this->GetEntryByNumber(0x0010,0x0010);
+   std::string s1 = GetEntryByNumber(0x0010,0x0010);
    std::string s2 = document.GetEntryByNumber(0x0010,0x0010);
    if(s1 < s2)
    {
@@ -2794,7 +2788,7 @@ bool gdcmDocument::operator<(gdcmDocument &document)
    else
    {
       // Patient ID
-      s1 = this->GetEntryByNumber(0x0010,0x0020);
+      s1 = GetEntryByNumber(0x0010,0x0020);
       s2 = document.GetEntryByNumber(0x0010,0x0020);
       if ( s1 < s2 )
       {
@@ -2807,7 +2801,7 @@ bool gdcmDocument::operator<(gdcmDocument &document)
       else
       {
          // Study Instance UID
-         s1 = this->GetEntryByNumber(0x0020,0x000d);
+         s1 = GetEntryByNumber(0x0020,0x000d);
          s2 = document.GetEntryByNumber(0x0020,0x000d);
          if ( s1 < s2 )
          {
@@ -2820,7 +2814,7 @@ bool gdcmDocument::operator<(gdcmDocument &document)
          else
          {
             // Serie Instance UID
-            s1 = this->GetEntryByNumber(0x0020,0x000e);
+            s1 = GetEntryByNumber(0x0020,0x000e);
             s2 = document.GetEntryByNumber(0x0020,0x000e);
             if ( s1 < s2 )
             {