1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-07-17 01:12:23 +02:00

Move cvtDoubleToStr() to strNewDbl().

This is a more logical location and it reduces the dependencies required to compile the common/convert module.
This commit is contained in:
David Steele
2021-01-27 11:50:10 -05:00
parent 87eb081a8f
commit 5d34bf3f38
11 changed files with 47 additions and 43 deletions

View File

@ -356,7 +356,7 @@ cmdArchivePush(void)
{ {
THROW_FMT( THROW_FMT(
ArchiveTimeoutError, "unable to push WAL file '%s' to the archive asynchronously after %s second(s)", 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 // Log success

View File

@ -130,8 +130,7 @@ cmdOption(void)
else if (cfgParseOptionType(optionId) == cfgOptTypeTime) else if (cfgParseOptionType(optionId) == cfgOptTypeTime)
{ {
valueList = strLstNew(); valueList = strLstNew();
strLstAdd( strLstAdd(valueList, strNewDbl((double)cfgOptionIdxInt64(optionId, optionIdx) / MSEC_PER_SEC));
valueList, cvtDoubleToStr((double)cfgOptionIdxInt64(optionId, optionIdx) / MSEC_PER_SEC));
} }
// Else only one value // Else only one value
else else

View File

@ -134,7 +134,7 @@ helpRenderValue(const Variant *value, ConfigOptionType type)
result = resultTemp; result = resultTemp;
} }
else if (type == cfgOptTypeTime) else if (type == cfgOptTypeTime)
result = cvtDoubleToStr((double)varInt64(value) / MSEC_PER_SEC); result = strNewDbl((double)varInt64(value) / MSEC_PER_SEC);
else else
result = varStrForce(value); result = varStrForce(value);
} }

View File

@ -1,5 +1,5 @@
/*********************************************************************************************************************************** /***********************************************************************************************************************************
Convert Base Data Types Convert C Types
***********************************************************************************************************************************/ ***********************************************************************************************************************************/
#include "build.auto.h" #include "build.auto.h"
@ -180,19 +180,6 @@ cvtDoubleToZ(double value, char *buffer, size_t bufferSize)
FUNCTION_TEST_RETURN((size_t)(end - buffer + 1)); 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 double
cvtZToDouble(const char *value) cvtZToDouble(const char *value)
{ {

View File

@ -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 #ifndef COMMON_TYPE_CONVERT_H
#define COMMON_TYPE_CONVERT_H #define COMMON_TYPE_CONVERT_H
@ -8,8 +10,6 @@ Convert Base Data Types
#include <stdbool.h> #include <stdbool.h>
#include <sys/types.h> #include <sys/types.h>
#include "common/type/string.h"
/*********************************************************************************************************************************** /***********************************************************************************************************************************
Required buffer sizes Required buffer sizes
***********************************************************************************************************************************/ ***********************************************************************************************************************************/
@ -22,9 +22,8 @@ Functions
// Convert char to zero-terminated string // Convert char to zero-terminated string
size_t cvtCharToZ(char value, char *buffer, size_t bufferSize); 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); size_t cvtDoubleToZ(double value, char *buffer, size_t bufferSize);
String *cvtDoubleToStr(double value);
double cvtZToDouble(const char *value); double cvtZToDouble(const char *value);
// Convert int to zero-terminated string and vice versa // Convert int to zero-terminated string and vice versa

View File

@ -94,6 +94,20 @@ strNew(const char *string)
FUNCTION_TEST_RETURN(this); 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 * String *
strNewBuf(const Buffer *buffer) strNewBuf(const Buffer *buffer)

View File

@ -64,6 +64,9 @@ String *strNew(const char *string);
// but only the data before the NULL character will be used as a string. // but only the data before the NULL character will be used as a string.
String *strNewBuf(const Buffer *buffer); 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) // Create a new string from a format string with parameters (i.e. sprintf)
String *strNewFmt(const char *format, ...) __attribute__((format(printf, 1, 2))); String *strNewFmt(const char *format, ...) __attribute__((format(printf, 1, 2)));

View File

@ -113,7 +113,7 @@ cfgExecParam(ConfigCommand commandId, ConfigCommandRole commandRoleId, const Key
else if (cfgParseOptionType(optionId) == cfgOptTypeTime) else if (cfgParseOptionType(optionId) == cfgOptTypeTime)
{ {
valueList = strLstNew(); 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 only one value
else else

View File

@ -51,16 +51,8 @@ unit:
depend: depend:
- common/debug - common/debug
- common/type/convert
- common/memContext - common/memContext
- common/stackTrace - 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 - name: stack-trace
@ -69,6 +61,13 @@ unit:
coverage: coverage:
- common/stackTrace - common/stackTrace
# ----------------------------------------------------------------------------------------------------------------------------
- name: type-convert
total: 11
coverage:
- common/type/convert
# ---------------------------------------------------------------------------------------------------------------------------- # ----------------------------------------------------------------------------------------------------------------------------
- name: assert-off - name: assert-off
total: 1 total: 1
@ -105,13 +104,6 @@ unit:
depend: depend:
- common/type/convert - common/type/convert
# ----------------------------------------------------------------------------------------------------------------------------
- name: type-convert
total: 11
coverage:
- common/type/convert
# ---------------------------------------------------------------------------------------------------------------------------- # ----------------------------------------------------------------------------------------------------------------------------
- name: time - name: time
total: 3 total: 3
@ -128,6 +120,13 @@ unit:
- common/type/string - common/type/string
- common/type/stringList - common/type/stringList
depend:
- common/type/buffer
- common/type/keyValue
- common/type/list
- common/type/variant
- common/type/variantList
# ---------------------------------------------------------------------------------------------------------------------------- # ----------------------------------------------------------------------------------------------------------------------------
- name: type-list - name: type-list
total: 4 total: 4

View File

@ -1,5 +1,5 @@
/*********************************************************************************************************************************** /***********************************************************************************************************************************
Test Convert Base Data Types Test Convert C Types
***********************************************************************************************************************************/ ***********************************************************************************************************************************/
#include "common/stackTrace.h" #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_UINT(cvtDoubleToZ(999.1234, buffer, STACK_TRACE_PARAM_MAX), 8, "convert double to string");
TEST_RESULT_Z(buffer, "999.1234", " check buffer"); 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_UINT(cvtDoubleToZ(999999999.123456, buffer, STACK_TRACE_PARAM_MAX), 16, "convert double to string");
TEST_RESULT_Z(buffer, "999999999.123456", " check buffer"); TEST_RESULT_Z(buffer, "999999999.123456", " check buffer");

View File

@ -15,7 +15,7 @@ testRun(void)
FUNCTION_HARNESS_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 // 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 // knowing about it
@ -44,6 +44,11 @@ testRun(void)
TEST_RESULT_STR_Z(strNewBuf(buffer), "12345678", "new string from buffer"); 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); string = strNewFmt("formatted %s %04d", "string", 1);
TEST_RESULT_STR_Z(string, "formatted string 0001", "new with formatted string"); TEST_RESULT_STR_Z(string, "formatted string 0001", "new with formatted string");