FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairDetParAsciiFileIo.cxx
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 //*-- AUTHOR : Ilse Koenig
9 //*-- Created : 21/10/2004
10 //*-- modified : 28/01/2009 by Ilse Koenig
12 // FairDetParAsciiFileIo
13 //
14 // Class for parameter input/output from/into Ascii file
15 // Base class for all detector Ascii I/Os
16 //
18 
19 #include "FairDetParAsciiFileIo.h"
20 
21 #include "FairParSet.h" // for FairParSet
22 
23 #include <fstream> // for fstream
24 #include <stdio.h> // for printf, sprintf
25 #include <string.h> // for strlen, strncmp
26 
27 using std::ios;
28 
30  : FairDetParIo()
31  , fHeader("")
32  , sepLine("##############################################################################\n")
33  , pFile(f)
34 {
35  // constructor
36  /* pFile=f;
37  sepLine=
38  "##############################################################################\n";
39  */
40 }
41 
42 Bool_t FairDetParAsciiFileIo::findContainer(const Text_t* name)
43 {
44  // searches the container in the file
45  const Int_t maxbuf = 4000;
46  Text_t buf[maxbuf];
47  Text_t buf2[maxbuf];
48  sprintf(buf2, "%s%s%s", "[", name, "]");
49  // cout << " buf2 " << buf2 << endl;
50  pFile->clear();
51  pFile->seekg(0, ios::beg);
52  while (!pFile->eof()) {
53  pFile->getline(buf, maxbuf);
54  if (buf[0] != '[') {
55  continue;
56  }
57  // cout << " buf: " << buf << endl;
58  if (!strncmp(buf, buf2, strlen(buf2))) {
59  break;
60  }
61  }
62  if (pFile->eof()) {
63  return kFALSE;
64  }
65  return kTRUE;
66 }
67 
68 Bool_t FairDetParAsciiFileIo::checkAllFound(Int_t* set, Int_t setSize)
69 {
70  // checks if all modules have been initialized
71  Bool_t allFound = kTRUE;
72  for (Int_t i = 0; i < setSize; i++) {
73  if (set[i] == 999) {
74  set[i] = 0;
75  printf(" %i", i);
76  }
77  if (set[i]) {
78  allFound = kFALSE;
79  }
80  }
81  printf("\n");
82  return allFound;
83 }
84 
85 void FairDetParAsciiFileIo::writeHeader(const Text_t* name,
86  const Text_t* context,
87  const Text_t* author,
88  const Text_t* description)
89 {
90  // calls the function putAsciiHeader(...) of the parameter container
91  // class and writes the header to the file
92  pFile->write(sepLine, strlen(sepLine));
93  pFile->write(fHeader.Data(), fHeader.Length());
94  pFile->write(sepLine, strlen(sepLine));
95  *pFile << "[" << name << "]\n";
96  if (strlen(context) > 0) {
97  *pFile << "// Parameter Context: " << context << "\n";
98  }
99  if (strlen(author) > 0) {
100  *pFile << "author: " << author << "\n";
101  }
102  TString t = description;
103  if (!t.IsNull()) {
104  t = t.ReplaceAll("\n", " \\\n");
105  *pFile << "description: " << t << "\n";
106  }
107  *pFile << "//-----------------------------------------------------------"
108  "-----------------\n";
109 }
110 
111 void FairDetParAsciiFileIo::readComment(const Char_t* lastBuf, FairParSet* pPar)
112 {
113  // reads the author or the description
114  const Int_t maxbuf = 4000;
115  Text_t buf[maxbuf];
116  TString s = lastBuf;
117  TString pName, pVal;
118  Ssiz_t n = s.First(':');
119  Ssiz_t m = s.Last('\\');
120  pName = s(0, n);
121  if (m > 0) {
122  pVal = s(n + 1, m - n - 1);
123  } else {
124  pVal = s(n + 1, s.Length() - n - 1);
125  }
126  pVal = pVal.Strip(pVal.kLeading);
127  while (m > 0) {
128  pVal += "\n";
129  pFile->getline(buf, maxbuf);
130  if (buf[0] != '/') {
131  s = buf;
132  m = s.Last('\\');
133  if (m > 0) {
134  pVal += s(0, m);
135  } else {
136  pVal += s;
137  }
138  }
139  }
140  if (pName.CompareTo("author") == 0) {
141  pPar->setAuthor(pVal.Data());
142  } else if (pName.CompareTo("description") == 0) {
143  pPar->setDescription(pVal.Data());
144  }
145 }
146 
148 {
149  TString t = pPar->getAuthor();
150  if (!t.IsNull()) {
151  *pFile << "author: " << t << "\n";
152  }
153  t = pPar->getDescription();
154  if (!t.IsNull()) {
155  t = t.ReplaceAll("\n", " \\\n");
156  *pFile << "description: " << t << "\n";
157  }
158 }
159 
161 /*
162 FairDetParAsciiFileIo::FairDetParAsciiFileIo(std::fstream* f) {
163 // constructor
164 pFile=f;
165 sepLine=
166 "##############################################################################\n";
167 }
168 
169 Bool_t FairDetParAsciiFileIo::findContainer(const Text_t* name) {
170 // searches the container in the file
171 const Int_t maxbuf=4000;
172 Text_t buf[maxbuf];
173 Text_t buf2[maxbuf];
174 sprintf(buf2,"%s%s%s","[",name,"]");
175 pFile->clear();
176 pFile->seekg(0,ios::beg);
177 while (!pFile->eof()) {
178 pFile->getline(buf,maxbuf);
179 if (buf[0]!='[') continue;
180 if (!strncmp(buf,buf2,strlen(buf2))) break;
181 }
182 if (pFile->eof()) return kFALSE;
183 return kTRUE;
184 }
185 
186 void FairDetParAsciiFileIo::writeHeader(const Text_t* name, const Text_t* context,
187  const Text_t* author, const Text_t* description) {
188 // calls the function putAsciiHeader(...) of the parameter container
189 // class and writes the header to the file
190 pFile->write(sepLine,strlen(sepLine));
191 pFile->write(fHeader.Data(),fHeader.Length());
192 pFile->write(sepLine,strlen(sepLine));
193 *pFile<<"["<<name<<"]\n";
194 if (strlen(context)>0) *pFile<<"// Parameter Context: "<<context<<"\n";
195 if (strlen(author)>0) *pFile<<"author: "<<author<<"\n";
196 TString t=description;
197 if (!t.IsNull()) {
198 t=t.ReplaceAll("\n"," \\\n");
199 *pFile<<"description: "<<t<<"\n";
200 }
201 *pFile<<"//-----------------------------------------------------------"
202  "-----------------\n";
203 }
204 
205 void FairDetParAsciiFileIo::readComment(const char* lastBuf, FairParSet* pPar) {
206 // reads the author or the description
207 const Int_t maxbuf=4000;
208 Text_t buf[maxbuf];
209 TString s=lastBuf;
210 TString pName, pVal;
211 Ssiz_t n=s.First(':');
212 Ssiz_t m=s.Last('\\');
213 pName=s(0,n);
214 if (m>0) pVal=s(n+1,m-n-1);
215 else pVal=s(n+1,s.Length()-n-1);
216 pVal=pVal.Strip(pVal.kLeading);
217 while (m>0) {
218 pVal+="\n";
219 pFile->getline(buf,maxbuf);
220 if (buf[0]!='/') {
221  s=buf;
222  m=s.Last('\\');
223  if (m>0) pVal+=s(0,m);
224  else pVal+=s;
225 }
226 }
227 if (pName.CompareTo("author")==0) pPar->setAuthor(pVal.Data());
228 else if ( pName.CompareTo("description")==0) pPar->setDescription(pVal.Data());
229 }
230 
231 void FairDetParAsciiFileIo::writeComment(FairParSet* pPar) {
232 TString t=pPar->getAuthor();
233 if (!t.IsNull()) *pFile<<"author: "<<t<<"\n";
234 t=pPar->getDescription();
235 if (!t.IsNull()) {
236 t=t.ReplaceAll("\n"," \\\n");
237 *pFile<<"description: "<<t<<"\n";
238 }
239 }
240 */
Bool_t findContainer(const Text_t *name)
pointer to ascii file
ClassImp(FairEventBuilder)
const char * getDescription() const
Definition: FairParSet.h:81
TString sepLine
header of container output in file
void readComment(const Char_t *, FairParSet *)
std::fstream * pFile
comment line
const char * getAuthor() const
Definition: FairParSet.h:78
void writeComment(FairParSet *)
void writeHeader(const Text_t *, const Text_t *context="", const Text_t *author="", const Text_t *description="")
Bool_t checkAllFound(Int_t *, Int_t)
void setAuthor(const char *s)
Definition: FairParSet.h:77
void setDescription(const char *s)
Definition: FairParSet.h:80
FairDetParAsciiFileIo(std::fstream *f)