00001 #include <stdlib.h>
00002
00003 #include "NdbEndfIO.h"
00004
00005 #define NUMBER_SIZE 11
00006
00007 char NdbEndfIO::_str[100];
00008
00009 ClassImp(NdbEndfIO)
00010
00011
00012 NdbEndfIO::NdbEndfIO( char *filename, Int_t mode )
00013 {
00014 f = fopen(filename,mode==TENDF_READ?"r":"w");
00015 matStart = 0;
00016 mfStart = 0;
00017 mtStart = 0;
00018 iMAT = 0;
00019 iMF = 0;
00020 iMT = 0;
00021 lineNum = 0;
00022 lineTxt[0] = 0;
00023 }
00024
00025
00026
00027
00028 Bool_t
00029 NdbEndfIO::FindMAT( Int_t mat, Bool_t rewind )
00030 {
00031 if (rewind)
00032 fseek(f, 0L, SEEK_SET);
00033
00034 while (ReadLine())
00035 if (iMAT == mat)
00036 return TRUE;
00037
00038 return FALSE;
00039 }
00040
00041
00042
00043 Bool_t
00044 NdbEndfIO::FindMATMF( Int_t mat, Int_t mf, Bool_t rewind )
00045 {
00046 FindMAT(mat,rewind);
00047
00048 do {
00049 if (iMF == mf)
00050 return TRUE;
00051 } while (ReadLine());
00052
00053 return FALSE;
00054 }
00055
00056
00057
00058 Bool_t
00059 NdbEndfIO::FindMATMFMT( Int_t mat, Int_t mf, Int_t mt, Bool_t rewind )
00060 {
00061 FindMATMF(mat,mf,rewind);
00062
00063 do {
00064 if (iMT == mt)
00065 return TRUE;
00066 } while (ReadLine());
00067
00068 return FALSE;
00069 }
00070
00071
00072 Bool_t
00073 NdbEndfIO::FindMFMT( Int_t mf, Int_t mt )
00074 {
00075
00076 if (mf*1000+mt >= iMF*1000+iMT) {
00077 RewindMAT();
00078 if (!ReadLine())
00079 return FALSE;
00080 }
00081
00082 while (mf*1000+mt > iMF*1000+iMT)
00083 if (!ReadLine())
00084 return FALSE;
00085 return TRUE;
00086 }
00087
00088
00089 char *
00090 NdbEndfIO::Substr(Int_t start, Int_t length)
00091 {
00092 if (start + length > lineLen) {
00093
00094 return NULL;
00095 }
00096 memcpy(_str, lineTxt+start, length);
00097 _str[length] = 0;
00098
00099 return _str;
00100 }
00101
00102
00103
00104
00105
00106 Bool_t
00107 NdbEndfIO::NextNumber(Int_t pos)
00108 {
00109 if (pos<0 || pos>5*NUMBER_SIZE) {
00110 lastNumPos += NUMBER_SIZE;
00111 if (lineTxt[0]=='\0' || lastNumPos>5*NUMBER_SIZE) {
00112 Int_t mf = iMF;
00113 Int_t mt = iMT;
00114 if (!ReadLine() || (mf*1000+mt != iMF*1000+iMT))
00115 return TRUE;
00116 lastNumPos = 0;
00117 }
00118 } else {
00119 lastNumPos = pos * NUMBER_SIZE;
00120 }
00121 return FALSE;
00122 }
00123
00124
00125 Int_t
00126 NdbEndfIO::SubReadInt(Int_t start, Int_t length)
00127 {
00128 NdbEndfIO::Substr(start,length);
00129 return atoi(_str);
00130 }
00131
00132
00133
00134
00135
00136
00137 Int_t
00138 NdbEndfIO::ReadInt(Bool_t *error, Int_t pos)
00139 {
00140 if (NextNumber(pos)) {
00141 *error = TRUE;
00142 return 0;
00143 }
00144 *error = FALSE;
00145 return SubReadInt(lastNumPos,NUMBER_SIZE);
00146 }
00147
00148
00149 Float_t
00150 NdbEndfIO::SubReadReal(Int_t start, Int_t length)
00151 {
00152 char numstr[20];
00153 char *dst, *src;
00154
00155 NdbEndfIO::Substr(start,length);
00156
00157
00158
00159
00160
00161
00162 dst = numstr;
00163 src = _str;
00164
00165
00166 while (*src==' ') src++;
00167
00168
00169 if (*src=='+' || *src=='-') *dst++ = *src++;
00170
00171
00172 while (1) {
00173 if (memchr(".0123456789",*src,11))
00174 *dst++ = *src++;
00175 else
00176 break;
00177 }
00178
00179
00180 *dst++ = 'E';
00181
00182
00183 if (*src=='+' || *src=='-') *dst++ = *src++;
00184
00185
00186 while (*src==' ') src++;
00187
00188
00189 while (1) {
00190 if (memchr("0123456789",*src,10))
00191 *dst++ = *src++;
00192 else
00193 break;
00194 }
00195
00196
00197 *dst = 0;
00198
00199
00200 if (*src) {
00201
00202 return 0.0;
00203 }
00204
00205 return atof(numstr);
00206 }
00207
00208
00209
00210
00211
00212
00213 Float_t
00214 NdbEndfIO::ReadReal(Bool_t *error, Int_t pos)
00215 {
00216 if (NextNumber(pos)) {
00217 *error = TRUE;
00218 return 0.0;
00219 }
00220 *error = FALSE;
00221 return SubReadReal(lastNumPos,NUMBER_SIZE);
00222 }
00223
00224
00225 Bool_t
00226 NdbEndfIO::ReadLine()
00227 {
00228 Long_t current_pos;
00229
00230
00231 lastNumPos = -NUMBER_SIZE;
00232
00233
00234 if (feof(f)) {
00235 lineTxt[0] = 0;
00236 return FALSE;
00237 }
00238
00239
00240 current_pos = ftell(f);
00241
00242
00243 fgets(lineTxt,sizeof(lineTxt),f);
00244
00245 lineLen = strlen(lineTxt);
00246
00247
00248 if (lineTxt[lineLen-1] == '\n')
00249 lineTxt[--lineLen] = 0;
00250
00251
00252 Int_t oldMAT = iMAT;
00253 Int_t oldMF = iMF;
00254 Int_t oldMT = iMT;
00255
00256
00257 iMAT = SubReadInt(66, 4);
00258 iMF = SubReadInt(70, 2);
00259 iMT = SubReadInt(72, 3);
00260 lineNum = SubReadInt(75, 5);
00261
00262
00263 if (iMAT && iMAT != oldMAT)
00264 matStart = current_pos;
00265
00266 if (iMF && iMF != oldMF)
00267 mfStart = current_pos;
00268
00269 if (iMT && iMT != oldMT)
00270 mtStart = current_pos;
00271
00272 return TRUE;
00273 }