]>
Commit | Line | Data |
---|---|---|
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 | //______________________________________________________________________ | |
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 | } | |
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,']'); | |
3c25381f | 111 | fCurrentName=Trail(fCurrentName); |
ba030c0e | 112 | fCurrentType=kChapter; |
113 | return true; | |
114 | } else { | |
115 | if (line.find_first_of("=") != string::npos ) { | |
116 | l.putback(c); | |
117 | getline(l,fCurrentName,'='); | |
3c25381f | 118 | fCurrentName = Trail(fCurrentName); |
ba030c0e | 119 | |
120 | getline(l,fCurrentValue); | |
3c25381f | 121 | fCurrentValue = Trail(fCurrentValue); |
ba030c0e | 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 | //______________________________________________________________________ | |
40c8ec24 | 136 | AliMUONSt1IniReader::ValueList AliMUONSt1IniReader::MakeCurrentValueList() |
ba030c0e | 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 | ||
40c8ec24 | 143 | ValueList ans; |
ba030c0e | 144 | while (true){ |
145 | if (fCurrentType==kValue){ | |
40c8ec24 | 146 | ans.push_back( ValuePair(fCurrentName,fCurrentValue)); |
ba030c0e | 147 | } else break; |
148 | ReadNextLine(); | |
149 | } | |
150 | return ans; | |
151 | } | |
152 | ||
153 | //______________________________________________________________________ | |
40c8ec24 | 154 | AliMUONSt1IniReader::Chapter AliMUONSt1IniReader::MakeCurrentChapter() |
ba030c0e | 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(); | |
40c8ec24 | 161 | if (Eof()) return Chapter(); |
ba030c0e | 162 | string name = fCurrentName; |
163 | ReadNextLine(); | |
40c8ec24 | 164 | return Chapter(name,MakeCurrentValueList()); |
ba030c0e | 165 | } |
166 | ||
167 | //______________________________________________________________________ | |
40c8ec24 | 168 | AliMUONSt1IniReader::ChapterList AliMUONSt1IniReader::MakeChapterList() |
ba030c0e | 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 | ||
40c8ec24 | 174 | ChapterList ans; |
ba030c0e | 175 | while (true) { |
176 | if (fCurrentType==kChapter) { | |
177 | string s= fCurrentName; | |
178 | ReadNextLine(); | |
40c8ec24 | 179 | //ans.insert(Chapter(s,MakeCurrentValueList())); |
5f91c9e8 | 180 | // does not compile on SunOS |
40c8ec24 | 181 | ans.insert(ChapterList::value_type(s,MakeCurrentValueList())); |
ba030c0e | 182 | } else ReadNextLine(); |
183 | if (fEndOfFile) break; | |
184 | } | |
185 | return ans; | |
186 | } | |
187 | ||
188 | //______________________________________________________________________ | |
3c25381f | 189 | string AliMUONSt1IniReader::Trail(const string& s) const |
ba030c0e | 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 | } |