]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONSt1IniReader.cxx
Adding local data container for clusterisation and tracking (Christian Finck)
[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 /* $Id$ */
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
31 #if !defined(__HP_aCC) && !defined(__alpha)
32   #include <sstream>
33 #endif
34
35 #include <Riostream.h>
36 #include <Rstrstream.h>
37
38 #include "AliMUONSt1IniReader.h"
39
40 //______________________________________________________________________
41 AliMUONSt1IniReader::AliMUONSt1IniReader()
42   :fFile(),fCurrentType(kUndef),fEndOfFile(true)
43 {
44 // default constructor
45 // ---
46 }
47
48 //______________________________________________________________________
49 AliMUONSt1IniReader::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 //______________________________________________________________________
61 AliMUONSt1IniReader::~AliMUONSt1IniReader()
62 {
63   //destructor
64  fFile.close();
65 }
66
67 //______________________________________________________________________
68 void 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 //______________________________________________________________________
80 bool 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   }
95 #if defined (__HP_aCC) || (__alpha)
96   strstream l;
97   l << line;
98 #else
99   istringstream l(line); 
100 #endif    
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   }
130   // fCurrentType=kUndef;
131   // return false;
132        // unreachable
133 }
134
135 //______________________________________________________________________
136 AliMUONSt1IniReader::ValueList 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   ValueList ans;
144   while (true){
145     if (fCurrentType==kValue){
146       ans.push_back( ValuePair(fCurrentName,fCurrentValue));
147     } else break;
148     ReadNextLine();
149   }
150   return ans;
151 }
152
153 //______________________________________________________________________
154 AliMUONSt1IniReader::Chapter 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 Chapter();
162   string name = fCurrentName;
163   ReadNextLine();
164   return Chapter(name,MakeCurrentValueList());
165 }
166
167 //______________________________________________________________________
168 AliMUONSt1IniReader::ChapterList 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   ChapterList ans;
175   while (true) {
176     if (fCurrentType==kChapter) {
177       string s= fCurrentName;
178       ReadNextLine();
179       //ans.insert(Chapter(s,MakeCurrentValueList()));
180                    // does not compile on SunOS
181       ans.insert(ChapterList::value_type(s,MakeCurrentValueList()));
182     } else ReadNextLine();
183     if (fEndOfFile) break;
184   }
185   return ans;
186 }
187
188 //______________________________________________________________________
189 string 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 }