Skip to content

Commit 5a14ea6

Browse files
committed
Cleanup.
1 parent 6124940 commit 5a14ea6

File tree

8 files changed

+108
-53
lines changed

8 files changed

+108
-53
lines changed

include/bx/filepath.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,13 @@ namespace bx
7979
///
8080
void join(const StringView& _str);
8181

82-
/// Returns C string to file path.
82+
/// Implicitly converts FilePath to StringView.
8383
///
84-
const char* get() const;
84+
operator StringView() const;
85+
86+
/// Returns zero-terminated C string pointer to file path.
87+
///
88+
const char* getCPtr() const;
8589

8690
/// If path is `/abv/gd/555/333/pod.mac` returns `/abv/gd/555/333/`.
8791
///

include/bx/platform.h

+37
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@
252252
# endif // BX_CRT_*
253253
#endif // !BX_CRT_NONE
254254

255+
///
255256
#define BX_PLATFORM_POSIX (0 \
256257
|| BX_PLATFORM_ANDROID \
257258
|| BX_PLATFORM_BSD \
@@ -266,6 +267,7 @@
266267
|| BX_PLATFORM_STEAMLINK \
267268
)
268269

270+
///
269271
#define BX_PLATFORM_NONE !(0 \
270272
|| BX_PLATFORM_ANDROID \
271273
|| BX_PLATFORM_BSD \
@@ -283,6 +285,41 @@
283285
|| BX_PLATFORM_XBOXONE \
284286
)
285287

288+
///
289+
#define BX_PLATFORM_OS_CONSOLE (0 \
290+
|| BX_PLATFORM_NX \
291+
|| BX_PLATFORM_PS4 \
292+
|| BX_PLATFORM_WINRT \
293+
|| BX_PLATFORM_XBOXONE \
294+
)
295+
296+
///
297+
#define BX_PLATFORM_OS_DESKTOP (0 \
298+
|| BX_PLATFORM_BSD \
299+
|| BX_PLATFORM_HURD \
300+
|| BX_PLATFORM_LINUX \
301+
|| BX_PLATFORM_OSX \
302+
|| BX_PLATFORM_WINDOWS \
303+
)
304+
305+
///
306+
#define BX_PLATFORM_OS_EMBEDDED (0 \
307+
|| BX_PLATFORM_RPI \
308+
|| BX_PLATFORM_STEAMLINK \
309+
)
310+
311+
///
312+
#define BX_PLATFORM_OS_MOBILE (0 \
313+
|| BX_PLATFORM_ANDROID \
314+
|| BX_PLATFORM_IOS \
315+
)
316+
317+
///
318+
#define BX_PLATFORM_OS_WEB (0 \
319+
|| BX_PLATFORM_EMSCRIPTEN \
320+
)
321+
322+
///
286323
#if BX_COMPILER_GCC
287324
# define BX_COMPILER_NAME "GCC " \
288325
BX_STRINGIZE(__GNUC__) "." \

include/compat/msvc/dirent.h

