XSElements.cxx

Go to the documentation of this file.
00001 /*
00002  * $Header$
00003  * $Log$
00004  */
00005 
00006 #include <ctype.h>
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <string.h>
00010 
00011 #include "XSElements.h"
00012 
00013 //ClassImp(XSElement)
00014 
00015 /* =================== XSElement ===================== */
00016 XSElement::XSElement()
00017 {
00018         z = 0;
00019         name    = NULL;
00020         symbol  = NULL;
00021         isotope = NULL;
00022         atomic_weight   = NULL;
00023         density = NULL;
00024         melting_point   = NULL;
00025         boiling_point   = NULL;
00026         oxidation_states        = NULL;
00027         isotope = NULL;
00028 } // XSElement
00029 
00030 /* ---------- ~XSElement ----------- */
00031 XSElement::~XSElement()
00032 {
00033         if (name)               free(name);
00034         if (symbol)             free(symbol);
00035         if (atomic_weight)      free(atomic_weight);
00036         if (density)            free(density);
00037         if (melting_point)      free(melting_point);
00038         if (boiling_point)      free(boiling_point);
00039         if (oxidation_states)   free(oxidation_states);
00040         for (int i=0; i<ni; i++) {
00041                 free(isotope[i]);
00042                 free(isotope_info[i]);
00043         }
00044         free(isotope);
00045         free(isotope_info);
00046         free(isotope_stable);
00047 } // ~XSElement
00048 
00049 /* ------- IsotopeInfo ------- */
00050 // Search for information by name
00051 const char*
00052 XSElement::IsotopeInfo( const char *isot )
00053 {
00054         for (int i=0; i<ni; i++)
00055                 if (!strcmp(isotope[i],isot))
00056                         return isotope_info[i];
00057 
00058         return "-";
00059 } // IsotopeInfo
00060 
00061 /* ------- ReadLine ------- */
00062 /* Reads one line and allocates a string for it */
00063 char *
00064 XSElement::ReadLine(FILE *f)
00065 {
00066         char    buf[256];
00067         char    *p=buf;
00068         char    ch;
00069 
00070         /* skip leading spaces */
00071         do {
00072                 ch=fgetc(f);
00073         } while (isspace(ch));
00074 
00075         do {
00076                 *p++ = ch;
00077                 ch = fgetc(f);
00078         } while (ch != '\n');
00079         *p = 0;
00080         return strdup(buf);
00081 } /* ReadLine */
00082 
00083 /* ------- Read -------- */
00084 void
00085 XSElement::Read(FILE *f)
00086 {
00087         char    tmpsym[5], tmpname[30];
00088         fscanf(f,"%d %s %s %d",&z,tmpsym,tmpname,&ni);
00089 
00090         symbol = strdup(tmpsym);
00091         name = strdup(tmpname);
00092 
00093         if (ni==0) return;
00094 
00095         atomic_weight = ReadLine(f);
00096         density = ReadLine(f);
00097         melting_point = ReadLine(f);
00098         boiling_point = ReadLine(f);
00099         oxidation_states = ReadLine(f);
00100 
00101         isotope = (char **)malloc(ni*sizeof(char*));
00102         isotope_info = (char **)malloc(ni*sizeof(char*));
00103         isotope_stable = (Bool_t *)malloc(ni*sizeof(Bool_t));
00104 
00105         for (int i=0; i<ni; i++) {
00106                 char    ch;
00107                 char    buf[30];
00108 
00109                 /* get first character */
00110                 ch = fgetc(f);
00111                 if (ch != '*')
00112                         ungetc(ch,f);   // Put it back
00113 
00114                 fscanf(f,"%s",buf);
00115                 isotope[i] = strdup(buf);
00116                 isotope_info[i] = ReadLine(f);
00117 
00118                 isotope_stable[i] = (ch=='*');
00119         }
00120 } // Read
00121 
00122 /* =================== XSElements ===================== */
00123 //ClassImp(XSElements)
00124 
00125 XSElements::XSElements(const char *filename)
00126 {
00127         FILE    *f;
00128 
00129         if ((f=fopen(filename,"r"))==NULL) {
00130                 fprintf(stderr,"XSElements::XSElements: Error opening file %s\n",filename);
00131                 exit(0);
00132         }
00133 
00134         fscanf(f,"%d",&NElements);
00135         elements = new TObjArray(NElements);
00136 
00137         for (UInt_t i=0; i<NElements; i++) {
00138                 elements->Add(new XSElement());
00139                 ((XSElement*)(*elements)[i])->Read(f);
00140         }
00141         fclose(f);
00142 } // XSElements
00143 
00144 /* --------- ~XSElements ---------- */
00145 XSElements::~XSElements()
00146 {
00147         delete  elements;
00148 } // ~XSElements
00149 
00150 /* --------- Find ----------- */
00151 UInt_t
00152 XSElements::Find(const char *str)
00153 {
00154         for (UInt_t z=1; z<=NElements; z++) {
00155                 if (!strcmp(str,Name(z)))
00156                         return z;
00157                 if (!strcmp(str,Mnemonic(z)))
00158                         return z;
00159         }
00160         return 0;
00161 } // Find

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