diff --git a/example1/example1.cpp b/example1/example1.cpp index 49668530..2e90da7e 100644 --- a/example1/example1.cpp +++ b/example1/example1.cpp @@ -20,6 +20,7 @@ #include "dds_defs.h" #include "crn_platform.h" +#include "example_platform.h" // stb_image, for loading/saving image files. #ifdef _MSC_VER @@ -332,24 +333,10 @@ int main(int argc, char* argv[]) { } // Split the source filename into its various components. -#if defined(_WIN32) - char drive_buf[_MAX_DRIVE], dir_buf[_MAX_DIR], fname_buf[_MAX_FNAME], ext_buf[_MAX_EXT]; - if (_splitpath_s(pSrc_filename, drive_buf, _MAX_DRIVE, dir_buf, _MAX_DIR, fname_buf, _MAX_FNAME, ext_buf, _MAX_EXT)) + char drive_buf[EXAMPLE_MAX_DRIVE], dir_buf[EXAMPLE_MAX_DIR], fname_buf[EXAMPLE_MAX_FNAME], ext_buf[EXAMPLE_MAX_EXT]; + + if (!example_splitpath(pSrc_filename, drive_buf, dir_buf, fname_buf, ext_buf)) return error("Invalid source filename!\n"); -#else - char in_filename[FILENAME_MAX]; - strncpy(in_filename, pSrc_filename, FILENAME_MAX); - const char drive_buf[] = ""; - char *dir_buf = dirname(in_filename); - char *fname_buf = basename(in_filename); - char *dot = strrchr(fname_buf, '.'); - char ext_buf[FILENAME_MAX]; - ext_buf[0] = '\0'; - if (dot && dot != fname_buf) { - strncpy(ext_buf, dot, strlen(dot)); - *dot = '\0'; - } -#endif // Load the source file into memory. printf("Loading source file: %s\n", pSrc_filename); @@ -387,19 +374,10 @@ int main(int argc, char* argv[]) { // If the user has explicitly specified an output file, check the output file's extension to ensure we write the expected format. if (out_filename[0]) { -#if defined(_WIN32) - char out_fname_buf[_MAX_FNAME], out_ext_buf[_MAX_EXT]; - _splitpath_s(out_filename, NULL, 0, NULL, 0, out_fname_buf, _MAX_FNAME, out_ext_buf, _MAX_EXT); -#else - char *out_fname_buf = basename( in_filename ); - dot = strrchr(out_fname_buf, '.'); - char out_ext_buf[FILENAME_MAX]; - out_ext_buf[0] = '\0'; - if (dot && dot != fname_buf) { - strncpy(out_ext_buf, dot, strlen(dot)); - *dot = '\0'; - } -#endif + char out_fname_buf[EXAMPLE_MAX_FNAME], out_ext_buf[EXAMPLE_MAX_EXT]; + + example_splitpath(out_filename, NULL, NULL, out_fname_buf, out_ext_buf); + if (!crnlib_stricmp(out_ext_buf, ".crn")) output_crn = true; else if (!crnlib_stricmp(out_ext_buf, ".dds")) @@ -561,19 +539,7 @@ int main(int argc, char* argv[]) { } else if (crnlib_stricmp(ext_buf, ".dds") == 0) { // Unpack DDS to one or more TGA's. if (out_filename[0]) { -#if defined(_WIN32) - _splitpath_s(out_filename, drive_buf, _MAX_DRIVE, dir_buf, _MAX_DIR, fname_buf, _MAX_FNAME, ext_buf, _MAX_EXT); -#else - dir_buf = dirname(out_filename); - fname_buf = basename(out_filename); - dot = strrchr(fname_buf, '.'); - ext_buf[FILENAME_MAX]; - ext_buf[0] = '\0'; - if (dot && dot != fname_buf) { - strncpy(ext_buf, dot, strlen(dot)); - *dot = '\0'; - } -#endif + example_splitpath(out_filename, drive_buf, dir_buf, fname_buf, ext_buf); } crn_texture_desc tex_desc; diff --git a/example1/example_platform.h b/example1/example_platform.h new file mode 100644 index 00000000..91952d51 --- /dev/null +++ b/example1/example_platform.h @@ -0,0 +1,48 @@ +#pragma once + +#include + +#if defined(_WIN32) +#define EXAMPLE_MAX_DRIVE _MAX_DRIVE +#define EXAMPLE_MAX_DIR _MAX_DIR +#define EXAMPLE_MAX_FNAME _MAX_FNAME +#define EXAMPLE_MAX_EXT _MAX_EXT +#else +#define EXAMPLE_MAX_DRIVE 1 +#define EXAMPLE_MAX_DIR FILENAME_MAX +#define EXAMPLE_MAX_FNAME FILENAME_MAX +#define EXAMPLE_MAX_EXT FILENAME_MAX +#endif + +// It is assumed fname_buf and ext_buf are never NULL. +bool example_splitpath(const char *filename, char *drive_buf, char *dir_buf, char *fname_buf, char *ext_buf) { +#if defined(_WIN32) + return !_splitpath_s(filename, drive_buf, _MAX_DRIVE, dir_buf, _MAX_DIR, fname_buf, _MAX_FNAME, ext_buf, _MAX_EXT); +#else + if (!filename || !filename[0]) { + return false; + } + if (drive_buf) { + drive_buf[0] = '\0'; + } + char in_filename[FILENAME_MAX]; + if (dir_buf) { + strncpy(in_filename, filename, FILENAME_MAX); + char *dir = dirname(in_filename); + strncpy(dir_buf, dir, FILENAME_MAX); + size_t len = strnlen(dir_buf, FILENAME_MAX); + dir_buf[len] = '/'; + dir_buf[len + 1] = '\0'; + } + strncpy(in_filename, filename, FILENAME_MAX); + char *fname = basename(in_filename); + strncpy(fname_buf, fname, FILENAME_MAX); + char *dot = strrchr(fname_buf, '.'); + ext_buf[0] = '\0'; + if (dot && dot != fname_buf) { + strncpy(ext_buf, dot, strlen(dot) + 1); + *dot = '\0'; + } + return true; +#endif +} diff --git a/example2/CMakeLists.txt b/example2/CMakeLists.txt index 08d9814c..8a1bc313 100644 --- a/example2/CMakeLists.txt +++ b/example2/CMakeLists.txt @@ -1,6 +1,7 @@ include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/../inc ${CMAKE_CURRENT_SOURCE_DIR}/../crnlib + ${CMAKE_CURRENT_SOURCE_DIR}/../example1 ) # Defines the source code for the library diff --git a/example2/example2.cpp b/example2/example2.cpp index 0cc40a32..bc0a4a51 100644 --- a/example2/example2.cpp +++ b/example2/example2.cpp @@ -25,6 +25,7 @@ #include "timer.h" #include "crn_platform.h" +#include "example_platform.h" using namespace crnlib; @@ -100,20 +101,9 @@ int main(int argc, char* argv[]) { } // Split the source filename into its various components. -#if defined(_WIN32) - char drive_buf[_MAX_DRIVE], dir_buf[_MAX_DIR], fname_buf[_MAX_FNAME], ext_buf[_MAX_EXT]; - if (_splitpath_s(pSrc_filename, drive_buf, _MAX_DRIVE, dir_buf, _MAX_DIR, fname_buf, _MAX_FNAME, ext_buf, _MAX_EXT)) + char drive_buf[EXAMPLE_MAX_DRIVE], dir_buf[EXAMPLE_MAX_DIR], fname_buf[EXAMPLE_MAX_FNAME], ext_buf[EXAMPLE_MAX_EXT]; + if (!example_splitpath(pSrc_filename, drive_buf, dir_buf, fname_buf, ext_buf)) return error("Invalid source filename!\n"); -#else - char in_filename[FILENAME_MAX]; - strncpy(in_filename, pSrc_filename, FILENAME_MAX); - const char drive_buf[] = ""; - char *dir_buf = dirname(in_filename); - char *fname_buf = basename(in_filename); - char *dot = strrchr(fname_buf, '.'); - if (dot && dot != fname_buf) - *dot = '\0'; -#endif // Load the source file into memory. printf("Loading source file: %s\n", pSrc_filename); diff --git a/example3/CMakeLists.txt b/example3/CMakeLists.txt index 3ae8b3e0..7c3e1528 100644 --- a/example3/CMakeLists.txt +++ b/example3/CMakeLists.txt @@ -1,6 +1,7 @@ include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/../inc ${CMAKE_CURRENT_SOURCE_DIR}/../crnlib + ${CMAKE_CURRENT_SOURCE_DIR}/../example1 ) # Defines the source code for the library diff --git a/example3/example3.cpp b/example3/example3.cpp index 80189b49..f1e5536e 100644 --- a/example3/example3.cpp +++ b/example3/example3.cpp @@ -19,6 +19,7 @@ #include "dds_defs.h" #include "crn_platform.h" +#include "example_platform.h" // stb_image, for loading/saving image files. #ifdef _MSC_VER @@ -136,20 +137,9 @@ int main(int argc, char* argv[]) { } // Split the source filename into its various components. -#if defined(_WIN32) - char drive_buf[_MAX_DRIVE], dir_buf[_MAX_DIR], fname_buf[_MAX_FNAME], ext_buf[_MAX_EXT]; - if (_splitpath_s(pSrc_filename, drive_buf, _MAX_DRIVE, dir_buf, _MAX_DIR, fname_buf, _MAX_FNAME, ext_buf, _MAX_EXT)) + char drive_buf[EXAMPLE_MAX_DRIVE], dir_buf[EXAMPLE_MAX_DIR], fname_buf[EXAMPLE_MAX_FNAME], ext_buf[EXAMPLE_MAX_EXT]; + if (!example_splitpath(pSrc_filename, drive_buf, dir_buf, fname_buf, ext_buf)) return error("Invalid source filename!\n"); -#else - char in_filename[FILENAME_MAX]; - strncpy(in_filename, pSrc_filename, FILENAME_MAX); - const char drive_buf[] = ""; - char *dir_buf = dirname(in_filename); - char *fname_buf = basename(in_filename); - char *dot = strrchr(fname_buf, '.'); - if (dot && dot != fname_buf) - *dot = '\0'; -#endif // Load the source image into memory. printf("Loading source file: %s\n", pSrc_filename);