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 COPYING file, which can be found at the root of the source code       *
9  * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
10  * If you do not have access to either file, you may request a copy from     *
11  * help@hdfgroup.org.                                                        *
12  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13 
14 /* Packet Table wrapper classes
15  *
16  * Wraps the H5PT Packet Table C functions in C++ objects
17  *
18  * Nat Furrer and James Laird
19  * February 2004
20  */
21 
22 /* High-level library internal header file */
23 #include "H5HLprivate2.h"
24 
25 #include "H5PacketTable.h"
26 
27     /********************************/
28     /* PacketTable superclass       */
29     /********************************/
30 
31     /* "Open" Constructor
32      * Opens an existing packet table, which can contain either fixed-length or
33      * variable-length packets.
34      */
PacketTable(hid_t fileID,const char * name)35     PacketTable::PacketTable(hid_t fileID, const char* name)
36     {
37         table_id = H5PTopen( fileID, name);
38     }
39 
40     /* "Open" Constructor - will be deprecated because of char* name */
PacketTable(hid_t fileID,char * name)41     PacketTable::PacketTable(hid_t fileID, char* name)
42     {
43         table_id = H5PTopen( fileID, name);
44     }
45 
46     /* Destructor
47      * Cleans up the packet table
48      */
~PacketTable()49     PacketTable::~PacketTable()
50     {
51         H5PTclose( table_id);
52     }
53 
54     /* IsValid
55      * Returns true if this packet table is valid, false otherwise.
56      * Use this after the constructor to ensure HDF did not have
57      * any trouble making or opening the packet table.
58      */
IsValid()59     bool PacketTable::IsValid()
60     {
61         if (H5PTis_valid(table_id) == 0)
62             return true;
63         else
64             return false;
65     }
66 
67     /* IsVariableLength
68      * Return 1 if this packet table uses variable-length datatype,
69      * and 0, otherwise.  Returns -1 if the table is invalid (not open).
70      */
IsVariableLength()71     int PacketTable::IsVariableLength()
72     {
73         return H5PTis_varlen(table_id);
74     }
75 
76     /* ResetIndex
77      * Sets the index to point to the first packet in the packet table
78      */
ResetIndex()79     void PacketTable::ResetIndex()
80     {
81         H5PTcreate_index(table_id);
82     }
83 
84     /* SetIndex
85      * Sets the index to point to the packet specified by index.
86      * Returns 0 on success, negative on failure (if index is out of bounds)
87      */
SetIndex(hsize_t index)88     int PacketTable::SetIndex(hsize_t index)
89     {
90         return H5PTset_index(table_id, index);
91     }
92 
93     /* SetIndex
94      * Sets the index to point to the packet specified by index.
95      * Returns 0 on success, negative on failure (if index is out of bounds)
96      */
GetIndex(int & error)97     hsize_t PacketTable::GetIndex(int &error)
98     {
99         hsize_t index;
100 
101         error = H5PTget_index(table_id, &index);
102         if(error < 0)
103            return 0;
104         else
105            return index;
106     }
107 
108     /* GetPacketCount
109      * Returns the number of packets in the packet table.  Error
110      * is set to 0 on success.  On failure, returns 0 and
111      * error is set to negative.
112      */
GetPacketCount(int & error)113     hsize_t PacketTable::GetPacketCount(int& error)
114     {
115         hsize_t npackets;
116 
117         error = H5PTget_num_packets(table_id, &npackets);
118         return npackets;
119     }
120 
121     /* GetTableId
122      * Returns the identifier of the packet table
123      */
GetTableId()124     hid_t PacketTable::GetTableId()
125     {
126         return table_id;
127     }
128 
129     /* GetDatatype
130      * Returns the datatype identifier used by the packet table, on success,
131      * or H5I_INVALID_HID, on failure.
132      * Note: it is best to avoid using this identifier in applications, unless
133      * the desired functionality cannot be performed via the packet table ID.
134      */
GetDatatype()135     hid_t PacketTable::GetDatatype()
136     {
137         return H5PTget_type(table_id);
138     }
139 
140     /* GetDataset
141      * Returns the dataset identifier associated with the packet table, on
142      * success, or H5I_INVALID_HID, on failure.
143      * Note: it is best to avoid using this identifier in applications, unless
144      * the desired functionality cannot be performed via the packet table ID.
145      */
GetDataset()146     hid_t PacketTable::GetDataset()
147     {
148         return H5PTget_dataset(table_id);
149     }
150 
151     /* FreeBuff
152      * Frees the buffers created when variable-length packets are read.
153      * Takes the number of hvl_t structs to be freed and a pointer to their
154      * location in memory.
155      * Returns 0 on success, negative on error.
156      */
FreeBuff(size_t numStructs,hvl_t * buffer)157     int PacketTable::FreeBuff(size_t numStructs, hvl_t * buffer)
158     {
159         return H5PTfree_vlen_buff( table_id, numStructs, buffer);
160     }
161 
162 
163     /********************************/
164     /* Fixed-Length Packet Table    */
165     /********************************/
166 
167     /* Constructor
168      * Creates a packet table to store either fixed- or variable-length packets.
169      * Takes the ID of the file the packet table will be created in, the ID of
170      * the property list to specify compression, the name of the packet table,
171      * the ID of the datatype, and the size of a memory chunk used in chunking.
172      */
FL_PacketTable(hid_t fileID,const char * name,hid_t dtypeID,hsize_t chunkSize,hid_t plistID)173     FL_PacketTable::FL_PacketTable(hid_t fileID, const char* name, hid_t dtypeID, hsize_t chunkSize, hid_t plistID)
174     {
175         table_id = H5PTcreate(fileID, name, dtypeID, chunkSize, plistID);
176     }
177 
178     /* Constructor - deprecated
179      * Creates a packet table to store either fixed- or variable-length packets.
180      * Takes the ID of the file the packet table will be created in, the ID of
181      * the property list to specify compression, the name of the packet table,
182      * the ID of the datatype, and the size of a memory chunk used in chunking.
183      * Note: The above constructor has a better prototype, which allows default
184      * values to be used.  This constructor was only released in 1.10.0.
185      */
FL_PacketTable(hid_t fileID,hid_t plistID,const char * name,hid_t dtypeID,hsize_t chunkSize)186     FL_PacketTable::FL_PacketTable(hid_t fileID, hid_t plistID, const char* name, hid_t dtypeID, hsize_t chunkSize)
187     {
188         table_id = H5PTcreate(fileID, name, dtypeID, chunkSize, plistID);
189     }
190 
191     /* Constructor
192      * Creates a packet table to store either fixed- or variable-length packets.
193      * Takes the ID of the file the packet table will be created in, the name of
194      * the packet table, the ID of the datatype of the set, and the size
195      * of a memory chunk used in chunking.
196      * Note: this overload will be deprecated in favor of the constructor above.
197      */
FL_PacketTable(hid_t fileID,char * name,hid_t dtypeID,hsize_t chunkSize,int compression)198     FL_PacketTable::FL_PacketTable(hid_t fileID, char* name, hid_t dtypeID, hsize_t chunkSize, int compression)
199     {
200         table_id = H5PTcreate_fl(fileID, name, dtypeID, chunkSize, compression);
201     }
202 
203     /* "Open" Constructor
204      * Opens an existing fixed-length packet table.
205      * Fails if the packet table specified is variable-length.
206      */
FL_PacketTable(hid_t fileID,const char * name)207     FL_PacketTable::FL_PacketTable(hid_t fileID, const char* name) : PacketTable(fileID, name) {}
208 
209     /* "Open" Constructor - will be deprecated because of char* name */
FL_PacketTable(hid_t fileID,char * name)210     FL_PacketTable::FL_PacketTable(hid_t fileID, char* name) : PacketTable(fileID, name) {}
211 
212     /* AppendPacket
213      * Adds a single packet to the packet table.  Takes a pointer
214      * to the location of the data in memory.
215      * Returns 0 on success, negative on failure
216      */
AppendPacket(void * data)217     int FL_PacketTable::AppendPacket(void * data)
218     {
219         return H5PTappend(table_id, 1, data);
220     }
221 
222     /* AppendPackets (multiple packets)
223      * Adds multiple packets to the packet table.  Takes the number of packets
224      * to be added and a pointer to their location in memory.
225      * Returns 0 on success, -1 on failure.
226      */
AppendPackets(size_t numPackets,void * data)227     int FL_PacketTable::AppendPackets(size_t numPackets, void * data)
228     {
229         return H5PTappend(table_id, numPackets, data);
230     }
231 
232     /* GetPacket (indexed)
233      * Gets a single packet from the packet table.  Takes the index
234      * of the packet (with 0 being the first packet) and a pointer
235      * to memory where the data should be stored.
236      * Returns 0 on success, negative on failure
237      */
GetPacket(hsize_t index,void * data)238     int FL_PacketTable::GetPacket(hsize_t index, void * data)
239     {
240         return H5PTread_packets(table_id, index, 1, data);
241     }
242 
243     /* GetPackets (multiple packets)
244      * Gets multiple packets at once, all packets between
245      * startIndex and endIndex inclusive.  Also takes a pointer to
246      * the memory where these packets should be stored.
247      * Returns 0 on success, negative on failure.
248      */
GetPackets(hsize_t startIndex,hsize_t endIndex,void * data)249     int FL_PacketTable::GetPackets(hsize_t startIndex, hsize_t endIndex, void * data)
250     {
251         // Make sure the range of indexes is valid
252         if (startIndex > endIndex)
253             return -1;
254 
255         return  H5PTread_packets(table_id, startIndex, (size_t)(endIndex-startIndex+1), data);
256     }
257 
258     /* GetNextPacket (single packet)
259      * Gets the next packet in the packet table.  Takes a pointer to
260      * memory where the packet should be stored.
261      * Returns 0 on success, negative on failure.  Index
262      * is not advanced to the next packet on failure.
263      */
GetNextPacket(void * data)264     int FL_PacketTable::GetNextPacket(void * data)
265     {
266         return H5PTget_next(table_id, 1, data);
267     }
268 
269     /* GetNextPackets (multiple packets)
270      * Gets the next numPackets packets in the packet table.  Takes a
271      * pointer to memory where these packets should be stored.
272      * Returns 0 on success, negative on failure.  Index
273      * is not advanced on failure.
274      */
GetNextPackets(size_t numPackets,void * data)275     int FL_PacketTable::GetNextPackets(size_t numPackets, void * data)
276     {
277         return H5PTget_next(table_id, numPackets, data);
278     }
279 
280 /* Removed "ifdef VLPT_REMOVED" block. 03/08/2016, -BMR */
281