1 /*=========================================================================
4 Module: $RCSfile: TestAllEntryVerify.cxx,v $
6 Date: $Date: 2008/09/15 15:49:20 $
7 Version: $Revision: 1.30 $
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 =========================================================================*/
26 typedef std::string EntryValueType; // same type as DataEntry::value
27 typedef std::map< GDCM_NAME_SPACE::TagKey, EntryValueType > MapEntryValues;
28 typedef MapEntryValues *MapEntryValuesPtr;
29 typedef std::string FileNameType;
30 typedef std::map< FileNameType, MapEntryValuesPtr > MapFileValuesType;
32 struct ParserException
35 static std::string Indent;
37 static std::string GetIndent() { return ParserException::Indent; }
38 ParserException( std::string ErrorMessage )
43 void Print() { std::cerr << Indent << error << std::endl; }
46 std::string ParserException::Indent = " ";
48 class ReferenceFileParser
51 ReferenceFileParser();
52 ~ReferenceFileParser();
54 bool Open( std::string &referenceFileName );
56 void SetDataPath(std::string&);
58 bool Check( std::string fileName );
61 bool AddKeyValuePairToMap( std::string &key, std::string &value );
63 std::istream &eatwhite(std::istream &is);
64 void eatwhite(std::string &toClean);
65 std::string ExtractFirstString(std::string &toSplit);
66 void CleanUpLine( std::string &line );
68 bool Check( MapFileValuesType::iterator &fileIt );
69 std::string ExtractValue(std::string &toSplit) throw ( ParserException );
70 void ParseRegularLine( std::string &line ) throw ( ParserException );
71 void FirstPassReferenceFile() throw ( ParserException );
72 bool SecondPassReferenceFile() throw ( ParserException );
73 void HandleFileName( std::string &line ) throw ( ParserException );
74 void HandleKey( std::string &line ) throw ( ParserException );
75 bool HandleValue( std::string &line ) throw ( ParserException );
76 static uint16_t axtoi( char* );
79 /// The directory containing the images to check:
82 /// The product of the parser:
83 MapFileValuesType ProducedMap;
85 /// The ifstream attached to the file we parse:
88 /// String prefixing every output
91 /// The current line position within the stream:
94 /// The currently parsed filename:
95 std::string CurrentFileName;
97 /// The currently parsed key:
98 std::string CurrentKey;
100 /// The currently parsed value:
101 std::string CurrentValue;
103 /// The current MapEntryValues pointer:
104 MapEntryValues *CurrentMapEntryValuesPtr;
107 ReferenceFileParser::ReferenceFileParser()
113 ReferenceFileParser::~ReferenceFileParser()
115 for (MapFileValuesType::iterator i = ProducedMap.begin();
116 i != ProducedMap.end();
124 /// http://community.borland.com/article/0,1410,17203,0.html
125 uint16_t ReferenceFileParser::axtoi(char *hexStg)
127 int n = 0; // position in string
128 int m = 0; // position in digit[] to shift
129 int count; // loop index
130 int intValue = 0; // integer value of hex string
131 int digit[5]; // hold values to convert
136 if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
137 digit[n] = hexStg[n] & 0x0f; //convert to int
138 else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
139 digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
140 else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
141 digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
150 // digit[n] is value of hex digit at position n
151 // (m << 2) is the number of positions to shift
152 // OR the bits into return value
153 intValue = intValue | (digit[n] << (m << 2));
154 m--; // adjust the position to set
155 n++; // next digit to process
157 return (uint16_t)intValue;
160 void ReferenceFileParser::SetDataPath( std::string &inDataPath )
162 DataPath = inDataPath;
165 bool ReferenceFileParser::AddKeyValuePairToMap( std::string &key,
168 if ( !CurrentMapEntryValuesPtr )
170 if ( CurrentMapEntryValuesPtr->count(key) != 0 )
172 (*CurrentMapEntryValuesPtr)[key] = value;
177 void ReferenceFileParser::Print()
179 for (MapFileValuesType::iterator i = ProducedMap.begin();
180 i != ProducedMap.end();
183 std::cout << Indent << "FileName: " << i->first << std::endl;
184 MapEntryValuesPtr KeyValues = i->second;
185 for (MapEntryValues::iterator j = KeyValues->begin();
186 j != KeyValues->end();
190 << " Key: " << j->first
191 << " Value: " << j->second
194 std::cout << Indent << std::endl;
196 std::cout << Indent << std::endl;
199 bool ReferenceFileParser::Check()
202 for (MapFileValuesType::iterator i = ProducedMap.begin();
203 i != ProducedMap.end();
208 std::cout << Indent << std::endl;
212 bool ReferenceFileParser::Check( std::string fileName )
214 MapFileValuesType::iterator it = ProducedMap.find(fileName);
215 if( it != ProducedMap.end() )
219 std::cerr << Indent << "Failed\n"
220 << Indent << "Image not found :"
221 << fileName << std::endl;
225 bool ReferenceFileParser::Check( MapFileValuesType::iterator &fileIt )
227 std::string fileName = DataPath + fileIt->first;
228 std::cout << Indent << "FileName: " << fileName << std::endl;
230 GDCM_NAME_SPACE::File *tested;
231 tested = new GDCM_NAME_SPACE::File( );
232 tested->SetFileName( fileName.c_str() );
234 if( !tested->IsReadable() )
236 std::cerr << Indent << "Failed\n"
237 << Indent << "Image not gdcm compatible:"
238 << fileName << std::endl;
243 MapEntryValuesPtr KeyValues = fileIt->second;
244 for (MapEntryValues::iterator j = KeyValues->begin();
245 j != KeyValues->end();
248 std::string key = j->first;
250 std::string groupString = key.substr( 0, 4 );
251 std::string groupElement = key.substr( key.find_first_of( "|" ) + 1, 4 );
253 uint16_t group = axtoi( &(groupString[0]) );
254 uint16_t element = axtoi( &(groupElement[0]) );
256 std::string testedValue = tested->GetEntryValue(group, element);
257 if ( testedValue != j->second )
259 // Oops make sure this is only the \0 that differ
260 if( testedValue[j->second.size()] != '\0' ||
261 strncmp(testedValue.c_str(),
262 j->second.c_str(), j->second.size()) != 0)
264 std::cout << Indent << "Failed\n"
265 << Indent << "Uncorrect value for key "
267 << Indent << " read value ["
268 << testedValue << "]" << std::endl
269 << Indent << " reference value ["
270 << j->second << "]" << std::endl;
276 std::cout << Indent << " OK" << std::endl;
281 std::istream &ReferenceFileParser::eatwhite( std::istream &is )
293 void ReferenceFileParser::eatwhite( std::string &toClean )
295 while( toClean.find_first_of( " " ) == 0 )
296 toClean.erase( 0, toClean.find_first_of( " " ) + 1 );
299 std::string ReferenceFileParser::ExtractFirstString( std::string &toSplit )
301 std::string firstString;
303 if ( toSplit.find( " " ) == std::string::npos ) {
304 firstString = toSplit;
308 firstString = toSplit.substr( 0, toSplit.find(" ") );
309 toSplit.erase( 0, toSplit.find(" ") + 1);
314 std::string ReferenceFileParser::ExtractValue( std::string &toSplit )
315 throw ( ParserException )
318 std::string::size_type beginPos = toSplit.find_first_of( '"' );
319 std::string::size_type endPos = toSplit.find_last_of( '"' );
321 // Make sure we have at most two " in toSplit:
322 //std::string noQuotes = toSplit.substr( beginPos + 1, endPos - beginPos - 1);
323 //if ( noQuotes.find_first_of( '"' ) != std::string::npos )
324 // throw ParserException( "more than two quote character" );
325 if ( toSplit.find_first_of( '"',beginPos+1 ) != endPos )
326 throw ParserException( "more than two quote character" );
328 // No leading quote means this is not a value:
329 if ( beginPos == std::string::npos )
331 return std::string();
334 if ( ( endPos == std::string::npos ) || ( beginPos == endPos ) )
335 throw ParserException( "unmatched \" (quote character)" );
339 std::ostringstream error;
340 error << "leading character ["
341 << toSplit.substr(beginPos -1, 1)
342 << "] before opening \" ";
343 throw ParserException( error.str() );
346 // When they are some extra characters at end of value, it must
348 if ( ( endPos != toSplit.length() - 1 )
349 && ( toSplit.substr(endPos + 1, 1) != " " ) )
351 std::ostringstream error;
352 error << "trailing character ["
353 << toSplit.substr(endPos + 1, 1)
354 << "] after value closing \" ";
355 throw ParserException( error.str() );
358 std::string value = toSplit.substr( beginPos + 1, endPos - beginPos - 1 );
359 toSplit.erase( beginPos, endPos - beginPos + 1);
364 /// \brief Checks the block syntax of the incoming ifstream. Checks that
365 /// - no nested blocks are present
366 /// - we encounter a matching succesion of "[" and "]"
367 /// - when ifstream terminates the last block is closed.
368 /// - a block is not opened and close on the same line
369 /// @param from The incoming ifstream to be checked.
370 /// @return True when incoming ifstream has a correct syntax, false otherwise.
371 /// \warning The underlying file pointer is not preseved.
372 void ReferenceFileParser::FirstPassReferenceFile() throw ( ParserException )
376 bool inBlock = false;
377 from.seekg( 0, std::ios::beg );
379 while ( ! from.eof() )
381 std::getline( from, line );
383 /// This is how we usually end the parsing because we hit EOF:
389 throw ParserException( "Syntax error: EOF reached when in block.");
392 // Don't try to parse comments (weed out anything after first "#"):
393 if ( line.find_first_of( "#" ) != std::string::npos )
395 line.erase( line.find_first_of( "#" ) );
398 // Two occurences of opening blocks on a single line implies nested
399 // blocks which is illegal:
400 if ( line.find_first_of( "[" ) != line.find_last_of( "[" ) )
402 std::ostringstream error;
403 error << "Syntax error: nested block (open) in reference file"
405 << ParserException::GetIndent()
406 << " at line " << lineNumber << std::endl;
407 throw ParserException( error.str() );
410 // Two occurences of closing blocks on a single line implies nested
411 // blocks which is illegal:
412 if ( line.find_first_of( "]" ) != line.find_last_of( "]" ) )
414 std::ostringstream error;
415 error << "Syntax error: nested block (close) in reference file"
417 << ParserException::GetIndent()
418 << " at line " << lineNumber << std::endl;
419 throw ParserException( error.str() );
422 bool beginBlock ( line.find_first_of("[") != std::string::npos );
423 bool endBlock ( line.find_last_of("]") != std::string::npos );
425 // Opening and closing of block on same line:
426 if ( beginBlock && endBlock )
428 std::ostringstream error;
429 error << "Syntax error: opening and closing on block on same line "
430 << lineNumber++ << std::endl;
431 throw ParserException( error.str() );
434 // Illegal closing block when block not open:
435 if ( !inBlock && endBlock )
437 std::ostringstream error;
438 error << "Syntax error: unexpected end of block at line "
439 << lineNumber++ << std::endl;
440 throw ParserException( error.str() );
443 // Uncommented line outside of block is not clean:
444 if ( !inBlock && !beginBlock )
449 if ( inBlock && beginBlock )
451 std::ostringstream error;
452 error << " Syntax error: illegal opening of nested block at line "
453 << lineNumber++ << std::endl;
454 throw ParserException( error.str() );
457 // Normal situation of opening block:
465 // Normal situation of closing block:
472 // This line had no block delimiter
476 // We need rewinding:
478 from.seekg( 0, std::ios::beg );
481 bool ReferenceFileParser::Open( std::string &referenceFileName )
483 from.open( referenceFileName.c_str(), std::ios::in );
484 if ( !from.is_open() )
486 std::cerr << Indent << "Can't open reference file." << std::endl;
491 FirstPassReferenceFile();
492 SecondPassReferenceFile();
494 catch ( ParserException except )
504 void ReferenceFileParser::CleanUpLine( std::string &line )
506 // Cleanup from comments:
507 if ( line.find_first_of( "#" ) != std::string::npos )
508 line.erase( line.find_first_of( "#" ) );
510 // Cleanup everything after end block delimiter:
511 if ( line.find_last_of( "]" ) != std::string::npos )
512 line.erase( line.find_last_of( "]" ) + 1 );
514 // Cleanup leading whites and skip empty lines:
518 void ReferenceFileParser::HandleFileName( std::string &line )
519 throw ( ParserException )
521 if ( line.length() == 0 )
522 throw ParserException( "empty line on call of HandleFileName" );
524 if ( CurrentFileName.length() != 0 )
527 CurrentFileName = ExtractFirstString(line);
530 void ReferenceFileParser::HandleKey( std::string &line )
531 throw ( ParserException )
533 if ( CurrentKey.length() != 0 )
536 CurrentKey = ExtractFirstString(line);
537 if ( CurrentKey.find_first_of( "|" ) == std::string::npos )
539 std::ostringstream error;
540 error << "uncorrect key:" << CurrentKey;
541 throw ParserException( error.str() );
545 bool ReferenceFileParser::HandleValue( std::string &line )
546 throw ( ParserException )
548 if ( line.length() == 0 )
549 throw ParserException( "empty line in HandleValue" );
551 if ( CurrentKey.length() == 0 )
553 std::cout << Indent << "No key present:" << CurrentKey << std::endl;
557 std::string newCurrentValue = ExtractValue(line);
558 if ( newCurrentValue.length() == 0 )
560 std::cout << Indent << "Warning: empty value for key:"
561 << CurrentKey << std::endl;
564 CurrentValue += newCurrentValue;
568 void ReferenceFileParser::ParseRegularLine( std::string &line)
569 throw ( ParserException )
571 if ( line.length() == 0 )
574 // First thing is to get a filename:
575 HandleFileName( line );
577 if ( line.length() == 0 )
580 // Second thing is to get a key:
583 if ( line.length() == 0 )
586 // Third thing is to get a value:
587 if ( ! HandleValue( line ) )
590 if ( CurrentKey.length() && CurrentValue.length() )
592 if ( ! AddKeyValuePairToMap( CurrentKey, CurrentValue ) )
593 throw ParserException( "adding to map of (key, value) failed" );
595 CurrentValue.erase();
599 bool ReferenceFileParser::SecondPassReferenceFile()
600 throw ( ParserException )
602 GDCM_NAME_SPACE::TagKey key;
603 EntryValueType value;
605 bool inBlock = false;
608 while ( !from.eof() )
610 std::getline( from, line );
615 // Empty lines don't require any treatement:
616 if ( line.length() == 0 )
619 bool beginBlock ( line.find_first_of("[") != std::string::npos );
620 bool endBlock ( line.find_last_of("]") != std::string::npos );
622 // Waiting for a block to be opened. Meanwhile, drop everything:
623 if ( !inBlock && !beginBlock )
629 line.erase( 0, line.find_first_of( "[" ) + 1 );
631 CurrentMapEntryValuesPtr = new MapEntryValues();
635 line.erase( line.find_last_of( "]" ) );
637 ParseRegularLine( line );
638 ProducedMap[CurrentFileName] = CurrentMapEntryValuesPtr;
640 CurrentFileName.erase();
643 // Outside block lines are dropped:
647 ParseRegularLine( line );
652 int TestAllEntryVerify(int argc, char *argv[])
656 std::cerr << " Usage: " << argv[0]
657 << " fileName" << std::endl;
661 std::string referenceDir = GDCM_DATA_ROOT;
663 std::string referenceFilename = referenceDir + "TestAllEntryVerifyReference.txt";
665 std::cout << " Description (Test::TestAllEntryVerify): "
667 std::cout << " For all images (not blacklisted in gdcm/Test/CMakeLists.txt)"
669 std::cout << " encountered in directory: " << GDCM_DATA_ROOT << std::endl;
670 std::cout << " apply the following tests : "<< std::endl;
671 std::cout << " step 1: parse the image and call IsReadable(). " << std::endl;
672 std::cout << " step 2: look for the entry corresponding to the image" << std::endl;
673 std::cout << " in the reference file: \n"
674 << " " << referenceFilename << std::endl;
675 std::cout << " step 3: check that each reference tag value listed for this"
677 std::cout << " entry matches the tag encountered at parsing step 1."
678 << std::endl << std::endl;
680 ReferenceFileParser Parser;
681 if ( !Parser.Open(referenceFilename) )
683 std::cout << " failed"
684 << " Corrupted reference file name: "
685 << referenceFilename << std::endl;
688 Parser.SetDataPath(referenceDir);
690 std::cout << "Reference file loaded -->\n"
691 << "Check files : \n";
696 ret = Parser.Check( argv[1] );
700 ret = Parser.Check();