pgbackrest.git / summary / log / commit / refs

commit    5d34bf3f38ff8ce3f88c8aed6c6ff01eb929d62b
Author:   David Steele <david@pgmasters.net>
Date:     Wed Jan 27 16:50:10 2021 +0000

    Move cvtDoubleToStr() to strNewDbl().
    
    This is a more logical location and it reduces the dependencies required to compile the common/convert module.


src/command/archive/push/push.c | 2 +- src/command/command.c | 3 +-- src/command/help/help.c | 2 +- src/common/type/convert.c | 15 +-------------- src/common/type/convert.h | 9 ++++----- src/common/type/string.c | 14 ++++++++++++++ src/common/type/string.h | 3 +++ src/config/exec.c | 2 +- test/define.yaml | 29 ++++++++++++++--------------- test/src/module/common/typeConvertTest.c | 4 +--- test/src/module/common/typeStringTest.c | 7 ++++++- 11 files changed, 47 insertions(+), 43 deletions(-) diff --git a/src/command/archive/push/push.c b/src/command/archive/push/push.c index af6ec2b29..bf7e08437 100644 --- a/src/command/archive/push/push.c +++ b/src/command/archive/push/push.c @@ -356,7 +356,7 @@ cmdArchivePush(void) { THROW_FMT( ArchiveTimeoutError, "unable to push WAL file '%s' to the archive asynchronously after %s second(s)", - strZ(archiveFile), strZ(cvtDoubleToStr((double)cfgOptionInt64(cfgOptArchiveTimeout) / MSEC_PER_SEC))); + strZ(archiveFile), strZ(strNewDbl((double)cfgOptionInt64(cfgOptArchiveTimeout) / MSEC_PER_SEC))); } // Log success diff --git a/src/command/command.c b/src/command/command.c index 115fabed2..856d6f1a4 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -130,8 +130,7 @@ cmdOption(void) else if (cfgParseOptionType(optionId) == cfgOptTypeTime) { valueList = strLstNew(); - strLstAdd( - valueList, cvtDoubleToStr((double)cfgOptionIdxInt64(optionId, optionIdx) / MSEC_PER_SEC)); + strLstAdd(valueList, strNewDbl((double)cfgOptionIdxInt64(optionId, optionIdx) / MSEC_PER_SEC)); } // Else only one value else diff --git a/src/command/help/help.c b/src/command/help/help.c index 63c4b61af..7cb6cc63e 100644 --- a/src/command/help/help.c +++ b/src/command/help/help.c @@ -134,7 +134,7 @@ helpRenderValue(const Variant *value, ConfigOptionType type) result = resultTemp; } else if (type == cfgOptTypeTime) - result = cvtDoubleToStr((double)varInt64(value) / MSEC_PER_SEC); + result = strNewDbl((double)varInt64(value) / MSEC_PER_SEC); else result = varStrForce(value); } diff --git a/src/common/type/convert.c b/src/common/type/convert.c index 192982b7c..b06fa2f3a 100644 --- a/src/common/type/convert.c +++ b/src/common/type/convert.c @@ -1,5 +1,5 @@ /*********************************************************************************************************************************** -Convert Base Data Types +Convert C Types ***********************************************************************************************************************************/ #include "build.auto.h" @@ -180,19 +180,6 @@ cvtDoubleToZ(double value, char *buffer, size_t bufferSize) FUNCTION_TEST_RETURN((size_t)(end - buffer + 1)); } -String *cvtDoubleToStr(double value) -{ - FUNCTION_TEST_BEGIN(); - FUNCTION_TEST_PARAM(DOUBLE, value); - FUNCTION_TEST_END(); - - char working[CVT_BASE10_BUFFER_SIZE]; - - cvtDoubleToZ(value, working, sizeof(working)); - - FUNCTION_TEST_RETURN(strNew(working)); -} - double cvtZToDouble(const char *value) { diff --git a/src/common/type/convert.h b/src/common/type/convert.h index 0010d7d05..a8cb01aa5 100644 --- a/src/common/type/convert.h +++ b/src/common/type/convert.h @@ -1,5 +1,7 @@ /*********************************************************************************************************************************** -Convert Base Data Types +Convert C Types + +Contains conversions to/from native C types. Conversions of project types should be defined in their respective type modules. ***********************************************************************************************************************************/ #ifndef COMMON_TYPE_CONVERT_H #define COMMON_TYPE_CONVERT_H @@ -8,8 +10,6 @@ Convert Base Data Types #include <stdbool.h> #include <sys/types.h> -#include "common/type/string.h" - /*********************************************************************************************************************************** Required buffer sizes ***********************************************************************************************************************************/ @@ -22,9 +22,8 @@ Functions // Convert char to zero-terminated string size_t cvtCharToZ(char value, char *buffer, size_t bufferSize); -// Convert double to zero-terminated string (or String) and vice versa +// Convert double to zero-terminated string and vice versa size_t cvtDoubleToZ(double value, char *buffer, size_t bufferSize); -String *cvtDoubleToStr(double value); double cvtZToDouble(const char *value); // Convert int to zero-terminated string and vice versa diff --git a/src/common/type/string.c b/src/common/type/string.c index 6fe951894..557d981ba 100644 --- a/src/common/type/string.c +++ b/src/common/type/string.c @@ -94,6 +94,20 @@ strNew(const char *string) FUNCTION_TEST_RETURN(this); } +/**********************************************************************************************************************************/ +String *strNewDbl(double value) +{ + FUNCTION_TEST_BEGIN(); + FUNCTION_TEST_PARAM(DOUBLE, value); + FUNCTION_TEST_END(); + + char working[CVT_BASE10_BUFFER_SIZE]; + + cvtDoubleToZ(value, working, sizeof(working)); + + FUNCTION_TEST_RETURN(strNew(working)); +} + /**********************************************************************************************************************************/ String * strNewBuf(const Buffer *buffer) diff --git a/src/common/type/string.h b/src/common/type/string.h index 7b59dc805..e7fd910da 100644 --- a/src/common/type/string.h +++ b/src/common/type/string.h @@ -64,6 +64,9 @@ String *strNew(const char *string); // but only the data before the NULL character will be used as a string. String *strNewBuf(const Buffer *buffer); +// Create a new string by converting the double value +String *strNewDbl(double value); + // Create a new string from a format string with parameters (i.e. sprintf) String *strNewFmt(const char *format, ...) __attribute__((format(printf, 1, 2))); diff --git a/src/config/exec.c b/src/config/exec.c index 27ff99b45..a0d6eccc5 100644 --- a/src/config/exec.c +++ b/src/config/exec.c @@ -113,7 +113,7 @@ cfgExecParam(ConfigCommand commandId, ConfigCommandRole commandRoleId, const Key else if (cfgParseOptionType(optionId) == cfgOptTypeTime) { valueList = strLstNew(); - strLstAdd(valueList, cvtDoubleToStr((double)varInt64(value) / MSEC_PER_SEC)); + strLstAdd(valueList, strNewDbl((double)varInt64(value) / MSEC_PER_SEC)); } // Else only one value else diff --git a/test/define.yaml b/test/define.yaml index a447f0e62..a53990ed2 100644 --- a/test/define.yaml +++ b/test/define.yaml @@ -51,16 +51,8 @@ unit: depend: - common/debug - - common/type/convert - common/memContext - common/stackTrace - - common/type/buffer - - common/type/keyValue - - common/type/list - - common/type/string - - common/type/stringList - - common/type/variant - - common/type/variantList # ---------------------------------------------------------------------------------------------------------------------------- - name: stack-trace @@ -69,6 +61,13 @@ unit: coverage: - common/stackTrace + # ---------------------------------------------------------------------------------------------------------------------------- + - name: type-convert + total: 11 + + coverage: + - common/type/convert + # ---------------------------------------------------------------------------------------------------------------------------- - name: assert-off total: 1 @@ -105,13 +104,6 @@ unit: depend: - common/type/convert - # ---------------------------------------------------------------------------------------------------------------------------- - - name: type-convert - total: 11 - - coverage: - - common/type/convert - # ---------------------------------------------------------------------------------------------------------------------------- - name: time total: 3 @@ -128,6 +120,13 @@ unit: - common/type/string - common/type/stringList + depend: + - common/type/buffer + - common/type/keyValue + - common/type/list + - common/type/variant + - common/type/variantList + # ---------------------------------------------------------------------------------------------------------------------------- - name: type-list total: 4 diff --git a/test/src/module/common/typeConvertTest.c b/test/src/module/common/typeConvertTest.c index 8700c6784..975e140e7 100644 --- a/test/src/module/common/typeConvertTest.c +++ b/test/src/module/common/typeConvertTest.c @@ -1,5 +1,5 @@ /*********************************************************************************************************************************** -Test Convert Base Data Types +Test Convert C Types ***********************************************************************************************************************************/ #include "common/stackTrace.h" @@ -45,8 +45,6 @@ testRun(void) TEST_RESULT_UINT(cvtDoubleToZ(999.1234, buffer, STACK_TRACE_PARAM_MAX), 8, "convert double to string"); TEST_RESULT_Z(buffer, "999.1234", " check buffer"); - TEST_RESULT_STR_Z(cvtDoubleToStr(999.1), "999.1", "convert double to string"); - TEST_RESULT_UINT(cvtDoubleToZ(999999999.123456, buffer, STACK_TRACE_PARAM_MAX), 16, "convert double to string"); TEST_RESULT_Z(buffer, "999999999.123456", " check buffer"); diff --git a/test/src/module/common/typeStringTest.c b/test/src/module/common/typeStringTest.c index 0e9cec3f2..b1dce06b8 100644 --- a/test/src/module/common/typeStringTest.c +++ b/test/src/module/common/typeStringTest.c @@ -15,7 +15,7 @@ testRun(void) FUNCTION_HARNESS_VOID(); // ***************************************************************************************************************************** - if (testBegin("strNew(), strNewBuf(), strNewN(), strEmpty(), strZ(), strZNull(), strSize(), and strFree()")) + if (testBegin("strNew(), strNewBuf(), strNewDbl(), strNewN(), strEmpty(), strZ(), strZNull(), strSize(), and strFree()")) { // We don't want this struct to grow since there are generally a lot of strings, so make sure it doesn't grow without us // knowing about it @@ -44,6 +44,11 @@ testRun(void) TEST_RESULT_STR_Z(strNewBuf(buffer), "12345678", "new string from buffer"); + // ------------------------------------------------------------------------------------------------------------------------- + TEST_TITLE("new from double"); + + TEST_RESULT_STR_Z(strNewDbl(999.1), "999.1", "new"); + // ------------------------------------------------------------------------------------------------------------------------- string = strNewFmt("formatted %s %04d", "string", 1); TEST_RESULT_STR_Z(string, "formatted string 0001", "new with formatted string"); [parent: 87eb081a8f7b]