]> Nishi Git Mirror - dataworks.git/commitdiff
can read version now
authornishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Sat, 18 May 2024 17:21:13 +0000 (17:21 +0000)
committernishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Sat, 18 May 2024 17:21:13 +0000 (17:21 +0000)
git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@37 d4a5a174-5a4a-5b4b-b672-37683c10d7d5

Client/main.c
Library/database.c
Library/dw_database.h
Library/dw_util.h
Library/util.c

index 1f241f08be689fc58789fca320b990d066420c1a..7487beb390cf5a5d67b0b5a6e754ec2ce9830483 100644 (file)
@@ -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");
index 95078fc56d4b7bf158985f4c7ab48ec2ba1e36eb..a20b94b91c1c738d3d486d4cd1ef221c075775db 100644 (file)
 
 #include "dw_database.h"
 
+#include "dw_util.h"
+
 #include <stdint.h>
 #include <stdio.h>
-#include <string.h>
 #include <stdlib.h>
+#include <string.h>
 
 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;
 }
index ec8ca1abbb401e9374c971fa00e194f0991bb10a..a96ed9c89b5c37ae24b91686429b5e74e72c74c4 100644 (file)
@@ -40,6 +40,7 @@
 extern "C" {
 #endif
 
+#include <stdint.h>
 #include <stdio.h>
 
 /**
@@ -54,6 +55,12 @@ struct dataworks_db {
         *
         */
        FILE* fp;
+       /**
+        * @~english
+        * @brief Version of the database version.
+        *
+        */
+       uint16_t version;
 };
 
 /**
index 1c5c1782aa639512fdc48999e5ec71e8f54d9278..549b0e536e2869da620deb38566b8aa64b2dcae9 100644 (file)
@@ -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
index 9273a17b7974bf0c64385f638a16ed2e4fbf5306..2ac0124219f97ffc28f5b16330f65599db06c391 100644 (file)
@@ -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;
 }