+9
Original file line numberDiff line numberDiff line change
@@ -1043,13 +1043,17 @@ dirent_mbstowcs_s(
10431043
DWORD flags;
10441044

10451045
/* Determine code page for multi-byte string */
1046+
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
10461047
if (AreFileApisANSI ()) {
10471048
/* Default ANSI code page */
10481049
cp = GetACP ();
10491050
} else {
10501051
/* Default OEM code page */
10511052
cp = GetOEMCP ();
10521053
}
1054+
#else
1055+
cp = CP_ACP;
1056+
#endif // WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
10531057

10541058
/*
10551059
* Determine flags based on the character set. For more information,
@@ -1134,13 +1138,18 @@ dirent_wcstombs_s(
11341138
LPBOOL pflag;
11351139

11361140
/* Determine code page for multi-byte string */
1141+
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
11371142
if (AreFileApisANSI ()) {
11381143
/* Default ANSI code page */
11391144
cp = GetACP ();
11401145
} else {
11411146
/* Default OEM code page */
11421147
cp = GetOEMCP ();
11431148
}
1149+
#else
1150+
cp = CP_ACP;
1151+
#endif // WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
1152+
11441153

11451154
/* Compute the length of input string without zero-terminator */
11461155
len = 0;

src/file.cpp

+16-14
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,24 @@
66
#include "bx_p.h"
77
#include <bx/file.h>
88

9-
#if BX_CRT_NONE
10-
# include "crt0.h"
11-
#else
12-
# include <dirent.h>
13-
# include <stdio.h>
14-
# include <sys/stat.h>
15-
#endif // !BX_CRT_NONE
16-
179
#ifndef BX_CONFIG_CRT_FILE_READER_WRITER
1810
# define BX_CONFIG_CRT_FILE_READER_WRITER !BX_CRT_NONE
1911
#endif // BX_CONFIG_CRT_FILE_READER_WRITER
2012

2113
#ifndef BX_CONFIG_CRT_DIRECTORY_READER
22-
# define BX_CONFIG_CRT_DIRECTORY_READER !BX_CRT_NONE
14+
# define BX_CONFIG_CRT_DIRECTORY_READER (BX_PLATFORM_OS_DESKTOP && !BX_CRT_NONE)
2315
#endif // BX_CONFIG_CRT_DIRECTORY_READER
2416

17+
#if BX_CRT_NONE
18+
# include "crt0.h"
19+
#else
20+
# if BX_CONFIG_CRT_DIRECTORY_READER
21+
# include <dirent.h>
22+
# endif // BX_CONFIG_CRT_DIRECTORY_READER
23+
# include <stdio.h>
24+
# include <sys/stat.h>
25+
#endif // !BX_CRT_NONE
26+
2527
namespace bx
2628
{
2729
class NoopWriterImpl : public FileWriterI
@@ -100,7 +102,7 @@ namespace bx
100102
return false;
101103
}
102104

103-
m_file = fopen(_filePath.get(), "rb");
105+
m_file = fopen(_filePath.getCPtr(), "rb");
104106
if (NULL == m_file)
105107
{
106108
BX_ERROR_SET(_err, BX_ERROR_READERWRITER_OPEN, "FileReader: Failed to open file.");
@@ -180,7 +182,7 @@ namespace bx
180182
return false;
181183
}
182184

183-
m_file = fopen(_filePath.get(), _append ? "ab" : "wb");
185+
m_file = fopen(_filePath.getCPtr(), _append ? "ab" : "wb");
184186

185187
if (NULL == m_file)
186188
{
@@ -576,7 +578,7 @@ namespace bx
576578
{
577579
BX_CHECK(NULL != _err, "Reader/Writer interface calling functions must handle errors.");
578580

579-
m_dir = opendir(_filePath.get() );
581+
m_dir = opendir(_filePath.getCPtr() );
580582

581583
if (NULL == m_dir)
582584
{
@@ -742,7 +744,7 @@ namespace bx
742744

743745
# if BX_COMPILER_MSVC
744746
struct ::_stat64 st;
745-
int32_t result = ::_stat64(_filePath.get(), &st);
747+
int32_t result = ::_stat64(_filePath.getCPtr(), &st);
746748

747749
if (0 != result)
748750
{
@@ -759,7 +761,7 @@ namespace bx
759761
}
760762
# else
761763
struct ::stat st;
762-
int32_t result = ::stat(_filePath.get(), &st);
764+
int32_t result = ::stat(_filePath.getCPtr(), &st);
763765
if (0 != result)
764766
{
765767
return false;

src/filepath.cpp

+35-31
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#if !BX_CRT_NONE
1212
# include <stdio.h> // remove
13-
# include <dirent.h> // opendir
1413

1514
# if BX_CRT_MSVC
1615
# include <direct.h> // _getcwd
@@ -336,7 +335,12 @@ namespace bx
336335
set(tmp);
337336
}
338337

339-
const char* FilePath::get() const
338+
FilePath::operator StringView() const
339+
{
340+
return StringView(m_filePath, strLen(m_filePath) );
341+
}
342+
343+
const char* FilePath::getCPtr() const
340344
{
341345
return m_filePath;
342346
}
@@ -360,7 +364,7 @@ namespace bx
360364
return StringView(fileName.getPtr()+1);
361365
}
362366

363-
return get();
367+
return getCPtr();
364368
}
365369

366370
StringView FilePath::getBaseName() const
@@ -414,14 +418,14 @@ namespace bx
414418
}
415419

416420
#if BX_CRT_MSVC
417-
int32_t result = ::_mkdir(_filePath.get() );
421+
int32_t result = ::_mkdir(_filePath.getCPtr() );
418422
#elif BX_CRT_MINGW
419-
int32_t result = ::mkdir(_filePath.get());
423+
int32_t result = ::mkdir(_filePath.getCPtr());
420424
#elif BX_CRT_NONE
421425
BX_UNUSED(_filePath);
422426
int32_t result = -1;
423427
#else
424-
int32_t result = ::mkdir(_filePath.get(), 0700);
428+
int32_t result = ::mkdir(_filePath.getCPtr(), 0700);
425429
#endif // BX_CRT_MSVC
426430

427431
if (0 != result)
@@ -455,7 +459,7 @@ namespace bx
455459
return false;
456460
}
457461

458-
const StringView dir = strRTrim(_filePath.get(), "/");
462+
const StringView dir = strRTrim(_filePath, "/");
459463
const StringView slash = strRFind(dir, '/');
460464

