| 1 |
//+********************************************************************** |
| 2 |
// |
| 3 |
// File: edf_reader.cpp |
| 4 |
// |
| 5 |
// Project: Fable data to NeXus translation |
| 6 |
// |
| 7 |
// Description: implementation of a edf_reader class. It is a library, |
| 8 |
// which makes possible reading data from EDF format. |
| 9 |
// EDF file is read using the constructor of the class. |
| 10 |
// Every key-values pairs from the header are stored |
| 11 |
// in the one of two dictionaries(private members). |
| 12 |
// Image is read into the memory allocated dynamically, |
| 13 |
// and a pointer on that memory is a class's private |
| 14 |
// member. |
| 15 |
// |
| 16 |
// Author(s): Jaroslaw Butanowicz |
| 17 |
// |
| 18 |
// Original: April 2006 |
| 19 |
// |
| 20 |
//+********************************************************************** |
| 21 |
|
| 22 |
#include "edf_reader.h" |
| 23 |
|
| 24 |
using namespace std; |
| 25 |
|
| 26 |
/** |
| 27 |
* Destructor method. It realese memory allocated for the image. |
| 28 |
*/ |
| 29 |
edf_reader::~edf_reader(){ |
| 30 |
// |
| 31 |
// release the image array |
| 32 |
// |
| 33 |
delete [] image; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Initial constructor |
| 38 |
*/ |
| 39 |
edf_reader::edf_reader(){ |
| 40 |
key_double.clear(); |
| 41 |
key_string.clear(); |
| 42 |
header_numbers[0]=0; |
| 43 |
header_numbers[1]=0; |
| 44 |
header_numbers[2]=0; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Constructor responsible for reading data from the EDF file. |
| 49 |
* It reads data from the header and stores key-value pairs in dictionry. |
| 50 |
* Also EDF image is read. |
| 51 |
*/ |
| 52 |
edf_reader::edf_reader(ifstream &in){ |
| 53 |
|
| 54 |
char buffer[256], value_string[256]; |
| 55 |
double value; |
| 56 |
char a = ' '; |
| 57 |
bool is_header = 0; |
| 58 |
int dim1, dim2; |
| 59 |
// |
| 60 |
//read first character from the file |
| 61 |
// |
| 62 |
in.get(a); |
| 63 |
// |
| 64 |
// read file until bace closing the header is met |
| 65 |
// |
| 66 |
while(in.peek()!='}'){ |
| 67 |
// |
| 68 |
//prevention from corrupted bits in file |
| 69 |
// |
| 70 |
if(in.fail()){ |
| 71 |
in.clear(in.rdstate() & ~ios::failbit); |
| 72 |
in.getline(buffer, 255); |
| 73 |
break; |
| 74 |
} |
| 75 |
// |
| 76 |
//check if the header begins |
| 77 |
// |
| 78 |
if((a=='{') || is_header==1){ |
| 79 |
// |
| 80 |
//set flag if first time in the loop |
| 81 |
// |
| 82 |
if(!is_header){ |
| 83 |
in.get(a); |
| 84 |
is_header = 1; |
| 85 |
} |
| 86 |
// |
| 87 |
//get key |
| 88 |
// |
| 89 |
in.getline(buffer, 255,' '); |
| 90 |
string key(buffer); |
| 91 |
// |
| 92 |
//check if key corresponds to float value |
| 93 |
// |
| 94 |
if(!strcmp(buffer,"Center_1")||!strcmp(buffer,"Center_2")||!strcmp(buffer,"DDummy")||!strcmp(buffer,"Dummy")||!strcmp(buffer,"Offset_2")||!strcmp(buffer,"Psize_1")||!strcmp(buffer,"Psize_2")||!strcmp(buffer,"SampleDistance")||!strcmp(buffer,"SaxsDataVersion")||!strcmp(buffer,"WaveLength")||!strcmp(buffer,"EDF_BinarySize")||!strcmp(buffer,"Image")||!strcmp(buffer, "Offset_1")) { |
| 95 |
|
| 96 |
// |
| 97 |
//get value corresponding to key |
| 98 |
// |
| 99 |
in.getline(buffer, 255, '='); |
| 100 |
while(in.peek()==' ')in.ignore(1,'\n'); |
| 101 |
in >> value; |
| 102 |
// |
| 103 |
//store pairs key-double value in key_double dictionary |
| 104 |
// |
| 105 |
key_double.insert(pair<string, double>(key, value)); |
| 106 |
} |
| 107 |
|
| 108 |
// |
| 109 |
//get dimension separately and store in dictionary |
| 110 |
// |
| 111 |
|
| 112 |
else if(!strcmp(buffer,"Dim_1")){ |
| 113 |
in.getline(buffer, 255, '='); |
| 114 |
while(in.peek()==' ')in.ignore(1,'\n'); |
| 115 |
in >> value; |
| 116 |
dim1 = (int)value; |
| 117 |
(void)dim1; // TODO unused varable - quiet warnings |
| 118 |
key_double.insert(pair<string, double>(key, value)); |
| 119 |
} |
| 120 |
|
| 121 |
// |
| 122 |
//get dimension separately and store in dictionary |
| 123 |
// |
| 124 |
|
| 125 |
else if(!strcmp(buffer,"Dim_2")){ |
| 126 |
in.getline(buffer, 255, '='); |
| 127 |
while(in.peek()==' ')in.ignore(1,'\n'); |
| 128 |
in >> value; |
| 129 |
dim2 = (int)value; |
| 130 |
(void)dim2; // TODO unused varable - quiet warnings |
| 131 |
key_double.insert(pair<string, double>(key, value)); |
| 132 |
} |
| 133 |
|
| 134 |
// |
| 135 |
//get size of image, store in the dictionary as well as in image_size field |
| 136 |
//it is used to allocate space for EDF image in the memory |
| 137 |
// |
| 138 |
|
| 139 |
else if(!strcmp(buffer,"Size")){ |
| 140 |
in.getline(buffer, 255, '='); |
| 141 |
while(in.peek()==' ')in.ignore(1,'\n'); |
| 142 |
in >> value; |
| 143 |
image_size = (int)value; |
| 144 |
key_double.insert(pair<string, double>(key, value)); |
| 145 |
} |
| 146 |
|
| 147 |
// |
| 148 |
//in case of HeaderID store in separate fields, not in dictionary |
| 149 |
// |
| 150 |
|
| 151 |
else if(!strcmp(buffer,"HeaderID")){ |
| 152 |
in.getline(buffer, 255, ':'); |
| 153 |
in >> header_numbers[0]; |
| 154 |
in.getline(buffer, 255, ':'); |
| 155 |
in >> header_numbers[1]; |
| 156 |
in.getline(buffer, 255, ':'); |
| 157 |
in >> header_numbers[2]; |
| 158 |
in.getline(buffer, 255, ';'); |
| 159 |
} |
| 160 |
|
| 161 |
// |
| 162 |
//values corresponding to the rest of keys store in key_string diciotnary |
| 163 |
// |
| 164 |
|
| 165 |
else{ |
| 166 |
if(!strcmp(buffer," ")||!strcmp(buffer,"\n")||strlen(buffer)==0){ |
| 167 |
in.get(buffer, 255); |
| 168 |
} |
| 169 |
else{ |
| 170 |
in.getline(buffer, 255, '='); |
| 171 |
while(in.peek()==' ')in.ignore(1,'\n'); |
| 172 |
in.getline(value_string, 255,';'); |
| 173 |
// |
| 174 |
//store pairs key-double value in key_string dictionary |
| 175 |
// |
| 176 |
key_string.insert(pair<string, string>(key, value_string)); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
// |
| 181 |
//go to next line |
| 182 |
// |
| 183 |
do{ |
| 184 |
in.get(a); |
| 185 |
} while(a!='\n'); |
| 186 |
} |
| 187 |
} |
| 188 |
// |
| 189 |
//after header was read, read the image |
| 190 |
// |
| 191 |
try{ |
| 192 |
// |
| 193 |
//allocate memory for the image |
| 194 |
// |
| 195 |
image = new char [image_size]; |
| 196 |
}catch (bad_alloc xa){ |
| 197 |
cout<<"Allocation Failure\n"; |
| 198 |
exit(1); |
| 199 |
} |
| 200 |
// |
| 201 |
//go at the beginning of the image |
| 202 |
// |
| 203 |
in.getline(buffer, 255, '\n'); |
| 204 |
// |
| 205 |
//read the image into the buffer |
| 206 |
// |
| 207 |
in.read(image, image_size); |
| 208 |
// |
| 209 |
//reset flags in stream for EOF |
| 210 |
// |
| 211 |
if(in.eof()){ |
| 212 |
in.clear(in.rdstate() & ~ios::eofbit & ~ios::failbit); |
| 213 |
} |
| 214 |
// |
| 215 |
//rewind at the beginnig of the file |
| 216 |
// |
| 217 |
in.seekg(0, ios::beg); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
/** |
| 222 |
* Method returns current header value. |
| 223 |
*/ |
| 224 |
int edf_reader::get_current_headerID(){ |
| 225 |
|
| 226 |
return header_numbers[0]; |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Method returns previous header value. |
| 232 |
*/ |
| 233 |
int edf_reader::get_previous_headerID(){ |
| 234 |
|
| 235 |
return header_numbers[2]; |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Method returns next header value. |
| 241 |
*/ |
| 242 |
int edf_reader::get_next_headerID(){ |
| 243 |
|
| 244 |
return header_numbers[1]; |
| 245 |
|
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Method returns pointer to the EDF image. |
| 250 |
*/ |
| 251 |
char* edf_reader::get_image(){ |
| 252 |
|
| 253 |
return image; |
| 254 |
|
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Method returns size of the image. |
| 259 |
*/ |
| 260 |
int edf_reader::get_image_size(){ |
| 261 |
|
| 262 |
return image_size; |
| 263 |
|
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Method returns number of entries in the key-double_values dictionary. |
| 268 |
*/ |
| 269 |
int edf_reader::size_map_double(){ |
| 270 |
|
| 271 |
return key_double.size(); |
| 272 |
|
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Method returns number of entries in the key-string_values dictionary. |
| 277 |
*/ |
| 278 |
int edf_reader::size_map_string(){ |
| 279 |
|
| 280 |
return key_string.size(); |
| 281 |
|
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Method returns the name of the key and corresponding value |
| 286 |
* from key-double_value dictionary. The key number parameter decides |
| 287 |
* which key is read. |
| 288 |
* Parameters: input - int key_number |
| 289 |
* output - double value |
| 290 |
* Returns: string of the key |
| 291 |
*/ |
| 292 |
string edf_reader::get_key_double(int key_number, double &value){ |
| 293 |
|
| 294 |
map<string, double>::iterator p; |
| 295 |
// |
| 296 |
//set iterator at the beginning of the dictionary |
| 297 |
// |
| 298 |
p = key_double.begin(); |
| 299 |
// |
| 300 |
//go to invoked pair |
| 301 |
// |
| 302 |
for(int i=1; i<key_number; i++)p++; |
| 303 |
// |
| 304 |
//read value |
| 305 |
// |
| 306 |
value = p->second; |
| 307 |
// |
| 308 |
//return key |
| 309 |
// |
| 310 |
return p->first; |
| 311 |
|
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Method returns the name of the key and corresponding value |
| 316 |
* from key-string_value dictionary. The key number parameter decides |
| 317 |
* which key is read. |
| 318 |
* Parameters: input - int key_number |
| 319 |
* output - string value |
| 320 |
* Returns: string name of the key |
| 321 |
*/ |
| 322 |
string edf_reader::get_key_string(int key_number, string &value){ |
| 323 |
|
| 324 |
map<string, string>::iterator p; |
| 325 |
p = key_string.begin(); |
| 326 |
for(int i=1; i<key_number; i++)p++; |
| 327 |
value = p->second; |
| 328 |
|
| 329 |
return p->first; |
| 330 |
|
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Method returns value corresponding to the key invoked by name(not position), |
| 335 |
* from key-double_value dictionary. |
| 336 |
* Parameters: input - string key |
| 337 |
* output - double value |
| 338 |
* Returns: true if key value was found, false otherwise |
| 339 |
*/ |
| 340 |
bool edf_reader::double_by_key(string key, double &value){ |
| 341 |
|
| 342 |
bool exist=0; |
| 343 |
map<string, double>::iterator p; |
| 344 |
// |
| 345 |
//look for key |
| 346 |
// |
| 347 |
p = key_double.find(key); |
| 348 |
// |
| 349 |
//key_double.end() is set if thekey does not exist in the dictionary |
| 350 |
// |
| 351 |
if(p!=key_double.end()){ |
| 352 |
// |
| 353 |
//read the value corresponding to the key |
| 354 |
// |
| 355 |
value = p->second; |
| 356 |
// |
| 357 |
//key exist in the dictionary |
| 358 |
// |
| 359 |
exist=1; |
| 360 |
} |
| 361 |
|
| 362 |
return exist; |
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* Method returns value corresponding to the key invoked by name(not position), |
| 367 |
* from key-string_value dictionary. |
| 368 |
* Parameters: input - string key |
| 369 |
* output - string value |
| 370 |
* Returns: true if key value was found, false otherwise |
| 371 |
*/ |
| 372 |
bool edf_reader::string_by_key(string key, string &value){ |
| 373 |
|
| 374 |
bool exist=0; |
| 375 |
map<string, string>::iterator p; |
| 376 |
|
| 377 |
p = key_string.find(key); |
| 378 |
|
| 379 |
if(p!=key_string.end()){ |
| 380 |
value = p->second; |
| 381 |
exist=1; |
| 382 |
} |
| 383 |
return exist; |
| 384 |
} |
| 385 |
|