Skip to content

Commit

Permalink
Add Pack error class
Browse files Browse the repository at this point in the history
  • Loading branch information
cfnptr committed Nov 3, 2024
1 parent 5505e51 commit 885114a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 30 deletions.
2 changes: 1 addition & 1 deletion libraries/mpio
9 changes: 3 additions & 6 deletions wrappers/cpp/pack/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
**********************************************************************************************************************/

#pragma once
#include <string>
#include <exception>
#include "pack/error.hpp"
#include <filesystem>

extern "C"
Expand All @@ -31,8 +30,6 @@ extern "C"
namespace pack
{

using namespace std;

/**
* @brief Common Pack functions.
* @details See the @ref common.h
Expand Down Expand Up @@ -61,14 +58,14 @@ class Common final
* @param[in] filePath target file path string
* @param[out] header reference to the @ref PackHeader structure
*
* @throw runtime_error with a @ref PackResult string on failure.
* @throw Error with a @ref PackResult string on failure.
*/
static void readHeader(const filesystem::path& filePath, PackHeader& header)
{
auto path = filePath.generic_string();
auto result = readPackHeader(path.c_str(), &header);
if (result != SUCCESS_PACK_RESULT)
throw runtime_error(packResultToString(result) + (", path: " + filePath.generic_string()));
throw Error(packResultToString(result) + (", path: " + filePath.generic_string()));
}

/**
Expand Down
48 changes: 48 additions & 0 deletions wrappers/cpp/pack/error.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2021-2024 Nikita Fediuchin. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/***********************************************************************************************************************
* @file
* @brief Common Pack error (exception) functions.
**********************************************************************************************************************/

#pragma once
#include <string>
#include <stdexcept>

namespace pack
{

using namespace std;

/**
* @brief Pack error (exception) class.
*/
class Error : public exception
{
string message;
public:
/**
* @brief Creates a new Pack error (exception) instance.
* @param message target error message
*/
Error(const string& message) : message(message) { }

/**
* @brief Returns Pack error message C-string.
*/
const char* what() const noexcept override { return message.c_str(); }
};

} // mpio
34 changes: 16 additions & 18 deletions wrappers/cpp/pack/reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
**********************************************************************************************************************/

#pragma once
#include <string>
#include "pack/error.hpp"

#include <thread>
#include <utility>
#include <exception>
#include <filesystem>
#include <string_view>

Expand All @@ -34,8 +34,6 @@ extern "C"
namespace pack
{

using namespace std;

/**
* @brief Pack reader instance handle.
* @details See the @ref reader.h
Expand Down Expand Up @@ -68,15 +66,15 @@ class Reader final
* @param isResourcesDirectory read from the resources directory (Android/iOS/macOS only)
* @param threadCount max concurrent read thread count
*
* @throw runtime_error with a @ref PackResult string on failure.
* @throw Error with a @ref PackResult string on failure.
*/
Reader(const filesystem::path& filePath, bool isResourcesDirectory = true,
uint32_t threadCount = thread::hardware_concurrency())
{
auto path = filePath.generic_string();
auto result = createFilePackReader(path.c_str(), isResourcesDirectory, threadCount, &instance);
if (result != SUCCESS_PACK_RESULT)
throw runtime_error(packResultToString(result) + (", path: " + filePath.generic_string()));
throw Error(packResultToString(result));
}

/**
Expand All @@ -93,7 +91,7 @@ class Reader final
* @param isResourcesDirectory read from the resources directory (Android/iOS/macOS only)
* @param threadCount max concurrent read thread count
*
* @throw runtime_error with a @ref PackResult string on failure.
* @throw Error with a @ref PackResult string on failure.
*/
void open(const filesystem::path& filePath, bool isResourcesDirectory = true,
uint32_t threadCount = thread::hardware_concurrency())
Expand All @@ -102,7 +100,7 @@ class Reader final
auto path = filePath.generic_string();
auto result = createFilePackReader(path.c_str(), isResourcesDirectory, threadCount, &instance);
if (result != SUCCESS_PACK_RESULT)
throw runtime_error(packResultToString(result) + (", path: " + filePath.generic_string()));
throw Error(packResultToString(result));
}

/**
Expand Down Expand Up @@ -149,15 +147,15 @@ class Reader final
*
* @param[in] path item path string used to pack the file
* @return The item index in the Pack.
* @throw runtime_error with a @ref PackResult string on failure.
* @throw Error if item does not exist.
*/
uint64_t getItemIndex(const filesystem::path& path) const
{
uint64_t index;
auto _path = path.generic_string();
auto result = getPackItemIndex(instance, _path.c_str(), &index);
if (!result)
throw runtime_error("Item is not exist");
throw Error("Item does not exist");
return index;
}

Expand Down Expand Up @@ -193,13 +191,13 @@ class Reader final
* @param[out] buffer pointer to the buffer where to read item data
* @param threadIndex current thread index or 0
*
* @throw runtime_error with a @ref PackResult string on failure.
* @throw Error with a @ref PackResult string on failure.
*/
void readItemData(uint64_t itemIndex, uint8_t* buffer, uint32_t threadIndex = 0) const
{
auto result = readPackItemData(instance, itemIndex, buffer, threadIndex);
if (result != SUCCESS_PACK_RESULT)
throw runtime_error(packResultToString(result) + (", index: " + to_string(itemIndex)));
throw Error(packResultToString(result));
}

/**
Expand All @@ -210,14 +208,14 @@ class Reader final
* @param[out] buffer reference to the buffer where to read item data
* @param threadIndex current thread index or 0
*
* @throw runtime_error with a @ref PackResult string on failure.
* @throw Error with a @ref PackResult string on failure.
*/
void readItemData(uint64_t itemIndex, vector<uint8_t>& buffer, uint32_t threadIndex = 0) const
{
buffer.resize(getPackItemDataSize(instance, itemIndex));
auto result = readPackItemData(instance, itemIndex, buffer.data(), threadIndex);
if (result != SUCCESS_PACK_RESULT)
throw runtime_error(packResultToString(result) + (", index: " + to_string(itemIndex)));
throw Error(packResultToString(result));
}

/**
Expand All @@ -228,15 +226,15 @@ class Reader final
* @param[out] buffer reference to the buffer where to read item data
* @param threadIndex current thread index or 0
*
* @throw runtime_error with a @ref PackResult string on failure.
* @throw Error with a @ref PackResult string on failure.
*/
void readItemData(const filesystem::path& path, vector<uint8_t>& buffer, uint32_t threadIndex = 0) const
{
auto itemIndex = getItemIndex(path);
buffer.resize(getPackItemDataSize(instance, itemIndex));
auto result = readPackItemData(instance, itemIndex, buffer.data(), threadIndex);
if (result != SUCCESS_PACK_RESULT)
throw runtime_error(packResultToString(result) + (", path: " + path.generic_string()));
throw Error(packResultToString(result));
}

/*******************************************************************************************************************
Expand Down Expand Up @@ -289,14 +287,14 @@ class Reader final
* @param[in] filePath target Pack file path string
* @param printProgress output unpacking progress to the stdout
*
* @throw runtime_error with a @ref PackResult string on failure.
* @throw Error with a @ref PackResult string on failure.
*/
static void unpack(const filesystem::path& filePath, bool printProgress = false)
{
auto path = filePath.generic_string();
auto result = unpackFiles(path.c_str(), printProgress);
if (result != SUCCESS_PACK_RESULT)
throw runtime_error(packResultToString(result) + (", path: " + filePath.generic_string()));
throw Error(packResultToString(result));
}
};

Expand Down
9 changes: 4 additions & 5 deletions wrappers/cpp/pack/writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
**********************************************************************************************************************/

#pragma once
#include <string>
#include <exception>
#include "pack/error.hpp"
#include <filesystem>

extern "C"
Expand All @@ -31,8 +30,6 @@ extern "C"
namespace pack
{

using namespace std;

/**
* @brief Pack writer functions.
* @details See the @ref writer.h
Expand All @@ -51,6 +48,8 @@ class Writer final
* @param printProgress output packing progress to the stdout
* @param[in] onPackFile file packing callback, or NULL
* @param[in] argument file packing callback argument, or NULL
*
* @throw Error with a @ref PackResult string on failure.
*/
static void pack(const filesystem::path& packPath,
uint64_t fileCount, const char** fileItemPaths,
Expand All @@ -61,7 +60,7 @@ class Writer final
auto result = packFiles(path.c_str(), fileCount, fileItemPaths,
zipThreshold, printProgress, onPackFile, argument);
if (result != SUCCESS_PACK_RESULT)
throw runtime_error(packResultToString(result));
throw Error(packResultToString(result));
}
};

Expand Down

0 comments on commit 885114a

Please sign in to comment.