461465
if (!slash.isEmpty()
@@ -487,18 +491,18 @@ namespace bx
487491
{
488492
if (FileType::Dir == fi.type)
489493
{
490-
result = ::_rmdir(_filePath.get() );
494+
result = ::_rmdir(_filePath.getCPtr() );
491495
}
492496
else
493497
{
494-
result = ::remove(_filePath.get() );
498+
result = ::remove(_filePath.getCPtr() );
495499
}
496500
}
497501
#elif BX_CRT_NONE
498502
BX_UNUSED(_filePath);
499503
int32_t result = -1;
500504
#else
501-
int32_t result = ::remove(_filePath.get() );
505+
int32_t result = ::remove(_filePath.getCPtr() );
502506
#endif // BX_CRT_MSVC
503507

504508
if (0 != result)
@@ -535,38 +539,38 @@ namespace bx
535539
return false;
536540
}
537541

538-
#if BX_CRT_NONE
539-
BX_UNUSED(_filePath);
540-
return false;
541-
#elif BX_PLATFORM_WINDOWS \
542-
|| BX_PLATFORM_LINUX \
543-
|| BX_PLATFORM_OSX
544-
DIR* dir = opendir(_filePath.get() );
545-
if (NULL == dir)
542+
Error err;
543+
DirectoryReader dr;
544+
545+
if (!bx::open(&dr, _filePath) )
546546
{
547547
BX_ERROR_SET(_err, BX_ERROR_NOT_DIRECTORY, "File already exist, and is not directory.");
548548
return false;
549549
}
550550

551-
for (dirent* item = readdir(dir); NULL != item; item = readdir(dir) )
551+
while (err.isOk() )
552552
{
553-
if (0 == strCmp(item->d_name, ".")
554-
|| 0 == strCmp(item->d_name, "..") )
555-
{
556-
continue;
557-
}
553+
bx::read(&dr, fi, &err);
558554

559-
FilePath path(_filePath);
560-
path.join(item->d_name);
561-
if (!removeAll(path, _err) )
555+
if (err.isOk() )
562556
{
563-
_err->reset();
564-
break;
557+
if (0 == strCmp(fi.filePath, ".")
558+
|| 0 == strCmp(fi.filePath, "..") )
559+
{
560+
continue;
561+
}
562+
563+
FilePath path(_filePath);
564+
path.join(fi.filePath);
565+
if (!removeAll(path, _err) )
566+
{
567+
_err->reset();
568+
break;
569+
}
565570
}
566571
}
567572

568-
closedir(dir);
569-
#endif // !BX_CRT_NONE
573+
bx::close(&dr);
570574

571575
return remove(_filePath, _err);
572576
}

src/os.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ namespace bx
174174
void* dlopen(const FilePath& _filePath)
175175
{
176176
#if BX_PLATFORM_WINDOWS
177-
return (void*)::LoadLibraryA(_filePath.get() );
177+
return (void*)::LoadLibraryA(_filePath.getCPtr() );
178178
#elif BX_PLATFORM_EMSCRIPTEN \
179179
|| BX_PLATFORM_PS4 \
180180
|| BX_PLATFORM_XBOXONE \
@@ -183,7 +183,7 @@ namespace bx
183183
BX_UNUSED(_filePath);
184184
return NULL;
185185
#else
186-
return ::dlopen(_filePath.get(), RTLD_LOCAL|RTLD_LAZY);
186+
return ::dlopen(_filePath.getCPtr(), RTLD_LOCAL|RTLD_LAZY);
187187
#endif // BX_PLATFORM_
188188
}
189189

src/process.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ namespace bx
4848
}
4949

5050
char tmp[kMaxFilePath*2] = "\"";
51-
strCat(tmp, BX_COUNTOF(tmp), _filePath.get() );
51+
strCat(tmp, BX_COUNTOF(tmp), _filePath);
5252
strCat(tmp, BX_COUNTOF(tmp), "\" ");
5353
strCat(tmp, BX_COUNTOF(tmp), _args);
5454

@@ -119,7 +119,7 @@ namespace bx
119119
}
120120

121121
char tmp[kMaxFilePath*2] = "\"";
122-
strCat(tmp, BX_COUNTOF(tmp), _filePath.get() );
122+
strCat(tmp, BX_COUNTOF(tmp), _filePath);
123123
strCat(tmp, BX_COUNTOF(tmp), "\" ");
124124
strCat(tmp, BX_COUNTOF(tmp), _args);
125125

tests/filepath_test.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ TEST_CASE("FilePath", "")
9696
const FilePathTest& test = s_filePathTest[ii];
9797

9898
fp.set(test.filePath);
99-
const bx::StringView result = fp.get();
10099

101-
REQUIRE(0 == bx::strCmp(test.expected, result) );
100+
REQUIRE(0 == bx::strCmp(test.expected, fp) );
102101
}
103102

104103
for (uint32_t ii = 0; ii < BX_COUNTOF(s_filePathSplit); ++ii)

0 commit comments

Comments
 (0)