]>
Commit | Line | Data |
---|---|---|
18de460b | 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 | // This is the class which is to be used during the writing of | |
18 | // simulated raw data (DDL files format). | |
19 | // It is using the root functionality in order to deal correctly | |
20 | // with little/big endian issue. By convention the detector raw | |
21 | // data payload is stored always with little endian (this corresponds | |
22 | // to the real life situation when the detector data is coming from | |
23 | // the hardware). The implementation of this class is based on Root | |
24 | // tobuf() method defined in Bytes.h | |
25 | //------------------------------------------------------------------------- | |
26 | ||
27 | #include <unistd.h> | |
28 | #include <Riostream.h> | |
29 | ||
30 | #include "AliFstream.h" | |
31 | #include "AliLog.h" | |
32 | ||
33 | ClassImp(AliFstream) | |
34 | ||
35 | //______________________________________________________________________________ | |
36 | AliFstream::AliFstream(): | |
37 | fFile(0x0), | |
38 | fBuffer(0x0), | |
39 | fBufferSize(0), | |
40 | fSwap(kFALSE) | |
41 | { | |
42 | // Default constructor | |
43 | ||
44 | for (Int_t i = 0; i < 8; i++) fBuffer[i] = 0; | |
45 | } | |
46 | ||
47 | //______________________________________________________________________________ | |
48 | AliFstream::AliFstream(const char *fileName): | |
49 | fFile(0x0), | |
50 | fBuffer(0x0), | |
51 | fBufferSize(0), | |
52 | fSwap(kFALSE) | |
53 | { | |
54 | // Constructor | |
55 | // Takes the input filename and | |
56 | // opens the output stream | |
57 | ||
58 | #ifndef __DECCXX | |
59 | fFile = new fstream(fileName, ios::binary|ios::out); | |
60 | #else | |
61 | fFile = new fstream(fileName, ios::out); | |
62 | #endif | |
63 | ||
64 | // Check endianess | |
65 | UInt_t temp = 1; | |
66 | UChar_t *ptemp = (UChar_t *)&temp; | |
67 | if (!ptemp[0]) fSwap = kTRUE; | |
68 | } | |
69 | ||
18de460b | 70 | //______________________________________________________________________________ |
71 | AliFstream::~AliFstream() | |
72 | { | |
73 | // Destructor | |
74 | // | |
75 | if (fFile) { | |
76 | fFile->close(); | |
77 | delete fFile; | |
78 | } | |
79 | if (fBuffer) delete [] fBuffer; | |
80 | } | |
81 | ||
82 | //______________________________________________________________________________ | |
41008432 | 83 | void AliFstream::Seekp(UInt_t position) |
18de460b | 84 | { |
85 | // Go to a given position | |
86 | // inside the output stream | |
41008432 | 87 | if (fFile) fFile->seekp(position); |
18de460b | 88 | } |
89 | ||
90 | //______________________________________________________________________________ | |
41008432 | 91 | UInt_t AliFstream::Tellp() |
18de460b | 92 | { |
93 | // Return the current | |
94 | // position inside the | |
95 | // output stream | |
41008432 | 96 | if (fFile) return fFile->tellp(); |
18de460b | 97 | else return 0; |
98 | } | |
99 | ||
100 | //______________________________________________________________________________ | |
101 | void AliFstream::WriteBuffer(const char *buffer, UInt_t size, Bool_t force) | |
102 | { | |
103 | // Write the buffer to a file | |
104 | // In case the user gives a 'force' | |
105 | // flag then the buffer is written | |
106 | // as it is. Otherwise, we check the | |
107 | // endianess and swap the buffer data | |
108 | // so that it is always stored using | |
109 | // little endian format. | |
41008432 | 110 | |
111 | // The raw data payload size is always | |
112 | // 4 bytes aligned | |
113 | if ((size % 4) != 0) | |
114 | AliFatal(Form("Size of the buffer is not multiple of 4 (size = %d) !",size)); | |
115 | ||
18de460b | 116 | if (force) { |
117 | fFile->write(buffer,size); | |
118 | } | |
119 | else { | |
120 | if (!fSwap) { | |
121 | // Little endian - do nothing | |
122 | fFile->write(buffer,size); | |
123 | } | |
124 | else { | |
125 | // Big endian - swap the buffer contents | |
126 | if (size > fBufferSize) { | |
127 | if (fBuffer) delete [] fBuffer; | |
128 | fBuffer = new UChar_t[size]; | |
129 | fBufferSize = size; | |
130 | } | |
131 | swab(buffer,fBuffer,size); | |
132 | fFile->write((const char *)fBuffer,size); | |
133 | } | |
134 | } | |
135 | } |