Skip to content

Commit

Permalink
Added mergePaths functions and made functions in Utils.hpp static inline
Browse files Browse the repository at this point in the history
  • Loading branch information
oruebel committed Sep 21, 2024
1 parent f99ed91 commit 6a4876f
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions src/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace AQNWB
* @brief Generates a UUID (Universally Unique Identifier) as a string.
* @return The generated UUID as a string.
*/
inline std::string generateUuid()
static inline std::string generateUuid()
{
boost::uuids::uuid uuid = boost::uuids::random_generator()();
std::string uuidStr = boost::uuids::to_string(uuid);
Expand All @@ -37,7 +37,7 @@ inline std::string generateUuid()
* @brief Get the current time in ISO 8601 format with the UTC offset.
* @return The current time as a string in ISO 8601 format.
*/
inline std::string getCurrentTime()
static inline std::string getCurrentTime()
{
// Set up boost time zone adjustment and time facet
using local_adj =
Expand Down Expand Up @@ -65,8 +65,8 @@ inline std::string getCurrentTime()
* @brief Factory method to create an IO object.
* @return A pointer to a BaseIO object
*/
inline std::shared_ptr<IO::BaseIO> createIO(const std::string& type,
const std::string& filename)
static inline std::shared_ptr<IO::BaseIO> createIO(const std::string& type,
const std::string& filename)
{
if (type == "HDF5") {
return std::make_shared<AQNWB::IO::HDF5::HDF5IO>(filename);
Expand All @@ -75,6 +75,40 @@ inline std::shared_ptr<IO::BaseIO> createIO(const std::string& type,
}
}

/**
* @brief Merge two paths into a single path, handling extra trailing and
* starting "/".
*
* This function is useful for constructing paths in HDF5 files, where paths
* always use "/".
*
* @param path1 The first path to merge.
* @param path2 The second path to merge.
*
* @return The merged path with no extra "/" at the start or end, and exactly
* one "/" between the two input paths.
*/
static inline std::string mergePaths(const std::string& path1,
const std::string& path2)
{
std::string result = path1;
// Remove trailing "/" from path1
while (!result.empty() && result.back() == '/') {
result.pop_back();
}
// Remove leading "/" from path2
size_t start = 0;
while (start < path2.size() && path2[start] == '/') {
start++;
}
// Append path2 to path1 with a "/" in between
if (!result.empty()) {
result += '/';
}
result += path2.substr(start);
return result;
}

/**
* @brief Method to convert float values to uint16 values. This method
* was adapted from JUCE AudioDataConverters using a default value of
Expand All @@ -83,9 +117,9 @@ inline std::shared_ptr<IO::BaseIO> createIO(const std::string& type,
* @param dest The destination for the converted uint16 data
* @param numSamples The number of samples to convert
*/
inline void convertFloatToInt16LE(const float* source,
void* dest,
int numSamples)
static inline void convertFloatToInt16LE(const float* source,
void* dest,
int numSamples)
{
// TODO - several steps in this function may be unnecessary for our use
// case. Consider simplifying the intermediate cast to char and the
Expand All @@ -109,9 +143,8 @@ inline void convertFloatToInt16LE(const float* source,
* @param conversion_factor The conversion factor to scale the data
* @param data The data to convert
*/
inline std::unique_ptr<int16_t[]> transformToInt16(SizeType numSamples,
float conversion_factor,
const float* data)
static inline std::unique_ptr<int16_t[]> transformToInt16(
SizeType numSamples, float conversion_factor, const float* data)
{
std::unique_ptr<float[]> scaledData = std::make_unique<float[]>(numSamples);
std::unique_ptr<int16_t[]> intData = std::make_unique<int16_t[]>(numSamples);
Expand Down

0 comments on commit 6a4876f

Please sign in to comment.