From 9d563d27360522b9c8edaa0368d492a7a5e8402e Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 11 Oct 2017 16:34:35 -0500 Subject: [PATCH] Add toollib files for oxfs tool --- src/ox/fs/toollib.cpp | 33 +++++++++++++++++++++++++++++++++ src/ox/fs/toollib.hpp | 14 ++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/ox/fs/toollib.cpp create mode 100644 src/ox/fs/toollib.hpp diff --git a/src/ox/fs/toollib.cpp b/src/ox/fs/toollib.cpp new file mode 100644 index 000000000..fc7836e9d --- /dev/null +++ b/src/ox/fs/toollib.cpp @@ -0,0 +1,33 @@ +/* + * Copyright 2015 - 2017 gtalent2@gmail.com + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include +#include + +#include "toollib.hpp" + +uint8_t *loadFileBuff(FILE *file, ::size_t *sizeOut) { + if (file) { + fseek(file, 0, SEEK_END); + const auto size = ftell(file); + rewind(file); + auto buff = new uint8_t[size]; + auto itemsRead = fread(buff, size, 1, file); + fclose(file); + if (sizeOut) { + *sizeOut = itemsRead ? size : 0; + } + return buff; + } else { + return nullptr; + } +} + +uint8_t *loadFileBuff(const char *path, ::size_t *sizeOut) { + return loadFileBuff(fopen(path, "rb"), sizeOut); +} diff --git a/src/ox/fs/toollib.hpp b/src/ox/fs/toollib.hpp new file mode 100644 index 000000000..8c51b7764 --- /dev/null +++ b/src/ox/fs/toollib.hpp @@ -0,0 +1,14 @@ +/* + * Copyright 2015 - 2017 gtalent2@gmail.com + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include +#include + +uint8_t *loadFileBuff(FILE *file, size_t *sizeOut = nullptr); + +uint8_t *loadFileBuff(const char *path, size_t *sizeOut = nullptr);