Skip to content

Commit

Permalink
examples: factorize splitpath, fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
illwieckz committed Jul 7, 2024
1 parent a67c199 commit 2842cd8
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 69 deletions.
52 changes: 9 additions & 43 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_platform.h"
#include "example_platform.h"

// stb_image, for loading/saving image files.
#ifdef _MSC_VER
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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;
Expand Down
48 changes: 48 additions & 0 deletions example1/example_platform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include <string.h>

#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
}
1 change: 1 addition & 0 deletions example2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 3 additions & 13 deletions example2/example2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "timer.h"

#include "crn_platform.h"
#include "example_platform.h"

using namespace crnlib;

Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions example3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 3 additions & 13 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_platform.h"
#include "example_platform.h"

// stb_image, for loading/saving image files.
#ifdef _MSC_VER
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2842cd8

Please sign in to comment.