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..49b14af --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,16 @@ +# 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}) diff --git a/FormatString.c b/FormatString.c index a3e490f..5fc8836 100644 --- a/FormatString.c +++ b/FormatString.c @@ -10,18 +10,16 @@ #endif #include "string.h" +#include "stdlib.h" +#include "stdio.h" -// 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])) +#include "stringExtInternal.h" + +#ifndef DTStructure +#include "astime.h" #endif + /* * */ diff --git a/GenerateTimestamp.c b/GenerateTimestamp.c index 91f33ec..b6da334 100644 --- a/GenerateTimestamp.c +++ b/GenerateTimestamp.c @@ -21,9 +21,15 @@ extern "C" }; #endif - #include +#include "stdlib.h" +#include "stdio.h" + +#include "stringExtInternal.h" +#ifndef DTStructure +#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..1054d0d 100644 --- a/GenerateTimestampMS.c +++ b/GenerateTimestampMS.c @@ -23,7 +23,9 @@ extern "C" #include - +#include "stdlib.h" +#include "stdio.h" +#include "stringExtInternal.h" //----------------------------------------------------------------------------- // Generate a time stamp string from a DTStructure variable 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/appendArrayIndex.c b/appendArrayIndex.c index dc247de..c43ff8c 100644 --- a/appendArrayIndex.c +++ b/appendArrayIndex.c @@ -10,6 +10,8 @@ }; #endif +#include "stringExtInternal.h" + #include diff --git a/stringConvert.c b/stringConvert.c index a03ad60..8ca65cd 100644 --- a/stringConvert.c +++ b/stringConvert.c @@ -9,7 +9,8 @@ }; #endif -#include "string.h" +#include +#include /* * @@ -42,6 +43,7 @@ unsigned long string2wstring(unsigned long pDest, const unsigned long pSrc, unsi return(s - src); /* count does not include NUL */ } + unsigned long wstring2string(unsigned long pDest, const unsigned long pSrc, unsigned long dSize) { char *dst = (char*) pDest; @@ -53,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) { @@ -84,4 +86,4 @@ unsigned char wchar2char(unsigned short c) { else { return ((char)c); } -} +} \ 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..4a00054 --- /dev/null +++ b/stringExtInternal.h @@ -0,0 +1,40 @@ +#ifndef _SG4_ +#include "bur.h" +#endif + +#include +#include "math.h" +#include "string.h" +#include +#ifndef brsitoa + #ifndef itoa + #define brsitoa(a,b) sprintf((b),"%d",(a)) + #else + #define brsitoa(a,b) strlen(itoa((a),(char*)(b),10)) + #endif +#endif + + +#ifndef brsftoa + #ifndef ftoa + #define brsftoa(a,b) sprintf((b),"%f",(a)) + #else + #define brsftoa(a,b) ftoa( (a), (b)) + #endif +#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 340a717..a4a5064 100644 --- a/stringlcat.c +++ b/stringlcat.c @@ -42,38 +42,83 @@ #include "string.h" -/* +#ifndef 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..7e717bc 100644 --- a/stringlcpy.c +++ b/stringlcpy.c @@ -10,6 +10,41 @@ #include "string.h" + +#ifndef 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 221e904..8a5c70c 100644 --- a/time.cpp +++ b/time.cpp @@ -1,27 +1,39 @@ - -#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" + + +#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 = {}; + 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 = {}; + strptime((char*)src, (char*)format, &t); + return mktime(&t); +}