From 6cdf20d9f230de54114c6ac1f2a55d99859762d7 Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Mon, 8 Jul 2024 01:34:40 +0200 Subject: [PATCH] examples: factorize strcpy --- example1/example1.cpp | 19 ++++--------------- example2/example2.cpp | 36 +++++++++++++++++++++++++----------- example3/example3.cpp | 13 +++---------- 3 files changed, 32 insertions(+), 36 deletions(-) diff --git a/example1/example1.cpp b/example1/example1.cpp index 465ca3ac..72b65c0c 100644 --- a/example1/example1.cpp +++ b/example1/example1.cpp @@ -20,6 +20,7 @@ #include "dds_defs.h" #include "crn_core.h" +#include "crn_strutils.h" #include "crn_file_utils.h" // stb_image, for loading/saving image files. @@ -313,11 +314,7 @@ int main(int argc, char* argv[]) { if (++i >= argc) return error("Expected output filename!"); -#if defined(_WIN32) - strcpy_s(out_filename, sizeof(out_filename), argv[i]); -#else - strncpy(out_filename, argv[i], sizeof(out_filename)); -#endif + strcpy_safe(out_filename, sizeof(out_filename), argv[i]); } else if (!crnlib_stricmp(argv[i], "-nonsrgb")) srgb_colorspace = false; else if (!crnlib_stricmp(argv[i], "-nomips")) @@ -482,11 +479,7 @@ int main(int argc, char* argv[]) { char dst_filename[FILENAME_MAX]; crnlib_snprintf(dst_filename, sizeof(dst_filename), "%s%s%s%s", drive.get_ptr(), dir.get_ptr(), fname.get_ptr(), output_crn ? ".crn" : ".dds"); if (out_filename[0]) -#if defined(_WIN32) - strcpy_s(dst_filename, sizeof(dst_filename), out_filename); -#else - strncpy(dst_filename, out_filename, sizeof(dst_filename)); -#endif + strcpy_safe(dst_filename, sizeof(dst_filename), out_filename); printf("Writing %s file: %s\n", output_crn ? "CRN" : "DDS", dst_filename); FILE* pFile = NULL; @@ -516,11 +509,7 @@ int main(int argc, char* argv[]) { char dst_filename[FILENAME_MAX]; crnlib_snprintf(dst_filename, sizeof(dst_filename), "%s%s%s.dds", drive.get_ptr(), dir.get_ptr(), fname.get_ptr()); if (out_filename[0]) -#if defined(_WIN32) - strcpy_s(dst_filename, sizeof(dst_filename), out_filename); -#else - strncpy(dst_filename, out_filename, sizeof(dst_filename)); -#endif + strcpy_safe(dst_filename, sizeof(dst_filename), out_filename); printf("Writing file: %s\n", dst_filename); FILE* pFile = NULL; diff --git a/example2/example2.cpp b/example2/example2.cpp index 3c6d845d..c19bb7c5 100644 --- a/example2/example2.cpp +++ b/example2/example2.cpp @@ -28,11 +28,29 @@ using namespace crnlib; -#if defined(_WIN32) -#define example2_strncpy(out, in, size) strcpy_s(out, size, in) -#else -#define example2_strncpy(out, in, size) strncpy(out, in, size) -#endif +// Copied from crn_strutils.cpp without asserts. +char* example2_strcpy_safe(char* pDst, uint dst_len, const char* pSrc) { + if (!dst_len) + return pDst; + + char* q = pDst; + char c; + + do { + if (dst_len == 1) { + *q++ = '\0'; + break; + } + + c = *pSrc++; + *q++ = c; + + dst_len--; + + } while (c); + + return pDst; +} static int print_usage() { printf("Description: Transcodes .CRN to .DDS files using crn_decomp.h.\n"); @@ -96,11 +114,7 @@ int main(int argc, char* argv[]) { if (++i >= argc) return error("Expected output filename!"); -#if defined(_WIN32) - strcpy_s(out_filename, sizeof(out_filename), argv[i]); -#else - strncpy(out_filename, argv[i], sizeof(out_filename)); -#endif + example2_strcpy_safe(out_filename, sizeof(out_filename), argv[i]); } else return error("Invalid option: %s\n", argv[i]); } @@ -143,7 +157,7 @@ int main(int argc, char* argv[]) { // Now create the DDS file. char dst_filename[FILENAME_MAX]; if (out_filename[0]) { - strcpy(dst_filename, out_filename); + example2_strcpy_safe(dst_filename, sizeof(dst_filename), out_filename); } else { unsigned int stripped_length = UINT32_MAX; const char* ext_begin = strrchr(pSrc_filename, '.'); diff --git a/example3/example3.cpp b/example3/example3.cpp index 685af177..aa11da45 100644 --- a/example3/example3.cpp +++ b/example3/example3.cpp @@ -19,6 +19,7 @@ #include "dds_defs.h" #include "crn_core.h" +#include "crn_strutils.h" #include "crn_file_utils.h" // stb_image, for loading/saving image files. @@ -94,11 +95,7 @@ int main(int argc, char* argv[]) { if (++i >= argc) return error("Expected output filename!"); -#if defined(_WIN32) - strcpy_s(out_filename, sizeof(out_filename), argv[i]); -#else - strncpy(out_filename, argv[i], sizeof(out_filename)); -#endif + strcpy_safe(out_filename, sizeof(out_filename), argv[i]); } else if (!crnlib_stricmp(argv[i], "-nonsrgb")) srgb_colorspace = false; else if (!crnlib_stricmp(argv[i], "-pixelformat")) { @@ -233,11 +230,7 @@ int main(int argc, char* argv[]) { char dst_filename[FILENAME_MAX]; crnlib_snprintf(dst_filename, sizeof(dst_filename), "%s%s%s.dds", drive.get_ptr(), dir.get_ptr(), fname.get_ptr()); if (out_filename[0]) -#if defined(_WIN32) - strcpy_s(dst_filename, sizeof(dst_filename), out_filename); -#else - strncpy(dst_filename, out_filename, sizeof(dst_filename)); -#endif + strcpy_safe(dst_filename, sizeof(dst_filename), out_filename); printf("Writing DDS file: %s\n", dst_filename);