1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright by The HDF Group.                                               *
3  * Copyright by the Board of Trustees of the University of Illinois.         *
4  * All rights reserved.                                                      *
5  *                                                                           *
6  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
7  * terms governing use, modification, and redistribution, is contained in    *
8  * the files COPYING and Copyright.html.  COPYING can be found at the root   *
9  * of the source code distribution tree; Copyright.html can be found at the  *
10  * root level of an installed copy of the electronic HDF5 document set and   *
11  * is linked from the top-level documents page.  It can also be found at     *
12  * http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have          *
13  * access to either file, you may request a copy from help@hdfgroup.org.     *
14  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /* Packet Table wrapper classes
17  *
18  * Wraps the H5PT Packet Table C functions in C++ objects
19  *
20  * Nat Furrer and James Laird
21  * February 2004
22  */
23 
24 /* High-level library internal header file */
25 #include "H5HLprivate2.h"
26 
27 #include "H5PacketTable.h"
28 
29     /********************************/
30     /* PacketTable superclass       */
31     /********************************/
32 
33     /* "Open" Constructor
34      * Opens an existing packet table, which can contain either fixed-length or
35      * variable-length packets.
36      */
PacketTable(hid_t fileID,char * name)37     PacketTable::PacketTable(hid_t fileID, char* name)
38     {
39         table_id = H5PTopen( fileID, name);
40     }
41 
42     /* Destructor
43      * Cleans up the packet table
44      */
~PacketTable()45     PacketTable::~PacketTable()
46     {
47         H5PTclose( table_id);
48     }
49 
50     /* IsValid
51      * Returns true if this packet table is valid, false otherwise.
52      * Use this after the constructor to ensure HDF did not have
53      * any trouble making or opening the packet table.
54      */
IsValid()55     bool PacketTable::IsValid()
56     {
57         if (H5PTis_valid(table_id) == 0)
58             return true;
59         else
60             return false;
61     }
62 
63 #ifdef VLPT_REMOVED
64     /* IsVariableLength
65      * Return 1 if this packet table is a Variable Length packet table,
66      * return 0 if it is Fixed Length.  Returns -1 if the table is
67      * invalid (not open).
68      */
IsVariableLength()69     int PacketTable::IsVariableLength()
70     {
71         return H5PTis_varlen(table_id);
72     }
73 #endif /* VLPT_REMOVED */
74 
75     /* ResetIndex
76      * Sets the index to point to the first packet in the packet table
77      */
ResetIndex()78     void PacketTable::ResetIndex()
79     {
80         H5PTcreate_index(table_id);
81     }
82 
83     /* SetIndex
84      * Sets the index to point to the packet specified by index.
85      * Returns 0 on success, negative on failure (if index is out of bounds)
86      */
SetIndex(hsize_t index)87     int PacketTable::SetIndex(hsize_t index)
88     {
89         return H5PTset_index(table_id, index);
90     }
91 
92     /* SetIndex
93      * Sets the index to point to the packet specified by index.
94      * Returns 0 on success, negative on failure (if index is out of bounds)
95      */
GetIndex(int & error)96     hsize_t PacketTable::GetIndex(int &error)
97     {
98         hsize_t index;
99 
100         error = H5PTget_index(table_id, &index);
101         if(error < 0)
102            return 0;
103         else
104            return index;
105     }
106 
107     /* GetPacketCount
108      * Returns the number of packets in the packet table.  Error
109      * is set to 0 on success.  On failure, returns 0 and
110      * error is set to negative.
111      */
GetPacketCount(int & error)112     hsize_t PacketTable::GetPacketCount(int& error)
113     {
114         hsize_t npackets;
115 
116         error = H5PTget_num_packets( table_id, (hsize_t *)&npackets);
117         return npackets;
118     }
119 
120     /********************************/
121     /* Fixed-Length Packet Table    */
122     /********************************/
123 
124     /* Constructor
125      * Creates a packet table in which to store fixed length packets.
126      * Takes the ID of the file the packet table will be created in, the name of
127      * the packet table, the ID of the datatype of the set, and the size
128      * of a memory chunk used in chunking.
129      */
FL_PacketTable(hid_t fileID,char * name,hid_t dtypeID,hsize_t chunkSize,int compression)130     FL_PacketTable::FL_PacketTable(hid_t fileID, char* name, hid_t dtypeID, hsize_t chunkSize, int compression)
131     {
132         table_id = H5PTcreate_fl ( fileID, name, dtypeID, chunkSize, compression);
133     }
134 
135     /* "Open" Constructor
136      * Opens an existing fixed-length packet table.
137      * Fails if the packet table specified is variable-length.
138      */
FL_PacketTable(hid_t fileID,char * name)139     FL_PacketTable::FL_PacketTable(hid_t fileID, char* name) : PacketTable(fileID, name)
140     {
141 #ifdef VLPT_REMOVED
142         if( H5PTis_varlen(table_id) != 0 )    // If this is not a fixed-length table
143         {
144             H5PTclose(table_id);
145             table_id = -1;
146         }
147 #endif /* VLPT_REMOVED */
148     }
149 
150     /* AppendPacket
151      * Adds a single packet to the packet table.  Takes a pointer
152      * to the location of the data in memory.
153      * Returns 0 on success, negative on failure
154      */
AppendPacket(void * data)155     int FL_PacketTable::AppendPacket(void * data)
156     {
157         return H5PTappend(table_id, 1, data);
158     }
159 
160     /* AppendPackets (multiple packets)
161      * Adds multiple packets to the packet table.  Takes the number of packets
162      * to be added and a pointer to their location in memory.
163      * Returns 0 on success, -1 on failure.
164      */
AppendPackets(size_t numPackets,void * data)165     int FL_PacketTable::AppendPackets(size_t numPackets, void * data)
166     {
167         return H5PTappend(table_id, numPackets, data);
168     }
169 
170     /* GetPacket (indexed)
171      * Gets a single packet from the packet table.  Takes the index
172      * of the packet (with 0 being the first packet) and a pointer
173      * to memory where the data should be stored.
174      * Returns 0 on success, negative on failure
175      */
GetPacket(hsize_t index,void * data)176     int FL_PacketTable::GetPacket(hsize_t index, void * data)
177     {
178         return H5PTread_packets(table_id, index, 1, data);
179     }
180 
181     /* GetPackets (multiple packets)
182      * Gets multiple packets at once, all packets between
183      * startIndex and endIndex inclusive.  Also takes a pointer to
184      * the memory where these packets should be stored.
185      * Returns 0 on success, negative on failure.
186      */
GetPackets(hsize_t startIndex,hsize_t endIndex,void * data)187     int FL_PacketTable::GetPackets(hsize_t startIndex, hsize_t endIndex, void * data)
188     {
189         // Make sure the range of indexes is valid
190         if (startIndex > endIndex)
191             return -1;
192 
193         return  H5PTread_packets(table_id, startIndex, (size_t)(endIndex-startIndex+1), data);
194     }
195 
196     /* GetNextPacket (single packet)
197      * Gets the next packet in the packet table.  Takes a pointer to
198      * memory where the packet should be stored.
199      * Returns 0 on success, negative on failure.  Index
200      * is not advanced to the next packet on failure.
201      */
GetNextPacket(void * data)202     int FL_PacketTable::GetNextPacket(void * data)
203     {
204         return H5PTget_next(table_id, 1, data);
205     }
206 
207     /* GetNextPackets (multiple packets)
208      * Gets the next numPackets packets in the packet table.  Takes a
209      * pointer to memory where these packets should be stored.
210      * Returns 0 on success, negative on failure.  Index
211      * is not advanced on failure.
212      */
GetNextPackets(size_t numPackets,void * data)213     int FL_PacketTable::GetNextPackets(size_t numPackets, void * data)
214     {
215         return H5PTget_next(table_id, numPackets, data);
216     }
217 
218 
219 #ifdef VLPT_REMOVED
220     /********************************/
221     /* Variable-Length Packet Table */
222     /********************************/
223 
224     /* Constructor
225      * Creates a packet table in which to store variable length packets.
226      * Takes the ID of the file the packet table will be created in, the name of
227      * the packet table, and the size of a memory chunk used in chunking.
228      */
VL_PacketTable(hid_t fileID,char * name,hsize_t chunkSize)229     VL_PacketTable::VL_PacketTable(hid_t fileID, char* name, hsize_t chunkSize)
230     {
231         table_id = H5PTcreate_vl ( fileID, name, chunkSize);
232     }
233 
234     /* "Open" Constructor
235      * Opens an existing variable-length packet table.
236      * Fails if the packet table specified is fixed-length.
237      */
VL_PacketTable(hid_t fileID,char * name)238     VL_PacketTable::VL_PacketTable(hid_t fileID, char* name) : PacketTable(fileID, name)
239     {
240         if( H5PTis_varlen(table_id) != 1 )    // If this is not a variable-length table
241         {
242             H5PTclose(table_id);
243             table_id = -1;
244         }
245     }
246 
247     /* AppendPacket (variable-length)
248      * Adds a single variable-length packet to the packet table.
249      * Takes a pointer to the location of the data in memory and the length of the data
250      * in bytes.
251      * Returns 0 on success, negative on failure.
252      */
AppendPacket(void * data,size_t length)253        int VL_PacketTable::AppendPacket(void * data, size_t length)
254     {
255         hvl_t packet;
256 
257         packet.len = length;
258         packet.p = data;
259 
260         return H5PTappend(table_id, 1, &packet);
261     }
262 
263     /* AppendPackets (multiple packets)
264      * Adds multiple variable-length packets to the packet table.  Takes the
265      * number of
266      * packets to be added and a pointer to an array of hvl_t structs in memory.
267      * Returns 0 on success, negative on failure.
268      */
AppendPackets(size_t numPackets,hvl_t * data)269     int VL_PacketTable::AppendPackets(size_t numPackets, hvl_t * data)
270     {
271         return H5PTappend(table_id, numPackets, data);
272     }
273 
274     /* GetPacket (indexed)
275      * Gets a single variable-length packet from the packet table.  Takes the
276      * index of the packet (with 0 being the first packet) and a pointer
277      * to a hvl_t struct in which to store the packet's size and location.
278      * Returns 0 on success, negative on failure.
279      */
GetPacket(hsize_t index,hvl_t * data)280     int VL_PacketTable::GetPacket(hsize_t index, hvl_t * data)
281     {
282         return H5PTread_packets(table_id, index, 1, data);
283     }
284 
285     /* GetPackets (multiple packets)
286      * Gets multiple variable-length packets at once, all packets between
287      * startIndex and endIndex inclusive.  Takes a pointer to an array
288      * of hvl_t structs in memory in which to store pointers to the packets.
289      * Returns 0 on success, negative on failure.
290      */
GetPackets(hsize_t startIndex,hsize_t endIndex,hvl_t * data)291     int VL_PacketTable::GetPackets(hsize_t startIndex, hsize_t endIndex, hvl_t * data)
292     {
293         // Make sure the range of indexes is valid
294         if (startIndex > endIndex)
295             return -1;
296 
297         return  H5PTread_packets(table_id, startIndex, endIndex-startIndex+1, data);
298     }
299 
300     /* GetNextPacket (single packet)
301      * Gets the next packet in the packet table.  Takes a pointer to
302      * an hvl_t struct where the packet should be stored.
303      * Returns 0 on success, negative on failure.  Index
304      * is not advanced to the next packet on failure.
305      */
GetNextPacket(hvl_t * data)306     int VL_PacketTable::GetNextPacket(hvl_t * data)
307     {
308         return H5PTget_next(table_id, 1, data);
309     }
310 
311     /* GetNextPackets (multiple packets)
312      * Gets the next numPackets packets in the packet table.  Takes a
313      * pointer to an array of hvl_t structs where pointers to the packets
314      * should be stored.
315      * Returns 0 on success, negative on failure.  Index
316      * is not advanced on failure.
317      */
GetNextPackets(size_t numPackets,hvl_t * data)318     int VL_PacketTable::GetNextPackets(size_t numPackets, hvl_t * data)
319     {
320         return H5PTget_next(table_id, numPackets, data);
321     }
322 
323     /* FreeReadbuff
324      * Frees the buffers created when variable-length packets are read.
325      * Takes the number of hvl_t structs to be freed and a pointer to their
326      * location in memory.
327      * Returns 0 on success, negative on error.
328      */
FreeReadbuff(size_t numStructs,hvl_t * buffer)329     int VL_PacketTable::FreeReadbuff(size_t numStructs, hvl_t * buffer)
330     {
331         return H5PTfree_vlen_readbuff( table_id, numStructs, buffer);
332     }
333 #endif /* VLPT_REMOVED */
334