From 45d02cf8dad5efef3320ab988dd5981ec612087b Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Mon, 8 Jul 2024 01:34:40 +0200 Subject: [PATCH] examples: factorize strncpy --- example1/example1.cpp | 18 +++--------------- example1/example_platform.h | 6 ++++++ example2/example2.cpp | 8 ++------ example3/example3.cpp | 12 ++---------- 4 files changed, 13 insertions(+), 31 deletions(-) diff --git a/example1/example1.cpp b/example1/example1.cpp index 2e90da7e..6a934109 100644 --- a/example1/example1.cpp +++ b/example1/example1.cpp @@ -313,11 +313,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 + example_strncpy(out_filename, argv[i], sizeof(out_filename)); } else if (!crnlib_stricmp(argv[i], "-nonsrgb")) srgb_colorspace = false; else if (!crnlib_stricmp(argv[i], "-nomips")) @@ -482,11 +478,7 @@ int main(int argc, char* argv[]) { char dst_filename[FILENAME_MAX]; crnlib_snprintf(dst_filename, sizeof(dst_filename), "%s%s%s%s", drive_buf, dir_buf, fname_buf, 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 + example_strncpy(dst_filename, out_filename, sizeof(dst_filename)); printf("Writing %s file: %s\n", output_crn ? "CRN" : "DDS", dst_filename); FILE* pFile = NULL; @@ -516,11 +508,7 @@ int main(int argc, char* argv[]) { char dst_filename[FILENAME_MAX]; crnlib_snprintf(dst_filename, sizeof(dst_filename), "%s%s%s.dds", drive_buf, dir_buf, fname_buf); 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 + example_strncpy(dst_filename, out_filename, sizeof(dst_filename)); printf("Writing file: %s\n", dst_filename); FILE* pFile = NULL; diff --git a/example1/example_platform.h b/example1/example_platform.h index 91952d51..aaff40fe 100644 --- a/example1/example_platform.h +++ b/example1/example_platform.h @@ -2,6 +2,12 @@ #include +#if defined(_WIN32) +#define example_strncpy(out, in, size) strcpy_s(out, size, in) +#else +#define example_strncpy(out, in, size) strncpy(out, in, size) +#endif + #if defined(_WIN32) #define EXAMPLE_MAX_DRIVE _MAX_DRIVE #define EXAMPLE_MAX_DIR _MAX_DIR diff --git a/example2/example2.cpp b/example2/example2.cpp index bc0a4a51..9d995416 100644 --- a/example2/example2.cpp +++ b/example2/example2.cpp @@ -91,11 +91,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 + example_strncpy(out_filename, argv[i], sizeof(out_filename)); } else return error("Invalid option: %s\n", argv[i]); } @@ -144,7 +140,7 @@ int main(int argc, char* argv[]) { char dst_filename[FILENAME_MAX]; crnlib_snprintf(dst_filename, sizeof(dst_filename), "%s%s%s.dds", drive_buf, dir_buf, fname_buf); if (out_filename[0]) - strcpy(dst_filename, out_filename); + example_strncpy(dst_filename, out_filename, FILENAME_MAX); printf("Writing DDS file: %s\n", dst_filename); diff --git a/example3/example3.cpp b/example3/example3.cpp index f1e5536e..f03c5f1e 100644 --- a/example3/example3.cpp +++ b/example3/example3.cpp @@ -94,11 +94,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 + example_strncpy(out_filename, argv[i], sizeof(out_filename)); } else if (!crnlib_stricmp(argv[i], "-nonsrgb")) srgb_colorspace = false; else if (!crnlib_stricmp(argv[i], "-pixelformat")) { @@ -233,11 +229,7 @@ int main(int argc, char* argv[]) { char dst_filename[FILENAME_MAX]; crnlib_snprintf(dst_filename, sizeof(dst_filename), "%s%s%s.dds", drive_buf, dir_buf, fname_buf); 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 + example_strncpy(dst_filename, out_filename, sizeof(dst_filename)); printf("Writing DDS file: %s\n", dst_filename);