]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONSt1IniReader.cxx
Code for MUON Station1 (I.Hrivnacova)
[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 Revision 1.1  2003/01/28 13:21:06  morsch
19 Improved response simulation for station 1.
20 (M. Mac Cormick, I. Hrivnacova, D. Guez)
21
22 */
23
24 // Authors: David Guez, Ivana Hrivnacova, Marion MacCormick; IPN Orsay
25 //
26 // Class AliMUONIniReader
27 // ----------------------
28 // General class to read data in ASCII file format,
29 // similar to the Windows ".ini" files (a set of sections tagged by a 
30 //                      [ sectionName ]
31 //  and values defined in the way:
32 //                    parameterName = value
33 //
34 // comment lines can be introduced if the first non-blank character
35 // is either ';' or '#'
36
37 #if !defined(__HP_aCC) && !defined(__alpha)
38   #include <sstream>
39 #endif
40
41 #include <Riostream.h>
42 #include <Rstrstream.h>
43
44 #include "AliMUONSt1IniReader.h"
45
46 //______________________________________________________________________
47 AliMUONSt1IniReader::AliMUONSt1IniReader()
48   :fFile(),fCurrentType(kUndef),fEndOfFile(true)
49 {
50 // default constructor
51 // ---
52 }
53
54 //______________________________________________________________________
55 AliMUONSt1IniReader::AliMUONSt1IniReader(string fileName)
56 {
57 // normal constructor
58 // ---
59
60   fFile.open(fileName.c_str());
61   if (!fFile) {cerr<<"Unable to open file "<<fileName<<endl;}
62   fEndOfFile = !fFile.good();
63   fCurrentType=kUndef;
64 }
65
66 //______________________________________________________________________
67 AliMUONSt1IniReader::~AliMUONSt1IniReader()
68 {
69   //destructor
70  fFile.close();
71 }
72
73 //______________________________________________________________________
74 void AliMUONSt1IniReader::Reset()
75 {
76 // Reset the input stream. The file can be re-read after calling this function
77 // ---
78
79   fFile.clear();
80   fFile.seekg(0,ios::beg);
81   fCurrentType=kUndef;
82   fEndOfFile=!fFile.good();
83 }
84
85 //______________________________________________________________________
86 bool AliMUONSt1IniReader::ReadNextLine()
87 {
88 // The main function of this class.
89 // Read next line in the file and set CurrentType(), CurrentName() and
90 // CurrentValue() with the line's content
91 // ---
92
93   if ( (!fFile) || (fFile.eof()) || (!fFile.good()) ) 
94     {fEndOfFile=true; fCurrentType=kUndef; return false;}
95
96   string line;
97   getline(fFile,line);
98   if ( line.empty()) {             // this is a blank line
99     return ReadNextLine();
100   }
101 #if defined (__HP_aCC) || (__alpha)
102   strstream l;
103   l << line;
104 #else
105   istringstream l(line); 
106 #endif    
107   
108   char c;
109
110   l>>c;
111   if ( (c==';') || (c=='#') ) {    // this is a comment
112     return ReadNextLine();
113   }
114   
115   if (c=='[') {              // this is a chapter name
116     getline(l,fCurrentName,']');
117     fCurrentName=trail(fCurrentName);
118     fCurrentType=kChapter;
119     return true;
120   } else {
121     if (line.find_first_of("=") != string::npos ) {
122       l.putback(c);
123       getline(l,fCurrentName,'=');
124       fCurrentName = trail(fCurrentName);
125        
126       getline(l,fCurrentValue);
127       fCurrentValue = trail(fCurrentValue);
128       fCurrentType=kValue;
129       return true;
130     } else {
131       cerr<<"Warning, badly formated line..."<<line<<endl;
132       fCurrentType=kUndef;
133       return false;
134     }
135   }
136   // fCurrentType=kUndef;
137   // return false;
138        // unreachable
139 }
140
141 //______________________________________________________________________
142 AliMUONSt1IniReader::TValueList AliMUONSt1IniReader::MakeCurrentValueList()
143 {
144 // Read the next lines in the file
145 // until eof() or a new section is found. 
146 // Return the list of (name,value) pairs read.
147 // ---
148
149   TValueList ans;
150   while (true){
151     if (fCurrentType==kValue){
152       ans.push_back( TValuePair(fCurrentName,fCurrentValue));
153     } else break;
154     ReadNextLine();
155   }
156   return ans;
157 }
158
159 //______________________________________________________________________
160 AliMUONSt1IniReader::TChapter AliMUONSt1IniReader::MakeCurrentChapter()
161 {
162 // Searches in the rest file for a new section
163 // and return it's name and the list of (name,value) pairs in it
164 // ---
165
166   while ((!Eof()) && (fCurrentType != kChapter)) ReadNextLine();
167   if (Eof()) return TChapter();
168   string name = fCurrentName;
169   ReadNextLine();
170   return TChapter(name,MakeCurrentValueList());
171 }
172
173 //______________________________________________________________________
174 AliMUONSt1IniReader::TChapterList AliMUONSt1IniReader::MakeChapterList()
175 {
176 // Read the rest of the file and return all the chapter names and
177 // (name,value) pair lists found after the current position
178 // ---
179
180   TChapterList ans;
181   while (true) {
182     if (fCurrentType==kChapter) {
183       string s= fCurrentName;
184       ReadNextLine();
185       //ans.insert(TChapter(s,MakeCurrentValueList()));
186                    // does not compile on SunOS
187       ans.insert(TChapterList::value_type(s,MakeCurrentValueList()));
188     } else ReadNextLine();
189     if (fEndOfFile) break;
190   }
191   return ans;
192 }
193
194 //______________________________________________________________________
195 string AliMUONSt1IniReader::trail(const string& s) const
196 {
197 // Utility function: clear the blanks before and after the string <s>
198 // ---
199
200   string::size_type p1=s.find_first_not_of(" ");
201   if (p1==string::npos) return "";
202   string::size_type p2=s.find_last_not_of(" ");
203   return s.substr(p1,p2-p1+1);
204 }