]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONSt1IniReader.cxx
Additional protection in the destructor
[u/mrichter/AliRoot.git] / MUON / AliMUONSt1IniReader.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /*
17 $Log$
18 */
19
20 // Authors: David Guez, Ivana Hrivnacova, Marion MacCormick; IPN Orsay
21 //
22 // Class AliMUONIniReader
23 // ----------------------
24 // General class to read data in ASCII file format,
25 // similar to the Windows ".ini" files (a set of sections tagged by a 
26 //                      [ sectionName ]
27 //  and values defined in the way:
28 //                    parameterName = value
29 //
30 // comment lines can be introduced if the first non-blank character
31 // is either ';' or '#'
32
33 #include <iostream>
34 #include <sstream>
35
36 #include "AliMUONSt1IniReader.h"
37
38 using namespace std;
39
40 //______________________________________________________________________
41 AliMUONSt1IniReader::AliMUONSt1IniReader()
42   :fFile(),fCurrentType(kUndef),fEndOfFile(true)
43 {
44 // default constructor
45 // ---
46 }
47
48 //______________________________________________________________________
49 AliMUONSt1IniReader::AliMUONSt1IniReader(string fileName)
50 {
51 // normal constructor
52 // ---
53
54   fFile.open(fileName.c_str());
55   if (!fFile) {cerr<<"Unable to open file "<<fileName<<endl;}
56   fEndOfFile = !fFile.good();
57   fCurrentType=kUndef;
58 }
59
60 //______________________________________________________________________
61 AliMUONSt1IniReader::~AliMUONSt1IniReader()
62 {
63   //destructor
64  fFile.close();
65 }
66
67 //______________________________________________________________________
68 void AliMUONSt1IniReader::Reset()
69 {
70 // Reset the input stream. The file can be re-read after calling this function
71 // ---
72
73   fFile.clear();
74   fFile.seekg(0,ios::beg);
75   fCurrentType=kUndef;
76   fEndOfFile=!fFile.good();
77 }
78
79 //______________________________________________________________________
80 bool AliMUONSt1IniReader::ReadNextLine()
81 {
82 // The main function of this class.
83 // Read next line in the file and set CurrentType(), CurrentName() and
84 // CurrentValue() with the line's content
85 // ---
86
87   if ( (!fFile) || (fFile.eof()) || (!fFile.good()) ) 
88     {fEndOfFile=true; fCurrentType=kUndef; return false;}
89
90   string line;
91   getline(fFile,line);
92   if ( line.empty()) {             // this is a blank line
93     return ReadNextLine();
94   }
95   istringstream l(line); 
96   
97   char c;
98
99   l>>c;
100   if ( (c==';') || (c=='#') ) {    // this is a comment
101     return ReadNextLine();
102   }
103   
104   if (c=='[') {              // this is a chapter name
105     getline(l,fCurrentName,']');
106     fCurrentName=trail(fCurrentName);
107     fCurrentType=kChapter;
108     return true;
109   } else {
110     if (line.find_first_of("=") != string::npos ) {
111       l.putback(c);
112       getline(l,fCurrentName,'=');
113       fCurrentName = trail(fCurrentName);
114        
115       getline(l,fCurrentValue);
116       fCurrentValue = trail(fCurrentValue);
117       fCurrentType=kValue;
118       return true;
119     } else {
120       cerr<<"Warning, badly formated line..."<<line<<endl;
121       fCurrentType=kUndef;
122       return false;
123     }
124   }
125   fCurrentType=kUndef;
126   return false;
127 }
128
129 //______________________________________________________________________
130 AliMUONSt1IniReader::TValueList AliMUONSt1IniReader::MakeCurrentValueList()
131 {
132 // Read the next lines in the file
133 // until eof() or a new section is found. 
134 // Return the list of (name,value) pairs read.
135 // ---
136
137   TValueList ans;
138   while (true){
139     if (fCurrentType==kValue){
140       ans.push_back( TValuePair(fCurrentName,fCurrentValue));
141     } else break;
142     ReadNextLine();
143   }
144   return ans;
145 }
146
147 //______________________________________________________________________
148 AliMUONSt1IniReader::TChapter AliMUONSt1IniReader::MakeCurrentChapter()
149 {
150 // Searches in the rest file for a new section
151 // and return it's name and the list of (name,value) pairs in it
152 // ---
153
154   while ((!Eof()) && (fCurrentType != kChapter)) ReadNextLine();
155   if (Eof()) return TChapter();
156   string name = fCurrentName;
157   ReadNextLine();
158   return TChapter(name,MakeCurrentValueList());
159 }
160
161 //______________________________________________________________________
162 AliMUONSt1IniReader::TChapterList AliMUONSt1IniReader::MakeChapterList()
163 {
164 // Read the rest of the file and return all the chapter names and
165 // (name,value) pair lists found after the current position
166 // ---
167
168   TChapterList ans;
169   while (true) {
170     if (fCurrentType==kChapter) {
171       string s= fCurrentName;
172       ReadNextLine();
173       ans.insert(TChapter(s,MakeCurrentValueList()));
174     } else ReadNextLine();
175     if (fEndOfFile) break;
176   }
177   return ans;
178 }
179
180 //______________________________________________________________________
181 string AliMUONSt1IniReader::trail(const string& s) const
182 {
183 // Utility function: clear the blanks before and after the string <s>
184 // ---
185
186   string::size_type p1=s.find_first_not_of(" ");
187   if (p1==string::npos) return "";
188   string::size_type p2=s.find_last_not_of(" ");
189   return s.substr(p1,p2-p1+1);
190 }