summaryrefslogtreecommitdiffstats
path: root/clib/table.h
blob: 36e4bd42f94c164928bd531053c3c42f7c4b11ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: clib/table.h $                                                */
/*                                                                        */
/* OpenPOWER FFS Project                                                  */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2014,2015                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */

/*!
 * @file table.h
 * @brief Table Container
 * @details For example,
 * @code
 * #include <clib/array.h>
 * #include <clib/array_iter.h>
 *
 * int main(const int argc, const char * argv[]) {
 *     table t;
 *     table_init(&t, COLS);
 *
 *     const char * str = "column0 is a really long string";
 *
 *     table_name(&t, "table name");
 *     table_name(&t, 0, str);
 *     table_name(&t, 1, "column1");
 *     table_name(&t, 2, "column2");
 *     table_name(&t, 3, "column3");
 *     table_name(&t, 4, "column4");
 *
 *     printf("%s\n", table_name(&t, 0));
 *
 *     size_t r, c;
 *     value v;
 *
 *     for (r=0; r<COLS; r++) {
 *         for (c=0; c<COLS; c++) {
 *             table_put(&t, r, c,
 *                       value_set(&v, r * COLS + c));
 *
 *             printf("t[%d][%d] --> %d\n", r, c, r * COLS + c);
 *         }
 *     }
 *
 *     table_iter it;
 *     table_iter_init(&it, &t);
 *
 *     value * val;
 *     table_for_each(&it, val)
 *         value_dump(val, stdout);
 *
 *     table_delete(&t);
 *
 *     return 0;
 * }
 * @endcode
 * @author Shaun Wetzstein <shaun@us.ibm.com>
 * @date 2010-2011
 */

#ifndef __TABLE_H__
#define __TABLE_H__

#include <stdint.h>
#include <stdbool.h>

#include "compare.h"
#include "vector.h"
#include "value.h"
#include "mqueue.h"

/* ======================================================================= */

#define TABLE_NAME_SIZE		52	//!< Maximum table name size (in bytes)

#define TABLE_MAGIC		"TBLE"	//!< Table magic number

#define TABLE_FLAG_LSB		0x01	//!< little-endian header data
#define TABLE_FLAG_MSB		0x02	//!< big-endian header data

#define INIT_TABLE_HEADER	{INIT_IDENT,{0,},0}
#define INIT_TABLE		{INIT_TABLE_HEADER,INIT_VECTOR,INIT_VECTOR,INIT_VECTOR}

/*!
 * @brief table container header
 */
struct table_header {
	ident_t id;		//!< Identification
	char name[TABLE_NAME_SIZE];	//!< Table name

	size_t col_nr;		//!< Number of columns
};
typedef struct table_header table_header_t;	//!< Alias for the @em table_header class

/*!
 * @brief table container
 */
struct table {			//!< The Table class
	table_header_t hdr;	//!< Table metadata

	vector_t table;		//!< @private
	vector_t string;	//!< @private
	vector_t blob;		//!< @private
};
typedef struct table table_t;	//!< Alias for the @em table class

/* ==================================================================== */

/*!
 * @brief Constructs a @em table object
 * @memberof table
 * @param self [in] table object @em self pointer
 * @param name [in] table object @em name string
 * @param col_nr [in] Number of columns
 * @return None
 */
extern int table_init(table_t *, const char *, size_t)
/*! @cond */
__nonnull((1, 2)) /*! @endcond */ ;

/*!
 * @brief Destructs an @em table container object
 * @details Deallocate all backing storage associated with this \em table object
 * @memberof table
 * @param self [in] table object @em self pointer
 * @return None
 */
extern int table_delete(table_t *)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @brief Copy content from the @em table
 * @memberof table
 * @param self [in] table object @em self pointer
 * @param row [in] Row number
 * @param col [out] Column number
 * @return Reference to @em value on success, NULL otherwise
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern value_t *table_get(table_t *, size_t, size_t)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

/*!
 * @fn const void * table_row(table_iter_t * self, size_t row_nr,
 * [ value_t * row ])
 */
/*! @cond */
#define table_row(...) STRCAT(table_row, NARGS(__VA_ARGS__))(__VA_ARGS__)
extern value_t *table_row2(table_t *, size_t)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;
extern int table_row3(table_t *, size_t, value_t *)
/*! @cond */
__nonnull((1, 3)) /*! @endcond */ ;
/* !endcond */

extern value_t *table_column(table_t *, value_t *, size_t)
/*! @cond */
__nonnull((1, 2)) /*! @endcond */ ;

/*!
 * @brief Assign new content to the @em table
 * @memberof table
 * @param self [in] table object @em self pointer
 * @param row [in] Row number
 * @param col [in] Column number
 * @param value [in] Reference to source @em value
 * @return None
 * @throws UNEXPECTED if @em self pointer is NULL
 */
extern int table_put(table_t *, size_t, size_t, value_t *)
/*! @cond */
__nonnull((1, 4)) /*! @endcond */ ;

/*!
 * @fn const char * table_name(table_t * self, size_t col, const char * name,
 * size_t len)
 * @brief Set or return the name of a column within a @em table
 * @note If the fourth parameter is omitted, the @em name is assumed to contain
 * a trailing NULL-byte
 * @note If the third parameter is omitted, the column's current name is
 * returned
 * @memberof table
 * @param self [in] table object @em self pointer
 * @param col [in] Column number
 * @param name [in] New column name string (optional)
 * @param len [in] New column name string length (optional)
 * @return None
 * @throws UNEXPECTED if @em self pointer is NULL
 */

/*! @cond */
#define table_name(...) STRCAT(table_name, NARGS(__VA_ARGS__))(__VA_ARGS__)
extern const char *table_name2(table_t *, size_t) __nonnull((1));

#define table_name3(t,c,s)						\
    choose_expr(const_expr(s),						\
                _table_name3((t),(c),(const char *)(s)),		\
                _table_name3((t),(c),(const char *)(s)))

extern int _table_name3(table_t *, size_t, const char *) __nonnull((1, 3));

#define table_name4(t,c,s,l)						\
    choose_expr(const_expr(s),						\
                _table_name4((t),(c),(const char *)(s),(l)),		\
                _table_name4((t),(c),(const char *)(s),(l)))

extern int _table_name4(table_t *, size_t, const char *, size_t)
__nonnull((1, 3));
/*! @endcond */

/*!
 * @brief Return the number of rows in the @em table
 * @memberof table
 * @param self [in] table object @em self pointer
 * @return None
 */
extern size_t table_rows(table_t *) /*! @cond */ __nonnull((1))	/*! @endcond */
;

/*!
 * @brief Return the number of column in the @em table
 * @memberof table
 * @param self [in] table object @em self pointer
 * @return None
 */
extern size_t table_columns(table_t *)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

extern int table_serialize(table_t *)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

extern int table_deserialize(table_t *)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

extern ssize_t table_save(table_t *, FILE *)
/*! @cond */
__nonnull((1, 2)) /*! @endcond */ ;

extern ssize_t table_load(table_t *, FILE *)
/*! @cond */
__nonnull((1, 2)) /*! @endcond */ ;

extern void table_print(table_t *, FILE *)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

extern void table_dump(table_t *, FILE *)
/*! @cond */
__nonnull((1)) /*! @endcond */ ;

extern int table_sort(table_t *, compare_f)
/*! @cond */
__nonnull((1, 2)) /*! @endcond */ ;

/* ==================================================================== */

#endif				/* __TABLE_H__ */
OpenPOWER on IntegriCloud