NdbEndfIO.cxx

Go to the documentation of this file.
00001 #include <stdlib.h>
00002 
00003 #include "NdbEndfIO.h"
00004 
00005 #define NUMBER_SIZE     11
00006 
00007 char    NdbEndfIO::_str[100];           // define static _str var
00008 
00009 ClassImp(NdbEndfIO)
00010 
00011 /* ============ NdbEndfIO ============== */
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 } // NdbEndfIO
00024 
00025 
00026 /* -------- FindMAT --------- */
00027 // Will search for the specific material
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 } // FindMAT
00040 
00041 /* -------- FindMATMF --------- */
00042 // Will search for the specific material - mf
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 } // FindMATMF
00055 
00056 /* -------- FindMATMFMT --------- */
00057 // Will search for the specific material - mf - mt
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 } // FindMATMFMT
00070 
00071 /* ------------ FindMFMT ------------- */
00072 Bool_t
00073 NdbEndfIO::FindMFMT( Int_t mf, Int_t mt )
00074 {
00075         // If current MFMT is higher than what we search go to the beggining
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 } // FindMFMT
00087 
00088 /* -------- Substr ---------- */
00089 char *
00090 NdbEndfIO::Substr(Int_t start, Int_t length)
00091 {
00092         if (start + length > lineLen) {
00093 //              error(ERR_INVALID_RECORD);
00094                 return NULL;
00095         }
00096         memcpy(_str, lineTxt+start, length);
00097         _str[length] = 0;
00098 
00099         return _str;
00100 } // Substr
00101 
00102 /* -------- NextNumber ---------- */
00103 /* Advance lastNumber Point to the correct number
00104  * so to read 6 numbers of width 11 in each line
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;       // Remember current MF, MT
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 } // NextNumber
00123 
00124 /* -------- SubReadInt ---------- */
00125 Int_t
00126 NdbEndfIO::SubReadInt(Int_t start, Int_t length)
00127 {
00128         NdbEndfIO::Substr(start,length);
00129         return atoi(_str);
00130 } // SubReadInt
00131 
00132 /* ---------- ReadInt ------------- */
00133 /* Read one by one the int numbers with length 11 from the current line.
00134  * If the this is the last one then it prompts for an new line
00135  * only if it stays inside the same MF:MT type
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 } // ReadInt
00147 
00148 /* -------- SubReadReal ---------- */
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         // Real numbers are in the following formats
00158         //      +/- N.NNNNNN+/-N
00159         //      +/- N.NNNNN+/-bN        (b=Blank)
00160         //      +/- N.NNNNN+/-NN
00161 
00162         dst = numstr;
00163         src = _str;
00164 
00165         /* skip blanks */
00166         while (*src==' ') src++;
00167 
00168         /* copy sign */
00169         if (*src=='+' || *src=='-') *dst++ = *src++;
00170 
00171         /* copy realpart */
00172         while (1) {
00173                 if (memchr(".0123456789",*src,11))
00174                         *dst++ = *src++;
00175                 else
00176                         break;
00177         }
00178 
00179         /* append the exponent */
00180         *dst++ = 'E';
00181 
00182         /* copy sign */
00183         if (*src=='+' || *src=='-') *dst++ = *src++;
00184 
00185         /* skip blanks */
00186         while (*src==' ') src++;
00187 
00188         /* copy exponent */
00189         while (1) {
00190                 if (memchr("0123456789",*src,10))
00191                         *dst++ = *src++;
00192                 else
00193                         break;
00194         }
00195 
00196         /* close the destination */
00197         *dst = 0;
00198 
00199         /* if something is still in source then we have an error number */
00200         if (*src) {
00201 //              error(ERR_INVALID_REAL_NUMBER);
00202                 return 0.0;
00203         }
00204 
00205         return atof(numstr);
00206 } // SubReadReal
00207 
00208 /* ---------- ReadReal ------------- */
00209 /* Read one by one the real numbers with length 11 from the current line.
00210  * If the this is the last one then it prompts for an new line
00211  * only if it stays inside the same MF:MT type
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 } // ReadReal
00223 
00224 /* -------- ReadLine -------- */
00225 Bool_t
00226 NdbEndfIO::ReadLine()
00227 {
00228         Long_t  current_pos;
00229 
00230         // --- Force the next number reading to be from the beggining of line
00231         lastNumPos = -NUMBER_SIZE;
00232 
00233         // --- Have we reached the end of file
00234         if (feof(f)) {
00235                 lineTxt[0] = 0;
00236                 return FALSE;
00237         }
00238 
00239         // --- Remember the position of start of line ---
00240         current_pos = ftell(f);
00241 
00242         // --- Read one line ---
00243         fgets(lineTxt,sizeof(lineTxt),f);
00244 
00245         lineLen = strlen(lineTxt);
00246 
00247         // Chop the trailing newline char
00248         if (lineTxt[lineLen-1] == '\n')
00249                 lineTxt[--lineLen] = 0;
00250 
00251         // --- Remember previous values ---
00252         Int_t   oldMAT  = iMAT;
00253         Int_t   oldMF   = iMF;
00254         Int_t   oldMT   = iMT;
00255 
00256         // --- Read the material, mf, mt and line number ---
00257         iMAT    = SubReadInt(66, 4);
00258         iMF     = SubReadInt(70, 2);
00259         iMT     = SubReadInt(72, 3);
00260         lineNum = SubReadInt(75, 5);
00261 
00262         // --- Update pointers if necessary ---
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 } // ReadLine

Generated on Tue Jul 5 15:15:03 2011 for ROOT_528-00b_version by  doxygen 1.5.1