From 618c5eaf2b5c4048bb42171e9fc390dba642cbc3 Mon Sep 17 00:00:00 2001 From: nishi Date: Sat, 25 May 2024 16:44:23 +0000 Subject: [PATCH] can get fields now git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@112 d4a5a174-5a4a-5b4b-b672-37683c10d7d5 --- Library/database_record.c | 2 +- Library/database_table.c | 53 +++++++++++++++++++++++++++++++++++++++ Library/dw_database.h | 20 +++++++++++---- 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/Library/database_record.c b/Library/database_record.c index e629b7e..6ade2f5 100644 --- a/Library/database_record.c +++ b/Library/database_record.c @@ -26,4 +26,4 @@ /* -------------------------------------------------------------------------- */ /* --- END LICENSE --- */ -#include "dw_database.h" \ No newline at end of file +#include "dw_database.h" diff --git a/Library/database_table.c b/Library/database_table.c index e8f19c6..eb69d4e 100644 --- a/Library/database_table.c +++ b/Library/database_table.c @@ -148,3 +148,56 @@ char** dataworks_database_get_table_list(struct dataworks_db* db) { return list; } } + +char** dataworks_database_get_table_fields(struct dataworks_db* db, const char* table) { + if(db->version == 1) { + __dw_lockfile(db->fp); + fseek(db->fp, 3 + 10, SEEK_SET); + int i; + struct dataworks_db_v1_indexentry index; + char* buf = malloc(1 + 8 + 1 + 256 + 4096); + int c = 0; + for(i = 0; i < 256; i++) { + fread(buf, 1, 1 + 8 + 1 + 256 + 4096, db->fp); + __dw_buffer_to_db_v1_indexentry(buf, index); + if(index.flag & DATAWORKS_V1_INDEXENTRY_USED) { + char* dbname = malloc(index.dbname_len + 1); + memcpy(dbname, index.dbname, index.dbname_len); + dbname[index.dbname_len] = 0; + if(strcmp(dbname, table) == 0) { + char** list = malloc(sizeof(*list)); + list[0] = NULL; + int j; + int start = 1; + for(j = 1;; j++) { + if(index.fields[j] == 0) { + char* nam = malloc(j - start + 1); + nam[start - j] = 0; + memcpy(nam, index.fields + start, j - start); + int k; + for(k = 0; list[k] != NULL; k++) + ; + char** newlist = malloc(sizeof(*newlist) * (k + 2)); + for(k = 0; list[k] != NULL; k++) newlist[k] = list[k]; + newlist[k] = nam; + newlist[k + 1] = NULL; + free(list); + list = newlist; + j++; + start = j + 1; + if(index.fields[j] == 0) break; + } + } + free(dbname); + free(buf); + __dw_unlockfile(db->fp); + return list; + } + free(dbname); + } + } + free(buf); + __dw_unlockfile(db->fp); + } + return NULL; +} diff --git a/Library/dw_database.h b/Library/dw_database.h index b475546..244d01f 100644 --- a/Library/dw_database.h +++ b/Library/dw_database.h @@ -140,7 +140,7 @@ enum DW_ERRORS { /** * @~english * @brief Type enum - * + * */ enum DW_RECORD_TYPES { /** @@ -160,21 +160,21 @@ enum DW_RECORD_TYPES { /** * @~english * @brief Double - * + * */ DW_RECORD_DOUBLE = 'D', /** * @~english * @brief Logical - * + * */ DW_RECORD_LOGICAL = 'L', /** * @~english * @brief HELP - * + * */ DW_RECORD_HELP = '?', }; @@ -245,7 +245,7 @@ struct dataworks_db_record { /** * @~english * @brief Type. See enum DW_RECORD_TYPES. - * + * */ char type; }; @@ -385,6 +385,16 @@ uint64_t dataworks_database_get_mtime(struct dataworks_db* db); */ char** dataworks_database_get_table_list(struct dataworks_db* db); +/** + * @~english + * @brief Get the fields of the table of the database. + * @param db Database + * @param table Table name + * @return Fields of the table + * + */ +char** dataworks_database_get_table_fields(struct dataworks_db* db, const char* table); + /** * @~english * @brief Creates a table. -- 2.43.0