5 #include "gdcmHeader.h"
8 #include "gdcmDataImages.h"
12 typedef string EntryValueType; // same type as gdcmValEntry::value
13 typedef map< gdcmTagKey, EntryValueType > MapEntryValues;
14 typedef MapEntryValues* MapEntryValuesPtr;
15 typedef string FileNameType;
16 typedef map< FileNameType, MapEntryValuesPtr > MapFileValuesType;
18 struct ParserException
23 static string GetIndent() { return ParserException::Indent; }
24 ParserException( string ErrorMessage )
29 void Print() { cerr << Indent << error << endl; }
32 string ParserException::Indent = " ";
34 class ReferenceFileParser
36 bool AddKeyValuePairToMap( string& key, string& value );
38 istream& eatwhite(istream& is);
39 void eatwhite(string& toClean);
40 string ExtractFirstString(string& toSplit);
41 void CleanUpLine( string& line );
43 string ExtractValue(string& toSplit) throw ( ParserException );
44 void ParseRegularLine( string& line ) throw ( ParserException );
45 void FirstPassReferenceFile() throw ( ParserException );
46 bool SecondPassReferenceFile() throw ( ParserException );
47 void HandleFileName( string& line ) throw ( ParserException );
48 void HandleKey( string& line ) throw ( ParserException );
49 bool HandleValue( string& line ) throw ( ParserException );
50 static uint16_t axtoi( char* );
52 ReferenceFileParser();
53 bool Open( string& referenceFileName );
55 void SetDataPath(string&);
58 /// The directory containing the images to check:
61 /// The product of the parser:
62 MapFileValuesType ProducedMap;
64 /// The ifstream attached to the file we parse:
67 /// String prefixing every output
70 /// The current line position within the stream:
73 /// The currently parsed filename:
74 string CurrentFileName;
76 /// The currently parsed key:
79 /// The currently parsed value:
82 /// The current MapEntryValues pointer:
83 MapEntryValues* CurrentMapEntryValuesPtr;
87 /// http://community.borland.com/article/0,1410,17203,0.html
88 uint16_t ReferenceFileParser::axtoi(char *hexStg) {
89 int n = 0; // position in string
90 int m = 0; // position in digit[] to shift
91 int count; // loop index
92 int intValue = 0; // integer value of hex string
93 int digit[5]; // hold values to convert
97 if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
98 digit[n] = hexStg[n] & 0x0f; //convert to int
99 else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
100 digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
101 else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
102 digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
110 // digit[n] is value of hex digit at position n
111 // (m << 2) is the number of positions to shift
112 // OR the bits into return value
113 intValue = intValue | (digit[n] << (m << 2));
114 m--; // adjust the position to set
115 n++; // next digit to process
120 void ReferenceFileParser::SetDataPath( string& inDataPath )
122 DataPath = inDataPath;
125 bool ReferenceFileParser::AddKeyValuePairToMap( string& key, string& value )
127 if ( !CurrentMapEntryValuesPtr )
129 if ( CurrentMapEntryValuesPtr->count(key) != 0 )
131 (*CurrentMapEntryValuesPtr)[key] = value;
136 void ReferenceFileParser::Print()
138 for (MapFileValuesType::iterator i = ProducedMap.begin();
139 i != ProducedMap.end();
142 cout << Indent << "FileName: " << i->first << endl;
143 MapEntryValuesPtr KeyValues = i->second;
144 for (MapEntryValues::iterator j = KeyValues->begin();
145 j != KeyValues->end();
149 << " Key: " << j->first
150 << " Value: " << j->second
153 cout << Indent << endl;
155 cout << Indent << endl;
158 bool ReferenceFileParser::Check()
160 for (MapFileValuesType::iterator i = ProducedMap.begin();
161 i != ProducedMap.end();
164 string fileName = DataPath + i->first;
165 cout << Indent << "FileName: " << fileName << endl;
166 gdcmHeader* tested = new gdcmHeader( fileName.c_str(), false, true );
167 if( !tested->IsReadable() )
169 cerr << Indent << "Image not gdcm compatible:"
175 MapEntryValuesPtr KeyValues = i->second;
176 for (MapEntryValues::iterator j = KeyValues->begin();
177 j != KeyValues->end();
180 string key = j->first;
182 string groupString = key.substr( 0, 4 );
184 groupCharPtr = new char(groupString.length() + 1);
185 strcpy( groupCharPtr, groupString.c_str() );
187 string groupElement = key.substr( key.find_first_of( "|" ) + 1, 4 );
188 char* groupElementPtr;
189 groupElementPtr = new char(groupElement.length() + 1);
190 strcpy( groupElementPtr, groupElement.c_str() );
192 uint16_t group = axtoi( groupCharPtr );
193 uint16_t element = axtoi( groupElementPtr );
195 string testedValue = tested->GetEntryByNumber(group, element);
196 if ( testedValue != j->second )
198 cout << Indent << "Uncorrect value for key " << key << endl
199 << Indent << " read value " << testedValue << endl
200 << Indent << " reference value " << j->second << endl;
205 cout << Indent << " OK" << endl;
207 cout << Indent << endl;
211 istream& ReferenceFileParser::eatwhite( istream& is )
223 void ReferenceFileParser::eatwhite( string& toClean )
225 while( toClean.find_first_of( " " ) == 0 )
226 toClean.erase( 0, toClean.find_first_of( " " ) + 1 );
229 string ReferenceFileParser::ExtractFirstString( string& toSplit )
231 std::string firstString;
233 if ( toSplit.find( " " ) == string::npos ) {
234 firstString = toSplit;
238 firstString = toSplit.substr( 0, toSplit.find(" ") );
239 toSplit.erase( 0, toSplit.find(" ") + 1);
244 string ReferenceFileParser::ExtractValue( string& toSplit )
245 throw ( ParserException )
248 string::size_type beginPos = toSplit.find_first_of( '"' );
249 string::size_type endPos = toSplit.find_last_of( '"' );
251 // Make sure we have at most to " in toSplit:
252 string noQuotes = toSplit.substr( beginPos + 1, endPos - beginPos - 1);
253 if ( noQuotes.find_first_of( '"' ) != string::npos )
254 throw ParserException( "more than two quote character" );
256 // No leading quote means this is not a value:
257 if ( beginPos == string::npos )
262 if ( ( endPos == string::npos ) || ( beginPos == endPos ) )
263 throw ParserException( "unmatched \" (quote character)" );
268 error << "leading character ["
269 << toSplit.substr(beginPos -1, 1)
270 << "] before opening \" ";
271 throw ParserException( error.str() );
274 // When they are some extra characters at end of value, it must
276 if ( ( endPos != toSplit.length() - 1 )
277 && ( toSplit.substr(endPos + 1, 1) != " " ) )
280 error << "trailing character ["
281 << toSplit.substr(endPos + 1, 1)
282 << "] after value closing \" ";
283 throw ParserException( error.str() );
286 string value = toSplit.substr( beginPos + 1, endPos - beginPos - 1 );
287 toSplit.erase( beginPos, endPos - beginPos + 1);
292 /// \brief Checks the block syntax of the incoming ifstream. Checks that
293 /// - no nested blocks are present
294 /// - we encounter a matching succesion of "[" and "]"
295 /// - when ifstream terminates the last block is closed.
296 /// - a block is not opened and close on the same line
297 /// @param from The incoming ifstream to be checked.
298 /// @return True when incoming ifstream has a correct syntax, false otherwise.
299 /// \warning The underlying file pointer is not preseved.
300 void ReferenceFileParser::FirstPassReferenceFile() throw ( ParserException )
304 bool inBlock = false;
305 from.seekg( 0, ios::beg );
307 while ( ! from.eof() )
309 getline( from, line );
311 /// This is how we usually end the parsing because we hit EOF:
317 throw ParserException( "Syntax error: EOF reached when in block.");
320 // Don't try to parse comments (weed out anything after first "#"):
321 if ( line.find_first_of( "#" ) != string::npos )
323 line.erase( line.find_first_of( "#" ) );
326 // Two occurences of opening blocks on a single line implies nested
327 // blocks which is illegal:
328 if ( line.find_first_of( "[" ) != line.find_last_of( "[" ) )
331 error << "Syntax error: nested block (open) in reference file"
333 << ParserException::GetIndent()
334 << " at line " << lineNumber << endl;
335 throw ParserException( error.str() );
338 // Two occurences of closing blocks on a single line implies nested
339 // blocks which is illegal:
340 if ( line.find_first_of( "]" ) != line.find_last_of( "]" ) )
343 error << "Syntax error: nested block (close) in reference file"
345 << ParserException::GetIndent()
346 << " at line " << lineNumber << endl;
347 throw ParserException( error.str() );
350 bool beginBlock ( line.find_first_of("[") != string::npos );
351 bool endBlock ( line.find_last_of("]") != string::npos );
353 // Opening and closing of block on same line:
354 if ( beginBlock && endBlock )
357 error << "Syntax error: opening and closing on block on same line "
358 << lineNumber++ << endl;
359 throw ParserException( error.str() );
362 // Illegal closing block when block not open:
363 if ( !inBlock && endBlock )
366 error << "Syntax error: unexpected end of block at line "
367 << lineNumber++ << endl;
368 throw ParserException( error.str() );
371 // Uncommented line outside of block is not clean:
372 if ( !inBlock && !beginBlock )
377 if ( inBlock && beginBlock )
380 error << " Syntax error: illegal opening of nested block at line "
381 << lineNumber++ << endl;
382 throw ParserException( error.str() );
385 // Normal situation of opening block:
393 // Normal situation of closing block:
400 // This line had no block delimiter
404 // We need rewinding:
406 from.seekg( 0, ios::beg );
409 ReferenceFileParser::ReferenceFileParser()
415 bool ReferenceFileParser::Open( string& referenceFileName )
417 from.open( referenceFileName.c_str(), ios::in );
418 if ( !from.is_open() )
420 cerr << Indent << "Can't open reference file." << endl;
425 FirstPassReferenceFile();
426 SecondPassReferenceFile();
428 catch ( ParserException except )
438 void ReferenceFileParser::CleanUpLine( string& line )
440 // Cleanup from comments:
441 if ( line.find_first_of( "#" ) != string::npos )
442 line.erase( line.find_first_of( "#" ) );
444 // Cleanup everything after end block delimiter:
445 if ( line.find_last_of( "]" ) != string::npos )
446 line.erase( line.find_last_of( "]" ) + 1 );
448 // Cleanup leanding whites and skip empty lines:
452 void ReferenceFileParser::HandleFileName( string& line )
453 throw ( ParserException )
455 if ( line.length() == 0 )
456 throw ParserException( "empty line on call of HandleFileName" );
458 if ( CurrentFileName.length() != 0 )
461 CurrentFileName = ExtractFirstString(line);
464 void ReferenceFileParser::HandleKey( string& line )
465 throw ( ParserException )
467 if ( CurrentKey.length() != 0 )
470 CurrentKey = ExtractFirstString(line);
471 if ( CurrentKey.find_first_of( "|" ) == string::npos )
474 error << "uncorrect key:" << CurrentKey;
475 throw ParserException( error.str() );
479 bool ReferenceFileParser::HandleValue( string& line )
480 throw ( ParserException )
482 if ( line.length() == 0 )
483 throw ParserException( "empty line in HandleValue" );
485 if ( CurrentKey.length() == 0 )
487 cout << Indent << "No key present:" << CurrentKey << endl;
491 string newCurrentValue = ExtractValue(line);
492 if ( newCurrentValue.length() == 0 )
495 error << "missing value for key:" << CurrentKey;
496 throw ParserException( error.str() );
499 CurrentValue += newCurrentValue;
503 void ReferenceFileParser::ParseRegularLine(string& line)
504 throw ( ParserException )
506 if ( line.length() == 0 )
509 // First thing is to get a filename:
510 HandleFileName( line );
512 if ( line.length() == 0 )
515 // Second thing is to get a key:
518 if ( line.length() == 0 )
521 // Third thing is to get a value:
522 if ( ! HandleValue( line ) )
525 if ( CurrentKey.length() && CurrentValue.length() )
527 if ( ! AddKeyValuePairToMap( CurrentKey, CurrentValue ) )
528 throw ParserException( "adding to map of (key, value) failed" );
530 CurrentValue.erase();
534 bool ReferenceFileParser::SecondPassReferenceFile()
535 throw ( ParserException )
538 EntryValueType value;
540 bool inBlock = false;
543 while ( !from.eof() )
545 getline( from, line );
550 // Empty lines don't require any treatement:
551 if ( line.length() == 0 )
554 bool beginBlock ( line.find_first_of("[") != string::npos );
555 bool endBlock ( line.find_last_of("]") != string::npos );
557 // Waiting for a block to be opened. Meanwhile, drop everything:
558 if ( !inBlock && !beginBlock )
564 line.erase( 0, line.find_first_of( "[" ) + 1 );
566 CurrentMapEntryValuesPtr = new MapEntryValues();
570 line.erase( line.find_last_of( "]" ) );
572 ParseRegularLine( line );
573 ProducedMap[CurrentFileName] = CurrentMapEntryValuesPtr;
575 CurrentFileName.erase();
578 // Outside block lines are dropped:
582 ParseRegularLine( line );
587 int TestAllEntryVerify(int argc, char* argv[])
591 cerr << " Usage: " << argv[0]
592 << " (no arguments needed)." << endl;
596 cout << " Description (Test::TestAllEntryVerify): "
598 cout << " For all images in gdcmData (and not blacklisted in "
599 "Test/CMakeLists.txt)"
601 cout << " apply the following to each filename.xxx: "
603 cout << " step 1: parse the image (as gdcmHeader) and call"
607 string referenceDir = GDCM_DATA_ROOT;
609 string referenceFilename = referenceDir + "TestAllEntryVerifyReference.txt";
611 ReferenceFileParser Parser;
612 Parser.Open(referenceFilename);
613 Parser.SetDataPath(referenceDir);
618 while( gdcmDataImages[i] != 0 )
620 string filename = GDCM_DATA_ROOT;
621 filename += "/"; //doh!
622 filename += gdcmDataImages[i++];
624 cout << " Testing: " << filename << endl;
626 gdcmHeader* tested = new gdcmHeader( filename.c_str(), false, true );
627 if( !tested->GetHeader()->IsReadable() )
629 cout << " Image not gdcm compatible:"
635 //////////////// Clean up: