Skip to content

Commit 0b20fdc

Browse files
committed
example2: rewrite without splitpath_s
1 parent c6a5d46 commit 0b20fdc

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

example2/example2.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,6 @@ int main(int argc, char* argv[]) {
9999
return error("Invalid option: %s\n", argv[i]);
100100
}
101101

102-
// Split the source filename into its various components.
103-
#if defined(_WIN32)
104-
char drive_buf[_MAX_DRIVE], dir_buf[_MAX_DIR], fname_buf[_MAX_FNAME], ext_buf[_MAX_EXT];
105-
if (_splitpath_s(pSrc_filename, drive_buf, _MAX_DRIVE, dir_buf, _MAX_DIR, fname_buf, _MAX_FNAME, ext_buf, _MAX_EXT))
106-
return error("Invalid source filename!\n");
107-
#else
108-
char in_filename[FILENAME_MAX];
109-
strncpy(in_filename, pSrc_filename, FILENAME_MAX);
110-
const char drive_buf[] = "";
111-
char *dir_buf = dirname(in_filename);
112-
char *fname_buf = basename(in_filename);
113-
char *dot = strrchr(fname_buf, '.');
114-
if (dot && dot != fname_buf)
115-
*dot = '\0';
116-
#endif
117-
118102
// Load the source file into memory.
119103
printf("Loading source file: %s\n", pSrc_filename);
120104
crn_uint32 src_file_size;
@@ -152,9 +136,22 @@ int main(int argc, char* argv[]) {
152136

153137
// Now create the DDS file.
154138
char dst_filename[FILENAME_MAX];
155-
crnlib_snprintf(dst_filename, sizeof(dst_filename), "%s%s%s.dds", drive_buf, dir_buf, fname_buf);
156-
if (out_filename[0])
139+
if (out_filename[0]) {
157140
strcpy(dst_filename, out_filename);
141+
} else {
142+
unsigned int stripped_length = UINT32_MAX;
143+
const char* ext_begin = strrchr(pSrc_filename, '.');
144+
if (ext_begin) {
145+
#ifdef _WIN32
146+
const char* sep = strpbrk(ext_begin, "/\\");
147+
#else
148+
const char* sep = strpbrk(ext_begin, "/");
149+
#endif
150+
if (!sep)
151+
stripped_length = ext_begin - pSrc_filename;
152+
}
153+
crnlib_snprintf(dst_filename, sizeof(dst_filename), "%-*s.dds", stripped_length, pSrc_filename);
154+
}
158155

159156
printf("Writing DDS file: %s\n", dst_filename);
160157

0 commit comments

Comments
 (0)