Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/cpp/misc/helpers.cpp

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/nit I feel like now when we have opened that flood gate of "util" or "helper" place we can drop here bunch stuff from other places.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which is a good thing, right?
rawtoaces_util is a public target containing image conversion functionality (poorly named in my opinion). The whole reason for adding helpers was that it's not util.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which is a good thing, right?
yeah. I meant it as a good think. Though, util and helper are all similar in my mind. Anyway, good. Let's move some more here.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Contributors to the rawtoaces Project.

#include "helpers.h"

#include <charconv>

namespace rta
{
namespace helpers
{

bool parse_float( const std::string &str, float &out )
{
auto [ptr, ec] =
std::from_chars( str.data(), str.data() + str.size(), out );
return ec == std::errc();
}

} // namespace helpers
} // namespace rta
24 changes: 24 additions & 0 deletions src/cpp/misc/helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Contributors to the rawtoaces Project.

#pragma once

#include <string>

namespace rta
{
namespace helpers
{

/// Parse a float from a string without throwing.
///
/// Unlike `std::stof`, this never throws: a non-numeric input simply
/// returns `false`.
///
/// @param str String to parse.
/// @param out Destination filled on success.
/// @return `true` if a float could be parsed from the start of `str`.
bool parse_float( const std::string &str, float &out );

} // namespace helpers
} // namespace rta
2 changes: 2 additions & 0 deletions src/cpp/rawtoaces_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ add_library( ${RAWTOACES_CORE_LIB} ${DO_SHARED}
rawtoaces_core.cpp
spectral_data.cpp
core_math.cpp
../misc/helpers.cpp

# Make the headers visible in IDEs. This should not affect the builds.
${CORE_PUBLIC_HEADER}
rawtoaces_core_priv.h
define.h
core_math.h
core_math_priv.h
../misc/helpers.h
)

target_link_libraries(
Expand Down
13 changes: 12 additions & 1 deletion src/cpp/rawtoaces_core/spectral_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright Contributors to the rawtoaces Project.

#include <rawtoaces/spectral_data.h>
#include "../misc/helpers.h"

#include <assert.h>
#include <fstream>
Expand Down Expand Up @@ -278,7 +279,17 @@ bool SpectralData::load(
for ( auto &[bin_wavelength, bin_values]:
spectral_data[set_name].items() )
{
float this_wavelength = std::stof( bin_wavelength );
float this_wavelength = 0.0f;
if ( !rta::helpers::parse_float(
bin_wavelength, this_wavelength ) )
{
if ( error_message != nullptr )
{
*error_message = "Error: Failed to parse wavelength '" +
bin_wavelength + "' in " + path + ".";
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you reckon this error handling and error adding can be integrated into helper ... I guess not, you'd need to pass error message in as a ref. and all that. ignore me.

return false;
}

if ( prev_wavelength != -1 )
{
Expand Down
2 changes: 2 additions & 0 deletions src/cpp/rawtoaces_util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ add_library ( ${RAWTOACES_UTIL_LIB} ${DO_SHARED}
exiftool.h
rawtoaces_util_priv.h
attributes.h
../misc/helpers.cpp
../misc/helpers.h

$<$<BOOL:${RTA_ENABLE_LENSFUN}>:
lens_correction.cpp
Expand Down
25 changes: 18 additions & 7 deletions src/cpp/rawtoaces_util/exiftool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright Contributors to the rawtoaces Project.

#include "exiftool.h"
#include "../misc/helpers.h"

#include <cerrno>
#include <cstdio>
Expand Down Expand Up @@ -314,18 +315,24 @@ bool fetch_metadata(
{
if ( exiftool_key == "FocusDistance" )
{
do_focus_distance = true;
focus_distance = std::stof( value );
if ( rta::helpers::parse_float( value, focus_distance ) )
{
do_focus_distance = true;
}
}
else if ( exiftool_key == "FocusDistanceUpper" )
{
do_focus_distance = true;
focus_distance_upper = std::stof( value );
if ( rta::helpers::parse_float( value, focus_distance_upper ) )
{
do_focus_distance = true;
}
}
else if ( exiftool_key == "FocusDistanceLower" )
{
do_focus_distance = true;
focus_distance_lower = std::stof( value );
if ( rta::helpers::parse_float( value, focus_distance_lower ) )
{
do_focus_distance = true;
}
}
else if ( exiftool_key != "FileName" )
{
Expand All @@ -337,7 +344,11 @@ bool fetch_metadata(

if ( to_float )
{
spec[oiio_key] = std::stof( value );
float parsed = 0.0f;
if ( rta::helpers::parse_float( value, parsed ) )
{
spec[oiio_key] = parsed;
}
}
else
{
Expand Down
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ add_test ( NAME Test_Logic COMMAND Test_Logic )
add_executable (
Test_Misc
testMisc.cpp
../../src/cpp/misc/helpers.cpp
../../src/cpp/misc/helpers.h
)

target_link_libraries(
Expand All @@ -210,6 +212,8 @@ add_executable (
test_utils.cpp
test_data_helpers.cpp
test_data_helpers.h
../../src/cpp/misc/helpers.cpp
../../src/cpp/misc/helpers.h
)

target_link_libraries(
Expand Down
27 changes: 27 additions & 0 deletions tests/testMisc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <filesystem>

#include "../src/cpp/rawtoaces_core/define.h"
#include "../src/cpp/misc/helpers.h"

#define DATA_PATH "../_deps/rawtoaces_data-src/data/"

Expand Down Expand Up @@ -111,13 +112,39 @@ void test_IsValidCT()
OIIO_CHECK_EQUAL( false, isValidCT( val14 ) );
}

void test_parse_float()
{
float out = 0.0f;

// Normal decimal value
OIIO_CHECK_EQUAL( true, rta::helpers::parse_float( "1.5", out ) );
OIIO_CHECK_EQUAL( 1.5f, out );

// Integer string
OIIO_CHECK_EQUAL( true, rta::helpers::parse_float( "100", out ) );
OIIO_CHECK_EQUAL( 100.0f, out );

// Negative value
OIIO_CHECK_EQUAL( true, rta::helpers::parse_float( "-2.25", out ) );
OIIO_CHECK_EQUAL( -2.25f, out );

// Prefix parse accepted (matches std::stof behaviour): "100/1" yields 100
OIIO_CHECK_EQUAL( true, rta::helpers::parse_float( "100/1", out ) );
OIIO_CHECK_EQUAL( 100.0f, out );

// Non-numeric input returns false without throwing
OIIO_CHECK_EQUAL( false, rta::helpers::parse_float( "abc", out ) );
OIIO_CHECK_EQUAL( false, rta::helpers::parse_float( "", out ) );
}

int main( int, char ** )
{
test_OpenDir();
test_LowerCase();
test_IsNumeric();
test_IsCTLetterDigit();
test_IsValidCT();
test_parse_float();

return unit_test_failures;
}
14 changes: 4 additions & 10 deletions tests/testUsageTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <OpenImageIO/unittest.h>

#include <rawtoaces/usage_timer.h>
#include "../../../src/cpp/misc/helpers.h"

#include <chrono>
#include <iostream>
Expand Down Expand Up @@ -35,16 +36,9 @@ float extractTimeFromOutput( const std::string &output )

if ( !timeStr.empty() )
{
try
{
return std::stof( timeStr );
}
catch ( const std::exception &e )
{
// always fails
OIIO_CHECK_EQUAL(
"Failed to parse time value: " + timeStr, e.what() );
}
float parsed = 0.0f;
if ( rta::helpers::parse_float( timeStr, parsed ) )
return parsed;
}
}
}
Expand Down
Loading