From 3e351332d4975a0f3a8f641385b9a033439d0e74 Mon Sep 17 00:00:00 2001 From: nishi Date: Sat, 18 May 2024 17:21:13 +0000 Subject: [PATCH] can read version now git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@37 d4a5a174-5a4a-5b4b-b672-37683c10d7d5 --- Client/main.c | 2 +- Library/database.c | 12 +++++++++++- Library/dw_database.h | 7 +++++++ Library/dw_util.h | 21 +++++++++++++++++++++ Library/util.c | 12 ++++++++---- 5 files changed, 48 insertions(+), 6 deletions(-) diff --git a/Client/main.c b/Client/main.c index 1f241f0..7487beb 100644 --- a/Client/main.c +++ b/Client/main.c @@ -118,7 +118,7 @@ int main(int argc, char** argv) { printf("Bad database file or non-existent.\n"); return 1; } - printf("Opened the database.\n"); + printf("Opened the database (Version %d).\n", db->version); printf("\n"); printf("Type a command (.help) for the help\n"); printf("\n"); diff --git a/Library/database.c b/Library/database.c index 95078fc..a20b94b 100644 --- a/Library/database.c +++ b/Library/database.c @@ -28,10 +28,12 @@ #include "dw_database.h" +#include "dw_util.h" + #include #include -#include #include +#include const char sig[3] = {0x7f, 'D', 'W'}; @@ -69,7 +71,15 @@ struct dataworks_db* dataworks_database_open(const char* fname) { fclose(fp); return NULL; } + __dw_lockfile(fp); + char ptrver[2]; + fread(ptrver, 1, 2, fp); + uint16_t be_ver = *(uint16_t*)(char*)ptrver; + uint16_t ver; + __dw_native_endian(be_ver, uint16_t, ver = __converted); + __dw_unlockfile(fp); struct dataworks_db* db = malloc(sizeof(*db)); db->fp = fp; + db->version = ver; return db; } diff --git a/Library/dw_database.h b/Library/dw_database.h index ec8ca1a..a96ed9c 100644 --- a/Library/dw_database.h +++ b/Library/dw_database.h @@ -40,6 +40,7 @@ extern "C" { #endif +#include #include /** @@ -54,6 +55,12 @@ struct dataworks_db { * */ FILE* fp; + /** + * @~english + * @brief Version of the database version. + * + */ + uint16_t version; }; /** diff --git a/Library/dw_util.h b/Library/dw_util.h index 1c5c178..549b0e5 100644 --- a/Library/dw_util.h +++ b/Library/dw_util.h @@ -52,6 +52,7 @@ bool __dw_unlockfile(FILE* fp); #define __dw_xstr(x) #x #define __dw_str(x) __dw_xstr(x) +/* Converts NE to BE */ #define __dw_big_endian(arg, type, exec) \ { \ type __original = arg; \ @@ -71,6 +72,26 @@ bool __dw_unlockfile(FILE* fp); exec; \ } +/* Converts BE to NE */ +#define __dw_native_endian(arg, type, exec) \ + { \ + type __original = arg; \ + signed char* __ptr = (signed char*)&__original; \ + type __converted; \ + signed char* __converted_ptr = (signed char*)&__converted; \ + int __i; \ + int __endian_check = 1; \ + char __endian = (1 == *(volatile char*)&__endian_check) ? 'L' : 'B'; \ + for(__i = 0; __i < sizeof(type); __i++) { \ + if(__endian == 'L') { \ + __converted_ptr[sizeof(type) - __i - 1] = __ptr[__i]; \ + } else { \ + __converted_ptr[__i] = __ptr[__i]; \ + } \ + } \ + exec; \ + } + #ifdef __cplusplus } #endif diff --git a/Library/util.c b/Library/util.c index 9273a17..2ac0124 100644 --- a/Library/util.c +++ b/Library/util.c @@ -46,24 +46,28 @@ bool __dw_strcaseequ(const char* a, const char* b) { return true; } -bool __dw_lockfile(FILE* fp){ +bool __dw_lockfile(FILE* fp) { + off_t off = ftell(fp); + fseek(fp, 0, SEEK_SET); #ifdef __MINGW32__ OVERLAPPED overlap = {0}; LockFileEx(fp, LOCKFILE_EXCLUSIVE_LOCK, 0, MAXDWORD, MAXDWORD, &overlap); #else - fseek(fp, SEEK_SET, 0); lockf(fileno(fp), F_LOCK, 0); #endif + fseek(fp, off, SEEK_SET); return false; } -bool __dw_unlockfile(FILE* fp){ +bool __dw_unlockfile(FILE* fp) { + off_t off = ftell(fp); + fseek(fp, 0, SEEK_SET); #ifdef __MINGW32__ OVERLAPPED overlap = {0}; UnlockFileEx(fp, 0, MAXDWORD, MAXDWORD, &overlap); #else - fseek(fp, SEEK_SET, 0); lockf(fileno(fp), F_ULOCK, 0); #endif + fseek(fp, off, SEEK_SET); return false; } -- 2.43.0