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 // Authors: David Guez, Ivana Hrivnacova, Marion MacCormick; IPN Orsay
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
25 // and values defined in the way:
26 // parameterName = value
28 // comment lines can be introduced if the first non-blank character
29 // is either ';' or '#'
31 #if !defined(__HP_aCC) && !defined(__alpha)
35 #include <Riostream.h>
36 #include <Rstrstream.h>
38 #include "AliMUONSt1IniReader.h"
40 //______________________________________________________________________
41 AliMUONSt1IniReader::AliMUONSt1IniReader()
42 :fFile(),fCurrentType(kUndef),fEndOfFile(true)
44 // default constructor
48 //______________________________________________________________________
49 AliMUONSt1IniReader::AliMUONSt1IniReader(string fileName)
54 fFile.open(fileName.c_str());
55 if (!fFile) {cerr<<"Unable to open file "<<fileName<<endl;}
56 fEndOfFile = !fFile.good();
60 //______________________________________________________________________
61 AliMUONSt1IniReader::~AliMUONSt1IniReader()
67 //______________________________________________________________________
68 void AliMUONSt1IniReader::Reset()
70 // Reset the input stream. The file can be re-read after calling this function
74 fFile.seekg(0,ios::beg);
76 fEndOfFile=!fFile.good();
79 //______________________________________________________________________
80 bool AliMUONSt1IniReader::ReadNextLine()
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
87 if ( (!fFile) || (fFile.eof()) || (!fFile.good()) )
88 {fEndOfFile=true; fCurrentType=kUndef; return false;}
92 if ( line.empty()) { // this is a blank line
93 return ReadNextLine();
95 #if defined (__HP_aCC) || (__alpha)
99 istringstream l(line);
105 if ( (c==';') || (c=='#') ) { // this is a comment
106 return ReadNextLine();
109 if (c=='[') { // this is a chapter name
110 getline(l,fCurrentName,']');
111 fCurrentName=trail(fCurrentName);
112 fCurrentType=kChapter;
115 if (line.find_first_of("=") != string::npos ) {
117 getline(l,fCurrentName,'=');
118 fCurrentName = trail(fCurrentName);
120 getline(l,fCurrentValue);
121 fCurrentValue = trail(fCurrentValue);
125 cerr<<"Warning, badly formated line..."<<line<<endl;
130 // fCurrentType=kUndef;
135 //______________________________________________________________________
136 AliMUONSt1IniReader::TValueList AliMUONSt1IniReader::MakeCurrentValueList()
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.
145 if (fCurrentType==kValue){
146 ans.push_back( TValuePair(fCurrentName,fCurrentValue));
153 //______________________________________________________________________
154 AliMUONSt1IniReader::TChapter AliMUONSt1IniReader::MakeCurrentChapter()
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
160 while ((!Eof()) && (fCurrentType != kChapter)) ReadNextLine();
161 if (Eof()) return TChapter();
162 string name = fCurrentName;
164 return TChapter(name,MakeCurrentValueList());
167 //______________________________________________________________________
168 AliMUONSt1IniReader::TChapterList AliMUONSt1IniReader::MakeChapterList()
170 // Read the rest of the file and return all the chapter names and
171 // (name,value) pair lists found after the current position
176 if (fCurrentType==kChapter) {
177 string s= fCurrentName;
179 //ans.insert(TChapter(s,MakeCurrentValueList()));
180 // does not compile on SunOS
181 ans.insert(TChapterList::value_type(s,MakeCurrentValueList()));
182 } else ReadNextLine();
183 if (fEndOfFile) break;
188 //______________________________________________________________________
189 string AliMUONSt1IniReader::trail(const string& s) const
191 // Utility function: clear the blanks before and after the string <s>
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);