From: nishi Date: Wed, 22 May 2024 04:09:27 +0000 (+0000) Subject: error message X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=3f4bb901fe2d79f0f641f1eab6f88f60e1f77cf8;p=dataworks.git error message git-svn-id: file:///raid/svn-main/nishi-dataworks/trunk@69 d4a5a174-5a4a-5b4b-b672-37683c10d7d5 --- diff --git a/Client/main.c b/Client/main.c index 44f1dda..e16e813 100644 --- a/Client/main.c +++ b/Client/main.c @@ -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); diff --git a/Library/database.c b/Library/database.c index 85baa56..edfea18 100644 --- a/Library/database.c +++ b/Library/database.c @@ -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]; } diff --git a/Library/dw_database.h b/Library/dw_database.h index b9082b5..eaf23b1 100644 --- a/Library/dw_database.h +++ b/Library/dw_database.h @@ -40,6 +40,7 @@ extern "C" { #endif +#include #include #include @@ -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.