1 /* 2 ** Copyright (c) 2010 Michael Dvorkin 3 ** 4 ** This program is free software; you can redistribute it and/or 5 ** modify it under the terms of the Simplified BSD License (also 6 ** known as the "2-Clause License" or "FreeBSD License".) 7 ** 8 ** This program is distributed in the hope that it will be useful, 9 ** but without any warranty; without even the implied warranty of 10 ** merchantability or fitness for a particular purpose. 11 */ 12 #if !defined(__TABLE_H__) 13 #define __TABLE_H__ 14 15 #define TABLE_HAS_ID 1 16 #define TABLE_HAS_CREATED_AT 2 17 #define TABLE_HAS_UPDATED_AT 4 18 #define TABLE_HAS_TIMESTAMPS (TABLE_HAS_CREATED_AT | TABLE_HAS_UPDATED_AT) 19 20 #define HAS_ID(pt) (pt && (pt->flags & TABLE_HAS_ID)) 21 #define HAS_CREATED_AT(pt) (pt && (pt->flags & TABLE_HAS_CREATED_AT)) 22 #define HAS_UPDATED_AT(pt) (pt && (pt->flags & TABLE_HAS_UPDATED_AT)) 23 #define HAS_TIMESTAMPS(pt) (HAS_CREATED_AT(pt) && HAS_UPDATED_AT(pt)) 24 25 typedef struct _Table { 26 int flags; /* Bit mask with table flags. */ 27 int record_size; /* Record size in bytes; all records are of fixed size. */ 28 int number_of_slots; /* Number of slots allocated, each slot is 'record_size' long. */ 29 int number_of_records; /* Number of records currently stored in slots. */ 30 int auto_increment; /* Current value of record id. */ 31 int index_size; /* The size of the index. */ 32 int current; /* The id of currently selected record, one per table. */ 33 char *slots; /* Memory chunk to store records; compacted when a record gets deleted (no holes). */ 34 char **index; /* Memory chunk to store pointers to individual records, holes for deleted record IDs. */ 35 } Table, *PTable; 36 37 PTable pit_table_initialize(int record_size, int flags); 38 void pit_table_free(PTable pt); 39 char *pit_table_find(PTable pt, int id); 40 char *pit_table_delete(PTable pt, int id); 41 char *pit_table_insert(PTable pt, char *record); 42 43 char *pit_table_current(PTable pt); 44 char *pit_table_mark(PTable pt, int id); 45 46 int pit_table_save(FILE *file, PTable pt); 47 PTable pit_table_load(FILE *file); 48 49 #endif