From a6c77d4b8f94c035a229f43327a3e6d6648724d9 Mon Sep 17 00:00:00 2001 From: JustinAJ Date: Mon, 18 May 2015 02:26:19 -0400 Subject: [PATCH] Added Jupiter::Database --- Jupiter/Database.cpp | 167 +++++++++++++++++++++++++++++++++++++++++++ Jupiter/Database.h | 71 ++++++++++++++++++ 2 files changed, 238 insertions(+) create mode 100644 Jupiter/Database.cpp create mode 100644 Jupiter/Database.h diff --git a/Jupiter/Database.cpp b/Jupiter/Database.cpp new file mode 100644 index 0000000..59bc6b4 --- /dev/null +++ b/Jupiter/Database.cpp @@ -0,0 +1,167 @@ +/** + * Copyright (C) Justin James - All Rights Reserved. + * Unauthorized use or copying of this file via any medium is strictly prohibited. + * This document is proprietary and confidential. + * This document should be immediately destroyed unless given explicit permission by Justin James. + * Written by Justin James + */ + +#include "Database.h" + +void Jupiter::Database::process_header(FILE *) +{ +} + +void Jupiter::Database::create_header(FILE *) +{ +} + +bool Jupiter::Database::process_file(Jupiter::ReadableString &file) +{ + Jupiter::Database::file_name = file; + return Jupiter::Database::process_file(); +} + +bool Jupiter::Database::process_file(Jupiter::CStringType &file) +{ + Jupiter::Database::file_name = file; + return Jupiter::Database::process_file(); +} + +bool Jupiter::Database::process_file(const char *file) +{ + Jupiter::Database::file_name = file; + return Jupiter::Database::process_file(); +} + +bool Jupiter::Database::process_file() +{ + FILE *file = fopen(Jupiter::Database::file_name.c_str(), "rb"); + if (file == nullptr) + { + if (Jupiter::Database::auto_create) + { + file = fopen(Jupiter::Database::file_name.c_str(), "wb"); + if (file != nullptr) + { + this->create_header(file); + return true; + } + } + return false; + } + bool r = Jupiter::Database::process_file(file); + fclose(file); + return r; +} + +bool Jupiter::Database::process_file(FILE *file) +{ + /** + * FILE FORMAT: + * HEADER + * DATA (Buffer[x]) + */ + Jupiter::DataBuffer buffer; + this->process_header(file); + fpos_t pos; + fgetpos(file, &pos); + + while (fgetc(file) != EOF) + { + fsetpos(file, &pos); + buffer.pop_from(file); + this->process_data(buffer, file, pos); + buffer.empty(); + fgetpos(file, &pos); + } + this->process_file_finish(file); + return true; +} + +void Jupiter::Database::process_file_finish(FILE *) +{ +} + +bool Jupiter::Database::append(Jupiter::DataBuffer &data) +{ + return Jupiter::Database::append(Jupiter::Database::file_name, data); +} + +bool Jupiter::Database::append(Jupiter::ReadableString &file, Jupiter::DataBuffer &data) +{ + char *str = new char[file.size() + 1]; + memcpy(str, file.ptr(), file.size() * sizeof(char)); + str[file.size()] = '\0'; + bool r = Jupiter::Database::append(str, data); + delete[] str; + return r; +} + +bool Jupiter::Database::append(Jupiter::CStringType &file, Jupiter::DataBuffer &data) +{ + return Jupiter::Database::append(file.c_str(), data); +} + +bool Jupiter::Database::append(const char *file, Jupiter::DataBuffer &data) +{ + FILE *f = fopen(file, "ab"); + if (f == nullptr) + return false; + bool r = Jupiter::Database::append(f, data); + fclose(f); + return r; +} + +template T *get_ptr(T &in) +{ + return ∈ +} + +bool Jupiter::Database::append(FILE *file, Jupiter::DataBuffer &data) +{ + fwrite(std::addressof(data.size()), sizeof(size_t), 1, file); + fwrite(data.getHead(), sizeof(uint8_t), data.size(), file); + return true; +} + +bool Jupiter::Database::create_database(Jupiter::ReadableString &file, Jupiter::DataBuffer *header) +{ + char *str = new char[file.size() + 1]; + memcpy(str, file.ptr(), file.size() * sizeof(char)); + str[file.size()] = '\0'; + bool r = Jupiter::Database::create_database(str, header); + delete[] str; + return r; +} + +bool Jupiter::Database::create_database(Jupiter::CStringType &file, Jupiter::DataBuffer *header) +{ + return Jupiter::Database::create_database(file.c_str(), header); +} + +bool Jupiter::Database::create_database(const char *file, Jupiter::DataBuffer *header) +{ + FILE *f = fopen(file, "wb"); + if (file == nullptr) + return false; + return Jupiter::Database::create_database(f, header); +} + +bool Jupiter::Database::create_database(FILE *file, Jupiter::DataBuffer *header) +{ + if (header != nullptr) + fwrite(header->getHead(), sizeof(uint8_t), header->size(), file); + fclose(file); + return true; +} + +bool Jupiter::Database::is_auto_create() +{ + return Jupiter::Database::auto_create; +} + +void Jupiter::Database::set_auto_create(bool auto_create_) +{ + Jupiter::Database::auto_create = auto_create_; +} \ No newline at end of file diff --git a/Jupiter/Database.h b/Jupiter/Database.h new file mode 100644 index 0000000..6b7e24d --- /dev/null +++ b/Jupiter/Database.h @@ -0,0 +1,71 @@ +/** + * Copyright (C) 2015 Justin James. + * + * This license must be preserved. + * Any applications, libraries, or code which make any use of any + * component of this program must not be commercial, unless explicit + * permission is granted from the original author. The use of this + * program for non-profit purposes is permitted. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * In the event that this license restricts you from making desired use of this program, contact the original author. + * Written by Justin James + */ + +#if !defined _DATABASE_H_HEADER +#define _DATABASE_H_HEADER + +/** + * @file Database.h + * @brief Defines a database file structure. + */ + +#include "CString.h" + +namespace Jupiter +{ + + /** + * @brief Provides an implementation for database files. + * + * @param T Element type which the String will store. Defaults to char. + */ + class JUPITER_API Database + { + public: + virtual void process_data(Jupiter::DataBuffer &buffer, FILE *file, fpos_t pos) = 0; + virtual void process_header(FILE *file); + virtual void create_header(FILE *file); + + bool process_file(Jupiter::ReadableString &file); + bool process_file(Jupiter::CStringType &file); + bool process_file(const char *file); + bool process_file(FILE *file); + virtual void process_file_finish(FILE *file); + + bool append(Jupiter::DataBuffer &data); + + static bool append(Jupiter::ReadableString &file, Jupiter::DataBuffer &data); + static bool append(Jupiter::CStringType &file, Jupiter::DataBuffer &data); + static bool append(const char *file, Jupiter::DataBuffer &data); + static bool append(FILE *file, Jupiter::DataBuffer &data); + + static bool create_database(Jupiter::ReadableString &file, Jupiter::DataBuffer *header = nullptr); + static bool create_database(Jupiter::CStringType &file, Jupiter::DataBuffer *header = nullptr); + static bool create_database(const char *file, Jupiter::DataBuffer *header = nullptr); + + bool is_auto_create(); + void set_auto_create(bool auto_create); + private: + bool process_file(); + static bool create_database(FILE *file, Jupiter::DataBuffer *header); + + bool auto_create = true; + Jupiter::CStringS file_name; + }; +} + +#endif // _DATABASE_H_HEADER \ No newline at end of file