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 '#'
30 // Included in AliRoot 2003/01/28
32 #if !defined(__HP_aCC) && !defined(__alpha)
36 #include <Riostream.h>
37 #include <Rstrstream.h>
39 #include "AliMUONSt1IniReader.h"
41 //______________________________________________________________________
42 AliMUONSt1IniReader::AliMUONSt1IniReader()
43 :fFile(),fCurrentType(kUndef),fEndOfFile(true)
45 // default constructor
49 //______________________________________________________________________
50 AliMUONSt1IniReader::AliMUONSt1IniReader(string fileName)
55 fFile.open(fileName.c_str());
56 if (!fFile) {cerr<<"Unable to open file "<<fileName<<endl;}
57 fEndOfFile = !fFile.good();
61 //______________________________________________________________________
62 AliMUONSt1IniReader::~AliMUONSt1IniReader()
68 //______________________________________________________________________
69 void AliMUONSt1IniReader::Reset()
71 // Reset the input stream. The file can be re-read after calling this function
75 fFile.seekg(0,ios::beg);
77 fEndOfFile=!fFile.good();
80 //______________________________________________________________________
81 bool AliMUONSt1IniReader::ReadNextLine()
83 // The main function of this class.
84 // Read next line in the file and set CurrentType(), CurrentName() and
85 // CurrentValue() with the line's content
88 if ( (!fFile) || (fFile.eof()) || (!fFile.good()) )
89 {fEndOfFile=true; fCurrentType=kUndef; return false;}
93 if ( line.empty()) { // this is a blank line
94 return ReadNextLine();
96 #if defined (__HP_aCC) || (__alpha)
100 istringstream l(line);
106 if ( (c==';') || (c=='#') ) { // this is a comment
107 return ReadNextLine();
110 if (c=='[') { // this is a chapter name
111 getline(l,fCurrentName,']');
112 fCurrentName=Trail(fCurrentName);
113 fCurrentType=kChapter;
116 if (line.find_first_of("=") != string::npos ) {
118 getline(l,fCurrentName,'=');
119 fCurrentName = Trail(fCurrentName);
121 getline(l,fCurrentValue);
122 fCurrentValue = Trail(fCurrentValue);
126 cerr<<"Warning, badly formated line..."<<line<<endl;
131 // fCurrentType=kUndef;
136 //______________________________________________________________________
137 AliMUONSt1IniReader::ValueList AliMUONSt1IniReader::MakeCurrentValueList()
139 // Read the next lines in the file
140 // until eof() or a new section is found.
141 // Return the list of (name,value) pairs read.
146 if (fCurrentType==kValue){
147 ans.push_back( ValuePair(fCurrentName,fCurrentValue));
154 //______________________________________________________________________
155 AliMUONSt1IniReader::Chapter AliMUONSt1IniReader::MakeCurrentChapter()
157 // Searches in the rest file for a new section
158 // and return it's name and the list of (name,value) pairs in it
161 while ((!Eof()) && (fCurrentType != kChapter)) ReadNextLine();
162 if (Eof()) return Chapter();
163 string name = fCurrentName;
165 return Chapter(name,MakeCurrentValueList());
168 //______________________________________________________________________
169 AliMUONSt1IniReader::ChapterList AliMUONSt1IniReader::MakeChapterList()
171 // Read the rest of the file and return all the chapter names and
172 // (name,value) pair lists found after the current position
177 if (fCurrentType==kChapter) {
178 string s= fCurrentName;
180 //ans.insert(Chapter(s,MakeCurrentValueList()));
181 // does not compile on SunOS
182 ans.insert(ChapterList::value_type(s,MakeCurrentValueList()));
183 } else ReadNextLine();
184 if (fEndOfFile) break;
189 //______________________________________________________________________
190 string AliMUONSt1IniReader::Trail(const string& s) const
192 // Utility function: clear the blanks before and after the string <s>
195 string::size_type p1=s.find_first_not_of(" ");
196 if (p1==string::npos) return "";
197 string::size_type p2=s.find_last_not_of(" ");
198 return s.substr(p1,p2-p1+1);