]>
Commit | Line | Data |
---|---|---|
04fa961a | 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 | /////////////////////////////////////////////////////////////////////////////// | |
17 | // | |
18 | // This is a class for reading a raw data file and providing | |
19 | // information about digits | |
20 | // | |
21 | /////////////////////////////////////////////////////////////////////////////// | |
22 | ||
23 | #include "AliRawReaderFile.h" | |
24 | ||
25 | ||
26 | ClassImp(AliRawReaderFile) | |
27 | ||
28 | ||
29 | AliRawReaderFile::AliRawReaderFile(const char* fileName, Bool_t addNumber) | |
30 | { | |
31 | // create an object to read digits from the given input file(s) | |
32 | // if addNumber is true, a number starting at 1 is appended to the file name | |
33 | ||
34 | fFileName = fileName; | |
35 | if (!addNumber) { | |
36 | fFileNumber = -1; | |
37 | #ifndef __DECCXX | |
38 | fStream = new fstream(fileName, ios::binary|ios::in); | |
39 | #else | |
40 | fStream = new fstream(fileName, ios::in); | |
41 | #endif | |
42 | } else { | |
43 | fFileNumber = 0; | |
44 | fStream = NULL; | |
45 | OpenNextFile(); | |
46 | } | |
47 | fMiniHeader = new AliMiniHeader; | |
48 | fBuffer = NULL; | |
49 | fBufferSize = 0; | |
50 | } | |
51 | ||
52 | AliRawReaderFile::~AliRawReaderFile() | |
53 | { | |
54 | // close the input file | |
55 | ||
56 | if (fStream) { | |
57 | #if defined(__HP_aCC) || defined(__DECCXX) | |
58 | if (fStream->rdbuf()->is_open()) fStream->close(); | |
59 | #else | |
60 | if (fStream->is_open()) fStream->close(); | |
61 | #endif | |
62 | delete fStream; | |
63 | } | |
64 | delete fMiniHeader; | |
65 | if (fBuffer) delete[] fBuffer; | |
66 | } | |
67 | ||
68 | ||
69 | Bool_t AliRawReaderFile::OpenNextFile() | |
70 | { | |
71 | if (fStream) { | |
72 | #if defined(__HP_aCC) || defined(__DECCXX) | |
73 | if (fStream->rdbuf()->is_open()) fStream->close(); | |
74 | #else | |
75 | if (fStream->is_open()) fStream->close(); | |
76 | #endif | |
77 | delete fStream; | |
78 | fStream = NULL; | |
79 | } | |
80 | if (fFileNumber < 0) return kFALSE; | |
81 | ||
82 | fFileNumber++; | |
83 | char fileName[256]; | |
84 | sprintf(fileName, "%s%d", fFileName.Data(), fFileNumber); | |
85 | #ifndef __DECCXX | |
86 | fStream = new fstream(fileName, ios::binary|ios::in); | |
87 | #else | |
88 | fStream = new fstream(fileName, ios::in); | |
89 | #endif | |
90 | #if defined(__HP_aCC) || defined(__DECCXX) | |
91 | return (fStream->rdbuf()->is_open()); | |
92 | #else | |
93 | return (fStream->is_open()); | |
94 | #endif | |
95 | } | |
96 | ||
97 | ||
98 | Bool_t AliRawReaderFile::ReadMiniHeader() | |
99 | { | |
100 | // read a mini header at the current stream position | |
101 | // returns kFALSE if the mini header could not be read | |
102 | ||
103 | if (!fStream) return kFALSE; | |
104 | do { | |
105 | if (fCount > 0) fStream->seekg(Int_t(fStream->tellg()) + fCount); | |
106 | while (!fStream->read((char*) fMiniHeader, sizeof(AliMiniHeader))) { | |
107 | if (!OpenNextFile()) return kFALSE; | |
108 | } | |
109 | CheckMiniHeader(); | |
110 | fCount = fMiniHeader->fSize; | |
111 | } while (!IsSelected()); | |
112 | return kTRUE; | |
113 | } | |
114 | ||
115 | Bool_t AliRawReaderFile::ReadNextData(UChar_t*& data) | |
116 | { | |
117 | // reads the next payload at the current stream position | |
118 | // returns kFALSE if the data could not be read | |
119 | ||
120 | while (fCount == 0) { | |
121 | if (!ReadMiniHeader()) return kFALSE; | |
122 | } | |
123 | if (fBufferSize < fCount) { | |
124 | if (fBuffer) delete[] fBuffer; | |
125 | fBufferSize = Int_t(fCount*1.2); | |
126 | fBuffer = new UChar_t[fBufferSize]; | |
127 | } | |
128 | if (!fStream->read((char*) fBuffer, fCount)) { | |
129 | Error("ReadNext", "could not read data!"); | |
130 | return kFALSE; | |
131 | } | |
132 | fCount = 0; | |
133 | ||
134 | data = fBuffer; | |
135 | return kTRUE; | |
136 | } | |
137 | ||
138 | Bool_t AliRawReaderFile::ReadNext(UChar_t* data, Int_t size) | |
139 | { | |
140 | // reads the next block of data at the current stream position | |
141 | // returns kFALSE if the data could not be read | |
142 | ||
143 | if (!fStream->read((char*) data, size)) { | |
144 | Error("ReadNext", "could not read data!"); | |
145 | return kFALSE; | |
146 | } | |
147 | fCount -= size; | |
148 | return kTRUE; | |
149 | } | |
150 | ||
151 | ||
152 | Bool_t AliRawReaderFile::Reset() | |
153 | { | |
154 | // reset the current stream position to the beginning of the file | |
155 | ||
156 | if ((fFileNumber > 0) && fStream) { | |
157 | #if defined(__HP_aCC) || defined(__DECCXX) | |
158 | if (fStream->rdbuf()->is_open()) fStream->close(); | |
159 | #else | |
160 | if (fStream->is_open()) fStream->close(); | |
161 | #endif | |
162 | delete fStream; | |
163 | fStream = NULL; | |
164 | fFileNumber = 0; | |
165 | } | |
166 | ||
167 | if (!fStream) { | |
168 | if (fFileNumber < 0) { | |
169 | #ifndef __DECCXX | |
170 | fStream = new fstream(fFileName, ios::binary|ios::in); | |
171 | #else | |
172 | fStream = new fstream(fFileName, ios::in); | |
173 | #endif | |
174 | } else { | |
175 | if (!OpenNextFile()) return kFALSE; | |
176 | } | |
177 | } | |
178 | ||
179 | if (!fStream || !fStream->rdbuf()->is_open()) return kFALSE; | |
180 | fStream->seekg(0); | |
181 | fCount = 0; | |
182 | return kTRUE; | |
183 | } | |
184 |