1
1
/*
2
2
Common code for Gust (Koei/Tecmo) PC games tools
3
- Copyright © 2019-2020 VitaSmith
3
+ Copyright © 2019-2021 VitaSmith
4
4
5
5
This program is free software: you can redistribute it and/or modify
6
6
it under the terms of the GNU General Public License as published by
@@ -28,12 +28,18 @@ bool create_path(char* path)
28
28
{
29
29
bool result = true;
30
30
struct stat64_t st ;
31
+ #if defined(_WIN32 )
32
+ // Ignore Windows drive names
33
+ if ((strlen (path ) == 2 ) && (path [1 ] == ':' ))
34
+ return true;
35
+ #endif
31
36
if (stat64_utf8 (path , & st ) != 0 ) {
32
37
// Directory doesn't exist, create it
33
38
size_t pos = 0 ;
34
39
for (size_t n = strlen (path ); n > 0 ; n -- ) {
35
40
if (path [n ] == PATH_SEP ) {
36
- pos = n ;
41
+ while ((n > 0 ) && (path [-- n ] == PATH_SEP ));
42
+ pos = n + 1 ;
37
43
break ;
38
44
}
39
45
}
@@ -61,6 +67,71 @@ bool create_path(char* path)
61
67
return result ;
62
68
}
63
69
70
+ // dirname/basename, that *PRESERVE* the string parameter.
71
+ // Note that these calls are not concurrent, meaning that you MUST be done
72
+ // using the returned string from a previous call before invoking again.
73
+ #if defined(_WIN32 )
74
+ char * _basename_win32 (const char * path , bool remove_extension )
75
+ {
76
+ static char basename [128 ];
77
+ static char ext [64 ];
78
+ ext [0 ] = 0 ;
79
+ _splitpath_s (path , NULL , 0 , NULL , 0 , basename , sizeof (basename ), ext , sizeof (ext ));
80
+ if ((ext [0 ] != 0 ) && !remove_extension )
81
+ strncat (basename , ext , sizeof (basename ) - strlen (basename ));
82
+ return basename ;
83
+ }
84
+
85
+ // This call should behave pretty similar to UNIX' dirname
86
+ char * _dirname_win32 (const char * path )
87
+ {
88
+ static char dir [PATH_MAX ];
89
+ static char drive [4 ];
90
+ int found_sep = 0 ;
91
+ memset (drive , 0 , sizeof (drive ));
92
+ _splitpath_s (path , drive , sizeof (drive ), dir , sizeof (dir ) - 3 , NULL , 0 , NULL , 0 );
93
+ // Only deal with drives that are one letter
94
+ drive [2 ] = 0 ;
95
+ drive [3 ] = 0 ;
96
+ if (drive [1 ] != ':' )
97
+ drive [0 ] = 0 ;
98
+ // Removing trailing path separators
99
+ for (int32_t n = (int32_t )strlen (dir ) - 1 ; (n > 0 ) && ((dir [n ] == '/' ) || (dir [n ] == '\\' )); n -- ) {
100
+ dir [n ] = 0 ;
101
+ found_sep ++ ;
102
+ }
103
+ if (dir [0 ] == 0 ) {
104
+ if (drive [0 ] == 0 )
105
+ return found_sep ? "\\" : "." ;
106
+ drive [2 ] = '\\' ;
107
+ return drive ;
108
+ }
109
+ if (drive [0 ] != 0 ) {
110
+ // Add the drive
111
+ memmove (& dir [2 ], dir , strlen (dir ) + 1 );
112
+ memcpy (dir , drive , strlen (drive ));
113
+ dir [2 ] = '\\' ;
114
+ }
115
+ return dir ;
116
+ }
117
+ #else
118
+ char * _basename_unix (const char * path )
119
+ {
120
+ static char path_copy [PATH_MAX ];
121
+ strncpy (path_copy , path , sizeof (path_copy ));
122
+ path_copy [PATH_MAX - 1 ] = 0 ;
123
+ return basename (path_copy );
124
+ }
125
+
126
+ char * _dirname_unix (const char * path )
127
+ {
128
+ static char path_copy [PATH_MAX ];
129
+ strncpy (path_copy , path , sizeof (path_copy ));
130
+ path_copy [PATH_MAX - 1 ] = 0 ;
131
+ return dirname (path_copy );
132
+ }
133
+ #endif
134
+
64
135
bool is_file (const char * path )
65
136
{
66
137
struct stat64_t st ;
@@ -76,7 +147,7 @@ bool is_directory(const char* path)
76
147
char * change_extension (const char * path , const char * extension )
77
148
{
78
149
static char new_path [PATH_MAX ];
79
- strncpy (new_path , basename ((char * )path ), sizeof (new_path ) - 1 );
150
+ strncpy (new_path , _basename ((char * )path ), sizeof (new_path ) - 1 );
80
151
for (size_t i = 0 ; i < sizeof (new_path ); i ++ ) {
81
152
if (new_path [i ] == '.' )
82
153
new_path [i ] = 0 ;
0 commit comments