/* --- END LICENSE --- */
#include <dataworks.h>
+#include <dw_database.h>
#include <dw_util.h>
#include <stdbool.h>
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;
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");
DataWorks Database Format 1.0
==============================================
-Extension should be .dwf
+Extension SHOULD be .dwf
All numbers MUST use big-endiam
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.)
.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)
--- /dev/null
+/* $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 <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+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;
+}
--- /dev/null
+/* $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 <stdio.h>
+
+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
--- /dev/null
+/* $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