1 /*=========================================================================
4 Module: $RCSfile: TestAllEntryVerify.cxx,v $
6 Date: $Date: 2005/04/19 10:01:02 $
7 Version: $Revision: 1.26 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
27 //#include "gdcmDataImages.h"
29 typedef std::string EntryValueType; // same type as ValEntry::value
30 typedef std::map< gdcm::TagKey, EntryValueType > MapEntryValues;
31 typedef MapEntryValues *MapEntryValuesPtr;
32 typedef std::string FileNameType;
33 typedef std::map< FileNameType, MapEntryValuesPtr > MapFileValuesType;
35 struct ParserException
38 static std::string Indent;
40 static std::string GetIndent() { return ParserException::Indent; }
41 ParserException( std::string ErrorMessage )
46 void Print() { std::cerr << Indent << error << std::endl; }
49 std::string ParserException::Indent = " ";
51 class ReferenceFileParser
54 ReferenceFileParser();
55 ~ReferenceFileParser();
57 bool Open( std::string &referenceFileName );
59 void SetDataPath(std::string&);
61 bool Check( std::string fileName );
64 bool AddKeyValuePairToMap( std::string &key, std::string &value );
66 std::istream &eatwhite(std::istream &is);
67 void eatwhite(std::string &toClean);
68 std::string ExtractFirstString(std::string &toSplit);
69 void CleanUpLine( std::string &line );
71 bool Check( MapFileValuesType::iterator &fileIt );
72 std::string ExtractValue(std::string &toSplit) throw ( ParserException );
73 void ParseRegularLine( std::string &line ) throw ( ParserException );
74 void FirstPassReferenceFile() throw ( ParserException );
75 bool SecondPassReferenceFile() throw ( ParserException );
76 void HandleFileName( std::string &line ) throw ( ParserException );
77 void HandleKey( std::string &line ) throw ( ParserException );
78 bool HandleValue( std::string &line ) throw ( ParserException );
79 static uint16_t axtoi( char* );
82 /// The directory containing the images to check:
85 /// The product of the parser:
86 MapFileValuesType ProducedMap;
88 /// The ifstream attached to the file we parse:
91 /// String prefixing every output
94 /// The current line position within the stream:
97 /// The currently parsed filename:
98 std::string CurrentFileName;
100 /// The currently parsed key:
101 std::string CurrentKey;
103 /// The currently parsed value:
104 std::string CurrentValue;
106 /// The current MapEntryValues pointer:
107 MapEntryValues *CurrentMapEntryValuesPtr;
110 ReferenceFileParser::ReferenceFileParser()
116 ReferenceFileParser::~ReferenceFileParser()
118 for (MapFileValuesType::iterator i = ProducedMap.begin();
119 i != ProducedMap.end();
127 /// http://community.borland.com/article/0,1410,17203,0.html
128 uint16_t ReferenceFileParser::axtoi(char *hexStg)
130 int n = 0; // position in string
131 int m = 0; // position in digit[] to shift
132 int count; // loop index
133 int intValue = 0; // integer value of hex string
134 int digit[5]; // hold values to convert
139 if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
140 digit[n] = hexStg[n] & 0x0f; //convert to int
141 else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
142 digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
143 else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
144 digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
153 // digit[n] is value of hex digit at position n
154 // (m << 2) is the number of positions to shift
155 // OR the bits into return value
156 intValue = intValue | (digit[n] << (m << 2));
157 m--; // adjust the position to set
158 n++; // next digit to process
160 return (uint16_t)intValue;
163 void ReferenceFileParser::SetDataPath( std::string &inDataPath )
165 DataPath = inDataPath;
168 bool ReferenceFileParser::AddKeyValuePairToMap( std::string &key,
171 if ( !CurrentMapEntryValuesPtr )
173 if ( CurrentMapEntryValuesPtr->count(key) != 0 )
175 (*CurrentMapEntryValuesPtr)[key] = value;
180 void ReferenceFileParser::Print()
182 for (MapFileValuesType::iterator i = ProducedMap.begin();
183 i != ProducedMap.end();
186 std::cout << Indent << "FileName: " << i->first << std::endl;
187 MapEntryValuesPtr KeyValues = i->second;
188 for (MapEntryValues::iterator j = KeyValues->begin();
189 j != KeyValues->end();
193 << " Key: " << j->first
194 << " Value: " << j->second
197 std::cout << Indent << std::endl;
199 std::cout << Indent << std::endl;
202 bool ReferenceFileParser::Check()
205 for (MapFileValuesType::iterator i = ProducedMap.begin();
206 i != ProducedMap.end();
211 std::cout << Indent << std::endl;
215 bool ReferenceFileParser::Check( std::string fileName )
217 MapFileValuesType::iterator it = ProducedMap.find(fileName);
218 if( it != ProducedMap.end() )
222 std::cerr << Indent << "Failed\n"
223 << Indent << "Image not found :"
224 << fileName << std::endl;
228 bool ReferenceFileParser::Check( MapFileValuesType::iterator &fileIt )
230 std::string fileName = DataPath + fileIt->first;
231 std::cout << Indent << "FileName: " << fileName << std::endl;
232 gdcm::File *tested = new gdcm::File( fileName.c_str() );
233 if( !tested->IsReadable() )
235 std::cerr << Indent << "Failed\n"
236 << Indent << "Image not gdcm compatible:"
237 << fileName << std::endl;
242 MapEntryValuesPtr KeyValues = fileIt->second;
243 for (MapEntryValues::iterator j = KeyValues->begin();
244 j != KeyValues->end();
247 std::string key = j->first;
249 std::string groupString = key.substr( 0, 4 );
250 std::string groupElement = key.substr( key.find_first_of( "|" ) + 1, 4 );
252 uint16_t group = axtoi( &(groupString[0]) );
253 uint16_t element = axtoi( &(groupElement[0]) );
255 std::string testedValue = tested->GetEntryValue(group, element);
256 if ( testedValue != j->second )
258 // Oops make sure this is only the \0 that differ
259 if( testedValue[j->second.size()] != '\0' ||
260 strncmp(testedValue.c_str(),
261 j->second.c_str(), j->second.size()) != 0)
263 std::cout << Indent << "Failed\n"
264 << Indent << "Uncorrect value for key "
266 << Indent << " read value ["
267 << testedValue << "]" << std::endl
268 << Indent << " reference value ["
269 << j->second << "]" << std::endl;
275 std::cout << Indent << " OK" << std::endl;
280 std::istream &ReferenceFileParser::eatwhite( std::istream &is )
292 void ReferenceFileParser::eatwhite( std::string &toClean )
294 while( toClean.find_first_of( " " ) == 0 )
295 toClean.erase( 0, toClean.find_first_of( " " ) + 1 );
298 std::string ReferenceFileParser::ExtractFirstString( std::string &toSplit )
300 std::string firstString;
302 if ( toSplit.find( " " ) == std::string::npos ) {
303 firstString = toSplit;
307 firstString = toSplit.substr( 0, toSplit.find(" ") );
308 toSplit.erase( 0, toSplit.find(" ") + 1);
313 std::string ReferenceFileParser::ExtractValue( std::string &toSplit )
314 throw ( ParserException )
317 std::string::size_type beginPos = toSplit.find_first_of( '"' );
318 std::string::size_type endPos = toSplit.find_last_of( '"' );
320 // Make sure we have at most two " in toSplit:
321 //std::string noQuotes = toSplit.substr( beginPos + 1, endPos - beginPos - 1);
322 //if ( noQuotes.find_first_of( '"' ) != std::string::npos )
323 // throw ParserException( "more than two quote character" );
324 if ( toSplit.find_first_of( '"',beginPos+1 ) != endPos )
325 throw ParserException( "more than two quote character" );
327 // No leading quote means this is not a value:
328 if ( beginPos == std::string::npos )
330 return std::string();
333 if ( ( endPos == std::string::npos ) || ( beginPos == endPos ) )
334 throw ParserException( "unmatched \" (quote character)" );
338 std::ostringstream error;
339 error << "leading character ["
340 << toSplit.substr(beginPos -1, 1)
341 << "] before opening \" ";
342 throw ParserException( error.str() );
345 // When they are some extra characters at end of value, it must
347 if ( ( endPos != toSplit.length() - 1 )
348 && ( toSplit.substr(endPos + 1, 1) != " " ) )
350 std::ostringstream error;
351 error << "trailing character ["
352 << toSplit.substr(endPos + 1, 1)
353 << "] after value closing \" ";
354 throw ParserException( error.str() );
357 std::string value = toSplit.substr( beginPos + 1, endPos - beginPos - 1 );
358 toSplit.erase( beginPos, endPos - beginPos + 1);
363 /// \brief Checks the block syntax of the incoming ifstream. Checks that
364 /// - no nested blocks are present
365 /// - we encounter a matching succesion of "[" and "]"
366 /// - when ifstream terminates the last block is closed.
367 /// - a block is not opened and close on the same line
368 /// @param from The incoming ifstream to be checked.
369 /// @return True when incoming ifstream has a correct syntax, false otherwise.
370 /// \warning The underlying file pointer is not preseved.
371 void ReferenceFileParser::FirstPassReferenceFile() throw ( ParserException )
375 bool inBlock = false;
376 from.seekg( 0, std::ios::beg );
378 while ( ! from.eof() )
380 std::getline( from, line );
382 /// This is how we usually end the parsing because we hit EOF:
388 throw ParserException( "Syntax error: EOF reached when in block.");
391 // Don't try to parse comments (weed out anything after first "#"):
392 if ( line.find_first_of( "#" ) != std::string::npos )
394 line.erase( line.find_first_of( "#" ) );
397 // Two occurences of opening blocks on a single line implies nested
398 // blocks which is illegal:
399 if ( line.find_first_of( "[" ) != line.find_last_of( "[" ) )
401 std::ostringstream error;
402 error << "Syntax error: nested block (open) in reference file"
404 << ParserException::GetIndent()
405 << " at line " << lineNumber << std::endl;
406 throw ParserException( error.str() );
409 // Two occurences of closing blocks on a single line implies nested
410 // blocks which is illegal:
411 if ( line.find_first_of( "]" ) != line.find_last_of( "]" ) )
413 std::ostringstream error;
414 error << "Syntax error: nested block (close) in reference file"
416 << ParserException::GetIndent()
417 << " at line " << lineNumber << std::endl;
418 throw ParserException( error.str() );
421 bool beginBlock ( line.find_first_of("[") != std::string::npos );
422 bool endBlock ( line.find_last_of("]") != std::string::npos );
424 // Opening and closing of block on same line:
425 if ( beginBlock && endBlock )
427 std::ostringstream error;
428 error << "Syntax error: opening and closing on block on same line "
429 << lineNumber++ << std::endl;
430 throw ParserException( error.str() );
433 // Illegal closing block when block not open:
434 if ( !inBlock && endBlock )
436 std::ostringstream error;
437 error << "Syntax error: unexpected end of block at line "
438 << lineNumber++ << std::endl;
439 throw ParserException( error.str() );
442 // Uncommented line outside of block is not clean:
443 if ( !inBlock && !beginBlock )
448 if ( inBlock && beginBlock )
450 std::ostringstream error;
451 error << " Syntax error: illegal opening of nested block at line "
452 << lineNumber++ << std::endl;
453 throw ParserException( error.str() );
456 // Normal situation of opening block:
464 // Normal situation of closing block:
471 // This line had no block delimiter
475 // We need rewinding:
477 from.seekg( 0, std::ios::beg );
480 bool ReferenceFileParser::Open( std::string &referenceFileName )
482 from.open( referenceFileName.c_str(), std::ios::in );
483 if ( !from.is_open() )
485 std::cerr << Indent << "Can't open reference file." << std::endl;
490 FirstPassReferenceFile();
491 SecondPassReferenceFile();
493 catch ( ParserException except )
503 void ReferenceFileParser::CleanUpLine( std::string &line )
505 // Cleanup from comments:
506 if ( line.find_first_of( "#" ) != std::string::npos )
507 line.erase( line.find_first_of( "#" ) );
509 // Cleanup everything after end block delimiter:
510 if ( line.find_last_of( "]" ) != std::string::npos )
511 line.erase( line.find_last_of( "]" ) + 1 );
513 // Cleanup leading whites and skip empty lines:
517 void ReferenceFileParser::HandleFileName( std::string &line )
518 throw ( ParserException )
520 if ( line.length() == 0 )
521 throw ParserException( "empty line on call of HandleFileName" );
523 if ( CurrentFileName.length() != 0 )
526 CurrentFileName = ExtractFirstString(line);
529 void ReferenceFileParser::HandleKey( std::string &line )
530 throw ( ParserException )
532 if ( CurrentKey.length() != 0 )
535 CurrentKey = ExtractFirstString(line);
536 if ( CurrentKey.find_first_of( "|" ) == std::string::npos )
538 std::ostringstream error;
539 error << "uncorrect key:" << CurrentKey;
540 throw ParserException( error.str() );
544 bool ReferenceFileParser::HandleValue( std::string &line )
545 throw ( ParserException )
547 if ( line.length() == 0 )
548 throw ParserException( "empty line in HandleValue" );
550 if ( CurrentKey.length() == 0 )
552 std::cout << Indent << "No key present:" << CurrentKey << std::endl;
556 std::string newCurrentValue = ExtractValue(line);
557 if ( newCurrentValue.length() == 0 )
559 std::cout << Indent << "Warning: empty value for key:"
560 << CurrentKey << std::endl;
563 CurrentValue += newCurrentValue;
567 void ReferenceFileParser::ParseRegularLine( std::string &line)
568 throw ( ParserException )
570 if ( line.length() == 0 )
573 // First thing is to get a filename:
574 HandleFileName( line );
576 if ( line.length() == 0 )
579 // Second thing is to get a key:
582 if ( line.length() == 0 )
585 // Third thing is to get a value:
586 if ( ! HandleValue( line ) )
589 if ( CurrentKey.length() && CurrentValue.length() )
591 if ( ! AddKeyValuePairToMap( CurrentKey, CurrentValue ) )
592 throw ParserException( "adding to map of (key, value) failed" );
594 CurrentValue.erase();
598 bool ReferenceFileParser::SecondPassReferenceFile()
599 throw ( ParserException )
602 EntryValueType value;
604 bool inBlock = false;
607 while ( !from.eof() )
609 std::getline( from, line );
614 // Empty lines don't require any treatement:
615 if ( line.length() == 0 )
618 bool beginBlock ( line.find_first_of("[") != std::string::npos );
619 bool endBlock ( line.find_last_of("]") != std::string::npos );
621 // Waiting for a block to be opened. Meanwhile, drop everything:
622 if ( !inBlock && !beginBlock )
628 line.erase( 0, line.find_first_of( "[" ) + 1 );
630 CurrentMapEntryValuesPtr = new MapEntryValues();
634 line.erase( line.find_last_of( "]" ) );
636 ParseRegularLine( line );
637 ProducedMap[CurrentFileName] = CurrentMapEntryValuesPtr;
639 CurrentFileName.erase();
642 // Outside block lines are dropped:
646 ParseRegularLine( line );
651 int TestAllEntryVerify(int argc, char *argv[])
655 std::cerr << " Usage: " << argv[0]
656 << " fileName" << std::endl;
660 std::string referenceDir = GDCM_DATA_ROOT;
662 std::string referenceFilename = referenceDir + "TestAllEntryVerifyReference.txt";
664 std::cout << " Description (Test::TestAllEntryVerify): "
666 std::cout << " For all images (not blacklisted in gdcm/Test/CMakeLists.txt)"
668 std::cout << " encountered in directory: " << GDCM_DATA_ROOT << std::endl;
669 std::cout << " apply the following tests : "<< std::endl;
670 std::cout << " step 1: parse the image and call IsReadable(). " << std::endl;
671 std::cout << " step 2: look for the entry corresponding to the image" << std::endl;
672 std::cout << " in the reference file: \n"
673 << " " << referenceFilename << std::endl;
674 std::cout << " step 3: check that each reference tag value listed for this"
676 std::cout << " entry matches the tag encountered at parsing step 1."
677 << std::endl << std::endl;
679 ReferenceFileParser Parser;
680 if ( !Parser.Open(referenceFilename) )
682 std::cout << " failed"
683 << " Corrupted reference file name: "
684 << referenceFilename << std::endl;
687 Parser.SetDataPath(referenceDir);
689 std::cout << "Reference file loaded -->\n"
690 << "Check files : \n";
695 ret = Parser.Check( argv[1] );
699 ret = Parser.Check();