]> Creatis software - gdcm.git/blobdiff - Testing/TestAllEntryVerify.cxx
Fix mistypings
[gdcm.git] / Testing / TestAllEntryVerify.cxx
index 0490f35291a66e7a0d57a4e60733ad00efa30d35..8ecd4072e5a33c7a7cf08193ae0f3c48ce15095f 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: TestAllEntryVerify.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/11/16 04:28:20 $
-  Version:   $Revision: 1.14 $
+  Date:      $Date: 2008/09/15 15:49:20 $
+  Version:   $Revision: 1.30 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
      PURPOSE.  See the above copyright notices for more information.
                                                                                 
 =========================================================================*/
-#include "gdcmHeader.h"
+#include "gdcmFile.h"
 
 #include <map>
 #include <list>
 #include <fstream>
 #include <iostream>
+#include <sstream>
 
-//Generated file:
-#include "gdcmDataImages.h"
-
-typedef std::string EntryValueType;   // same type as ValEntry::value
-typedef std::map< gdcm::TagKey, EntryValueType > MapEntryValues;
-typedef MapEntryValues* MapEntryValuesPtr;
+typedef std::string EntryValueType;   // same type as DataEntry::value
+typedef std::map< GDCM_NAME_SPACE::TagKey, EntryValueType > MapEntryValues;
+typedef MapEntryValues *MapEntryValuesPtr;
 typedef std::string FileNameType;
 typedef std::map< FileNameType, MapEntryValuesPtr > MapFileValuesType;
 
@@ -49,27 +47,34 @@ std::string ParserException::Indent = "      ";
 
 class ReferenceFileParser
 {
-   bool AddKeyValuePairToMap( std::string& key, std::string& value );
-
-   std::istream& eatwhite(std::istream& is);
-   void eatwhite(std::string& toClean);
-   std::string ExtractFirstString(std::string& toSplit);
-   void CleanUpLine( std::string& line );
-
-   std::string ExtractValue(std::string& toSplit)  throw ( ParserException );
-   void ParseRegularLine( std::string& line ) throw ( ParserException );
-   void FirstPassReferenceFile()         throw ( ParserException );
-   bool SecondPassReferenceFile()        throw ( ParserException );
-   void HandleFileName( std::string& line )   throw ( ParserException );
-   void HandleKey( std::string& line )        throw ( ParserException );
-   bool HandleValue( std::string& line )      throw ( ParserException );
-   static uint16_t axtoi( char* );
 public:
    ReferenceFileParser();
-   bool Open( std::string& referenceFileName );
+   ~ReferenceFileParser();
+
+   bool Open( std::string &referenceFileName );
    void Print();
    void SetDataPath(std::string&);
    bool Check();
+   bool Check( std::string fileName );
+
+private:
+   bool AddKeyValuePairToMap( std::string &key, std::string &value );
+
+   std::istream &eatwhite(std::istream &is);
+   void eatwhite(std::string &toClean);
+   std::string ExtractFirstString(std::string &toSplit);
+   void CleanUpLine( std::string &line );
+
+   bool Check( MapFileValuesType::iterator &fileIt );
+   std::string ExtractValue(std::string &toSplit)  throw ( ParserException );
+   void ParseRegularLine( std::string &line )      throw ( ParserException );
+   void FirstPassReferenceFile()                   throw ( ParserException );
+   bool SecondPassReferenceFile()                  throw ( ParserException );
+   void HandleFileName( std::string &line )        throw ( ParserException );
+   void HandleKey( std::string &line )             throw ( ParserException );
+   bool HandleValue( std::string &line )           throw ( ParserException );
+   static uint16_t axtoi( char* );
+
 private:
    /// The directory containing the images to check:
    std::string DataPath;
@@ -96,49 +101,69 @@ private:
    std::string CurrentValue;
 
    /// The current MapEntryValues pointer:
-   MapEntryValuesCurrentMapEntryValuesPtr;
+   MapEntryValues *CurrentMapEntryValuesPtr;
 };
 
