From 817abd8cf77a79551cfea29cde5b2ef6ab04717b Mon Sep 17 00:00:00 2001 From: nishi Date: Sat, 18 May 2024 06:09:55 +0000 Subject: [PATCH] add database git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@21 d4a5a174-5a4a-5b4b-b672-37683c10d7d5 --- Client/main.c | 26 +++++++++++++++ FORMAT | 4 +-- Library/Makefile | 2 +- Library/database.c | 68 ++++++++++++++++++++++++++++++++++++++++ Library/dw_database.h | 73 +++++++++++++++++++++++++++++++++++++++++++ Library/dw_parser.h | 47 ++++++++++++++++++++++++++++ 6 files changed, 217 insertions(+), 3 deletions(-) create mode 100644 Library/database.c create mode 100644 Library/dw_database.h create mode 100644 Library/dw_parser.h diff --git a/Client/main.c b/Client/main.c index 3259232..582d893 100644 --- a/Client/main.c +++ b/Client/main.c @@ -27,6 +27,7 @@ /* --- END LICENSE --- */ #include +#include #include #include @@ -51,19 +52,33 @@ void padleft(int leftpad, const char* str) { int main(int argc, char** argv) { int i; bool noclear = false; + bool create = false; + const char* fname = NULL; for(i = 1; i < argc; i++) { if(argv[i][0] == '-') { if(strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "-V") == 0) { printf("DataWorks version %s %s %s\n", dataworks_get_version(), dataworks_get_compile_date(), dataworks_get_platform()); return 0; + } else if(strcmp(argv[i], "--create") == 0 || strcmp(argv[i], "-C") == 0) { + create = true; } else if(strcmp(argv[i], "--noclear") == 0) { noclear = true; } else { fprintf(stderr, "%s: %s: invalid option\n", argv[0], argv[i]); return 1; } + } else { + if(fname != NULL) { + fprintf(stderr, "%s: %s: cannot use more than 1 database at same time.\n", argv[0], argv[i]); + return 1; + } + fname = argv[i]; } } + if(fname == NULL) { + fprintf(stderr, "%s: filename needed\n", argv[0]); + return 1; + } #ifdef __MINGW32__ winstdout = GetStdHandle(STD_OUTPUT_HANDLE); DWORD mode = 0; @@ -85,6 +100,17 @@ int main(int argc, char** argv) { printf("\n"); printf("Copyright (c) Nishi 2024\n"); printf("All rights reserved.\n"); + if(create) { + printf("\n"); + printf("Creating the database: %s\n", fname); + int n = dataworks_database_create(fname); + if(n != 0) { + printf("Failed to create.\n"); + return 1; + } else { + printf("Created successfully.\n"); + } + } printf("\n"); printf("Type a command (.help) for the help\n"); printf("\n"); diff --git a/FORMAT b/FORMAT index 3f70d55..633ac8a 100644 --- a/FORMAT +++ b/FORMAT @@ -1,6 +1,6 @@ DataWorks Database Format 1.0 ============================================== -Extension should be .dwf +Extension SHOULD be .dwf All numbers MUST use big-endiam @@ -29,7 +29,7 @@ Used : 0b10000000 Flag this if you are using the entry. Index entry (`indexentry`) MUST be in this format: flag : 1 byte uint8_t count : 8 bytes uint64_t -dbname_len : 8 bytes uint8_t +dbname_len : 1 byte uint8_t dbname : 256 bytes ASCII fields : 4096 bytes ASCII (Separate field names using NUL. Put NUL twice on the last field name.) diff --git a/Library/Makefile b/Library/Makefile index dd91494..d572074 100644 --- a/Library/Makefile +++ b/Library/Makefile @@ -3,7 +3,7 @@ .PHONY: all clean .SUFFIXES: .c .o -OBJS = parser.o util.o dataworks.o +OBJS = parser.o database.o util.o dataworks.o all: $(LIB_PREFIX)dataworks$(LIB_SUFFIX) diff --git a/Library/database.c b/Library/database.c new file mode 100644 index 0000000..dbb0c63 --- /dev/null +++ b/Library/database.c @@ -0,0 +1,68 @@ +/* $Id$ */ +/* --- START LICENSE --- */ +/* -------------------------------------------------------------------------- */ +/* Copyright (c) 2024 Nishi. */ +/* Redistribution and use in source and binary forms, with or without modific */ +/* ation, are permitted provided that the following conditions are met: */ +/* 1. Redistributions of source code must retain the above copyright noti */ +/* ce, this list of conditions and the following disclaimer. */ +/* 2. Redistributions in binary form must reproduce the above copyright n */ +/* otice, this list of conditions and the following disclaimer in the documen */ +/* tation and/or other materials provided with the distribution. */ +/* 3. Neither the name of the copyright holder nor the names of its contr */ +/* ibutors may be used to endorse or promote products derived from this softw */ +/* are without specific prior written permission. */ +/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS */ +/* " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, TH */ +/* E IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPO */ +/* SE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS */ +/* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CON */ +/* SEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITU */ +/* TE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPT */ +/* ION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, S */ +/* TRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN AN */ +/* Y WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY */ +/* OF SUCH DAMAGE. */ +/* -------------------------------------------------------------------------- */ +/* --- END LICENSE --- */ + +#include "dw_database.h" + +#include +#include +#include + +const char sig[3] = {0x7f, 'D', 'W'}; + +int dataworks_database_create(const char* fname) { + FILE* f = fopen(fname, "wb"); + if(f == NULL) { + return 1; + } + uint8_t nul[4096]; + int i; + fwrite(sig, 1, 3, f); + nul[0] = 0; + nul[1] = 1; + fwrite(nul, 1, 2, f); + for(i = 0; i < 4096; i++) nul[i] = 0; + for(i = 0; i < 256; i++) { + fwrite(nul, 1, 1, f); + fwrite(nul, 1, 8, f); + fwrite(nul, 1, 1, f); + fwrite(nul, 1, 256, f); + fwrite(nul, 1, 4096, f); + } + fclose(f); + return 0; +} + +struct dataworks_db* dataworks_database_open(const char* fname) { + FILE* fp = fopen(fname, "rb+"); + if(fp == NULL) { + return NULL; + } + struct dataworks_db* db = malloc(sizeof(*db)); + db->fp = fp; + return db; +} diff --git a/Library/dw_database.h b/Library/dw_database.h new file mode 100644 index 0000000..2c52ff1 --- /dev/null +++ b/Library/dw_database.h @@ -0,0 +1,73 @@ +/* $Id: dw_database.h 7 2024-05-16 15:56:44Z nishi $ */ +/* --- START LICENSE --- */ +/* -------------------------------------------------------------------------- */ +/* Copyright (c) 2024 Nishi. */ +/* Redistribution and use in source and binary forms, with or without modific */ +/* ation, are permitted provided that the following conditions are met: */ +/* 1. Redistributions of source code must retain the above copyright noti */ +/* ce, this list of conditions and the following disclaimer. */ +/* 2. Redistributions in binary form must reproduce the above copyright n */ +/* otice, this list of conditions and the following disclaimer in the documen */ +/* tation and/or other materials provided with the distribution. */ +/* 3. Neither the name of the copyright holder nor the names of its contr */ +/* ibutors may be used to endorse or promote products derived from this softw */ +/* are without specific prior written permission. */ +/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS */ +/* " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, TH */ +/* E IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPO */ +/* SE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS */ +/* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CON */ +/* SEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITU */ +/* TE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPT */ +/* ION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, S */ +/* TRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN AN */ +/* Y WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY */ +/* OF SUCH DAMAGE. */ +/* -------------------------------------------------------------------------- */ +/* --- END LICENSE --- */ + +#ifndef __DATAWORKS_DW_DATABASE_H__ +#define __DATAWORKS_DW_DATABASE_H__ + +/** + * @file dw_database.h + * @~english + * @brief DataWorks database + * + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +struct dataworks_db { + FILE* fp; +}; + +/** + * @~english + * @brief Creates the database. + * @param fname Filename + * @return 0 if successful + * + */ +int dataworks_database_create(const char* fname); + +/** + * @~english + * @brief Opens the database. + * @param fname Filename + * @return + * - `NULL` if it failed to open the database. + * - non-`NULL` if it could open the database. + * + */ +struct dataworks_db* dataworks_database_open(const char* fname); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Library/dw_parser.h b/Library/dw_parser.h new file mode 100644 index 0000000..b14c38c --- /dev/null +++ b/Library/dw_parser.h @@ -0,0 +1,47 @@ +/* $Id: dw_util.h 7 2024-05-16 15:56:44Z nishi $ */ +/* --- START LICENSE --- */ +/* -------------------------------------------------------------------------- */ +/* Copyright (c) 2024 Nishi. */ +/* Redistribution and use in source and binary forms, with or without modific */ +/* ation, are permitted provided that the following conditions are met: */ +/* 1. Redistributions of source code must retain the above copyright noti */ +/* ce, this list of conditions and the following disclaimer. */ +/* 2. Redistributions in binary form must reproduce the above copyright n */ +/* otice, this list of conditions and the following disclaimer in the documen */ +/* tation and/or other materials provided with the distribution. */ +/* 3. Neither the name of the copyright holder nor the names of its contr */ +/* ibutors may be used to endorse or promote products derived from this softw */ +/* are without specific prior written permission. */ +/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS */ +/* " AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, TH */ +/* E IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPO */ +/* SE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS */ +/* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CON */ +/* SEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITU */ +/* TE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPT */ +/* ION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, S */ +/* TRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN AN */ +/* Y WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY */ +/* OF SUCH DAMAGE. */ +/* -------------------------------------------------------------------------- */ +/* --- END LICENSE --- */ + +#ifndef __DATAWORKS_DW_PARSER_H__ +#define __DATAWORKS_DW_PARSER_H__ + +/** + * @file dw_parser.h + * @~english + * @brief DataWorks parser + * + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef __cplusplus +} +#endif + +#endif -- 2.43.0