]> Nishi Git Mirror - dataworks.git/commitdiff
error message
authornishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Wed, 22 May 2024 04:09:27 +0000 (04:09 +0000)
committernishi <nishi@d4a5a174-5a4a-5b4b-b672-37683c10d7d5>
Wed, 22 May 2024 04:09:27 +0000 (04:09 +0000)
git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@69 d4a5a174-5a4a-5b4b-b672-37683c10d7d5

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

index 44f1ddabd7ff6a64987321d8a89176c0657979ae..e16e813172ae3a233b218a3684e1203a3db57a9c 100644 (file)
@@ -115,8 +115,9 @@ int main(int argc, char** argv) {
        printf("\n");
        printf("Opening the database: %s\n", fname);
        struct dataworks_db* db = dataworks_database_open(fname);
-       if(db == NULL) {
-               printf("Bad database file or non-existent.\n");
+       if(db->error) {
+               printf("%s.\n", dataworks_database_strerror(db->errnum));
+               dataworks_database_close(db);
                return 1;
        }
        time_t mtime = (time_t)dataworks_database_get_mtime(db);
index 85baa567100c26dcad8f36b65f5ea341d56a2ee0..edfea1861b5cc73e6c48a1ebf1620d7a1e625c5e 100644 (file)
@@ -81,16 +81,28 @@ int dataworks_database_create(const char* fname) {
        return 0;
 }
 
+void dataworks_database_close(struct dataworks_db* db) {
+       if(db->fp != NULL) fclose(db->fp);
+       free(db);
+}
+
 struct dataworks_db* dataworks_database_open(const char* fname) {
+       struct dataworks_db* db = malloc(sizeof(*db));
+       db->error = false;
+       db->fp = NULL;
        FILE* fp = fopen(fname, "rb+");
        if(fp == NULL) {
-               return NULL;
+               db->error = true;
+               db->errnum = DW_ERR_FAIL_FOPEN;
+               return db;
        }
+       fseek(fp, 0, SEEK_SET);
        char readsig[sizeof(sig)];
        fread(readsig, 1, sizeof(sig), fp);
        if(memcmp(readsig, sig, sizeof(sig)) != 0) {
-               fclose(fp);
-               return NULL;
+               db->error = true;
+               db->errnum = DW_ERR_INVALID_SIGNATURE;
+               return db;
        }
        __dw_lockfile(fp);
        char ptrver[8];
@@ -104,14 +116,15 @@ struct dataworks_db* dataworks_database_open(const char* fname) {
        __dw_native_endian(be_mtime, uint64_t, mtime = __converted);
        __dw_unlockfile(fp);
        if(ver == 1) {
-               struct dataworks_db* db = malloc(sizeof(*db));
                db->fp = fp;
                db->version = ver;
                db->mtime = mtime;
                return db;
        } else {
                fclose(fp);
-               return NULL;
+               db->error = true;
+               db->errnum = DW_ERR_INVALID_VERSION;
+               return db;
        }
 }
 
@@ -119,7 +132,9 @@ int dataworks_database_get_version(struct dataworks_db* db) { return db->version
 
 uint64_t dataworks_database_get_mtime(struct dataworks_db* db) { return db->mtime; }
 
-const char* dw_errors[] = {"Success", "Used already"};
+int dataworks_database_get_error_number(struct dataworks_db* db) { return db->errnum; }
+
+const char* dw_errors[] = {"Success", "Used already", "File open fail", "Invalid signature", "Invalid version"};
 
 const char* dataworks_database_strerror(int n) { return dw_errors[n]; }
 
index b9082b5d4beb05e45a1b347a9b6ad0fa4f7bb9dd..eaf23b1e9e97d91750cfe0fd2f323560e5b13e60 100644 (file)
@@ -40,6 +40,7 @@
 extern "C" {
 #endif
 
+#include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
 
@@ -70,12 +71,34 @@ enum DW_ERRORS {
         *
         */
        DW_ERR_SUCCESS = 0,
+
        /**
         * @~english
         * @brief Used already
         *
         */
-       DW_ERR_USED
+       DW_ERR_USED,
+
+       /**
+        * @~english
+        * @brief File open fail
+        *
+        */
+       DW_ERR_FAIL_FOPEN,
+
+       /**
+        * @~english
+        * @brief Invalid signature
+        *
+        */
+       DW_ERR_INVALID_SIGNATURE,
+
+       /**
+        * @~english
+        * @brief Invalid version
+        *
+        */
+       DW_ERR_INVALID_VERSION,
 };
 
 /**
@@ -104,6 +127,20 @@ struct dataworks_db {
         *
         */
        uint64_t mtime;
+
+       /**
+        * @~english
+        * @brief True if this is an error.
+        *
+        */
+       bool error;
+
+       /**
+        * @~english
+        * @brief Error number.
+        *
+        */
+       int errnum;
 };
 
 /**
@@ -162,6 +199,14 @@ int dataworks_database_create(const char* fname);
  */
 struct dataworks_db* dataworks_database_open(const char* fname);
 
+/**
+ * @~english
+ * @brief Closes the database.
+ * @param db Database
+ *
+ */
+void dataworks_database_close(struct dataworks_db* db);
+
 /**
  * @~english
  * @brief Get the version of the database.
@@ -171,6 +216,24 @@ struct dataworks_db* dataworks_database_open(const char* fname);
  */
 int dataworks_database_get_version(struct dataworks_db* db);
 
+/**
+ * @~english
+ * @brief Get the error flag of the database.
+ * @param db Database
+ * @return Error flag of the database
+ *
+ */
+bool dataworks_database_get_error(struct dataworks_db* db);
+
+/**
+ * @~english
+ * @brief Get the error number of the database.
+ * @param db Database
+ * @return Error number of the database
+ *
+ */
+int dataworks_database_get_error_number(struct dataworks_db* db);
+
 /**
  * @~english
  * @brief Get the last modified time of the database.