16 specFILE = fopen(sName.c_str(),
"r");
18 specFILE = fopen(sName.c_str(),
"rb");
44 bool ReadSpek::InitData(
int slen)
52 specData =
new float[specLength];
53 memset(specData, 0,
sizeof(
float)*specLength);
64 if(!specData || specLength != slen)
69 if(specFormat ==
"S") {
70 readOK = ReadSpek_S(ns,
false);
72 else if(specFormat ==
"US") {
73 readOK = ReadSpek_S(ns,
true);
75 else if(specFormat ==
"I") {
76 readOK = ReadSpek_I(ns,
false);
78 else if(specFormat ==
"UI") {
79 readOK = ReadSpek_I(ns,
true);
81 else if(specFormat ==
"L") {
82 readOK = ReadSpek_L(ns,
false);
84 else if(specFormat ==
"UL") {
85 readOK = ReadSpek_L(ns,
true);
87 else if(specFormat ==
"F") {
88 readOK = ReadSpek_F(ns);
90 else if(specFormat ==
"D") {
91 readOK = ReadSpek_D(ns);
93 else if(specFormat ==
"A") {
94 readOK = ReadSpek_A(ns);
102 bool ReadSpek::ReadSpek_C(
int ns,
bool uns)
105 long offset = ns*specLength*
sizeof(
unsigned char);
106 if(fseek(specFILE, offset, SEEK_SET) )
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) {
114 for(
int ii = 0; ii < specLength; ii++)
115 specData[ii] =
float(iData[ii]);
120 long offset = ns*specLength*
sizeof(char);
121 if(fseek(specFILE, offset, SEEK_SET) )
123 char *iData =
new char[specLength];
124 size_t nr = fread(iData,
sizeof(
char), specLength, specFILE);
125 if(nr < (
size_t)specLength) {
129 for(
int ii = 0; ii < specLength; ii++)
130 specData[ii] =
float(iData[ii]);
136 bool ReadSpek::ReadSpek_S(
int ns,
bool uns)
139 long offset = ns*specLength*
sizeof(
unsigned short);
140 if(fseek(specFILE, offset, SEEK_SET) )
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) {
148 for(
int ii = 0; ii < specLength; ii++)
149 specData[ii] =
float(iData[ii]);
154 long offset = ns*specLength*
sizeof(short);
155 if(fseek(specFILE, offset, SEEK_SET) )
157 short *iData =
new short[specLength];
158 size_t nr = fread(iData,
sizeof(
short), specLength, specFILE);
159 if(nr < (
size_t)specLength) {
163 for(
int ii = 0; ii < specLength; ii++)
164 specData[ii] =
float(iData[ii]);
170 bool ReadSpek::ReadSpek_I(
int ns,
bool uns)
173 long offset = ns*specLength*
sizeof(
unsigned int);
174 if(fseek(specFILE, offset, SEEK_SET) )
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) {
182 for(
int ii = 0; ii < specLength; ii++)
183 specData[ii] =
float(iData[ii]);
188 long offset = ns*specLength*
sizeof(int);
189 if(fseek(specFILE, offset, SEEK_SET) )
191 int *iData =
new int[specLength];
192 size_t nr = fread(iData,
sizeof(
int), specLength, specFILE);
193 if(nr < (
size_t)specLength) {
197 for(
int ii = 0; ii < specLength; ii++)
198 specData[ii] =
float(iData[ii]);
204 bool ReadSpek::ReadSpek_L(
int ns,
bool uns)
207 long offset = ns*specLength*
sizeof(
unsigned long long);
208 if(fseek(specFILE, offset, SEEK_SET) )
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) {
216 for(
int ii = 0; ii < specLength; ii++)
217 specData[ii] =
float(iData[ii]);
222 long offset = ns*specLength*
sizeof(
long long);
223 if(fseek(specFILE, offset, SEEK_SET) )
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) {
231 for(
int ii = 0; ii < specLength; ii++)
232 specData[ii] =
float(iData[ii]);
238 bool ReadSpek::ReadSpek_F(
int ns)
240 long offset = ns*specLength*
sizeof(float);
241 if( fseek(specFILE, offset, SEEK_SET) )
244 memset(specData, 0,
sizeof(
float)*specLength);
245 size_t nr = fread(specData,
sizeof(
float), specLength, specFILE);
246 if(nr < (
size_t)specLength)
252 bool ReadSpek::ReadSpek_D(
int ns)
254 long offset = ns*specLength*
sizeof(double);
255 if( fseek(specFILE, offset, SEEK_SET) )
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) {
266 for(
int ii = 0; ii < specLength; ii++)
267 specData[ii] =
float(dData[ii]);
273 bool ReadSpek::ReadSpek_A(
int ns)
280 ifstream ifs(specName.c_str(), std::ifstream::in);
282 memset(specData, 0,
sizeof(
float)*specLength);
285 int skip = specLength*ns;
289 while( getline(ifs, line) ) {
293 istringstream il(line);
301 else if(seen < specLength) {
302 specData[seen++] = ff;
303 if(seen == specLength)
315 bool ReadSpek::StringTrim(std::string& str,
const std::string& cmt)
319 size_t ll = str.length();
324 size_t nn = str.find_first_of(cmt);
325 if(nn == string::npos)
329 str = str.substr(0, nn);
337 void ReadSpek::StringTrim(std::string &str)
339 size_t ll = str.length();
345 if(str[ll-1] ==
'\r') {
346 str = str.substr(0, ll-1);
353 size_t ii = str.find_first_not_of(
" \t");
355 if(ii == string::npos) {
362 str = str.substr(ii);
366 ii = str.find_last_not_of(
" \t");
369 str = str.substr(0, ii+1);
380 slen = (ok&&vv.size()>0) ? vv[vv.size()-1] : 0;
384 bool SpecNameDecode (std::string sName, std::string &sForm, std::vector<int>& vv)
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)
396 size_t sep2 = sName.find(
"__", sep1+2);
397 if(sep2 == string::npos)
399 size_t sdot = sName.find(
".", sep1);
400 if(sdot != string::npos) {
401 if(sdot < sep2 && sdot > sep1)
405 string sCoded = sName.substr(sep1+2, sep2-sep1-2);
406 size_t dash = sCoded.find_last_of(
'-');
407 if(dash == string::npos) {
412 sForm = sCoded.substr(dash+1);
413 sCoded = sCoded.substr(0, dash);
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);
431 for(
size_t nn = 0; nn < vv.size(); nn++) {
446 bool SpecNameEncode(std::string& sName, std::string sForm, std::vector<int> vlen)
448 size_t special = sName.find_first_of(
"?");
449 if(special == string::npos)
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] <<
"-";
458 sName = fn1 +
"__" + sfn.str() + sForm +
"__" + fn2;
bool SpecNameDecode(std::string sName, std::string &sForm, int &slen)
bool Read(int slen, int snum=0)
bool Open(std::string sName, std::string sForm)
bool SpecNameEncode(std::string &sName, std::string sForm, int slen)