FairRoot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FairGenericParAsciiFileIo.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 
12 // FairGenericParAsciiFileIo
13 //
14 // Interface class to ASCII file for input/output of parameters derived
15 // from FairParGenericSet
16 //
18 
20 
21 #include "FairParGenericSet.h" // for FairParGenericSet
22 #include "FairParSet.h" // for FairParSet
23 #include "FairParamList.h" // for FairParamObj, FairParamList
24 
25 #include <TCollection.h> // for TIter
26 #include <THashTable.h> // for THashTable
27 #include <TString.h> // for TString, operator<<
28 #include <fstream> // for fstream
29 #include <stdio.h> // for printf, sscanf
30 #include <string.h> // for strcmp, memcpy, strtok, etc
31 
32 using std::ios;
33 
35 
38 {
39  // constructor
40  // sets the name of the I/O class "FairGenericParIo"
41  // gets the pointer to the ASCII file
42  fName = "FairGenericParIo";
43 }
44 
46 {
47  // initializes the parameter container from ASCII file
48  if (!pFile) {
49  return kFALSE;
50  }
51 
52  if (pPar->InheritsFrom("FairParGenericSet")) {
53  return readGenericSet(static_cast<FairParGenericSet *>(pPar));
54  }
55 
56  Error("init(FairParSet*)", "%s does not inherit from FairParGenericSet", pPar->GetName());
57  return kFALSE;
58 }
59 
61 {
62  // writes the parameter container to ASCII file
63  if (!pFile) {
64  return -1;
65  }
66  if (pPar->InheritsFrom("FairParGenericSet")) {
67  return writeGenericSet(static_cast<FairParGenericSet *>(pPar));
68  }
69  Error("write(FairParSet*)", "%s does not inherit from FairParGenericSet", pPar->GetName());
70  return -1;
71 }
72 
73 template<class type>
74 const UChar_t *FairGenericParAsciiFileIo::readData(type t, const Char_t *format, TString &line, Int_t &length)
75 {
76  // reads c-type single data and arrays
77  const Int_t st = sizeof(t);
78  const Int_t maxbuf = 8000;
79  const Int_t bufSizeExt = 10000 * st;
80  const Char_t d[] = " \t";
81  Text_t buf[maxbuf];
82  TString s;
83  Int_t l = 0, bufSize = bufSizeExt;
84  UChar_t *val = new UChar_t[bufSize];
85  Ssiz_t m = line.Last('\\');
86  if (m < 0) {
87  sscanf(line.Data(), format, &t);
88  memcpy(&val[l], &t, st);
89  length = st;
90  } else {
91  do {
92  pFile->getline(buf, maxbuf);
93  if (buf[0] != '/' && buf[0] != '#') {
94  s = buf;
95  m = s.Last('\\');
96  if (m > 0 && m < s.Length()) {
97  s = s(0, m - 1);
98  }
99  if ((bufSize - 1000) < l) {
100  bufSize += bufSizeExt;
101  UChar_t *va = new UChar_t[bufSize];
102  memcpy(va, val, l);
103  delete[] val;
104  val = va;
105  }
106  Char_t *ss = strtok(const_cast<Char_t *>(s.Data()), d);
107  while (ss != 0) {
108  sscanf(ss, format, &t);
109  memcpy(&val[l], &t, st);
110  l += st;
111  ss = strtok(NULL, d);
112  }
113  }
114  } while (buf[0] != '#' && !pFile->eof() && m > 0);
115  length = l;
116  }
117  return val;
118 }
119 
120 template<class type>
121 void FairGenericParAsciiFileIo::writeData(type *val, Int_t nParams)
122 {
123  // writes c-type arrays
124  Int_t i = 0, k = 0;
125  while (k < nParams) {
126  if (i == 10) {
127  *pFile << " \\\n ";
128  i = 0;
129  }
130  *pFile << val[k] << " ";
131  i++;
132  k++;
133  }
134  *pFile << std::endl;
135 }
136 
137 Bool_t FairGenericParAsciiFileIo::readGenericSet(FairParGenericSet *pPar)
138 {
139  // reads condition-stype parameter containers from ASCII file
140  if (!pFile) {
141  return kFALSE;
142  }
143  pFile->clear();
144  pFile->seekg(0, ios::beg);
145  const Text_t *name = (pPar->GetName());
146 
147  if (!findContainer(name)) {
148  return kFALSE;
149  }
150 
151  const Char_t d[] = " \t";
152  FairParamList *paramList = new FairParamList;
153  const Int_t maxbuf = 8000;
154  Text_t buf[maxbuf];
155  buf[0] = '\0';
156  TString s, pName, pVal, pType;
157  Ssiz_t n, m;
158  while (buf[0] != '#' && !pFile->eof()) {
159  pFile->getline(buf, maxbuf);
160  if (buf[0] != '/' && buf[0] != '#') {
161  s = buf;
162  n = s.First(':');
163  // <DB> Check if empty buffer
164  if (s.IsNull())
165  continue;
166  if (n == -1) {
167  Error("readCond(FairParGenericSet*)", "%s:\n Missing backslash for parameter %s", name, pName.Data());
168  delete paramList;
169  return kFALSE;
170  }
171  pName = s(0, n);
172  s = s(n + 1, s.Length() - n - 1);
173  s = s.Strip(s.kLeading);
174  if (pName.CompareTo("author") == 0 || pName.CompareTo("description") == 0) {
175  m = s.Last('\\');
176  if (m <= 0) {
177  pVal = s;
178  } else {
179  pVal = s(0, m);
180  pVal += "\n";
181  while (m > 0) {
182  pFile->getline(buf, maxbuf);
183  if (buf[0] != '/') {
184  s = buf;
185  m = s.Last('\\');
186  if (m > 0) {
187  pVal += s(0, m);
188  pVal += "\n";
189  } else {
190  pVal += s;
191  }
192  }
193  }
194  }
195  if (pName.CompareTo("author") == 0) {
196  pPar->setAuthor(pVal.Data());
197  } else if (pName.CompareTo("description") == 0) {
198  pPar->setDescription(pVal.Data());
199  }
200  } else {
201  Char_t *ss = strtok(const_cast<Char_t *>(s.Data()), d);
202  pType = TString(ss);
203  Int_t mt = s.Index(pType);
204  s = s(mt + pType.Length() + 1, s.Length() - mt - pType.Length() - 1);
205  if (pType.CompareTo("Text_t") == 0) {
206  m = s.Last('\\');
207  if (m < 0) {
208  pVal = s;
209  } else {
210  pVal = "";
211  while (m >= 0) {
212  pFile->getline(buf, maxbuf);
213  if (buf[0] != '/') {
214  s = buf;
215  m = s.Last('\\');
216  if (m > 0) {
217  pVal += s(0, m);
218  pVal += "\n";
219  } else {
220  pVal += s;
221  }
222  }
223  }
224  }
225  pVal = pVal.Strip(pVal.kLeading);
226  if (pVal.Length() > 0) {
227  paramList->add(pName.Data(), pVal.Data());
228  }
229  } else {
230  const UChar_t *val = 0;
231  Int_t length = 0;
232  if (pType.CompareTo("Int_t") == 0) {
233  Int_t v = 0;
234  val = readData(v, "%i", s, length);
235  } else if (pType.CompareTo("Float_t") == 0) {
236  Float_t v = 0.F;
237  val = readData(v, "%f", s, length);
238  } else if (pType.CompareTo("Double_t") == 0) {
239  Double_t v = 0.;
240  val = readData(v, "%lf", s, length);
241  } else if (pType.CompareTo("Char_t") == 0) {
242  Char_t v = '0';
243  val = readData(v, "%c", s, length);
244  } else {
245  Error("readCond(FairParGenericSet*)",
246  "%s:\n Parameter %s with unsupported type %s",
247  name,
248  pName.Data(),
249  pType.Data());
250  delete paramList;
251  return kFALSE;
252  }
253  FairParamObj *obj = new FairParamObj(pName.Data());
254  obj->setParamType(pType.Data());
255  UChar_t *pValue = obj->setLength(length);
256  memcpy(pValue, val, length);
257  paramList->getList()->Add(obj);
258  delete[] val;
259  }
260  }
261  }
262  }
263  Bool_t allFound = pPar->getParams(paramList);
264  if (allFound) {
265  pPar->setInputVersion(1, inputNumber);
266  pPar->setChanged();
267  printf("%s initialized from Ascii file\n", name);
268  } else {
269  pPar->setInputVersion(-1, inputNumber);
270  }
271  delete paramList;
272  return allFound;
273 }
274 
275 Int_t FairGenericParAsciiFileIo::writeGenericSet(FairParGenericSet *pPar)
276 {
277  // writes condition-stype parameter containers to ASCII file
278  if (pFile) {
279  // Int_t version=1;
280  const Text_t *name = pPar->GetName();
281  const Text_t *context = pPar->getParamContext();
282  *pFile << sepLine;
283  *pFile << "# Class: " << name << "\n# Context: " << context << "\n";
284  *pFile << sepLine;
285  *pFile << "[" << name << "]\n";
286  writeComment(pPar);
287  *pFile << "//-----------------------------------------------------------"
288  "-----------------\n";
289  FairParamList *paramList = new FairParamList;
290  pPar->putParams(paramList);
291  THashTable *pList = paramList->getList();
292  TIter next(pList);
293  FairParamObj *po;
294  while ((po = static_cast<FairParamObj *>(next()))) {
295  const Char_t *pType = po->getParamType();
296  UChar_t *pValue = po->getParamValue();
297  if (po->isBasicType() && strcmp(pType, "UChar_t") != 0) {
298  if (strcmp(pType, "Text_t") == 0) {
299  TString val(reinterpret_cast<Char_t *>(pValue), po->getLength());
300  val.ReplaceAll("\n", " \\\n");
301  *pFile << po->GetName() << ": " << pType << " \\\n " << val.Data() << std::endl;
302  } else {
303  Int_t nParams = po->getNumParams();
304  if (nParams == 1) {
305  *pFile << po->GetName() << ": " << pType << " ";
306  } else {
307  *pFile << po->GetName() << ": " << pType << " \\\n ";
308  }
309  if (strcmp(pType, "Char_t") == 0) {
310  writeData(reinterpret_cast<Char_t *>(pValue), nParams);
311  } else if (strcmp(pType, "Int_t") == 0) {
312  writeData(reinterpret_cast<Int_t *>(pValue), nParams);
313  } else if (strcmp(pType, "Float_t") == 0) {
314  writeData(reinterpret_cast<Float_t *>(pValue), nParams);
315  } else if (strcmp(pType, "Double_t") == 0) {
316  writeData(reinterpret_cast<Double_t *>(pValue), nParams);
317  }
318  }
319  } else {
320  Error("writeCond(FairParGenericSet*)",
321  "Type %s of parameter %s not supported by ASCII I/O",
322  pType,
323  po->GetName());
324  // version=-1;
325  }
326  }
327  *pFile << sepLine;
328  delete paramList;
329  pPar->setChanged(kFALSE);
330  return 1;
331  }
332  Error("writeCond(FairParGenericSet*)", "Output is not writable");
333  return -1;
334 }
const char * getParamContext() const
Definition: FairParSet.h:75
virtual const char * GetName() const
Definition: FairParSet.h:38
const char * getParamType()
Definition: FairParamList.h:61
Bool_t findContainer(const Text_t *name)
pointer to ascii file
THashTable * getList()
void setChanged(Bool_t flag=kTRUE)
Definition: FairParSet.h:72
ClassImp(FairEventBuilder)
Int_t inputNumber
Definition: FairDetParIo.h:19
virtual Bool_t getParams(FairParamList *)=0
TString sepLine
header of container output in file
UChar_t * setLength(Int_t l)
UChar_t * getParamValue()
Definition: FairParamList.h:59
FairGenericParAsciiFileIo(std::fstream *f=0)
std::fstream * pFile
comment line
void setInputVersion(Int_t v=-1, Int_t i=0)
Definition: FairParSet.h:51
Bool_t isBasicType()
Definition: FairParamList.h:60
Int_t getLength()
Definition: FairParamList.h:64
virtual void putParams(FairParamList *)=0
void writeComment(FairParSet *)
Int_t getNumParams()
void setParamType(const Text_t *t)
void setAuthor(const char *s)
Definition: FairParSet.h:77
void setDescription(const char *s)
Definition: FairParSet.h:80
void add(FairParamObj &)