Skip to content

Commit

Permalink
examples: factorize strcpy
Browse files Browse the repository at this point in the history
  • Loading branch information
illwieckz committed Jul 7, 2024
1 parent 7d91e97 commit 6cdf20d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 36 deletions.
19 changes: 4 additions & 15 deletions example1/example1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
36 changes: 25 additions & 11 deletions example2/example2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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]);
}
Expand Down Expand Up @@ -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, '.');
Expand Down
13 changes: 3 additions & 10 deletions example3/example3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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")) {
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 6cdf20d

Please sign in to comment.