From 630e87996160a51776ea6974d163e8f45895e1d3 Mon Sep 17 00:00:00 2001 From: Stewart Smith Date: Tue, 15 Dec 2015 15:39:39 +1100 Subject: remove unused table and table_iter Signed-off-by: Stewart Smith --- clib/Rules.mk | 1 - clib/src/table.c | 686 -------------------------------------------------- clib/src/table_iter.c | 145 ----------- clib/table.h | 279 -------------------- clib/table_iter.h | 179 ------------- clib/test/table.c | 109 -------- 6 files changed, 1399 deletions(-) delete mode 100644 clib/src/table.c delete mode 100644 clib/src/table_iter.c delete mode 100644 clib/table.h delete mode 100644 clib/table_iter.h delete mode 100644 clib/test/table.c diff --git a/clib/Rules.mk b/clib/Rules.mk index 342fd0f..b04180e 100644 --- a/clib/Rules.mk +++ b/clib/Rules.mk @@ -34,7 +34,6 @@ OBJS = err.o crc32.o misc.o ecc.o \ list.o list_iter.o \ tree.o tree_iter.o \ vector.o vector_iter.o \ - table.o table_iter.o \ value.o mq.o \ memory_leak_detection.o \ trace_indent.o checksum.o diff --git a/clib/src/table.c b/clib/src/table.c deleted file mode 100644 index 82a35aa..0000000 --- a/clib/src/table.c +++ /dev/null @@ -1,686 +0,0 @@ -/* IBM_PROLOG_BEGIN_TAG */ -/* This is an automatically generated prolog. */ -/* */ -/* $Source: clib/src/table.c $ */ -/* */ -/* 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.c - * Author: Shaun Wetzstein - * Descr: - * Note: - * Date: 08/21/10 - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "libclib.h" -#include "table.h" -#include "vector_iter.h" - -#define TABLE_PAGE_SIZE 4096 -#define TABLE_PAGE_DIVISOR 32 - -/* ======================================================================= */ -int table_init(table_t * self, const char *name, uint32_t col_nr) -{ - assert(self != NULL); - assert(name != NULL); - - if (col_nr == 0) { - UNEXPECTED("'%d' invalid column number", col_nr); - return -1; - } - - self->hdr.id[IDENT_MAGIC_0] = TABLE_MAGIC[IDENT_MAGIC_0]; - self->hdr.id[IDENT_MAGIC_1] = TABLE_MAGIC[IDENT_MAGIC_1]; - self->hdr.id[IDENT_MAGIC_2] = TABLE_MAGIC[IDENT_MAGIC_2]; - self->hdr.id[IDENT_MAGIC_3] = TABLE_MAGIC[IDENT_MAGIC_3]; - - self->hdr.id[IDENT_MAJOR] = CLIB_MAJOR; - self->hdr.id[IDENT_MINOR] = CLIB_MINOR; - self->hdr.id[IDENT_PATCH] = CLIB_PATCH; - - if (__BYTE_ORDER == __LITTLE_ENDIAN) - self->hdr.id[IDENT_FLAGS] |= TABLE_FLAG_LSB; - if (__BYTE_ORDER == __BIG_ENDIAN) - self->hdr.id[IDENT_FLAGS] |= TABLE_FLAG_MSB; - - /* FIX ME -- handle this more elegantly */ - //assert((col_nr * sizeof(value_t)) < TABLE_PAGE_SIZE); - - self->hdr.col_nr = col_nr; - - if (name == NULL || *name == '\0') - memset(self->hdr.name, 0, sizeof(self->hdr.name)); - else - strncpy(self->hdr.name, name, sizeof(self->hdr.name)); - - char name_vector[strlen(self->hdr.name) + 5]; - - size_t row_size = self->hdr.col_nr * sizeof(value_t); - - sprintf(name_vector, "%s.table", self->hdr.name); - vector_init(&self->table, name_vector, row_size, - __round_pow2(row_size * TABLE_PAGE_DIVISOR)); - - sprintf(name_vector, "%s.string", self->hdr.name); - vector_init(&self->string, name_vector, 1, TABLE_PAGE_SIZE); - - sprintf(name_vector, "%s.blob", self->hdr.name); - vector_init(&self->blob, name_vector, 1, TABLE_PAGE_SIZE); - - return 0; -} - -int table_delete(table_t * self) -{ - if (unlikely(self == NULL)) - return 0; - - if (MAGIC_CHECK(self->hdr.id, TABLE_MAGIC)) { - UNEXPECTED("'%2.2x%2.2x%2.2x%2.2x' invalid table magic " - "'%2.2x%2.2x%2.2x%2.2x", - self->hdr.id[IDENT_MAGIC_0], - self->hdr.id[IDENT_MAGIC_1], - self->hdr.id[IDENT_MAGIC_2], - self->hdr.id[IDENT_MAGIC_3], - TABLE_MAGIC[IDENT_MAGIC_0], - TABLE_MAGIC[IDENT_MAGIC_2], - TABLE_MAGIC[IDENT_MAGIC_3], - TABLE_MAGIC[IDENT_MAGIC_3]); - return -1; - } - - if (0 < vector_size(&self->table)) { - vector_iter_t it; - if (vector_iter_init(&it, &self->table, VI_FLAG_FWD) < 0) - return -1; - - value_t *v; - vector_for_each(&it, v) - for (size_t c = 0; c < self->hdr.col_nr; c++) - value_clear(v + c); - } - - if (vector_delete(&self->table) < 0) - return -1; - if (vector_delete(&self->string) < 0) - return -1; - if (vector_delete(&self->blob) < 0) - return -1; - - return 0; -} - -value_t *table_get(table_t * self, size_t row, size_t col) -{ - assert(self != NULL); - assert(!MAGIC_CHECK(self->hdr.id, TABLE_MAGIC)); - - row++; /* hide the column names */ - - return (value_t *) vector_at(&self->table, - row * self->hdr.col_nr + col); -} - -value_t *table_row2(table_t * self, size_t row_nr) -{ - assert(self != NULL); - assert(!MAGIC_CHECK(self->hdr.id, TABLE_MAGIC)); - - row_nr++; /* hide the column names */ - - return (value_t *) vector_at(&self->table, row_nr * self->hdr.col_nr); -} - -int table_row3(table_t * self, size_t row_nr, value_t * row) -{ - assert(self != NULL); - assert(row != NULL); - assert(!MAGIC_CHECK(self->hdr.id, TABLE_MAGIC)); - - row_nr++; /* hide the column names */ - - if (vector_size(&self->table) <= row_nr + 1) { - if (vector_size(&self->table, row_nr + 1) < 0) - return -1; - } else { - value_t *old; - old = (value_t *) vector_at(&self->table, row_nr); - assert(old != NULL); - - for (size_t col_nr = 0; col_nr < self->hdr.col_nr; col_nr++) - value_clear(old + col_nr); - } - - if (vector_put(&self->table, row_nr, row) < 0) - return -1; - - for (size_t col_nr = 0; col_nr < self->hdr.col_nr; col_nr++) - if (value_type(row + col_nr) == VT_STR || - value_type(row + col_nr) == VT_BLOB) - value_type(row + col_nr, VT_UNKNOWN); - - return 0; -} - -value_t *table_column(table_t * self, value_t * row, size_t col) -{ - assert(self != NULL); - assert(row != NULL); - assert(!MAGIC_CHECK(self->hdr.id, TABLE_MAGIC)); - assert(col < self->hdr.col_nr); - - return (void *)row + (vector_elem_size(&self->table) * col); -} - -int table_put(table_t * self, size_t row, size_t col, value_t * val) -{ - assert(self != NULL); - assert(val != NULL); - assert(!MAGIC_CHECK(self->hdr.id, TABLE_MAGIC)); - - row++; /* hide the column names */ - - size_t size = (row + 1) * self->hdr.col_nr; - - if (vector_size(&self->table) <= size) - if (vector_size(&self->table, size) < 0) - return -1; - - /* free existing pointer data */ - value_t *old = (value_t *) vector_at(&self->table, - row * self->hdr.col_nr + col); - assert(old != NULL); - value_clear(old); - - vector_put(&self->table, row * self->hdr.col_nr + col, (void *)val, 1); - - if ((value_type(val) == VT_STR) || (value_type(val) == VT_BLOB)) - value_type(val, VT_UNKNOWN); - - return 0; -} - -const char *table_name2(table_t * self, size_t col_nr) -{ - assert(self != NULL); - assert(!MAGIC_CHECK(self->hdr.id, TABLE_MAGIC)); - assert(col_nr < self->hdr.col_nr); - - const char *rc = NULL; - - value_t *row = (value_t *) vector_at(&self->table, 0); - if (row != NULL) { - value_t *col = row + col_nr; - switch (value_type(col)) { - case VT_STR_OFF: - rc = vector_at(&self->string, col->u64); - break; - case VT_STR_INLINE: - rc = (const char *)col->data; - break; - case VT_STR_CONST: - case VT_STR: - rc = value_string(col); - break; - default: - rc = NULL; - } - } - - return rc; -} - -int _table_name3(table_t * self, size_t col_nr, const char *name) -{ - return _table_name4(self, col_nr, name, strlen(name)); -} - -int _table_name4(table_t * self, size_t col_nr, const char *name, size_t len) -{ - assert(self != NULL); - assert(!MAGIC_CHECK(self->hdr.id, TABLE_MAGIC)); - assert(col_nr < self->hdr.col_nr); - - if (vector_size(&self->table) <= 0) - if (vector_size(&self->table, 1) < 0) - return -1; - - value_t *row = (value_t *) vector_at(&self->table, 0); - if (row != NULL) - value_string(row + col_nr, name, len); - - return 0; -} - -size_t table_rows(table_t * self) -{ - assert(self != NULL); - assert(!MAGIC_CHECK(self->hdr.id, TABLE_MAGIC)); - return vector_size(&self->table) - 1; -} - -size_t table_columns(table_t * self) -{ - assert(self != NULL); - assert(!MAGIC_CHECK(self->hdr.id, TABLE_MAGIC)); - return self->hdr.col_nr; -} - -int table_serialize(table_t * self) -{ - assert(self != NULL); - assert(!MAGIC_CHECK(self->hdr.id, TABLE_MAGIC)); - - vector_iter_t it; - vector_iter_init(&it, &self->table, VI_FLAG_FWD); - - value_t *row; - vector_for_each(&it, row) { - for (size_t c = 0; c < self->hdr.col_nr; c++) { - value_t *col = row + c; - - value_type_t type = value_type(col); - size_t len = value_size(col); - - vector_t *vec = NULL; - if (type == VT_STR) - vec = &self->string, type = VT_STR_OFF, len++; - else if (type == VT_BLOB) - vec = &self->blob, type = VT_BLOB_OFF; - else - continue; - - uint64_t off = vector_size(vec); - - if (vector_size(vec, off + len) < 0) - return -1; - if (vector_put(vec, off, col->ptr, len) < 0) - return -1; - - free(col->ptr), col->ptr = NULL; - - col->u64 = off; - value_type(col, type); - } - } - - return 0; -} - -ssize_t table_save(table_t * self, FILE * out) -{ - assert(self != NULL); - assert(out != NULL); - assert(!MAGIC_CHECK(self->hdr.id, TABLE_MAGIC)); - - /* ============= */ - - int header_swap(table_header_t * hdr) { - assert(hdr != NULL); - - if (hdr->id[IDENT_FLAGS] & TABLE_FLAG_MSB) { - hdr->col_nr = htobe32(hdr->col_nr); - } else if (hdr->id[IDENT_FLAGS] & TABLE_FLAG_LSB) { - hdr->col_nr = htole32(hdr->col_nr); - } else { - UNEXPECTED("'%s' invalid or corrupt table object => " - "'%x'", hdr->name, hdr->id[IDENT_FLAGS]); - return -1; - } - - return 0; - } - - /* ============= */ - - ssize_t len = 0; - - table_header_t hdr = self->hdr; - if (header_swap(&hdr) < 0) - return -1; - - clearerr(out); - len = fwrite(&self->hdr, 1, sizeof(self->hdr), out); - if (len != sizeof(self->hdr)) { - if (ferror(out)) { - ERRNO(errno); - return -1; - } - } - - ssize_t rc = vector_save(&self->table, out); - if (rc < 0) - return -1; - len += rc; - - rc = vector_save(&self->string, out); - if (rc < 0) - return -1; - len += rc; - - rc = vector_save(&self->blob, out); - if (rc < 0) - return -1; - len += rc; - - return len; -} - -int table_deserialize(table_t * self) -{ - assert(self != NULL); - assert(!MAGIC_CHECK(self->hdr.id, TABLE_MAGIC)); - - vector_iter_t it; - vector_iter_init(&it, &self->table, VI_FLAG_FWD); - - value_t *row; - vector_for_each(&it, row) { - for (size_t c = 0; c < self->hdr.col_nr; c++) { - value_t *col = row + c; - - value_type_t type = value_type(col); - size_t len = value_size(col); - - switch (type) { - uint64_t off; - case VT_STR_OFF: - len++; - value_type(col, VT_STR); - case VT_BLOB_OFF: - off = col->u64; - col->ptr = malloc(len); - - if (col->ptr == NULL) { - ERRNO(errno); - return -1; - } - - if (vector_get(&self->string, off, col->ptr, - len) < 0) - return -1; - - if (value_type(col) == VT_BLOB_OFF) - value_type(col, VT_BLOB); - break; - case VT_STR: - case VT_BLOB: - case VT_STR_CONST: - UNEXPECTED("'%s' invalid or corrupt type %d", - self->hdr.name, type); - return -1; - default: - ; - } - } - } - - if (vector_delete(&self->string) < 0) - return -1; - if (vector_delete(&self->blob) < 0) - return -1; - - return 0; -} - -ssize_t table_load(table_t * self, FILE * in) -{ - assert(self != NULL); - - /* ============= */ - - int header_swap(table_header_t * hdr) { - assert(hdr != NULL); - if (hdr->id[IDENT_FLAGS] & TABLE_FLAG_MSB) { - hdr->col_nr = be32toh(hdr->col_nr); - } else if (hdr->id[IDENT_FLAGS] & TABLE_FLAG_LSB) { - hdr->col_nr = le32toh(hdr->col_nr); - } else { - UNEXPECTED("'%s' invalid or corrupt table object => " - "'%x'", hdr->name, hdr->id[IDENT_FLAGS]); - return -1; - } - - return 0; - } - - int table_swap(table_t * tbl) { - vector_iter_t it; - if (vector_iter_init(&it, &tbl->table, VI_FLAG_FWD) < 0) - return -1; - - value_t *row; - vector_for_each(&it, row) { - for (size_t c = 0; c < self->hdr.col_nr; c++) { - value_t *col = row + c; - - value_type_t type = value_type(row); - -#define iBE(s) value_i##s(col, be##s##toh(value_i##s(col))) -#define uBE(s) value_u##s(col, be##s##toh(value_u##s(col))) -#define iLE(s) value_i##s(col, le##s##toh(value_i##s(col))) -#define uLE(s) value_u##s(col, le##s##toh(value_u##s(col))) - - if (tbl->hdr.id[IDENT_FLAGS] & TABLE_FLAG_MSB) { - switch (type) { - case VT_I16: - iBE(16); - break; - case VT_U16: - uBE(16); - break; - case VT_I32: - iBE(32); - break; - case VT_U32: - uBE(32); - break; - case VT_I64: - iBE(64); - break; - case VT_U64: - uBE(64); - break; - case VT_STR_OFF: - case VT_BLOB_OFF: - col->u64 = be64toh(col->u64); - break; - default: - ; - } - } else if (tbl->hdr.id[IDENT_FLAGS] & - TABLE_FLAG_LSB) { - switch (type) { - case VT_I16: - iLE(16); - break; - case VT_U16: - uLE(16); - break; - case VT_I32: - iLE(32); - break; - case VT_U32: - uLE(32); - break; - case VT_I64: - iLE(64); - break; - case VT_U64: - uLE(64); - break; - case VT_STR_OFF: - case VT_BLOB_OFF: - col->u64 = le64toh(col->u64); - break; - default: - ; - } - } else { - UNEXPECTED("'%s' invalid or corrupt " - "table object => '%x'", - tbl->hdr.name, - tbl->hdr.id[IDENT_FLAGS]); - return -1; - } - } - } - - return 0; - } - - /* ============= */ - - // zero'd table will cause a magic check - (void)table_delete(self); - - clearerr(in); - size_t len = fread(&self->hdr, 1, sizeof(self->hdr), in); - if (len != sizeof(self->hdr)) { - if (feof(in)) { - UNEXPECTED("'%s' end-of-file encountered", - self->hdr.name); - return -1; - } - if (ferror(in)) { - ERRNO(errno); - return -1; - } - } - - assert(!MAGIC_CHECK(self->hdr.id, TABLE_MAGIC)); - - if (header_swap(&self->hdr) < 0) - return -1; - - ssize_t rc = vector_load(&self->table, in); - if (rc < 0) - return -1; - len += rc; - - if (table_swap(self) < 0) - return -1; - - rc = vector_load(&self->string, in); - if (rc < 0) - return -1; - len += rc; - - rc = vector_load(&self->blob, in); - if (rc < 0) - return -1; - len += rc; - - return len; -} - -void table_print(table_t * self, FILE * out) -{ - if (self != NULL) { - if (unlikely(MAGIC_CHECK(self->hdr.id, TABLE_MAGIC))) { - UNEXPECTED("'%s' invalid or corrupt table object", - self->hdr.name); - return; - } - - vector_iter_t it; - vector_iter_init(&it, &self->table, VI_FLAG_FWD); - - value_t *row; - vector_for_each(&it, row) - for (size_t c = 0; c < self->hdr.col_nr; c++) - value_dump(row + c, out); - - vector_dump(&self->string, out); - } -} - -void table_dump(table_t * self, FILE * out) -{ - if (self != NULL) { - if (unlikely(MAGIC_CHECK(self->hdr.id, TABLE_MAGIC))) { - UNEXPECTED("'%s' invalid or corrupt table object", - self->hdr.name); - return; - } - - fprintf(out, - "table: [ size: %ld cols: %ld rows: %ld name: '%s']\n", - sizeof(value_t), table_columns(self), table_rows(self), - self->hdr.name); - - dump_memory(out, (unsigned long)&self->hdr, &self->hdr, - sizeof(self->hdr)); - - vector_dump(&self->table, out); - vector_dump(&self->string, out); - vector_dump(&self->blob, out); - } -} - -#if 0 -void table_sort(table_t * self, compare_f cmp) -{ - /* The exchange function swaps two rows within the table - * exchange(table_t * self, size_t i, size_t j) - */ - - /* The partition method receives a list or sublist, and places the first element - * in its correct position within the list. It also ensures that all elements to - * the left of this are smaller, and all to the right are larger. - * - * partition(a[], p, r) - * i = p - * j = r + 1 - * pivot = a[p] - * do { - * do i = i + 1 while (a[i]pivot) - * if (i < j) exchange(a[i], a[j]) - * } while (i p then - * j = partition(a[], p, r) - * quicksort(a[], p, j-1) - * quicksort(a[], j+1, r) - * - */ -} -#endif - -/* ======================================================================= */ diff --git a/clib/src/table_iter.c b/clib/src/table_iter.c deleted file mode 100644 index e8a3f59..0000000 --- a/clib/src/table_iter.c +++ /dev/null @@ -1,145 +0,0 @@ -/* IBM_PROLOG_BEGIN_TAG */ -/* This is an automatically generated prolog. */ -/* */ -/* $Source: clib/src/table_iter.c $ */ -/* */ -/* 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_iter.c - * Author: Shaun Wetzstein - * Descr: - * Note: - * Date: 10/22/10 - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "assert.h" -#include "misc.h" - -#include "table_iter.h" - -/* ======================================================================= */ - -int table_iter_init(table_iter_t * self, table_t * table, uint32_t flags) -{ - assert(self != NULL); - assert(table != NULL); - - self->flags = flags; - self->table = table; - - if (self->flags & TI_FLAG_BWD) { - if (vector_iter_init(&self->it, &table->table, VI_FLAG_BWD) < 0) - return -1; - } else { - if (vector_iter_init(&self->it, &table->table, VI_FLAG_FWD) < 0) - return -1; - } - - return vector_iter_inc(&self->it) != NULL; -} - -int table_iter_clear(table_iter_t * self) -{ - assert(self != NULL); - - if (vector_iter_clear(&self->it) < 0) - return -1; - - self->table = NULL; - - return 0; -} - -const value_t *table_iter_elem(table_iter_t * self) -{ - assert(self != NULL); - return (value_t *) vector_iter_elem(&self->it); -} - -const value_t *table_iter_inc1(table_iter_t * self) -{ - return table_iter_inc2(self, 1); -} - -const value_t *table_iter_inc2(table_iter_t * self, size_t count) -{ - assert(self != NULL); - - const value_t *v = NULL; - - for (size_t i = 0; i < count; i++) { - if (self->flags & TI_FLAG_BWD) { - v = (value_t *) vector_iter_dec(&self->it); // columns - } else { - v = (value_t *) vector_iter_inc(&self->it); // columns - } - } - - return v; -} - -const value_t *table_iter_dec1(table_iter_t * self) -{ - return table_iter_dec2(self, 1); -} - -const value_t *table_iter_dec2(table_iter_t * self, size_t count) -{ - assert(self != NULL); - - const value_t *v = NULL; - - for (size_t i = 0; i < count; i++) { - if (self->flags & TI_FLAG_BWD) { - v = (value_t *) vector_iter_inc(&self->it); // columns - } else { - v = (value_t *) vector_iter_dec(&self->it); // columns - } - } - - return v; -} - -size_t table_iter_pos1(table_iter_t * self) -{ - assert(self != NULL); - return vector_iter_pos(&self->it) + 1; -} - -int table_iter_pos2(table_iter_t * self, size_t pos) -{ - assert(self != NULL); - assert(pos < table_rows(self->table)); - return vector_iter_pos(&self->it, pos + 1); -} - -/* ======================================================================= */ diff --git a/clib/table.h b/clib/table.h deleted file mode 100644 index ceed6d1..0000000 --- a/clib/table.h +++ /dev/null @@ -1,279 +0,0 @@ -/* 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 - * #include - * - * 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 %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 - * @date 2010-2011 - */ - -#ifndef __TABLE_H__ -#define __TABLE_H__ - -#include -#include - -#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 *, uint32_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__ */ diff --git a/clib/table_iter.h b/clib/table_iter.h deleted file mode 100644 index 664534b..0000000 --- a/clib/table_iter.h +++ /dev/null @@ -1,179 +0,0 @@ -/* IBM_PROLOG_BEGIN_TAG */ -/* This is an automatically generated prolog. */ -/* */ -/* $Source: clib/table_iter.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_iter.h - * @brief Table Iterator - * @code - * ... - * table_iter_t it; - * table_iter_init(&it, &t); - * - * value_t * val; - * table_for_each(&it, val) { - * if (value_type(val) != VT_UNKNOWN) - * value_dump(val, stdout); - * } - * ... - * @endcode - * @author Shaun Wetzstein - * @date 2010-2011 - */ - -#ifndef __TABLE_ITER_H__ -#define __TABLE_ITER_H__ - -#include -#include - -#include "attribute.h" - -#include "value.h" -#include "table.h" -#include "vector_iter.h" - -/* ======================================================================= */ - -typedef struct table_iter table_iter_t; //!< Alias for the @em table class - -/*! - * @brief Table iterator - */ -struct table_iter { - table_t *table; //!< Reference to the target table object - vector_iter_t it; //!< Current position of the table - uint32_t flags; //!< Iterator configuration flags -}; - -/* ==================================================================== */ - -#define TI_FLAG_NONE 0x00000000 //!< No flag mask -#define TI_FLAG_FWD 0x00000001 //!< Forward (FWD) flag mask -#define TI_FLAG_BWD 0x00000002 //!< Backwards (BWD) flag mask -#define TI_FLAG_MASK 0x00000003 //!< All flag mask - -/*! - * @brief Initializes an @em table_iter iterator object - * @memberof table_iter - * @param self [in] table_iter object @em self pointer - * @param table [in] table container object to iterate - * @param flags [in] iterator configuration flags - * @return None - * @throws UNEXPECTED if @em self pointer is NULL - * @throws UNEXPECTED if @em table pointer is NULL - */ -extern int table_iter_init(table_iter_t *, table_t *, uint32_t) -/*! @cond */ -__nonnull((1, 2)) /*! @endcond */ ; - -/*! - * @brief Resets an @em table iterator object - * @memberof table_iter - * @param self [in] table_iter object @em self pointer - * @return None - * @throws UNEXPECTED if @em self pointer is NULL - */ -extern int table_iter_clear(table_iter_t *) -/*! @cond */ -__nonnull((1)) /*! @endcond */ ; - -/*! - * @brief Return a pointer to @em value element at the current iterator position - * @memberof table_iter - * @param self [in] table_iter object @em self pointer - * @throws UNEXPECTED if @em self pointer is NULL - */ -extern const value_t *table_iter_elem(table_iter_t *) -/*! @cond */ -__nonnull((1)) /*! @endcond */ ; - -/*! - * @brief Increment the position of an @em table iterator - * @memberof table_iter - * @param self [in] table_iter object @em self pointer - * @param count [in] Number of positions to increment - * @return None - * @throws UNEXPECTED if @em self pointer is NULL - */ -#define table_iter_inc(...) STRCAT(table_iter_inc, NARGS(__VA_ARGS__))(__VA_ARGS__) -extern const value_t *table_iter_inc1(table_iter_t *) -/*! @cond */ -__nonnull((1)) /*! @endcond */ ; -extern const value_t *table_iter_inc2(table_iter_t *, size_t) -/*! @cond */ -__nonnull((1)) /*! @endcond */ ; - -/*! - * @brief Decrement the position of an @em table iterator - * @memberof table_iter - * @param self [in] table_iter object @em self pointer - * @param count [in] Number of positions to decrement - * @return None - * @throws UNEXPECTED if @em self pointer is NULL - */ -#define table_iter_dec(...) STRCAT(table_iter_dec, NARGS(__VA_ARGS__))(__VA_ARGS__) -extern const value_t *table_iter_dec1(table_iter_t *) -/*! @cond */ -__nonnull((1)) /*! @endcond */ ; -extern const value_t *table_iter_dec2(table_iter_t *, size_t) -/*! @cond */ -__nonnull((1)) /*! @endcond */ ; - -/*! - * @fn const void * table_iter_pos(table_iter_t * self, size_t pos) - * @brief Return or set the iterator position witin the @em table - * @details If the second (2nd) parameter is omitted, this functions returns - * the current position. - * @memberof table_iter - * @param self [in] table_iter object @em self pointer - * @param pos [in] new iterator position (optional) - * @return None - * @throws UNEXPECTED if @em self pointer is NULL - */ -/*! @cond */ -#define table_iter_pos(...) STRCAT(table_iter_pos, NARGS(__VA_ARGS__))(__VA_ARGS__) -extern size_t table_iter_pos1(table_iter_t *) -/*! @cond */ -__nonnull((1)) /*! @endcond */ ; -extern int table_iter_pos2(table_iter_t *, size_t) -/*! @cond */ -__nonnull((1)) /*! @endcond */ ; -/*! @endcond */ - -/*! - * @def table_for_each_row(it, i) - * @hideinitializer - * @brief Table for-each-row element algorithm - * @param it [in] Table iterator object - * @param r [in] Value element variable (row) - */ -#define table_for_each(it,r) \ - for (r = (typeof(r))table_iter_elem((it)); \ - r != NULL; \ - r = (typeof(r))table_iter_inc((it))) - -/* ==================================================================== */ - -#endif /* __TABLE_ITER_H__ */ diff --git a/clib/test/table.c b/clib/test/table.c deleted file mode 100644 index e93dddb..0000000 --- a/clib/test/table.c +++ /dev/null @@ -1,109 +0,0 @@ -/* IBM_PROLOG_BEGIN_TAG */ -/* This is an automatically generated prolog. */ -/* */ -/* $Source: clib/test/table.c $ */ -/* */ -/* 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 */ - -#include -#include -#include - -#include -#include - -#define COLS 5 - -int main(void) { - table_t t = INIT_TABLE; - table_init(&t, "table", COLS); - - const char * str = "column4 is a really long assed string"; - - table_name(&t, 0, "column0"); - table_name(&t, 1, "column1"); - table_name(&t, 2, "column2"); - table_name(&t, 3, "column3"); - table_name(&t, 4, str); - - printf("%s\n", table_name(&t, 4)); - printf("cols: %d\n", table_columns(&t)); - printf("rows: %d\n", table_rows(&t)); - - table_dump(&t, stdout); - - table_iter_t it; - table_iter_init(&it, &t, TI_FLAG_FWD); - - size_t r, c; - - value_t * val; - table_for_each(&it, val) { - for (c=0; c %d\n", r, c, r * COLS + c); - value_i32(v+c, r * COLS + c); - value_dump(v+c, stdout); - } - table_row(&t, r, v); - } - table_dump(&t, stdout); - printf("yyy =================\n"); - - table_for_each(&it, val) { - for (c=0; c