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 65e6c8a commit f31808a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 31 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
8 changes: 2 additions & 6 deletions example2/example2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,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_strncpy(out_filename, argv[i], sizeof(out_filename));
} else
return error("Invalid option: %s\n", argv[i]);
}
Expand Down Expand Up @@ -143,7 +139,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_strncpy(dst_filename, out_filename, FILENAME_MAX);
} 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 f31808a

Please sign in to comment.