]> git.uio.no Git - u/mrichter/AliRoot.git/blame - MUON/AliMUONSt1IniReader.cxx
fWSN->Eval(0.001) to avoid fpe.
[u/mrichter/AliRoot.git] / MUON / AliMUONSt1IniReader.cxx
CommitLineData
ba030c0e 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
88cb7938 16/* $Id$ */
ba030c0e 17
18// Authors: David Guez, Ivana Hrivnacova, Marion MacCormick; IPN Orsay
19//
20// Class AliMUONIniReader
21// ----------------------
22// General class to read data in ASCII file format,
23// similar to the Windows ".ini" files (a set of sections tagged by a
24// [ sectionName ]
25// and values defined in the way:
26// parameterName = value
27//
28// comment lines can be introduced if the first non-blank character
29// is either ';' or '#'
30
5f91c9e8 31#if !defined(__HP_aCC) && !defined(__alpha)
32 #include <sstream>
33#endif
ba030c0e 34
5f91c9e8 35#include <Riostream.h>
36#include <Rstrstream.h>
ba030c0e 37
5f91c9e8 38#include "AliMUONSt1IniReader.h"
ba030c0e 39
40//______________________________________________________________________
41AliMUONSt1IniReader::AliMUONSt1IniReader()
42 :fFile(),fCurrentType(kUndef),fEndOfFile(true)
43{
44// default constructor
45// ---
46}
47
48//______________________________________________________________________
49AliMUONSt1IniReader::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//______________________________________________________________________
61AliMUONSt1IniReader::~AliMUONSt1IniReader()
62{
63 //destructor
64 fFile.close();
65}
66
67//______________________________________________________________________
68void 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//______________________________________________________________________
80bool 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 }
5f91c9e8 95#if defined (__HP_aCC) || (__alpha)
96 strstream l;
97 l << line;
98#else
ba030c0e 99 istringstream l(line);
5f91c9e8 100#endif
ba030c0e 101
102 char c;
103
104 l>>c;
105 if ( (c==';') || (c=='#') ) { // this is a comment
106 return ReadNextLine();
107 }
108
109 if (c=='[') { // this is a chapter name
110 getline(l,fCurrentName,']');
111 fCurrentName=trail(fCurrentName);
112 fCurrentType=kChapter;
113 return true;
114 } else {
115 if (line.find_first_of("=") != string::npos ) {
116 l.putback(c);
117 getline(l,fCurrentName,'=');
118 fCurrentName = trail(fCurrentName);
119
120 getline(l,fCurrentValue);
121 fCurrentValue = trail(fCurrentValue);
122 fCurrentType=kValue;
123 return true;
124 } else {
125 cerr<<"Warning, badly formated line..."<<line<<endl;
126 fCurrentType=kUndef;
127 return false;
128 }
129 }
5f91c9e8 130 // fCurrentType=kUndef;
131 // return false;
132 // unreachable
ba030c0e 133}
134
135//______________________________________________________________________
136AliMUONSt1IniReader::TValueList AliMUONSt1IniReader::MakeCurrentValueList()
137{
138// Read the next lines in the file
139// until eof() or a new section is found.
140// Return the list of (name,value) pairs read.
141// ---
142
143 TValueList ans;
144 while (true){
145 if (fCurrentType==kValue){
146 ans.push_back( TValuePair(fCurrentName,fCurrentValue));
147 } else break;
148 ReadNextLine();
149 }
150 return ans;
151}
152
153//______________________________________________________________________
154AliMUONSt1IniReader::TChapter AliMUONSt1IniReader::MakeCurrentChapter()
155{
156// Searches in the rest file for a new section
157// and return it's name and the list of (name,value) pairs in it
158// ---
159
160 while ((!Eof()) && (fCurrentType != kChapter)) ReadNextLine();
161 if (Eof()) return TChapter();
162 string name = fCurrentName;
163 ReadNextLine();
164 return TChapter(name,MakeCurrentValueList());
165}
166
167//______________________________________________________________________
168AliMUONSt1IniReader::TChapterList AliMUONSt1IniReader::MakeChapterList()
169{
170// Read the rest of the file and return all the chapter names and
171// (name,value) pair lists found after the current position
172// ---
173
174 TChapterList ans;
175 while (true) {
176 if (fCurrentType==kChapter) {
177 string s= fCurrentName;
178 ReadNextLine();
5f91c9e8 179 //ans.insert(TChapter(s,MakeCurrentValueList()));
180 // does not compile on SunOS
181 ans.insert(TChapterList::value_type(s,MakeCurrentValueList()));
ba030c0e 182 } else ReadNextLine();
183 if (fEndOfFile) break;
184 }
185 return ans;
186}
187
188//______________________________________________________________________
189string AliMUONSt1IniReader::trail(const string& s) const
190{
191// Utility function: clear the blanks before and after the string <s>
192// ---
193
194 string::size_type p1=s.find_first_not_of(" ");
195 if (p1==string::npos) return "";
196 string::size_type p2=s.find_last_not_of(" ");
197 return s.substr(p1,p2-p1+1);
198}