forked from Unity-Technologies/crunch
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: factorize splitpath, fix bugs
- Loading branch information
Showing
6 changed files
with
65 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters