GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ReadSpek.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <sstream>
3 #include <memory.h>
4 #include <stdlib.h>
5 
6 #include "ReadSpek.h"
7 
8 using namespace std;
9 
10 bool ReadSpek::Open(std::string sName, std::string sForm)
11 {
12  if(specFILE)
13  Close();
14 
15  if(sForm == "A")
16  specFILE = fopen(sName.c_str(), "r");
17  else
18  specFILE = fopen(sName.c_str(), "rb");
19  if(!specFILE) {
20  return false;
21  }
22  specName = sName;
23  specFormat = sForm; // should verify validity
24  return true;
25 }
26 
28 {
29  Close();
30  if(specData) {
31  delete [] specData;
32  specData = NULL;
33  }
34 }
35 
37 {
38  if(specFILE)
39  fclose(specFILE);
40  specFILE = NULL;
41  return true;
42 }
43 
44 bool ReadSpek::InitData(int slen)
45 {
46  if(specData) {
47  delete [] specData;
48  specData = NULL;
49  }
50 
51  specLength = slen;
52  specData = new float[specLength];
53  memset(specData, 0, sizeof(float)*specLength);
54 
55  return true;
56 }
57 
58 bool ReadSpek::Read(int slen, int ns)
59 {
60  if(!specFILE) {
61  return false; // open it first
62  }
63 
64  if(!specData || specLength != slen)
65  InitData(slen);
66 
67  bool readOK;
68 
69  if(specFormat == "S") {
70  readOK = ReadSpek_S(ns, false);
71  }
72  else if(specFormat == "US") {
73  readOK = ReadSpek_S(ns, true);
74  }
75  else if(specFormat == "I") {
76  readOK = ReadSpek_I(ns, false);
77  }
78  else if(specFormat == "UI") {
79  readOK = ReadSpek_I(ns, true);
80  }
81  else if(specFormat == "L") {
82  readOK = ReadSpek_L(ns, false);
83  }
84  else if(specFormat == "UL") {
85  readOK = ReadSpek_L(ns, true);
86  }
87  else if(specFormat == "F") {
88  readOK = ReadSpek_F(ns);
89  }
90  else if(specFormat == "D") {
91  readOK = ReadSpek_D(ns);
92  }
93  else if(specFormat == "A") {
94  readOK = ReadSpek_A(ns);
95  }
96  else {
97  readOK = false;
98  }
99  return readOK;
100 }
101 
102 bool ReadSpek::ReadSpek_C(int ns, bool uns)
103 {
104  if(uns) {
105  long offset = ns*specLength*sizeof(unsigned char);
106  if(fseek(specFILE, offset, SEEK_SET) )
107  return false;
108  unsigned char *iData = new unsigned char[specLength];
109  size_t nr = fread(iData, sizeof(unsigned char), specLength, specFILE);
110  if(nr < (size_t)specLength) {
111  delete [] iData;
112  return false;
113  }
114  for(int ii = 0; ii < specLength; ii++)
115  specData[ii] = float(iData[ii]);
116  delete [] iData;
117  return true;
118  }
119  else {
120  long offset = ns*specLength*sizeof(char);
121  if(fseek(specFILE, offset, SEEK_SET) )
122  return false;
123  char *iData = new char[specLength];
124  size_t nr = fread(iData, sizeof(char), specLength, specFILE);
125  if(nr < (size_t)specLength) {
126  delete [] iData;
127  return false;
128  }
129  for(int ii = 0; ii < specLength; ii++)
130  specData[ii] = float(iData[ii]);
131  delete [] iData;
132  return true;
133  }
134 }
135 
136 bool ReadSpek::ReadSpek_S(int ns, bool uns)
137 {
138  if(uns) {
139  long offset = ns*specLength*sizeof(unsigned short);
140  if(fseek(specFILE, offset, SEEK_SET) )
141  return false;
142  unsigned short *iData = new unsigned short[specLength];
143  size_t nr = fread(iData, sizeof(unsigned short), specLength, specFILE);
144  if(nr < (size_t)specLength) {
145  delete [] iData;
146  return false;
147  }
148  for(int ii = 0; ii < specLength; ii++)
149  specData[ii] = float(iData[ii]);
150  delete [] iData;
151  return true;
152  }
153  else {
154  long offset = ns*specLength*sizeof(short);
155  if(fseek(specFILE, offset, SEEK_SET) )
156  return false;
157  short *iData = new short[specLength];
158  size_t nr = fread(iData, sizeof(short), specLength, specFILE);
159  if(nr < (size_t)specLength) {
160  delete [] iData;
161  return false;
162  }
163  for(int ii = 0; ii < specLength; ii++)
164  specData[ii] = float(iData[ii]);
165  delete [] iData;
166  return true;
167  }
168 }
169 
170 bool ReadSpek::ReadSpek_I(int ns, bool uns)
171 {
172  if(uns) {
173  long offset = ns*specLength*sizeof(unsigned int);
174  if(fseek(specFILE, offset, SEEK_SET) )
175  return false;
176  unsigned int *iData = new unsigned int[specLength];
177  size_t nr = fread(iData, sizeof(unsigned int), specLength, specFILE);
178  if(nr < (size_t)specLength) {
179  delete [] iData;
180  return false;
181  }
182  for(int ii = 0; ii < specLength; ii++)
183  specData[ii] = float(iData[ii]);
184  delete [] iData;
185  return true;
186  }
187  else {
188  long offset = ns*specLength*sizeof(int);
189  if(fseek(specFILE, offset, SEEK_SET) )
190  return false;
191  int *iData = new int[specLength];
192  size_t nr = fread(iData, sizeof(int), specLength, specFILE);
193  if(nr < (size_t)specLength) {
194  delete [] iData;
195  return false;
196  }
197  for(int ii = 0; ii < specLength; ii++)
198  specData[ii] = float(iData[ii]);
199  delete [] iData;
200  return true;
201  }
202 }
203 
204 bool ReadSpek::ReadSpek_L(int ns, bool uns)
205 {
206  if(uns) {
207  long offset = ns*specLength*sizeof(unsigned long long);
208  if(fseek(specFILE, offset, SEEK_SET) )
209  return false;
210  unsigned long long *iData = new unsigned long long[specLength];
211  size_t nr = fread(iData, sizeof(unsigned long long), specLength, specFILE);
212  if(nr < (size_t)specLength) {
213  delete [] iData;
214  return false;
215  }
216  for(int ii = 0; ii < specLength; ii++)
217  specData[ii] = float(iData[ii]);
218  delete [] iData;
219  return true;
220  }
221  else {
222  long offset = ns*specLength*sizeof(long long);
223  if(fseek(specFILE, offset, SEEK_SET) )
224  return false;
225  long long *iData = new long long[specLength];
226  size_t nr = fread(iData, sizeof(long long), specLength, specFILE);
227  if(nr < (size_t)specLength) {
228  delete [] iData;
229  return false;
230  }
231  for(int ii = 0; ii < specLength; ii++)
232  specData[ii] = float(iData[ii]);
233  delete [] iData;
234  return true;
235  }
236 }
237 
238 bool ReadSpek::ReadSpek_F(int ns)
239 {
240  long offset = ns*specLength*sizeof(float);
241  if( fseek(specFILE, offset, SEEK_SET) )
242  return false;
243 
244  memset(specData, 0, sizeof(float)*specLength);
245  size_t nr = fread(specData, sizeof(float), specLength, specFILE);
246  if(nr < (size_t)specLength)
247  return false;
248  else
249  return true;
250 }
251 
252 bool ReadSpek::ReadSpek_D(int ns)
253 {
254  long offset = ns*specLength*sizeof(double);
255  if( fseek(specFILE, offset, SEEK_SET) )
256  return false;
257 
258  memset(specData, 0, sizeof(float)*specLength);
259  double *dData = new double[specLength];
260  size_t nr = fread(specData, sizeof(double), specLength, specFILE);
261  if(nr < (size_t)specLength) {
262  delete [] dData;
263  return false;
264  }
265  else {
266  for(int ii = 0; ii < specLength; ii++)
267  specData[ii] = float(dData[ii]);
268  delete [] dData;
269  return true;
270  }
271 }
272 
273 bool ReadSpek::ReadSpek_A(int ns)
274 {
275  //if( fseek(specFILE, 0, SEEK_SET) ) // rewind the file
276  // return false;
277  //ifstream ifs(specFILE); // constructor not available in g++
278 
279  Close();
280  ifstream ifs(specName.c_str(), std::ifstream::in);
281 
282  memset(specData, 0, sizeof(float)*specLength);
283  ns = max(0, ns);
284 
285  int skip = specLength*ns;
286  int seen = 0;
287  string line;
288 
289  while( getline(ifs, line) ) {
290  //StringTrim(line, "#");
291  //if(line.size() == 0) // empty or commented-out
292  // continue;
293  istringstream il(line);
294  for(;;) {
295  float ff;
296  il >> ff;
297  if( il.fail() )
298  break;
299  if(skip > 0)
300  skip--;
301  else if(seen < specLength) {
302  specData[seen++] = ff;
303  if(seen == specLength)
304  return true;
305  }
306  if( il.eof() )
307  break;
308  }
309  }
310  return (seen > 0);
311 }
312 
313 #if 0
314 // imported from Misc
315 bool ReadSpek::StringTrim(std::string& str, const std::string& cmt)
316 {
317  StringTrim(str);
318 
319  size_t ll = str.length();
320  if(ll <= 0)
321  return false;
322 
323  // check for comment substring
324  size_t nn = str.find_first_of(cmt);
325  if(nn == string::npos)
326  return false;
327 
328  // and remove it to the end of line
329  str = str.substr(0, nn);
330 
331  // and remove possible white space at the end
332  StringTrim(str);
333 
334  return false;
335 }
336 
337 void ReadSpek::StringTrim(std::string &str)
338 {
339  size_t ll = str.length();
340  // get rid of empty strings
341  if(ll <= 0)
342  return;
343  // get rid of the CR character
344  // (e.g. files written on Windows (\r\n) and read on Unix(\n) )
345  if(str[ll-1] == '\r') {
346  str = str.substr(0, ll-1);
347  ll--;
348  }
349  // discard if now empty
350  if(ll <= 0)
351  return;
352 
353  size_t ii = str.find_first_not_of(" \t");
354  // contains only white space
355  if(ii == string::npos) {
356  str.clear();
357  return;
358  }
359 
360  // remove white space at the beginning
361  if(ii > 0) {
362  str = str.substr(ii);
363  ll = str.length();
364  }
365 
366  ii = str.find_last_not_of(" \t");
367  // remove white space at the end
368  if(ii < ll-1)
369  str = str.substr(0, ii+1);
370 }
371 #endif
372 
373 // Taken from TkT NameSpecDecode
374 // excluding the special cases (UA, UT, WT2, ...) where part of the info has to be extracted from the data
375 
376 bool SpecNameDecode (std::string sName, std::string &sForm, int& slen)
377 {
378  vector<int> vv;
379  bool ok = SpecNameDecode (sName, sForm, vv);
380  slen = (ok&&vv.size()>0) ? vv[vv.size()-1] : 0;
381  return ok;
382 }
383 
384 bool SpecNameDecode (std::string sName, std::string &sForm, std::vector<int>& vv)
385 {
386  sForm.clear();
387  vv.clear();
388 
389  // dire/Psa__2-10-100-100-US__XYZR.matr
390 
391  size_t sdir = sName.find_last_of("/");
392  if(sdir == string::npos) sdir = 0;
393  size_t sep1 = sName.find("__", sdir);
394  if(sep1 == string::npos)
395  return false;
396  size_t sep2 = sName.find("__", sep1+2);
397  if(sep2 == string::npos)
398  return false;
399  size_t sdot = sName.find(".", sep1);
400  if(sdot != string::npos) {
401  if(sdot < sep2 && sdot > sep1) // not in the middle of the code
402  return false;
403  }
404 
405  string sCoded = sName.substr(sep1+2, sep2-sep1-2);
406  size_t dash = sCoded.find_last_of('-');
407  if(dash == string::npos) {
408  sForm = sCoded;
409  }
410  else {
411  // format must be present as last item
412  sForm = sCoded.substr(dash+1);
413  sCoded = sCoded.substr(0, dash);
414  // now decode the other tokens
415  if(sCoded[0] == '-')
416  sCoded = sCoded.substr(1);
417  while( sCoded.size() ) {
418  vv.push_back(atoi(sCoded.c_str()));
419  size_t pos = sCoded.find("-");
420  if(pos != std::string::npos)
421  sCoded = sCoded.substr(pos+1);
422  else
423  break;
424  }
425  }
426 
427  //if(vv.size() == 1) {
428  // vv.insert(vv.begin(), 1); // in TkT, add numK=1 to simplify life of caller
429  //}
430 
431  for(size_t nn = 0; nn < vv.size(); nn++) {
432  if(vv[nn] < 1)
433  vv[nn] = 1;
434  }
435 
436  return true;
437 }
438 
439 bool SpecNameEncode(std::string& sName, std::string sForm, int slen)
440 {
441  vector<int> vv;
442  vv.push_back(slen);
443  return SpecNameEncode (sName, sForm, vv);
444 }
445 
446 bool SpecNameEncode(std::string& sName, std::string sForm, std::vector<int> vlen)
447 {
448  size_t special = sName.find_first_of("?");
449  if(special == string::npos)
450  return false;
451 
452  string fn1 = sName.substr(0,special);
453  string fn2 = sName.substr(special+1);
454  std::ostringstream sfn;
455  for(size_t nn = 0; nn < vlen.size(); nn++) {
456  sfn << vlen[nn] << "-";
457  }
458  sName = fn1 + "__" + sfn.str() + sForm + "__" + fn2;
459  return true;
460 }
461 
bool SpecNameDecode(std::string sName, std::string &sForm, int &slen)
Definition: ReadSpek.cpp:376
bool Read(int slen, int snum=0)
Definition: ReadSpek.cpp:58
bool Open(std::string sName, std::string sForm)
Definition: ReadSpek.cpp:10
bool Close()
Definition: ReadSpek.cpp:36
~ReadSpek()
Definition: ReadSpek.cpp:27
bool SpecNameEncode(std::string &sName, std::string sForm, int slen)
Definition: ReadSpek.cpp:439