From d1d6129c10ffca72cffededbf54605c97aefae80 Mon Sep 17 00:00:00 2001 From: dfblackburn Date: Thu, 21 Oct 2021 12:12:16 -0400 Subject: [PATCH 1/8] Add support for strptime in gcc 6 --- time.cpp | 57 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/time.cpp b/time.cpp index 221e904..bf1db87 100644 --- a/time.cpp +++ b/time.cpp @@ -1,27 +1,30 @@ - -#include -#ifdef __cplusplus - extern "C" - { -#endif -#include "StringExt.h" -#ifdef __cplusplus - }; -#endif - -#include "string.h" -#include "time.h" - -unsigned long stringfTime(unsigned long dest, unsigned long destSize, unsigned long format, DATE_AND_TIME time) { - if(!dest || !destSize || !format) return 0; // Return if null pointers or if destSize is 0 - tm t = {}; - gmtime_r((time_t*)&time, &t); - return strftime((char*)dest, destSize, (char*)format, &t); -} - -DATE_AND_TIME stringpTime(unsigned long src, unsigned long format) { - if(!src || !format) return 0; // Return if null pointers - tm t = {}; - strptime((char*)src, (char*)format, &t); - return mktime(&t); -} + +#include +#ifdef __cplusplus + extern "C" + { +#endif +#include "StringExt.h" +#ifdef __cplusplus + }; +#endif + +#include "string.h" + +#define __XSI_VISIBLE 1 +#include "time.h" + + +unsigned long stringfTime(unsigned long dest, unsigned long destSize, unsigned long format, DATE_AND_TIME time) { + if(!dest || !destSize || !format) return 0; // Return if null pointers or if destSize is 0 + tm t = {}; + gmtime_r((time_t*)&time, &t); + return strftime((char*)dest, destSize, (char*)format, &t); +} + +DATE_AND_TIME stringpTime(unsigned long src, unsigned long format) { + if(!src || !format) return 0; // Return if null pointers + tm t = {}; + strptime((char*)src, (char*)format, &t); + return mktime(&t); +} From 8f05eb8824f75fe47015b74a5a2daccb285e4349 Mon Sep 17 00:00:00 2001 From: sclaiborne Date: Tue, 26 Oct 2021 15:57:36 -0700 Subject: [PATCH 2/8] First pass at supporting compiling without BR --- AsTimeWrapper.h | 365 ++++++++++++++++++++++++++++++++++++++++++ CMakeLists.txt | 21 +++ FormatString.c | 5 + GenerateTimestamp.c | 5 + GenerateTimestampMS.c | 3 + NOT_BR.h | 8 + 6 files changed, 407 insertions(+) create mode 100644 AsTimeWrapper.h create mode 100644 CMakeLists.txt create mode 100644 NOT_BR.h diff --git a/AsTimeWrapper.h b/AsTimeWrapper.h new file mode 100644 index 0000000..d99f9bc --- /dev/null +++ b/AsTimeWrapper.h @@ -0,0 +1,365 @@ +/* Automation Studio generated header file */ +/* Do not edit ! */ + +#ifndef _ASTIME_ +#define _ASTIME_ +#ifdef __cplusplus +extern "C" +{ +#endif + +#include + +#include + +#ifndef _BUR_PUBLIC +#define _BUR_PUBLIC +#endif +/* Constants */ +#ifdef _REPLACE_CONST + #define timERR_AR 33213U + #define timERR_INVALID_DTSTRUCTURE 33212U + #define timERR_INVALID_LEN 33211U + #define timERR_INVALID_PARAMETER 33210U + #define timREDUND_INTERFACE 3U + #define timTIME_SERVER 2U + #define timREAL_TIME_CLOCK 1U + #define timNO_DST 3U + #define timDAYLIGHT_SAVING_TIME 2U + #define timNORMAL_TIME 1U + #define TIME_MAX 2073600000 + #define TIME_MIN (-2073600000) + #define DATE_AND_TIME_MAX 4102444799U + #define timEXSETTIME_NO_OPTION 0U + #define timEXSETTIME_NO_LOGENTRY 1U +#else + #ifndef _GLOBAL_CONST + #define _GLOBAL_CONST _WEAK const + #endif + _GLOBAL_CONST unsigned short timERR_AR; + _GLOBAL_CONST unsigned short timERR_INVALID_DTSTRUCTURE; + _GLOBAL_CONST unsigned short timERR_INVALID_LEN; + _GLOBAL_CONST unsigned short timERR_INVALID_PARAMETER; + _GLOBAL_CONST unsigned char timREDUND_INTERFACE; + _GLOBAL_CONST unsigned char timTIME_SERVER; + _GLOBAL_CONST unsigned char timREAL_TIME_CLOCK; + _GLOBAL_CONST unsigned char timNO_DST; + _GLOBAL_CONST unsigned char timDAYLIGHT_SAVING_TIME; + _GLOBAL_CONST unsigned char timNORMAL_TIME; + _GLOBAL_CONST signed long TIME_MAX; + _GLOBAL_CONST signed long TIME_MIN; + _GLOBAL_CONST unsigned long DATE_AND_TIME_MAX; + _GLOBAL_CONST unsigned char timEXSETTIME_NO_OPTION; + _GLOBAL_CONST unsigned char timEXSETTIME_NO_LOGENTRY; +#endif + + + + +/* Datatypes and datatypes of function blocks */ +typedef struct TIMEStructure +{ signed char day; + unsigned char hour; + unsigned char minute; + unsigned char second; + unsigned short millisec; + unsigned short microsec; +} TIMEStructure; + +typedef struct DTStructure +{ unsigned short year; + unsigned char month; + unsigned char day; + unsigned char wday; + unsigned char hour; + unsigned char minute; + unsigned char second; + unsigned short millisec; + unsigned short microsec; +} DTStructure; + +typedef struct DTExSetTime +{ + /* VAR_INPUT (analog) */ + plcdt DT1; + unsigned char Option; + /* VAR_OUTPUT (analog) */ + unsigned short status; + /* VAR_INPUT (digital) */ + plcbit enable; +} DTExSetTime_typ; + +typedef struct DTSetTime +{ + /* VAR_INPUT (analog) */ + plcdt DT1; + /* VAR_OUTPUT (analog) */ + unsigned short status; + /* VAR_INPUT (digital) */ + plcbit enable; +} DTSetTime_typ; + +typedef struct DTGetTime +{ + /* VAR_OUTPUT (analog) */ + unsigned short status; + plcdt DT1; + /* VAR_INPUT (digital) */ + plcbit enable; +} DTGetTime_typ; + +typedef struct UtcDTGetTime +{ + /* VAR_OUTPUT (analog) */ + unsigned short status; + plcdt DT1; + /* VAR_INPUT (digital) */ + plcbit enable; +} UtcDTGetTime_typ; + +typedef struct UtcDTSetTime +{ + /* VAR_INPUT (analog) */ + plcdt DT1; + /* VAR_OUTPUT (analog) */ + unsigned short status; + /* VAR (analog) */ + unsigned short i_state; + unsigned short i_result; + unsigned long i_tmp; + /* VAR_INPUT (digital) */ + plcbit enable; +} UtcDTSetTime_typ; + +typedef struct UtcDTExSetTime +{ + /* VAR_INPUT (analog) */ + plcdt DT1; + unsigned char Option; + /* VAR_OUTPUT (analog) */ + unsigned short status; + /* VAR (analog) */ + unsigned short i_state; + unsigned short i_result; + unsigned long i_tmp; + /* VAR_INPUT (digital) */ + plcbit enable; +} UtcDTExSetTime_typ; + +typedef struct DTStructureGetTime +{ + /* VAR_INPUT (analog) */ + unsigned long pDTStructure; + /* VAR_OUTPUT (analog) */ + unsigned short status; + /* VAR_INPUT (digital) */ + plcbit enable; +} DTStructureGetTime_typ; + +typedef struct DTStructureSetTime +{ + /* VAR_INPUT (analog) */ + unsigned long pDTStructure; + /* VAR_OUTPUT (analog) */ + unsigned short status; + /* VAR_INPUT (digital) */ + plcbit enable; +} DTStructureSetTime_typ; + +typedef struct DTStructureExSetTime +{ + /* VAR_INPUT (analog) */ + unsigned long pDTStructure; + unsigned char Option; + /* VAR_OUTPUT (analog) */ + unsigned short status; + /* VAR_INPUT (digital) */ + plcbit enable; +} DTStructureExSetTime_typ; + +typedef struct UtcDTStructureGetTime +{ + /* VAR_INPUT (analog) */ + unsigned long pDTStructure; + /* VAR_OUTPUT (analog) */ + unsigned short status; + /* VAR_INPUT (digital) */ + plcbit enable; +} UtcDTStructureGetTime_typ; + +typedef struct UtcDTStructureSetTime +{ + /* VAR_INPUT (analog) */ + unsigned long pDTStructure; + /* VAR_OUTPUT (analog) */ + unsigned short status; + /* VAR (analog) */ + unsigned short i_state; + unsigned short i_result; + unsigned long i_tmp; + /* VAR_INPUT (digital) */ + plcbit enable; +} UtcDTStructureSetTime_typ; + +typedef struct UtcDTStructureExSetTime +{ + /* VAR_INPUT (analog) */ + unsigned long pDTStructure; + unsigned char Option; + /* VAR_OUTPUT (analog) */ + unsigned short status; + /* VAR (analog) */ + unsigned short i_state; + unsigned short i_result; + unsigned long i_tmp; + /* VAR_INPUT (digital) */ + plcbit enable; +} UtcDTStructureExSetTime_typ; + +typedef struct UtcDT_TO_LocalDTStructure +{ + /* VAR_INPUT (analog) */ + plcdt DT1; + unsigned long pDTStructure; + /* VAR_OUTPUT (analog) */ + unsigned short status; + /* VAR (analog) */ + unsigned short i_state; + unsigned short i_result; + unsigned long i_tmp; + /* VAR_INPUT (digital) */ + plcbit enable; +} UtcDT_TO_LocalDTStructure_typ; + +typedef struct LocalDT_TO_UtcDTStructure +{ + /* VAR_INPUT (analog) */ + plcdt DT1; + unsigned long pDTStructure; + /* VAR_OUTPUT (analog) */ + unsigned short status; + /* VAR (analog) */ + unsigned short i_state; + unsigned short i_result; + unsigned long i_tmp; + /* VAR_INPUT (digital) */ + plcbit enable; +} LocalDT_TO_UtcDTStructure_typ; + +typedef struct UtcDTStructure_TO_LocalDT +{ + /* VAR_INPUT (analog) */ + unsigned long pDTStructure; + /* VAR_OUTPUT (analog) */ + plcdt DT1; + unsigned short status; + /* VAR (analog) */ + unsigned short i_state; + unsigned short i_result; + unsigned long i_tmp; + /* VAR_INPUT (digital) */ + plcbit enable; +} UtcDTStructure_TO_LocalDT_typ; + +typedef struct LocalDTStructure_TO_UtcDT +{ + /* VAR_INPUT (analog) */ + unsigned long pDTStructure; + /* VAR_OUTPUT (analog) */ + plcdt DT1; + unsigned short status; + /* VAR (analog) */ + unsigned short i_state; + unsigned short i_result; + unsigned long i_tmp; + /* VAR_INPUT (digital) */ + plcbit enable; +} LocalDTStructure_TO_UtcDT_typ; + +typedef struct TimeDeviceGetInfo +{ + /* VAR_INPUT (analog) */ + unsigned long pServer; + unsigned char len; + /* VAR_OUTPUT (analog) */ + unsigned char timeDevice; + float avgDeviation; + unsigned short status; + /* VAR (analog) */ + unsigned short i_state; + unsigned short i_result; + unsigned long i_tmp; + /* VAR_INPUT (digital) */ + plcbit enable; +} TimeDeviceGetInfo_typ; + +typedef struct DstGetInfo +{ + /* VAR_INPUT (analog) */ + unsigned long pDTStructure; + /* VAR_OUTPUT (analog) */ + unsigned char dstState; + unsigned short status; + /* VAR (analog) */ + unsigned short i_state; + unsigned short i_result; + unsigned long i_tmp; + /* VAR_INPUT (digital) */ + plcbit enable; +} DstGetInfo_typ; + +typedef struct DstGetInfoDT +{ + /* VAR_INPUT (analog) */ + plcdt DT1; + /* VAR_OUTPUT (analog) */ + unsigned char dstState; + unsigned short status; + /* VAR (analog) */ + unsigned short i_state; + unsigned short i_result; + unsigned long i_tmp; + /* VAR_INPUT (digital) */ + plcbit enable; +} DstGetInfoDT_typ; + + + +/* Prototyping of functions and function blocks */ +_BUR_PUBLIC void DTExSetTime(struct DTExSetTime* inst); +_BUR_PUBLIC void DTSetTime(struct DTSetTime* inst); +_BUR_PUBLIC void DTGetTime(struct DTGetTime* inst); +_BUR_PUBLIC void UtcDTGetTime(struct UtcDTGetTime* inst); +_BUR_PUBLIC void UtcDTSetTime(struct UtcDTSetTime* inst); +_BUR_PUBLIC void UtcDTExSetTime(struct UtcDTExSetTime* inst); +_BUR_PUBLIC void DTStructureGetTime(struct DTStructureGetTime* inst); +_BUR_PUBLIC void DTStructureSetTime(struct DTStructureSetTime* inst); +_BUR_PUBLIC void DTStructureExSetTime(struct DTStructureExSetTime* inst); +_BUR_PUBLIC void UtcDTStructureGetTime(struct UtcDTStructureGetTime* inst); +_BUR_PUBLIC void UtcDTStructureSetTime(struct UtcDTStructureSetTime* inst); +_BUR_PUBLIC void UtcDTStructureExSetTime(struct UtcDTStructureExSetTime* inst); +_BUR_PUBLIC void UtcDT_TO_LocalDTStructure(struct UtcDT_TO_LocalDTStructure* inst); +_BUR_PUBLIC void LocalDT_TO_UtcDTStructure(struct LocalDT_TO_UtcDTStructure* inst); +_BUR_PUBLIC void UtcDTStructure_TO_LocalDT(struct UtcDTStructure_TO_LocalDT* inst); +_BUR_PUBLIC void LocalDTStructure_TO_UtcDT(struct LocalDTStructure_TO_UtcDT* inst); +_BUR_PUBLIC void TimeDeviceGetInfo(struct TimeDeviceGetInfo* inst); +_BUR_PUBLIC void DstGetInfo(struct DstGetInfo* inst); +_BUR_PUBLIC void DstGetInfoDT(struct DstGetInfoDT* inst); +_BUR_PUBLIC plctime clock_ms(void); +_BUR_PUBLIC unsigned long ascTIMEStructure(unsigned long pTIMEStructure, unsigned long pStr, unsigned long len); +_BUR_PUBLIC unsigned long ascDTStructure(unsigned long pDTStructure, unsigned long pStr, unsigned long len); +_BUR_PUBLIC unsigned long ascTIME(plctime TIME1, unsigned long pStr, unsigned long len); +_BUR_PUBLIC unsigned long ascDT(plcdt DT1, unsigned long pStr, unsigned long len); +_BUR_PUBLIC plctime TIMEStructure_TO_TIME(unsigned long pTIMEStructure); +_BUR_PUBLIC unsigned long TIME_TO_TIMEStructure(plctime TIME1, unsigned long pTIMEStructure); +_BUR_PUBLIC plcdt DTStructure_TO_DT(unsigned long pDTStructure); +_BUR_PUBLIC unsigned long DT_TO_DTStructure(plcdt DT1, unsigned long pDTStructure); +_BUR_PUBLIC unsigned long DiffT(plctime TIME2, plctime TIME1); +_BUR_PUBLIC unsigned long DiffDT(plcdt DT2, plcdt DT1); + + +#ifdef __cplusplus +}; +#endif +#endif /* _ASTIME_ */ + diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..bc16fcc --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,21 @@ +# version 3.11 or later of CMake or needed later for installing GoogleTest +# so let's require it now. +cmake_minimum_required(VERSION 3.11-3.18) + +project(StringExt) + +file(GLOB StringExt_SRC +"*.cpp" +"*.c" +) + +# add_compile_options(-fpermissive -Wno-write-strings) +add_definitions(-D_BUR_FORMAT_BRELF) +add_definitions(-D_REPLACE_CONST) + +add_library(${PROJECT_NAME} ${StringExt_SRC}) + +target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/../includes/) +target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/../includes/loupe/Includes/) + + diff --git a/FormatString.c b/FormatString.c index a3e490f..d89ff43 100644 --- a/FormatString.c +++ b/FormatString.c @@ -11,6 +11,11 @@ #include "string.h" +#ifdef _NOT_BR +#define brsitoa(a, b) itoa(a, b) +#define brsftoa(a, b) ftoa(a, b) +#endif + // Define some basic macros #ifndef MIN #define MIN(a, b) ((a) < (b) ? (a) : (b)) diff --git a/GenerateTimestamp.c b/GenerateTimestamp.c index 91f33ec..169843a 100644 --- a/GenerateTimestamp.c +++ b/GenerateTimestamp.c @@ -24,6 +24,11 @@ extern "C" #include +#if !defined(_SG4) || defined(_NOT_BR) +#include "bur.h" +#include "NOT_BR.h" +#include "astime.h" +#endif /********************************************************************/ /* Generate a time stamp string from a DATE_AND_TIME variable */ diff --git a/GenerateTimestampMS.c b/GenerateTimestampMS.c index 3f0e5de..8595057 100644 --- a/GenerateTimestampMS.c +++ b/GenerateTimestampMS.c @@ -24,6 +24,9 @@ extern "C" #include +#ifdef _NOT_BR +#include "NOT_BR.h" +#endif //----------------------------------------------------------------------------- // Generate a time stamp string from a DTStructure variable diff --git a/NOT_BR.h b/NOT_BR.h new file mode 100644 index 0000000..c333dcd --- /dev/null +++ b/NOT_BR.h @@ -0,0 +1,8 @@ +#ifndef STREXT_NOT_BR_H +#define STREXT_NOT_BR_H +#ifdef _NOT_BR +#include +#define brsitoa(a, b) itoa(a, b) +#define brsftoa(a, b) ftoa(a, b) +#endif +#endif \ No newline at end of file From e0bb0fb08c778d5640308419019a5418346be59e Mon Sep 17 00:00:00 2001 From: sclaiborne Date: Thu, 28 Oct 2021 13:04:49 -0700 Subject: [PATCH 3/8] Add support for no BR --- FormatString.c | 74 +++++++++++++++++++++++++++++++++ GenerateTimestamp.c | 75 ++++++++++++++++++++++++++++++++- GenerateTimestampMS.c | 74 ++++++++++++++++++++++++++++++++- NOT_BR.h | 2 +- Timestamp_TO_DT.c | 76 ++++++++++++++++++++++++++++++++- stringlcat.c | 97 +++++++++++++++++++++++++++++++------------ stringlcpy.c | 35 ++++++++++++++++ time.cpp | 13 +++++- 8 files changed, 412 insertions(+), 34 deletions(-) diff --git a/FormatString.c b/FormatString.c index d89ff43..df2756b 100644 --- a/FormatString.c +++ b/FormatString.c @@ -10,6 +10,80 @@ #endif #include "string.h" +#include "stdlib.h" +#include "stdio.h" + +#ifndef brsitoa +#define brsitoa(a,b) strlen(itoa(a,(char*)b,10)) +#endif +#ifndef brsftoa +#include "math.h" +// Reverses a string 'str' of length 'len' +void reverse(char* str, int len) +{ + int i = 0, j = len - 1, temp; + while (i < j) { + temp = str[i]; + str[i] = str[j]; + str[j] = temp; + i++; + j--; + } +} + +// Converts a given integer x to string str[]. +// d is the number of digits required in the output. +// If d is more than the number of digits in x, +// then 0s are added at the beginning. +int intToStr(int x, char str[], int d) +{ + int i = 0; + while (x) { + str[i++] = (x % 10) + '0'; + x = x / 10; + } + + // If number of digits required is more, then + // add 0s at the beginning + while (i < d) + str[i++] = '0'; + + reverse(str, i); + str[i] = '\0'; + return i; +} + +// Converts a floating-point/double number to a string. +size_t ftoa(float n, char* res, int afterpoint) +{ + // Extract integer part + int ipart = (int)n; + + // Extract floating part + float fpart = n - (float)ipart; + + // convert integer part to string + int i = intToStr(ipart, res, 0); + + // check for display option after point + if (afterpoint != 0) { + res[i] = '.'; // add dot + + // Get the value of fraction part upto given no. + // of points after dot. The third parameter + // is needed to handle cases like 233.007 + fpart = fpart * pow(10, afterpoint); + + intToStr((int)fpart, res + i + 1, afterpoint); + } + + return strlen(res); +} +#define brsftoa(a,b) ftoa(a,(char*)b,14) +#endif +#ifndef DTStructure +#include "astime.h" +#endif #ifdef _NOT_BR #define brsitoa(a, b) itoa(a, b) diff --git a/GenerateTimestamp.c b/GenerateTimestamp.c index 169843a..7da008f 100644 --- a/GenerateTimestamp.c +++ b/GenerateTimestamp.c @@ -21,8 +21,81 @@ extern "C" }; #endif - #include +#include "stdlib.h" +#include "stdio.h" + +#ifndef brsitoa +#define brsitoa(a,b) strlen(itoa(a,(char*)b,10)) +#endif +#ifndef brsftoa +#include "math.h" +// Reverses a string 'str' of length 'len' +void reverse(char* str, int len) +{ + int i = 0, j = len - 1, temp; + while (i < j) { + temp = str[i]; + str[i] = str[j]; + str[j] = temp; + i++; + j--; + } +} + +// Converts a given integer x to string str[]. +// d is the number of digits required in the output. +// If d is more than the number of digits in x, +// then 0s are added at the beginning. +int intToStr(int x, char str[], int d) +{ + int i = 0; + while (x) { + str[i++] = (x % 10) + '0'; + x = x / 10; + } + + // If number of digits required is more, then + // add 0s at the beginning + while (i < d) + str[i++] = '0'; + + reverse(str, i); + str[i] = '\0'; + return i; +} + +// Converts a floating-point/double number to a string. +size_t ftoa(float n, char* res, int afterpoint) +{ + // Extract integer part + int ipart = (int)n; + + // Extract floating part + float fpart = n - (float)ipart; + + // convert integer part to string + int i = intToStr(ipart, res, 0); + + // check for display option after point + if (afterpoint != 0) { + res[i] = '.'; // add dot + + // Get the value of fraction part upto given no. + // of points after dot. The third parameter + // is needed to handle cases like 233.007 + fpart = fpart * pow(10, afterpoint); + + intToStr((int)fpart, res + i + 1, afterpoint); + } + + return strlen(res); +} +#define brsftoa(a,b) ftoa(a,(char*)b,14) +#endif +#ifndef DTStructure +#include "astime.h" +#endif #if !defined(_SG4) || defined(_NOT_BR) #include "bur.h" diff --git a/GenerateTimestampMS.c b/GenerateTimestampMS.c index 8595057..0556e80 100644 --- a/GenerateTimestampMS.c +++ b/GenerateTimestampMS.c @@ -23,9 +23,79 @@ extern "C" #include +#include "stdlib.h" +#include "stdio.h" -#ifdef _NOT_BR -#include "NOT_BR.h" +#ifndef brsitoa +#define brsitoa(a,b) strlen(itoa(a,(char*)b,10)) +#endif +#ifndef brsftoa +#include "math.h" +// Reverses a string 'str' of length 'len' +void reverse(char* str, int len) +{ + int i = 0, j = len - 1, temp; + while (i < j) { + temp = str[i]; + str[i] = str[j]; + str[j] = temp; + i++; + j--; + } +} + +// Converts a given integer x to string str[]. +// d is the number of digits required in the output. +// If d is more than the number of digits in x, +// then 0s are added at the beginning. +int intToStr(int x, char str[], int d) +{ + int i = 0; + while (x) { + str[i++] = (x % 10) + '0'; + x = x / 10; + } + + // If number of digits required is more, then + // add 0s at the beginning + while (i < d) + str[i++] = '0'; + + reverse(str, i); + str[i] = '\0'; + return i; +} + +// Converts a floating-point/double number to a string. +size_t ftoa(float n, char* res, int afterpoint) +{ + // Extract integer part + int ipart = (int)n; + + // Extract floating part + float fpart = n - (float)ipart; + + // convert integer part to string + int i = intToStr(ipart, res, 0); + + // check for display option after point + if (afterpoint != 0) { + res[i] = '.'; // add dot + + // Get the value of fraction part upto given no. + // of points after dot. The third parameter + // is needed to handle cases like 233.007 + fpart = fpart * pow(10, afterpoint); + + intToStr((int)fpart, res + i + 1, afterpoint); + } + + return strlen(res); +} +#define brsftoa(a,b) ftoa(a,(char*)b,14) +#endif +#ifndef DTStructure +#include "astime.h" #endif //----------------------------------------------------------------------------- diff --git a/NOT_BR.h b/NOT_BR.h index c333dcd..765d7c3 100644 --- a/NOT_BR.h +++ b/NOT_BR.h @@ -2,7 +2,7 @@ #define STREXT_NOT_BR_H #ifdef _NOT_BR #include -#define brsitoa(a, b) itoa(a, b) +#define brsitoa(a, b) itoa(a, (char*)b, 10) #define brsftoa(a, b) ftoa(a, b) #endif #endif \ No newline at end of file diff --git a/Timestamp_TO_DT.c b/Timestamp_TO_DT.c index b85b0d9..20bdb1f 100644 --- a/Timestamp_TO_DT.c +++ b/Timestamp_TO_DT.c @@ -22,9 +22,81 @@ }; #endif - #include -#include +#include "stdlib.h" +#include "stdio.h" + +#ifndef brsitoa +#define brsitoa(a,b) strlen(itoa(a,(char*)b,10)) +#endif +#ifndef brsftoa +#include "math.h" +// Reverses a string 'str' of length 'len' +void reverse(char* str, int len) +{ + int i = 0, j = len - 1, temp; + while (i < j) { + temp = str[i]; + str[i] = str[j]; + str[j] = temp; + i++; + j--; + } +} + +// Converts a given integer x to string str[]. +// d is the number of digits required in the output. +// If d is more than the number of digits in x, +// then 0s are added at the beginning. +int intToStr(int x, char str[], int d) +{ + int i = 0; + while (x) { + str[i++] = (x % 10) + '0'; + x = x / 10; + } + + // If number of digits required is more, then + // add 0s at the beginning + while (i < d) + str[i++] = '0'; + + reverse(str, i); + str[i] = '\0'; + return i; +} + +// Converts a floating-point/double number to a string. +size_t ftoa(float n, char* res, int afterpoint) +{ + // Extract integer part + int ipart = (int)n; + + // Extract floating part + float fpart = n - (float)ipart; + + // convert integer part to string + int i = intToStr(ipart, res, 0); + + // check for display option after point + if (afterpoint != 0) { + res[i] = '.'; // add dot + + // Get the value of fraction part upto given no. + // of points after dot. The third parameter + // is needed to handle cases like 233.007 + fpart = fpart * pow(10, afterpoint); + + intToStr((int)fpart, res + i + 1, afterpoint); + } + + return strlen(res); +} +#define brsftoa(a,b) ftoa(a,(char*)b,14) +#endif +#ifndef DTStructure +#include "astime.h" +#endif // Converts output of GenerateTimestamp to DATE_AND_TIME diff --git a/stringlcat.c b/stringlcat.c index 340a717..6685c4e 100644 --- a/stringlcat.c +++ b/stringlcat.c @@ -42,38 +42,83 @@ #include "string.h" -/* +#ifndef HAVE_STRLCAT + /* + * '_cups_strlcat()' - Safely concatenate two strings. + */ + + size_t /* O - Length of string */ + strlcat(char *dst, /* O - Destination string */ + const char *src, /* I - Source string */ + size_t size) /* I - Size of destination string buffer */ + { + size_t srclen; /* Length of source string */ + size_t dstlen; /* Length of destination string */ + + /* + * Figure out how much room is left... + */ + + dstlen = strlen(dst); + size -= dstlen + 1; + + if (!size) + return (dstlen); /* No room, return immediately... */ + + /* + * Figure out how much room is needed... + */ + + srclen = strlen(src); + + /* + * Copy the appropriate amount... + */ + + if (srclen > size) + srclen = size; + + memcpy(dst + dstlen, src, srclen); + dst[dstlen + srclen] = '\0'; + + return (dstlen + srclen); + } +#endif /* !HAVE_STRLCAT */ + + /* * Appends src to string dst of size dSize (unlike strncat, dSize is the * full size of pDest, not space left). At most dSize-1 characters * will be copied. Always NUL terminates (unless dSize <= strlen(pDest)). * Returns strlen(pSrc) + MIN(dSize, strlen(initial pDest)). * If retval >= dSize, truncation occurred. */ -unsigned long stringlcat(unsigned long pDest, const unsigned long pSrc, unsigned long dSize) -{ - char *dst = (char*) pDest; - char *src = (char*) pSrc; - char *d = dst; - const char *s = src; - unsigned long n = dSize; - unsigned long dlen; - - /* Find the end of dst and adjust bytes left but don't go past end */ - while (n-- != 0 && *d != '\0') - d++; - dlen = d - dst; - n = dSize - dlen; - - if (n == 0) - return(dlen + strlen(s)); - while (*s != '\0') { - if (n != 1) { - *d++ = *s; - n--; + unsigned long stringlcat(unsigned long pDest, const unsigned long pSrc, unsigned long dSize) + { + char *dst = (char *)pDest; + char *src = (char *)pSrc; + char *d = dst; + const char *s = src; + unsigned long n = dSize; + unsigned long dlen; + + /* Find the end of dst and adjust bytes left but don't go past end */ + while (n-- != 0 && *d != '\0') + d++; + dlen = d - dst; + n = dSize - dlen; + + if (n == 0) + return (dlen + strlen(s)); + while (*s != '\0') + { + if (n != 1) + { + *d++ = *s; + n--; + } + s++; } - s++; - } - *d = '\0'; + *d = '\0'; - return(dlen + (s - src)); /* count does not include NUL */ + return (dlen + (s - src)); /* count does not include NUL */ } diff --git a/stringlcpy.c b/stringlcpy.c index 4d6fb69..f44320c 100644 --- a/stringlcpy.c +++ b/stringlcpy.c @@ -10,6 +10,41 @@ #include "string.h" + +#ifndef HAVE_STRLCPY + /* + * '_cups_strlcpy()' - Safely copy two strings. + */ + + size_t /* O - Length of string */ + strlcpy(char *dst, /* O - Destination string */ + const char *src, /* I - Source string */ + size_t size) /* I - Size of destination string buffer */ + { + size_t srclen; /* Length of source string */ + + /* + * Figure out how much room is needed... + */ + + size--; + + srclen = strlen(src); + + /* + * Copy the appropriate amount... + */ + + if (srclen > size) + srclen = size; + + memcpy(dst, src, srclen); + dst[srclen] = '\0'; + + return (srclen); + } +#endif /* !HAVE_STRLCPY */ + /* * Copy string pSrc to buffer dst of size dSize. At most dSize-1 * chars will be copied. Always NUL terminates (unless dSize == 0). diff --git a/time.cpp b/time.cpp index bf1db87..8a5c70c 100644 --- a/time.cpp +++ b/time.cpp @@ -15,13 +15,22 @@ #include "time.h" +#ifndef gmtime_r // In not BR gmtime_r is not supported +#define gmtime_r(a,b) b = gmtime(a) +#endif +#ifndef strptime +#define strptime(a,b,c) a // strptime( +#endif + unsigned long stringfTime(unsigned long dest, unsigned long destSize, unsigned long format, DATE_AND_TIME time) { if(!dest || !destSize || !format) return 0; // Return if null pointers or if destSize is 0 tm t = {}; - gmtime_r((time_t*)&time, &t); - return strftime((char*)dest, destSize, (char*)format, &t); + tm* pt = &t; + gmtime_r((time_t*)&time, pt); // This may modify where pt is pointing on some systems + return strftime((char*)dest, destSize, (char*)format, pt); } +// Not supported on all systems DATE_AND_TIME stringpTime(unsigned long src, unsigned long format) { if(!src || !format) return 0; // Return if null pointers tm t = {}; From d0e510baa520796e2791ca96238415071f74ed02 Mon Sep 17 00:00:00 2001 From: David Wiens Date: Tue, 9 Nov 2021 08:39:37 -0800 Subject: [PATCH 4/8] Add NOT_BR check in appendArrayIndex --- appendArrayIndex.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/appendArrayIndex.c b/appendArrayIndex.c index dc247de..6162417 100644 --- a/appendArrayIndex.c +++ b/appendArrayIndex.c @@ -10,6 +10,11 @@ }; #endif +#if !defined(_SG4) || defined(_NOT_BR) +#include "bur.h" +#include "NOT_BR.h" +#endif + #include From dbb9f32af19084d6ebff3032cb6eb525e7bb4621 Mon Sep 17 00:00:00 2001 From: Josh Polansky Date: Sun, 21 Nov 2021 14:53:20 -0800 Subject: [PATCH 5/8] Cleanup B&R overrides --- FormatString.c | 83 +------------------------------------------ GenerateTimestamp.c | 76 ++------------------------------------- GenerateTimestampMS.c | 73 +------------------------------------ NOT_BR.h | 8 ----- appendArrayIndex.c | 5 +-- stringConvert.c | 4 +++ stringExtInternal.c | 65 +++++++++++++++++++++++++++++++++ stringExtInternal.h | 36 +++++++++++++++++++ stringlcat.c | 2 +- stringlcpy.c | 2 +- 10 files changed, 112 insertions(+), 242 deletions(-) delete mode 100644 NOT_BR.h create mode 100644 stringExtInternal.c create mode 100644 stringExtInternal.h diff --git a/FormatString.c b/FormatString.c index df2756b..5fc8836 100644 --- a/FormatString.c +++ b/FormatString.c @@ -13,93 +13,12 @@ #include "stdlib.h" #include "stdio.h" -#ifndef brsitoa -#define brsitoa(a,b) strlen(itoa(a,(char*)b,10)) -#endif -#ifndef brsftoa -#include "math.h" -// Reverses a string 'str' of length 'len' -void reverse(char* str, int len) -{ - int i = 0, j = len - 1, temp; - while (i < j) { - temp = str[i]; - str[i] = str[j]; - str[j] = temp; - i++; - j--; - } -} - -// Converts a given integer x to string str[]. -// d is the number of digits required in the output. -// If d is more than the number of digits in x, -// then 0s are added at the beginning. -int intToStr(int x, char str[], int d) -{ - int i = 0; - while (x) { - str[i++] = (x % 10) + '0'; - x = x / 10; - } - - // If number of digits required is more, then - // add 0s at the beginning - while (i < d) - str[i++] = '0'; - - reverse(str, i); - str[i] = '\0'; - return i; -} - -// Converts a floating-point/double number to a string. -size_t ftoa(float n, char* res, int afterpoint) -{ - // Extract integer part - int ipart = (int)n; - - // Extract floating part - float fpart = n - (float)ipart; - - // convert integer part to string - int i = intToStr(ipart, res, 0); - - // check for display option after point - if (afterpoint != 0) { - res[i] = '.'; // add dot - - // Get the value of fraction part upto given no. - // of points after dot. The third parameter - // is needed to handle cases like 233.007 - fpart = fpart * pow(10, afterpoint); - - intToStr((int)fpart, res + i + 1, afterpoint); - } +#include "stringExtInternal.h" - return strlen(res); -} -#define brsftoa(a,b) ftoa(a,(char*)b,14) -#endif #ifndef DTStructure #include "astime.h" #endif -#ifdef _NOT_BR -#define brsitoa(a, b) itoa(a, b) -#define brsftoa(a, b) ftoa(a, b) -#endif - -// Define some basic macros -#ifndef MIN -#define MIN(a, b) ((a) < (b) ? (a) : (b)) -#endif -#ifndef MAX -#define MAX(a, b) ((a) > (b) ? (a) : (b)) -#endif -#ifndef ARRAY_ENTRIES -#define ARRAY_ENTRIES(array) (sizeof(array)/sizeof(array[0])) -#endif /* * diff --git a/GenerateTimestamp.c b/GenerateTimestamp.c index 7da008f..b6da334 100644 --- a/GenerateTimestamp.c +++ b/GenerateTimestamp.c @@ -25,81 +25,9 @@ extern "C" #include "stdlib.h" #include "stdio.h" -#ifndef brsitoa -#define brsitoa(a,b) strlen(itoa(a,(char*)b,10)) -#endif -#ifndef brsftoa -#include "math.h" -// Reverses a string 'str' of length 'len' -void reverse(char* str, int len) -{ - int i = 0, j = len - 1, temp; - while (i < j) { - temp = str[i]; - str[i] = str[j]; - str[j] = temp; - i++; - j--; - } -} - -// Converts a given integer x to string str[]. -// d is the number of digits required in the output. -// If d is more than the number of digits in x, -// then 0s are added at the beginning. -int intToStr(int x, char str[], int d) -{ - int i = 0; - while (x) { - str[i++] = (x % 10) + '0'; - x = x / 10; - } - - // If number of digits required is more, then - // add 0s at the beginning - while (i < d) - str[i++] = '0'; - - reverse(str, i); - str[i] = '\0'; - return i; -} - -// Converts a floating-point/double number to a string. -size_t ftoa(float n, char* res, int afterpoint) -{ - // Extract integer part - int ipart = (int)n; - - // Extract floating part - float fpart = n - (float)ipart; - - // convert integer part to string - int i = intToStr(ipart, res, 0); - - // check for display option after point - if (afterpoint != 0) { - res[i] = '.'; // add dot - - // Get the value of fraction part upto given no. - // of points after dot. The third parameter - // is needed to handle cases like 233.007 - fpart = fpart * pow(10, afterpoint); - - intToStr((int)fpart, res + i + 1, afterpoint); - } - - return strlen(res); -} -#define brsftoa(a,b) ftoa(a,(char*)b,14) -#endif -#ifndef DTStructure -#include "astime.h" -#endif +#include "stringExtInternal.h" -#if !defined(_SG4) || defined(_NOT_BR) -#include "bur.h" -#include "NOT_BR.h" +#ifndef DTStructure #include "astime.h" #endif diff --git a/GenerateTimestampMS.c b/GenerateTimestampMS.c index 0556e80..1054d0d 100644 --- a/GenerateTimestampMS.c +++ b/GenerateTimestampMS.c @@ -25,78 +25,7 @@ extern "C" #include #include "stdlib.h" #include "stdio.h" - -#ifndef brsitoa -#define brsitoa(a,b) strlen(itoa(a,(char*)b,10)) -#endif -#ifndef brsftoa -#include "math.h" -// Reverses a string 'str' of length 'len' -void reverse(char* str, int len) -{ - int i = 0, j = len - 1, temp; - while (i < j) { - temp = str[i]; - str[i] = str[j]; - str[j] = temp; - i++; - j--; - } -} - -// Converts a given integer x to string str[]. -// d is the number of digits required in the output. -// If d is more than the number of digits in x, -// then 0s are added at the beginning. -int intToStr(int x, char str[], int d) -{ - int i = 0; - while (x) { - str[i++] = (x % 10) + '0'; - x = x / 10; - } - - // If number of digits required is more, then - // add 0s at the beginning - while (i < d) - str[i++] = '0'; - - reverse(str, i); - str[i] = '\0'; - return i; -} - -// Converts a floating-point/double number to a string. -size_t ftoa(float n, char* res, int afterpoint) -{ - // Extract integer part - int ipart = (int)n; - - // Extract floating part - float fpart = n - (float)ipart; - - // convert integer part to string - int i = intToStr(ipart, res, 0); - - // check for display option after point - if (afterpoint != 0) { - res[i] = '.'; // add dot - - // Get the value of fraction part upto given no. - // of points after dot. The third parameter - // is needed to handle cases like 233.007 - fpart = fpart * pow(10, afterpoint); - - intToStr((int)fpart, res + i + 1, afterpoint); - } - - return strlen(res); -} -#define brsftoa(a,b) ftoa(a,(char*)b,14) -#endif -#ifndef DTStructure -#include "astime.h" -#endif +#include "stringExtInternal.h" //----------------------------------------------------------------------------- // Generate a time stamp string from a DTStructure variable diff --git a/NOT_BR.h b/NOT_BR.h deleted file mode 100644 index 765d7c3..0000000 --- a/NOT_BR.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef STREXT_NOT_BR_H -#define STREXT_NOT_BR_H -#ifdef _NOT_BR -#include -#define brsitoa(a, b) itoa(a, (char*)b, 10) -#define brsftoa(a, b) ftoa(a, b) -#endif -#endif \ No newline at end of file diff --git a/appendArrayIndex.c b/appendArrayIndex.c index 6162417..c43ff8c 100644 --- a/appendArrayIndex.c +++ b/appendArrayIndex.c @@ -10,10 +10,7 @@ }; #endif -#if !defined(_SG4) || defined(_NOT_BR) -#include "bur.h" -#include "NOT_BR.h" -#endif +#include "stringExtInternal.h" #include diff --git a/stringConvert.c b/stringConvert.c index a03ad60..22e0da0 100644 --- a/stringConvert.c +++ b/stringConvert.c @@ -42,6 +42,8 @@ unsigned long string2wstring(unsigned long pDest, const unsigned long pSrc, unsi return(s - src); /* count does not include NUL */ } +#ifdef brwcslen + unsigned long wstring2string(unsigned long pDest, const unsigned long pSrc, unsigned long dSize) { char *dst = (char*) pDest; @@ -85,3 +87,5 @@ unsigned char wchar2char(unsigned short c) { return ((char)c); } } + +#endif \ No newline at end of file diff --git a/stringExtInternal.c b/stringExtInternal.c new file mode 100644 index 0000000..2a06617 --- /dev/null +++ b/stringExtInternal.c @@ -0,0 +1,65 @@ +#include "stringExtInternal.h" + +#include "math.h" +#include "string.h" +// Reverses a string 'str' of length 'len' +void reverse(char* str, int len) +{ + int i = 0, j = len - 1, temp; + while (i < j) { + temp = str[i]; + str[i] = str[j]; + str[j] = temp; + i++; + j--; + } +} + +// Converts a given integer x to string str[]. +// d is the number of digits required in the output. +// If d is more than the number of digits in x, +// then 0s are added at the beginning. +int intToStr(int x, char str[], int d) +{ + int i = 0; + while (x) { + str[i++] = (x % 10) + '0'; + x = x / 10; + } + + // If number of digits required is more, then + // add 0s at the beginning + while (i < d) + str[i++] = '0'; + + reverse(str, i); + str[i] = '\0'; + return i; +} + +// Converts a floating-point/double number to a string. +size_t ftoa(float n, char* res, int afterpoint) +{ + // Extract integer part + int ipart = (int)n; + + // Extract floating part + float fpart = n - (float)ipart; + + // convert integer part to string + int i = intToStr(ipart, res, 0); + + // check for display option after point + if (afterpoint != 0) { + res[i] = '.'; // add dot + + // Get the value of fraction part upto given no. + // of points after dot. The third parameter + // is needed to handle cases like 233.007 + fpart = fpart * pow(10, afterpoint); + + intToStr((int)fpart, res + i + 1, afterpoint); + } + + return strlen(res); +} diff --git a/stringExtInternal.h b/stringExtInternal.h new file mode 100644 index 0000000..49e54c2 --- /dev/null +++ b/stringExtInternal.h @@ -0,0 +1,36 @@ +#ifndef _SG4_ +#include "bur.h" +#endif + +#ifndef brsitoa + #ifndef itoa + #include + #define brsitoa(a,b) sprintf(b,"%d",a) + #else + #include + #define brsitoa(a,b) strlen(itoa(a,(char*)b,10)) + #endif +#endif + + +#ifndef brsftoa +#include "math.h" +#include "string.h" +#define brsftoa(a,b) ftoa((a),(char*)(b),14) +#endif + +#ifndef DTStructure +#include "astime.h" +#endif + +// Define some basic macros +#ifndef MIN +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#endif +#ifndef MAX +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#endif +#ifndef ARRAY_ENTRIES +#define ARRAY_ENTRIES(array) (sizeof(array)/sizeof(array[0])) +#endif + diff --git a/stringlcat.c b/stringlcat.c index 6685c4e..a4a5064 100644 --- a/stringlcat.c +++ b/stringlcat.c @@ -42,7 +42,7 @@ #include "string.h" -#ifndef HAVE_STRLCAT +#ifndef strlcat /* * '_cups_strlcat()' - Safely concatenate two strings. */ diff --git a/stringlcpy.c b/stringlcpy.c index f44320c..7e717bc 100644 --- a/stringlcpy.c +++ b/stringlcpy.c @@ -11,7 +11,7 @@ #include "string.h" -#ifndef HAVE_STRLCPY +#ifndef strlcpy /* * '_cups_strlcpy()' - Safely copy two strings. */ From 75f9b984ab4c9e6a043044a5149ee775c3480a0d Mon Sep 17 00:00:00 2001 From: Josh Polansky Date: Sun, 21 Nov 2021 15:04:13 -0800 Subject: [PATCH 6/8] Fix brsftoa --- stringExtInternal.h | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/stringExtInternal.h b/stringExtInternal.h index 49e54c2..4a00054 100644 --- a/stringExtInternal.h +++ b/stringExtInternal.h @@ -2,21 +2,25 @@ #include "bur.h" #endif +#include +#include "math.h" +#include "string.h" +#include #ifndef brsitoa #ifndef itoa - #include - #define brsitoa(a,b) sprintf(b,"%d",a) + #define brsitoa(a,b) sprintf((b),"%d",(a)) #else - #include - #define brsitoa(a,b) strlen(itoa(a,(char*)b,10)) + #define brsitoa(a,b) strlen(itoa((a),(char*)(b),10)) #endif #endif #ifndef brsftoa -#include "math.h" -#include "string.h" -#define brsftoa(a,b) ftoa((a),(char*)(b),14) + #ifndef ftoa + #define brsftoa(a,b) sprintf((b),"%f",(a)) + #else + #define brsftoa(a,b) ftoa( (a), (b)) + #endif #endif #ifndef DTStructure From 42c4122b7922f105189bfa2670096f36b8052eb3 Mon Sep 17 00:00:00 2001 From: Josh Polansky Date: Wed, 18 May 2022 15:02:48 -0700 Subject: [PATCH 7/8] remove check for brwcslen Swap it out for the ever present wchar.h wcslen. --- stringConvert.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/stringConvert.c b/stringConvert.c index 22e0da0..8ca65cd 100644 --- a/stringConvert.c +++ b/stringConvert.c @@ -9,7 +9,8 @@ }; #endif -#include "string.h" +#include +#include /* * @@ -42,7 +43,6 @@ unsigned long string2wstring(unsigned long pDest, const unsigned long pSrc, unsi return(s - src); /* count does not include NUL */ } -#ifdef brwcslen unsigned long wstring2string(unsigned long pDest, const unsigned long pSrc, unsigned long dSize) { @@ -55,7 +55,7 @@ unsigned long wstring2string(unsigned long pDest, const unsigned long pSrc, unsi n = dSize; if (n == 0) - return(brwcslen((UINT*)src)); + return(wcslen((UINT*)src)); while (*s != '\0') { if (n != 1) { if(*s > 0xFF) { @@ -86,6 +86,4 @@ unsigned char wchar2char(unsigned short c) { else { return ((char)c); } -} - -#endif \ No newline at end of file +} \ No newline at end of file From c1b5da6fca9890517b0b4d666ebd563bb7d52d63 Mon Sep 17 00:00:00 2001 From: Josh Polansky Date: Wed, 18 May 2022 17:06:35 -0700 Subject: [PATCH 8/8] Remove explicit include dirs from cmake --- CMakeLists.txt | 5 ----- 1 file changed, 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bc16fcc..49b14af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,8 +14,3 @@ add_definitions(-D_BUR_FORMAT_BRELF) add_definitions(-D_REPLACE_CONST) add_library(${PROJECT_NAME} ${StringExt_SRC}) - -target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/../includes/) -target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/../includes/loupe/Includes/) - -