1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
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)
24 // Authors: David Guez, Ivana Hrivnacova, Marion MacCormick; IPN Orsay
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
31 // and values defined in the way:
32 // parameterName = value
34 // comment lines can be introduced if the first non-blank character
35 // is either ';' or '#'
37 #if !defined(__HP_aCC) && !defined(__alpha)
41 #include <Riostream.h>
42 #include <Rstrstream.h>
44 #include "AliMUONSt1IniReader.h"
46 //______________________________________________________________________
47 AliMUONSt1IniReader::AliMUONSt1IniReader()
48 :fFile(),fCurrentType(kUndef),fEndOfFile(true)
50 // default constructor
54 //______________________________________________________________________
55 AliMUONSt1IniReader::AliMUONSt1IniReader(string fileName)
60 fFile.open(fileName.c_str());
61 if (!fFile) {cerr<<"Unable to open file "<<fileName<<endl;}
62 fEndOfFile = !fFile.good();
66 //______________________________________________________________________
67 AliMUONSt1IniReader::~AliMUONSt1IniReader()
73 //______________________________________________________________________
74 void AliMUONSt1IniReader::Reset()
76 // Reset the input stream. The file can be re-read after calling this function
80 fFile.seekg(0,ios::beg);
82 fEndOfFile=!fFile.good();
85 //______________________________________________________________________
86 bool AliMUONSt1IniReader::ReadNextLine()
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
93 if ( (!fFile) || (fFile.eof()) || (!fFile.good()) )
94 {fEndOfFile=true; fCurrentType=kUndef; return false;}
98 if ( line.empty()) { // this is a blank line
99 return ReadNextLine();
101 #if defined (__HP_aCC) || (__alpha)
105 istringstream l(line);
111 if ( (c==';') || (c=='#') ) { // this is a comment
112 return ReadNextLine();
115 if (c=='[') { // this is a chapter name
116 getline(l,fCurrentName,']');
117 fCurrentName=trail(fCurrentName);
118 fCurrentType=kChapter;
121 if (line.find_first_of("=") != string::npos ) {
123 getline(l,fCurrentName,'=');
124 fCurrentName = trail(fCurrentName);
126 getline(l,fCurrentValue);
127 fCurrentValue = trail(fCurrentValue);
131 cerr<<"Warning, badly formated line..."<<line<<endl;
136 // fCurrentType=kUndef;
141 //______________________________________________________________________
142 AliMUONSt1IniReader::TValueList AliMUONSt1IniReader::MakeCurrentValueList()
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.
151 if (fCurrentType==kValue){
152 ans.push_back( TValuePair(fCurrentName,fCurrentValue));
159 //______________________________________________________________________
160 AliMUONSt1IniReader::TChapter AliMUONSt1IniReader::MakeCurrentChapter()
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
166 while ((!Eof()) && (fCurrentType != kChapter)) ReadNextLine();
167 if (Eof()) return TChapter();
168 string name = fCurrentName;
170 return TChapter(name,MakeCurrentValueList());
173 //______________________________________________________________________
174 AliMUONSt1IniReader::TChapterList AliMUONSt1IniReader::MakeChapterList()
176 // Read the rest of the file and return all the chapter names and
177 // (name,value) pair lists found after the current position
182 if (fCurrentType==kChapter) {
183 string s= fCurrentName;
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;
194 //______________________________________________________________________
195 string AliMUONSt1IniReader::trail(const string& s) const
197 // Utility function: clear the blanks before and after the string <s>
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);