-/// As gotten from:
+ReferenceFileParser::ReferenceFileParser()
+{
+   lineNumber = 1;
+   Indent = "      ";
+}
+
+ReferenceFileParser::~ReferenceFileParser()
+{
+   for (MapFileValuesType::iterator i  = ProducedMap.begin();
+                                    i != ProducedMap.end();
+                                  ++i)
+   {
+      delete i->second;
+   }
+}
+
+/// As got from:
 /// http://community.borland.com/article/0,1410,17203,0.html
-uint16_t ReferenceFileParser::axtoi(char *hexStg) {
-  int n = 0;         // position in string
-  int m = 0;         // position in digit[] to shift
-  int count;         // loop index
-  int intValue = 0;  // integer value of hex string
-  int digit[5];      // hold values to convert
-  while (n < 4) {
-     if (hexStg[n]=='\0')
-        break;
-     if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
-        digit[n] = hexStg[n] & 0x0f;            //convert to int
-     else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
-        digit[n] = (hexStg[n] & 0x0f) + 9;      //convert to int
-     else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
-        digit[n] = (hexStg[n] & 0x0f) + 9;      //convert to int
-     else break;
-    n++;
-  }
-  count = n;
-  m = n - 1;
-  n = 0;
-  while(n < count) {
-     // digit[n] is value of hex digit at position n
-     // (m << 2) is the number of positions to shift
-     // OR the bits into return value
-     intValue = intValue | (digit[n] << (m << 2));
-     m--;   // adjust the position to set
-     n++;   // next digit to process
-  }
-  return intValue;
+uint16_t ReferenceFileParser::axtoi(char *hexStg) 
+{
+   int n = 0;         // position in string
+   int m = 0;         // position in digit[] to shift
+   int count;         // loop index
+   int intValue = 0;  // integer value of hex string
+   int digit[5];      // hold values to convert
+   while (n < 4) 
+   {
+      if (hexStg[n]=='\0')
+         break;
+      if (hexStg[n] > 0x29 && hexStg[n] < 0x40 )    //if 0 to 9
+         digit[n] = hexStg[n] & 0x0f;               //convert to int
+      else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
+         digit[n] = (hexStg[n] & 0x0f) + 9;         //convert to int
+      else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
+         digit[n] = (hexStg[n] & 0x0f) + 9;         //convert to int
+      else break;
+     n++;
+   }
+   count = n;
+   m = n - 1;
+   n = 0;
+   while(n < count) 
+   {
+      // digit[n] is value of hex digit at position n
+      // (m << 2) is the number of positions to shift
+      // OR the bits into return value
+      intValue = intValue | (digit[n] << (m << 2));
+      m--;   // adjust the position to set
+      n++;   // next digit to process
+   }
+   return (uint16_t)intValue;
 }
 
-void ReferenceFileParser::SetDataPath( std::stringinDataPath )
+void ReferenceFileParser::SetDataPath( std::string &inDataPath )
 {
    DataPath = inDataPath;
 }
 
-bool ReferenceFileParser::AddKeyValuePairToMap( std::string& key, std::string& value )
+bool ReferenceFileParser::AddKeyValuePairToMap( std::string &key, 
+                                                std::string &value )
 {
    if ( !CurrentMapEntryValuesPtr )
       return false;
@@ -153,16 +178,16 @@ void ReferenceFileParser::Print()
 {
    for (MapFileValuesType::iterator i  = ProducedMap.begin();
                                     i != ProducedMap.end();
-                                    ++i)
+                                  ++i)
    {
       std::cout << Indent << "FileName: " << i->first << std::endl;
       MapEntryValuesPtr KeyValues = i->second;
       for (MapEntryValues::iterator j  = KeyValues->begin();
                                     j != KeyValues->end();
-                                    ++j)
+                                  ++j)
       {
          std::cout << Indent
-              << "  Key: " << j->first
+              << "  Key: "   << j->first
               << "  Value: " << j->second
               << std::endl;
       }
@@ -173,67 +198,87 @@ void ReferenceFileParser::Print()
 
 bool ReferenceFileParser::Check()
 {
+   bool ret = true;
    for (MapFileValuesType::iterator i  = ProducedMap.begin();
                                     i != ProducedMap.end();
-                                    ++i)
+                                  ++i)
    {
-      std::string fileName = DataPath + i->first;
-      std::cout << Indent << "FileName: " << fileName << std::endl;
-      gdcm::Header* tested = new gdcm::Header( fileName.c_str() );
-      if( !tested->IsReadable() )
-      {
-        std::cerr << Indent << "Image not gdcm compatible:"
+      ret &= Check(i);
+   }
+   std::cout << Indent << std::endl;
+   return ret;
+}
+
+bool ReferenceFileParser::Check( std::string fileName )
+{
+   MapFileValuesType::iterator it = ProducedMap.find(fileName);
+   if( it != ProducedMap.end() )
+   {
+      return Check(it);
+   }
+   std::cerr << Indent << "Failed\n"
+             << Indent << "Image not found :"
              << fileName << std::endl;
-        delete tested;
-        return false;
-      }
+   return false;
+}
 
-      MapEntryValuesPtr KeyValues = i->second;
-      for (MapEntryValues::iterator j  = KeyValues->begin();
-                                    j != KeyValues->end();
-                                    ++j)
-      {
-         std::string key = j->first;
+bool ReferenceFileParser::Check( MapFileValuesType::iterator &fileIt )
+{
+   std::string fileName = DataPath + fileIt->first;
+   std::cout << Indent << "FileName: " << fileName << std::endl;
+   
+   GDCM_NAME_SPACE::File *tested;
+   tested = new GDCM_NAME_SPACE::File( );
+   tested->SetFileName( fileName.c_str() );
+   tested->Load( );
+   if( !tested->IsReadable() )
+   {
+     std::cerr << Indent << "Failed\n"
+               << Indent << "Image not gdcm compatible:"
+               << fileName << std::endl;
+     delete tested;
+     return false;
+   }
 
-         std::string groupString  = key.substr( 0, 4 );
-         char* groupCharPtr;
-         groupCharPtr = new char(groupString.length() + 1);
-         strcpy( groupCharPtr, groupString.c_str() ); 
+   MapEntryValuesPtr KeyValues = fileIt->second;
+   for (MapEntryValues::iterator j  = KeyValues->begin();
+                                 j != KeyValues->end();
+                               ++j)
+   {
+      std::string key = j->first;
 
-         std::string groupElement = key.substr( key.find_first_of( "|" ) + 1, 4 );
-         char* groupElementPtr;
-         groupElementPtr = new char(groupElement.length() + 1);
-         strcpy( groupElementPtr, groupElement.c_str() ); 
+      std::string groupString  = key.substr( 0, 4 );
+      std::string groupElement = key.substr( key.find_first_of( "|" ) + 1, 4 );
 
-         uint16_t group   = axtoi( groupCharPtr );
-         uint16_t element = axtoi( groupElementPtr );
+      uint16_t group   = axtoi( &(groupString[0]) );
+      uint16_t element = axtoi( &(groupElement[0]) );
 
-         std::string testedValue = tested->GetEntryByNumber(group, element);
-         if ( testedValue != j->second )
+      std::string testedValue = tested->GetEntryValue(group, element);
+      if ( testedValue != j->second )
+      {
+         // Oops make sure this is only the \0 that differ
+         if( testedValue[j->second.size()] != '\0' ||
+             strncmp(testedValue.c_str(), 
+                     j->second.c_str(), j->second.size()) != 0)
          {
-            // Oops make sure this is only the \0 that differ
-            if( testedValue[j->second.size()] != '\0' ||
-                strncmp(testedValue.c_str(), 
-                        j->second.c_str(), j->second.size()) != 0)
-            {
-               std::cout << Indent << "Uncorrect value for key " 
-                         << key << std::endl
-                         << Indent << "   read value      [" 
-                         << testedValue << "]" << std::endl
-                         << Indent << "   reference value [" 
-                         << j->second << "]" << std::endl;
+            std::cout << Indent << "Failed\n"
+                      << Indent << "Uncorrect value for key " 
+                      << key << std::endl
+                      << Indent << "   read value      [" 
+                      << testedValue << "]" << std::endl
+                      << Indent << "   reference value [" 
+                      << j->second << "]" << std::endl;
             return false;
-            }
          }
       }
-      delete tested;
-      std::cout << Indent << "  OK" << std::endl;
    }
-   std::cout << Indent << std::endl;
+   delete tested;
+   std::cout << Indent << "  OK" << std::endl;
+
    return true;
 }
 
-std::istream& ReferenceFileParser::eatwhite( std::istream& is )
+std::istream &ReferenceFileParser::eatwhite( std::istream &is )
 {
    char c;
    while (is.get(c)) {
@@ -245,13 +290,13 @@ std::istream& ReferenceFileParser::eatwhite( std::istream& is )
    return is;
 }
 
-void ReferenceFileParser::eatwhite( std::stringtoClean )
+void ReferenceFileParser::eatwhite( std::string &toClean )
 {
    while( toClean.find_first_of( " " ) == 0  )
       toClean.erase( 0, toClean.find_first_of( " " ) + 1 );
 }
 
-std::string ReferenceFileParser::ExtractFirstString( std::stringtoSplit )
+std::string ReferenceFileParser::ExtractFirstString( std::string &toSplit )
 {
    std::string firstString;
    eatwhite( toSplit );
@@ -266,7 +311,7 @@ std::string ReferenceFileParser::ExtractFirstString( std::string& toSplit )
    return firstString;
 }
 
-std::string ReferenceFileParser::ExtractValue( std::stringtoSplit )
+std::string ReferenceFileParser::ExtractValue( std::string &toSplit )
    throw ( ParserException )
 {
    eatwhite( toSplit );
@@ -274,8 +319,10 @@ std::string ReferenceFileParser::ExtractValue( std::string& toSplit )
    std::string::size_type   endPos = toSplit.find_last_of( '"' );
 
    // Make sure we have at most two " in toSplit:
-   std::string noQuotes = toSplit.substr( beginPos + 1, endPos - beginPos - 1);
-   if ( noQuotes.find_first_of( '"' ) != std::string::npos )
+   //std::string noQuotes = toSplit.substr( beginPos + 1, endPos - beginPos - 1);
+   //if ( noQuotes.find_first_of( '"' ) != std::string::npos )
+   //   throw ParserException( "more than two quote character" );
+   if ( toSplit.find_first_of( '"',beginPos+1 ) != endPos )
       throw ParserException( "more than two quote character" );
 
    // No leading quote means this is not a value:
@@ -431,13 +478,7 @@ void ReferenceFileParser::FirstPassReferenceFile() throw ( ParserException )
    from.seekg( 0, std::ios::beg );
 }
 
-ReferenceFileParser::ReferenceFileParser()
-{
-   lineNumber = 1;
-   Indent = "      ";
-}
-
-bool ReferenceFileParser::Open( std::string& referenceFileName )
+bool ReferenceFileParser::Open( std::string &referenceFileName )
 {
    from.open( referenceFileName.c_str(), std::ios::in );
    if ( !from.is_open() )
@@ -460,7 +501,7 @@ bool ReferenceFileParser::Open( std::string& referenceFileName )
    return true; //??
 }
 
-void ReferenceFileParser::CleanUpLine( std::stringline )
+void ReferenceFileParser::CleanUpLine( std::string &line )
 {
    // Cleanup from comments:
    if ( line.find_first_of( "#" ) != std::string::npos )
@@ -474,7 +515,7 @@ void ReferenceFileParser::CleanUpLine( std::string& line )
    eatwhite( line );
 }
 
-void ReferenceFileParser::HandleFileName( std::stringline )
+void ReferenceFileParser::HandleFileName( std::string &line )
    throw ( ParserException )
 {
    if ( line.length() == 0 )
@@ -486,7 +527,7 @@ void ReferenceFileParser::HandleFileName( std::string& line )
    CurrentFileName = ExtractFirstString(line);
 }
 
-void ReferenceFileParser::HandleKey( std::stringline )
+void ReferenceFileParser::HandleKey( std::string &line )
    throw ( ParserException )
 {
    if ( CurrentKey.length() != 0 )
@@ -501,7 +542,7 @@ void ReferenceFileParser::HandleKey( std::string& line )
    }
 }
 
-bool ReferenceFileParser::HandleValue( std::stringline )
+bool ReferenceFileParser::HandleValue( std::string &line )
    throw ( ParserException )
 {
    if ( line.length() == 0 )
@@ -524,7 +565,7 @@ bool ReferenceFileParser::HandleValue( std::string& line )
    return true;
 }
 
-void ReferenceFileParser::ParseRegularLine( std::stringline)
+void ReferenceFileParser::ParseRegularLine( std::string &line)
    throw ( ParserException )
 {
    if ( line.length() == 0 )
@@ -558,7 +599,7 @@ void ReferenceFileParser::ParseRegularLine( std::string& line)
 bool ReferenceFileParser::SecondPassReferenceFile()
    throw ( ParserException )
 {
-   gdcm::TagKey key;
+   GDCM_NAME_SPACE::TagKey key;
    EntryValueType value;
    std::string line;
    bool inBlock = false;
@@ -608,12 +649,12 @@ bool ReferenceFileParser::SecondPassReferenceFile()
    return true; //??
 }
 
-int TestAllEntryVerify(int argc, charargv[]) 
+int TestAllEntryVerify(int argc, char *argv[]) 
 {
-   if ( argc > 1 )
+   if ( argc > 2 )
    {
       std::cerr << "   Usage: " << argv[0]
-                << " (no arguments needed)." << std::endl;
+                << " fileName" << std::endl;
       return 1;
    }
 
@@ -629,7 +670,8 @@ int TestAllEntryVerify(int argc, char* argv[])
    std::cout << "   apply the following tests : "<< std::endl;
    std::cout << "   step 1: parse the image and call IsReadable(). "  << std::endl;
    std::cout << "   step 2: look for the entry corresponding to the image" << std::endl;
-   std::cout << "           in the reference file: " << referenceFilename << std::endl;
+   std::cout << "           in the reference file: \n" 
+             << "           " << referenceFilename << std::endl;
    std::cout << "   step 3: check that each reference tag value listed for this"
         << std::endl;
    std::cout << "           entry matches the tag encountered at parsing step 1."
@@ -638,13 +680,24 @@ int TestAllEntryVerify(int argc, char* argv[])
    ReferenceFileParser Parser;
    if ( !Parser.Open(referenceFilename) )
    {
-      std::cout << "   Corrupted reference file name: "
-           << referenceFilename << std::endl;
+      std::cout << "   failed"
+                << "   Corrupted reference file name: "
+                << referenceFilename << std::endl;
       return 1;
    }
    Parser.SetDataPath(referenceDir);
    // Parser.Print();
-   if ( Parser.Check() )
-      return 0;
-   return 1;
+   std::cout << "Reference file loaded -->\n"
+             << "Check files : \n";
+
+   int ret;
+   if ( argc >= 2 )
+   {
+      ret = Parser.Check( argv[1] );
+   }
+   else
+   {
+      ret = Parser.Check(); 
+   }
+   return !ret;
 }