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);
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];
__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;
}
}
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]; }
extern "C" {
#endif
+#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
*
*/
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,
};
/**
*
*/
uint64_t mtime;
+
+ /**
+ * @~english
+ * @brief True if this is an error.
+ *
+ */
+ bool error;
+
+ /**
+ * @~english
+ * @brief Error number.
+ *
+ */
+ int errnum;
};
/**
*/
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.
*/
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.