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;
134 void ReferenceFileParser::Print()
136 for (MapFileValuesType::iterator i = ProducedMap.begin();
137 i != ProducedMap.end();
140 cout << Indent << "FileName: " << i->first << endl;
141 MapEntryValuesPtr KeyValues = i->second;
142 for (MapEntryValues::iterator j = KeyValues->begin();
143 j != KeyValues->end();
147 << " Key: " << j->first
148 << " Value: " << j->second
151 cout << Indent << endl;
153 cout << Indent << endl;
156 bool ReferenceFileParser::Check()
158 for (MapFileValuesType::iterator i = ProducedMap.begin();
159 i != ProducedMap.end();
162 string fileName = DataPath + i->first;
163 cout << Indent << "FileName: " << fileName << endl;
164 gdcmHeader* tested = new gdcmHeader( fileName.c_str(), false, true );
165 if( !tested->IsReadable() )
167 cerr << Indent << "Image not gdcm compatible:"
173 MapEntryValuesPtr KeyValues = i->second;
174 for (MapEntryValues::iterator j = KeyValues->begin();
175 j != KeyValues->end();
178 string key = j->first;
180 string groupString = key.substr( 0, 4 );
182 groupCharPtr = new char(groupString.length() + 1);
183 strcpy( groupCharPtr, groupString.c_str() );
185 string groupElement = key.substr( key.find_first_of( "|" ) + 1, 4 );
186 char* groupElementPtr;
187 groupElementPtr = new char(groupElement.length() + 1);
188 strcpy( groupElementPtr, groupElement.c_str() );
190 uint16_t group = axtoi( groupCharPtr );
191 uint16_t element = axtoi( groupElementPtr );
193 string testedValue = tested->GetEntryByNumber(group, element);
194 if ( testedValue != j->second )
196 cout << Indent << "Uncorrect value for key " << key << endl
197 << Indent << " read value " << testedValue << endl
198 << Indent << " reference value " << j->second << endl;
203 cout << Indent << " OK" << endl;
205 cout << Indent << endl;
208 istream& ReferenceFileParser::eatwhite( istream& is )
220 void ReferenceFileParser::eatwhite( string& toClean )
222 while( toClean.find_first_of( " " ) == 0 )
223 toClean.erase( 0, toClean.find_first_of( " " ) + 1 );
226 string ReferenceFileParser::ExtractFirstString( string& toSplit )
228 std::string firstString;
230 if ( toSplit.find( " " ) == string::npos ) {
231 firstString = toSplit;
235 firstString = toSplit.substr( 0, toSplit.find(" ") );
236 toSplit.erase( 0, toSplit.find(" ") + 1);
241 string ReferenceFileParser::ExtractValue( string& toSplit )
242 throw ( ParserException )
245 string::size_type beginPos = toSplit.find_first_of( '"' );
246 string::size_type endPos = toSplit.find_last_of( '"' );
248 // Make sure we have at most to " in toSplit:
249 string noQuotes = toSplit.substr( beginPos + 1, endPos - beginPos - 1);
250 if ( noQuotes.find_first_of( '"' ) != string::npos )
251 throw ParserException( "more than two quote character" );
253 // No leading quote means this is not a value:
254 if ( beginPos == string::npos )
259 if ( ( endPos == string::npos ) || ( beginPos == endPos ) )
260 throw ParserException( "unmatched \" (quote character)" );
265 error << "leading character ["
266 << toSplit.substr(beginPos -1, 1)
267 << "] before opening \" ";
268 throw ParserException( error.str() );
271 // When they are some extra characters at end of value, it must
273 if ( ( endPos != toSplit.length() - 1 )
274 && ( toSplit.substr(endPos + 1, 1) != " " ) )
277 error << "trailing character ["
278 << toSplit.substr(endPos + 1, 1)
279 << "] after value closing \" ";
280 throw ParserException( error.str() );
283 string value = toSplit.substr( beginPos + 1, endPos - beginPos - 1 );
284 toSplit.erase( beginPos, endPos - beginPos + 1);
289 /// \brief Checks the block syntax of the incoming ifstream. Checks that
290 /// - no nested blocks are present
291 /// - we encounter a matching succesion of "[" and "]"
292 /// - when ifstream terminates the last block is closed.
293 /// - a block is not opened and close on the same line
294 /// @param from The incoming ifstream to be checked.
295 /// @return True when incoming ifstream has a correct syntax, false otherwise.
296 /// \warning The underlying file pointer is not preseved.
297 void ReferenceFileParser::FirstPassReferenceFile() throw ( ParserException )
301 bool inBlock = false;
302 from.seekg( 0, ios::beg );
304 while ( ! from.eof() )
306 getline( from, line );
308 /// This is how we usually end the parsing because we hit EOF:
314 throw ParserException( "Syntax error: EOF reached when in block.");
317 // Don't try to parse comments (weed out anything after first "#"):
318 if ( line.find_first_of( "#" ) != string::npos )
320 line.erase( line.find_first_of( "#" ) );
323 // Two occurences of opening blocks on a single line implies nested
324 // blocks which is illegal:
325 if ( line.find_first_of( "[" ) != line.find_last_of( "[" ) )
328 error << "Syntax error: nested block (open) in reference file"
330 << ParserException::GetIndent()
331 << " at line " << lineNumber << endl;
332 throw ParserException( error.str() );
335 // Two occurences of closing blocks on a single line implies nested
336 // blocks which is illegal:
337 if ( line.find_first_of( "]" ) != line.find_last_of( "]" ) )
340 error << "Syntax error: nested block (close) in reference file"
342 << ParserException::GetIndent()
343 << " at line " << lineNumber << endl;
344 throw ParserException( error.str() );
347 bool beginBlock ( line.find_first_of("[") != string::npos );
348 bool endBlock ( line.find_last_of("]") != string::npos );
350 // Opening and closing of block on same line:
351 if ( beginBlock && endBlock )
354 error << "Syntax error: opening and closing on block on same line "
355 << lineNumber++ << endl;
356 throw ParserException( error.str() );
359 // Illegal closing block when block not open:
360 if ( !inBlock && endBlock )
363 error << "Syntax error: unexpected end of block at line "
364 << lineNumber++ << endl;
365 throw ParserException( error.str() );
368 // Uncommented line outside of block is not clean:
369 if ( !inBlock && !beginBlock )
374 if ( inBlock && beginBlock )
377 error << " Syntax error: illegal opening of nested block at line "
378 << lineNumber++ << endl;
379 throw ParserException( error.str() );
382 // Normal situation of opening block:
390 // Normal situation of closing block:
397 // This line had no block delimiter
401 // We need rewinding:
403 from.seekg( 0, ios::beg );
406 ReferenceFileParser::ReferenceFileParser()
412 bool ReferenceFileParser::Open( string& referenceFileName )
414 from.open( referenceFileName.c_str(), ios::in );
415 if ( !from.is_open() )
417 cerr << Indent << "Can't open reference file." << endl;
422 FirstPassReferenceFile();
423 SecondPassReferenceFile();
425 catch ( ParserException except )
434 void ReferenceFileParser::CleanUpLine( string& line )
436 // Cleanup from comments:
437 if ( line.find_first_of( "#" ) != string::npos )
438 line.erase( line.find_first_of( "#" ) );
440 // Cleanup everything after end block delimiter:
441 if ( line.find_last_of( "]" ) != string::npos )
442 line.erase( line.find_last_of( "]" ) + 1 );
444 // Cleanup leanding whites and skip empty lines:
448 void ReferenceFileParser::HandleFileName( string& line )
449 throw ( ParserException )
451 if ( line.length() == 0 )
452 throw ParserException( "empty line on call of HandleFileName" );
454 if ( CurrentFileName.length() != 0 )
457 CurrentFileName = ExtractFirstString(line);
460 void ReferenceFileParser::HandleKey( string& line )
461 throw ( ParserException )
463 if ( CurrentKey.length() != 0 )
466 CurrentKey = ExtractFirstString(line);
467 if ( CurrentKey.find_first_of( "|" ) == string::npos )
470 error << "uncorrect key:" << CurrentKey;
471 throw ParserException( error.str() );
475 bool ReferenceFileParser::HandleValue( string& line )
476 throw ( ParserException )
478 if ( line.length() == 0 )
479 throw ParserException( "empty line in HandleValue" );
481 if ( CurrentKey.length() == 0 )
483 cout << Indent << "No key present:" << CurrentKey << endl;
487 string newCurrentValue = ExtractValue(line);
488 if ( newCurrentValue.length() == 0 )
491 error << "missing value for key:" << CurrentKey;
492 throw ParserException( error.str() );
495 CurrentValue += newCurrentValue;
499 void ReferenceFileParser::ParseRegularLine(string& line)
500 throw ( ParserException )
502 if ( line.length() == 0 )
505 // First thing is to get a filename:
506 HandleFileName( line );
508 if ( line.length() == 0 )
511 // Second thing is to get a key:
514 if ( line.length() == 0 )
517 // Third thing is to get a value:
518 if ( ! HandleValue( line ) )
521 if ( CurrentKey.length() && CurrentValue.length() )
523 if ( ! AddKeyValuePairToMap( CurrentKey, CurrentValue ) )
524 throw ParserException( "adding to map of (key, value) failed" );
526 CurrentValue.erase();
530 bool ReferenceFileParser::SecondPassReferenceFile()
531 throw ( ParserException )
534 EntryValueType value;
536 bool inBlock = false;
539 while ( !from.eof() )
541 getline( from, line );
546 // Empty lines don't require any treatement:
547 if ( line.length() == 0 )
550 bool beginBlock ( line.find_first_of("[") != string::npos );
551 bool endBlock ( line.find_last_of("]") != string::npos );
553 // Waiting for a block to be opened. Meanwhile, drop everything:
554 if ( !inBlock && !beginBlock )
560 line.erase( 0, line.find_first_of( "[" ) + 1 );
562 CurrentMapEntryValuesPtr = new MapEntryValues();
566 line.erase( line.find_last_of( "]" ) );
568 ParseRegularLine( line );
569 ProducedMap[CurrentFileName] = CurrentMapEntryValuesPtr;
571 CurrentFileName.erase();
574 // Outside block lines are dropped:
578 ParseRegularLine( line );
582 int TestAllEntryVerify(int argc, char* argv[])
586 cerr << " Usage: " << argv[0]
587 << " (no arguments needed)." << endl;
591 cout << " Description (Test::TestAllEntryVerify): "
593 cout << " For all images in gdcmData (and not blacklisted in "
594 "Test/CMakeLists.txt)"
596 cout << " apply the following to each filename.xxx: "
598 cout << " step 1: parse the image (as gdcmHeader) and call"
602 string referenceDir = GDCM_DATA_ROOT;
604 string referenceFilename = referenceDir + "TestAllEntryVerifyReference.txt";
606 ReferenceFileParser Parser;
607 Parser.Open(referenceFilename);
608 Parser.SetDataPath(referenceDir);
613 while( gdcmDataImages[i] != 0 )
615 string filename = GDCM_DATA_ROOT;
616 filename += "/"; //doh!
617 filename += gdcmDataImages[i++];
619 cout << " Testing: " << filename << endl;
621 gdcmHeader* tested = new gdcmHeader( filename.c_str(), false, true );
622 if( !tested->GetHeader()->IsReadable() )
624 cout << " Image not gdcm compatible:"
630 //////////////// Clean up: