]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/misc/AliHLTDDLRawReaderFile.cxx
compilation warnings corrected
[u/mrichter/AliRoot.git] / HLT / misc / AliHLTDDLRawReaderFile.cxx
1 // @(#) $Id$
2
3 // Author: Constantin Loizides <mailto:loizides@ikf.uni-frankfurt.de>
4 //*-- Copyright &copy ALICE HLT Group
5
6 #ifdef use_root
7 #include <Riostream.h>
8 #else
9 #include <iostream.h>
10 #endif
11
12 #include "AliHLTRootTypes.h"
13 #include "AliHLTStandardIncludes.h"
14 #include "AliHLTLogging.h"
15
16 #include "AliHLTDDLRawReaderFile.h"
17
18 #if __GNUC__ == 3
19 using namespace std;
20 #endif
21
22 /**************************************************************************
23  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
24  *                                                                        *
25  * Author: The ALICE Off-line Project.                                    *
26  * Contributors are mentioned in the code where appropriate.              *
27  *                                                                        *
28  * Permission to use, copy, modify and distribute this software and its   *
29  * documentation strictly for non-commercial purposes is hereby granted   *
30  * without fee, provided that the above copyright notice appears in all   *
31  * copies and that both the copyright notice and this permission notice   *
32  * appear in the supporting documentation. The authors make no claims     *
33  * about the suitability of this software for any purpose. It is          *
34  * provided "as is" without express or implied warranty.                  *
35  **************************************************************************/
36
37 /** \class AliHLTDDLRawReaderFile
38 <pre>
39 //_____________________________________________________________
40 // AliHLTDDLRawReaderFile (taken from the offline AliROOT code,
41 // original authors: D.Favretto and A.K.Mohanty)
42 //
43 // This is the base class for reading ddl raw data 
44 // and providing information about digits
45 </pre>
46 */
47
48 ClassImp(AliHLTDDLRawReaderFile)
49
50 AliHLTDDLRawReaderFile::AliHLTDDLRawReaderFile(const Char_t* name, Bool_t addnum)
51 {
52   // create an object to read digits from the given input file(s)
53   // if addNumber is true, a number starting at 1 is appended to the file name
54
55   fFileName = new Char_t[1024];
56   strcpy(fFileName,name);
57   if (!addnum) {
58     fFileNumber = -1;
59 #ifndef __DECCXX
60     fStream = new fstream(fFileName, ios::binary|ios::in);
61 #else
62     fStream = new fstream(fFileName, ios::in);
63 #endif
64   } else {
65     fFileNumber = 0;
66     fStream = NULL;
67     OpenNextFile();
68   }
69   fMiniHeader = new AliHLTDDLMiniHeader;
70   fBuffer = NULL;
71   fBufferSize = 0;
72 }
73
74 AliHLTDDLRawReaderFile::~AliHLTDDLRawReaderFile()
75 {
76   // close the input file
77   if(fFileName) delete fFileName;
78
79   if (fStream) {
80 #if defined(__HP_aCC) || defined(__DECCXX)
81     if (fStream->rdbuf()->is_open()) fStream->close();
82 #else
83     if (fStream->is_open()) fStream->close();
84 #endif
85     delete fStream;
86   }
87   delete fMiniHeader;
88   if (fBuffer) delete[] fBuffer;
89 }
90
91 Bool_t AliHLTDDLRawReaderFile::OpenNextFile()
92 {
93   if (fStream) {
94 #if defined(__HP_aCC) || defined(__DECCXX)
95     if (fStream->rdbuf()->is_open()) fStream->close();
96 #else
97     if (fStream->is_open()) fStream->close();
98 #endif
99     delete fStream;
100     fStream = NULL;
101   }
102   if (fFileNumber < 0) {
103     LOG(AliHLTLog::kError,"AliHLTDDLRawReaderFile::OpenNextFile","File")
104       <<"Could not open file, file number is negative."<<ENDLOG;
105     return kFALSE;
106   }
107
108   fFileNumber++;
109   Char_t fileName[1024];
110   sprintf(fileName, "%s%d", fFileName, fFileNumber);
111
112 #ifndef __DECCXX
113   fStream = new fstream(fileName, ios::binary|ios::in);
114 #else
115   fStream = new fstream(fileName, ios::in);
116 #endif
117 #if defined(__HP_aCC) || defined(__DECCXX)
118   return (fStream->rdbuf()->is_open());
119 #else
120   return (fStream->is_open());
121 #endif
122 }
123
124 Bool_t AliHLTDDLRawReaderFile::ReadMiniHeader()
125 {
126   // read a mini header at the current stream position
127   // returns kFALSE if the mini header could not be read
128
129   if (!fStream) return kFALSE;
130   do {
131     if (fCount > 0) fStream->seekg(Int_t(fStream->tellg()) + fCount);
132     while (!fStream->read((Char_t*) fMiniHeader, sizeof(AliHLTDDLMiniHeader))) {
133       if (!OpenNextFile()) return kFALSE;
134     }
135     //cout << fMiniHeader->fSize << " " << fMiniHeader->fDetectorID << " " << fMiniHeader->fVersion << " " << fMiniHeader->fCompressionFlag << " " << fMiniHeader->fDDLID << endl;
136     //cout << "loop here " << (Int_t)fMiniHeader->fDDLID<< endl;
137     CheckMiniHeader();
138     fCount = fMiniHeader->fSize;
139   } while (!IsSelected());
140   return kTRUE;
141 }
142
143 Bool_t AliHLTDDLRawReaderFile::ReadNextData(UChar_t*& data)
144 {
145   // reads the next payload at the current stream position
146   // returns kFALSE if the data could not be read
147
148   while (fCount == 0) {
149     if (!ReadMiniHeader()) return kFALSE;
150   }
151   if (fBufferSize < fCount) {
152     if (fBuffer) delete[] fBuffer;
153     fBufferSize = Int_t(fCount*1.2);
154     fBuffer = new UChar_t[fBufferSize];
155   }
156   if (!fStream->read((Char_t*) fBuffer, fCount)) {
157     LOG(AliHLTLog::kError,"AliHLTDDLRawReaderFile::ReadNextData","Data")
158       <<"Could not read next data!"<<ENDLOG;
159     return kFALSE;
160   }
161   fCount = 0;
162
163   data = fBuffer;
164   return kTRUE;
165 }
166
167 Bool_t AliHLTDDLRawReaderFile::ReadNext(UChar_t* data, Int_t size)
168 {
169   // reads the next block of data at the current stream position
170   // returns kFALSE if the data could not be read
171
172   if (!fStream->read((Char_t*) data, size)) {
173     LOG(AliHLTLog::kError,"AliHLTDDLRawReaderFile::ReadNext","Data")
174       <<"Could not read next data!"<<ENDLOG;
175     return kFALSE;
176   }
177   fCount -= size;
178   return kTRUE;
179 }
180
181 Bool_t AliHLTDDLRawReaderFile::Reset()
182 {
183   // reset the current stream position to the beginning of the file
184
185   if ((fFileNumber > 0) && fStream) {
186 #if defined(__HP_aCC) || defined(__DECCXX)
187     if (fStream->rdbuf()->is_open()) fStream->close();
188 #else
189     if (fStream->is_open()) fStream->close();
190 #endif
191     delete fStream;
192     fStream = NULL;
193     fFileNumber = 0;
194   }
195
196   if (!fStream) {
197     if (fFileNumber < 0) {
198 #ifndef __DECCXX
199       fStream = new fstream(fFileName, ios::binary|ios::in);
200 #else
201       fStream = new fstream(fFileName, ios::in);
202 #endif
203     } else {
204       if (!OpenNextFile()){
205         LOG(AliHLTLog::kError,"AliHLTDDLRawReaderFile::Reset","Data")
206           <<"Could not reset data stream!"<<ENDLOG;
207         return kFALSE;
208       }
209     }
210   }
211
212   if (!fStream || !fStream->rdbuf()->is_open()) return kFALSE;
213   fStream->seekg(0);
214   fCount = 0;
215   return kTRUE;
216 }