NdbMTReacDesc.cxx

Go to the documentation of this file.
00001 #include <ctype.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 
00005 #include "NdbMTReacDesc.h"
00006 
00007 ClassImp(NdbMTReacDesc)
00008 
00009 /* ================ NdbMTReacDesc ================== */
00010 NdbMTReacDesc::NdbMTReacDesc(const char *filename)
00011 {
00012         Init(filename);
00013 } // NdbMTReacDesc
00014 
00015 /* ------- ~NdbMTReacDesc ------- */
00016 NdbMTReacDesc::~NdbMTReacDesc()
00017 {
00018         if (shrt) {
00019                 for (int i=0; i<mt.GetSize(); i++) {
00020                         if (shrt[i]) free(shrt[i]);
00021                         if (desc[i]) free(desc[i]);
00022                         if (comment[i]) free(comment[i]);
00023                 }
00024                 free(shrt);
00025                 free(desc);
00026                 free(comment);
00027         }
00028 } // ~NdbMTReacDesc
00029 
00030 /* ------ Init ------ */
00031 void
00032 NdbMTReacDesc::Init(const char *filename)
00033 {
00034         shrt = NULL;
00035         desc = NULL;
00036         comment = NULL;
00037 
00038         FILE    *f;
00039 
00040         if ((f=fopen(filename,"r"))==NULL) {
00041                 fprintf(stderr,"ERROR: NdbMTReacDesc::NdbMTReacDesc(%s) cannot open file.\n",filename);
00042                 return;
00043         }
00044 
00045         /* ----- First read total number of MT's ------ */
00046         Int_t   N;
00047 
00048         fscanf(f,"%d",&N);
00049         if (N<=0) {
00050                 fprintf(stderr,"ERROR: NdbMTReacDesc::NdbMTReacDesc(%s) error reading from file.\n",filename);
00051                 fclose(f);
00052         }
00053 
00054         /* ----- Allocate memory ----- */
00055         mt.Set(N);
00056         shrt = (char **)malloc(N * sizeof(char*));
00057         desc = (char **)malloc(N * sizeof(char*));
00058         comment = (char **)malloc(N * sizeof(char*));
00059 
00060         for (int i=0; i<N; i++) {
00061                 /* --- read mt number and short description --- */
00062                 Int_t   mt_num, ch;
00063                 char    str[512], str2[256];
00064 
00065                 fscanf(f,"%d",&mt_num);
00066 
00067                 mt.AddAt(mt_num,i);
00068 
00069                 /* --- skip blanks --- */
00070                 do {} while (isspace(ch=fgetc(f)));
00071                 ungetc(ch,f);
00072 
00073                 /* get rest of line */
00074                 fgets(str,sizeof(str),f);
00075 
00076                 /* --- strip last newline char --- */
00077                 str[strlen(str)-1] = 0;
00078 
00079                 shrt[i] = strdup(str);
00080 
00081                 /* --- Read the description line until the empty line --- */
00082                 str[0] = 0;
00083                 while (1) {
00084                         fgets(str2,sizeof(str2),f);
00085                         if (str2[0] == '\n') break;
00086                         strlcat(str,str2,512);
00087                 }
00088                 str[strlen(str)-1] = 0;
00089                 desc[i] = strdup(str);
00090 
00091                 /* --- Read the description line until the empty line --- */
00092                 str[0] = 0;
00093                 while (1) {
00094                         fgets(str2,sizeof(str2),f);
00095                         if (str2[0] == '\n') break;
00096                         strlcat(str,str2,512);
00097                 }
00098                 if (str && str[0])
00099                         str[strlen(str)-1] = 0;
00100                 comment[i] = strdup(str);
00101         }
00102         fclose(f);
00103 } // NdbMTReacDesc
00104 
00105 /* ------- FindMT -------- */
00106 Int_t
00107 NdbMTReacDesc::FindMT( Int_t MT )
00108 {
00109         /* Make a linear search */
00110         if (shrt)
00111                 for (int i=0; i<mt.GetSize(); i++)
00112                         if (mt[i] == MT)
00113                                 return i;
00114                         else
00115                         if (mt[i] > MT)
00116                                 break;
00117         return -1;
00118 } // FindMT
00119 
00120 /* ------- GetShort -------- */
00121 char *
00122 NdbMTReacDesc::GetShort(Int_t MT)
00123 {
00124         Int_t   idx = FindMT(MT);
00125         if (idx<0)
00126                 return NULL;
00127 
00128         return shrt[idx];
00129 } // GetShort
00130 
00131 /* ------- GetDescription -------- */
00132 char *
00133 NdbMTReacDesc::GetDescription(Int_t MT)
00134 {
00135         Int_t   idx = FindMT(MT);
00136         if (idx<0)
00137                 return NULL;
00138 
00139         return desc[idx];
00140 } // GetDescription
00141 
00142 /* ------- GetComment -------- */
00143 char *
00144 NdbMTReacDesc::GetComment(Int_t MT)
00145 {
00146         Int_t   idx = FindMT(MT);
00147         if (idx<0)
00148                 return NULL;
00149 
00150         return comment[idx];
00151 } // GetComment

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