And a few more (up to 3000). These defines are used to read certain fields from a DICOM image, that give information about that image (Patient Name, etc…). However I don’t wish to read all these fields. Instead I’m trying to load an .ini configuration which will configure which fields to read. For example:
1 2 3
[PROPERTIES]
# Fields to read
fields=PatienId,Modality
This would only retreive the Patient Name and a Modality.
The problem is, the values I retrieve from the .ini file (reading and parsing the file with std::fstream) come as a string. How could I retrieve, for example, the defined value from PatientId, i.e. “(0x0010, 0x0020)”? Something like std::cout << # << "PatientId"; won't work.
Using some advanced text searching algorithms you can get it to work very quickly, but you might as well just read every line and check to see if it contains one of the fields.
void read_fields(
map <string, string> & fields,
istream& ins,
vector <string> fields_to_read
) {
string s;
while (getline( ins, s ))
{
// don't bother if this line is not a '#define'
if (s[ 0 ] != '#') continue;
// try to find the field name
for (vector <string> ::const_iterator
field_name = fields_to_read.begin();
field_name != fields_to_read.end();
++field_name)
{
size_t n = s.find( string( " " ) + *field_name + " " );
if (n != string::npos)
{
fields[ *field_name ] = trim( s.substr( n + 2 + field_name->length() ) );
fields_to_read.erase( field_name );
break;
}
}
}
}
If you can guarantee that the fields to read list is in the same order as the fields in the C++ file will be encountered, you can optimize this significantly more.
This is a fairly simplistic answer. It makes several assumptions about what kind of text will be found in the cpp files.
Another option would be to use a regular expression search over the file for the field names.
Glad to be of help. Coming back I've been worried that the check on line 10 of my code is not sufficient, since it is not uncommon for some C/C++ programmer to put spaces or tabs before the hash (#) introducing a #define. If you consider it a possible problem in the future you might want to put a quick 'skip past whitespace' there before looking for the hash sign.