Fixes needed on HP
[u/mrichter/AliRoot.git] / STEER / AliRawReader.cxx
CommitLineData
a6e7b125 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 the base class for reading a raw data file and providing
19// information about digits
20//
21///////////////////////////////////////////////////////////////////////////////
22
23#include "AliRawReader.h"
24
25ClassImp(AliRawReader)
26
27
28AliRawReader::AliRawReader(const char* fileName, Bool_t addNumber)
29{
30// create an object to read digits from the given input file(s)
31// if addNumber is true, a number starting at 1 is appended to the file name
32
33 fFileName = fileName;
34 if (!addNumber) {
35 fFileNumber = -1;
36 fStream = new fstream(fileName, ios::binary|ios::in);
37 } else {
38 fFileNumber = 0;
39 fStream = NULL;
40 OpenNextFile();
41 }
42 fCount = 0;
43}
44
45AliRawReader::~AliRawReader()
46{
47// close the input file
48
49 if (fStream) {
f75eca3a 50#ifndef __HP_aCC
a6e7b125 51 if (fStream->is_open()) fStream->close();
f75eca3a 52#else
53 if (fStream->rdbuf()->is_open()) fStream->close();
54#endif
a6e7b125 55 delete fStream;
56 }
57}
58
59
60Bool_t AliRawReader::OpenNextFile()
61{
62 if (fStream) {
f75eca3a 63#ifndef __HP_aCC
a6e7b125 64 if (fStream->is_open()) fStream->close();
f75eca3a 65#else
66 if (fStream->rdbuf()->is_open()) fStream->close();
67#endif
a6e7b125 68 delete fStream;
69 fStream = NULL;
70 }
71 if (fFileNumber < 0) return kFALSE;
72
73 fFileNumber++;
74 char fileName[256];
75 sprintf(fileName, "%s%d", fFileName, fFileNumber);
76 fStream = new fstream(fileName, ios::binary|ios::in);
f75eca3a 77#ifndef __HP_aCC
a6e7b125 78 return (fStream->is_open());
f75eca3a 79#else
80 return (fStream->rdbuf()->is_open());
81#endif
a6e7b125 82}
83
84
85Bool_t AliRawReader::ReadMiniHeader()
86{
87// read a mini header at the current stream position
88// returns kFALSE if the mini header could not be read
89
90 if (!fStream) return kFALSE;
91 while (!fStream->read((char*) &fMiniHeader, sizeof(fMiniHeader))) {
92 if (!OpenNextFile()) return kFALSE;
93 }
94 if ((fMiniHeader.fMagicWord[2] != 0x12) ||
95 (fMiniHeader.fMagicWord[1] != 0x34) ||
96 (fMiniHeader.fMagicWord[0] != 0x56))
97 Error("ReadMiniHeader", "wrong magic word!");
98 fCount = fMiniHeader.fSize;
99 return kTRUE;
100}
101
102Bool_t AliRawReader::ReadNextInt(UInt_t& data)
103{
104// reads the next 4 bytes at the current stream position
105// returns kFALSE if the data not be read
106
107 while (fCount == 0) {
108 if (!ReadMiniHeader()) return kFALSE;
109 }
110 if (!fStream->read((char*) &data, sizeof(data))) {
111 Error("ReadNextInt", "could not read data!");
112 return kFALSE;
113 }
114 fCount -= sizeof(data);
115 return kTRUE;
116}
117
118Bool_t AliRawReader::ReadNextShort(UShort_t& data)
119{
120// reads the next 2 bytes at the current stream position
121// returns kFALSE if the data not be read
122
123 while (fCount == 0) {
124 if (!ReadMiniHeader()) return kFALSE;
125 }
126 if (!fStream->read((char*) &data, sizeof(data))) {
127 Error("ReadNextShort", "could not read data!");
128 return kFALSE;
129 }
130 fCount -= sizeof(data);
131 return kTRUE;
132}
133
134Bool_t AliRawReader::ReadNextChar(UChar_t& data)
135{
136// reads the next 1 byte at the current stream position
137// returns kFALSE if the data not be read
138
139 while (fCount == 0) {
140 if (!ReadMiniHeader()) return kFALSE;
141 }
142 if (!fStream->read((char*) &data, sizeof(data))) {
143 Error("ReadNextChar", "could not read data!");
144 return kFALSE;
145 }
146 fCount -= sizeof(data);
147 return kTRUE;
148}
149