Skip to content

Commit 1305484

Browse files
committed
refac: remove dumb root dir func from pl_string.h
1 parent f540c80 commit 1305484

4 files changed

Lines changed: 129 additions & 68 deletions

File tree

extensions/pl_vfs_ext.c

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,60 @@ plVfsResult pl_vfs_mount_memory (const char* directory, plVfsMountFlags);
147147
// [SECTION] internal api
148148
//-----------------------------------------------------------------------------
149149

150+
static bool
151+
pl__str_get_root_directory(const char* pcFilePath, char* pcDirectoryOut, size_t szOutSize)
152+
{
153+
size_t szLen = strlen(pcFilePath);
154+
strncpy(pcDirectoryOut, pcFilePath, szOutSize);
155+
156+
if(szLen > szOutSize || szOutSize < 2)
157+
return false;
158+
159+
if(pcDirectoryOut[0] == '/' || pcDirectoryOut[0] == '\\')
160+
{
161+
size_t szCurrentLocation = 1;
162+
bool bHit = false;
163+
while(szCurrentLocation < szLen)
164+
{
165+
if(pcDirectoryOut[szCurrentLocation] == '/' || pcDirectoryOut[szCurrentLocation] == '\\')
166+
{
167+
pcDirectoryOut[szCurrentLocation + 1] = 0;
168+
bHit = true;
169+
break;
170+
}
171+
172+
szCurrentLocation++;
173+
}
174+
if(!bHit)
175+
{
176+
pcDirectoryOut[1] = 0;
177+
}
178+
return true;
179+
}
180+
else if(pcDirectoryOut[1] == ':')
181+
{
182+
size_t szCurrentLocation = 3;
183+
while(szCurrentLocation < szLen)
184+
{
185+
if(pcDirectoryOut[szCurrentLocation] == '/' || pcDirectoryOut[szCurrentLocation] == '\\')
186+
{
187+
pcDirectoryOut[szCurrentLocation + 1] = 0;
188+
break;
189+
}
190+
191+
szCurrentLocation++;
192+
}
193+
return true;
194+
}
195+
else
196+
{
197+
pcDirectoryOut[0] = '.';
198+
pcDirectoryOut[1] = '/';
199+
pcDirectoryOut[2] = 0;
200+
}
201+
return true;
202+
}
203+
150204
static inline plVfsMemoryFile*
151205
pl__vfs_get_memory_file(plVfsFileSystem* ptSystem, const char* pcFile)
152206
{
@@ -176,6 +230,8 @@ pl__vfs_get_memory_file_index(plVfsFileSystem* ptSystem, const char* pcFile)
176230
static inline plVfsFile*
177231
pl__vfs_get_file(plVfsFileHandle tHandle)
178232
{
233+
if(tHandle.uData == UINT64_MAX)
234+
return NULL;
179235
if(gptVfsCtx->sbtFiles[tHandle.uIndex].uGeneration != tHandle.uGeneration)
180236
return NULL;
181237
return &gptVfsCtx->sbtFiles[tHandle.uIndex];
@@ -186,7 +242,7 @@ pl__vfs_get_system(const char* pcFile)
186242
{
187243
// retrieve root directory in order to find expected file system
188244
char acDirectory[PL_VFS_MAX_PATH_LENGTH] = {0};
189-
pl_str_get_root_directory(pcFile, acDirectory, PL_VFS_MAX_PATH_LENGTH);
245+
pl__str_get_root_directory(pcFile, acDirectory, PL_VFS_MAX_PATH_LENGTH);
190246

191247
// find file system
192248
const uint32_t uFileSystemIndexCount = pl_sb_size(gptVfsCtx->sbtFileSystems);
@@ -226,7 +282,7 @@ pl__vfs_register_file(const char* pcFile, bool bMustExist)
226282
if(ptTargetFileSystem->tType != PL_FILE_SYSTEM_TYPE_PHYSICAL)
227283
{
228284
char acDirectory[PL_VFS_MAX_PATH_LENGTH] = {0};
229-
pl_str_get_root_directory(pcFile, acDirectory, PL_VFS_MAX_PATH_LENGTH);
285+
pl__str_get_root_directory(pcFile, acDirectory, PL_VFS_MAX_PATH_LENGTH);
230286
szRootLength = strnlen(acDirectory, PL_VFS_MAX_PATH_LENGTH);
231287
}
232288

@@ -490,7 +546,7 @@ pl_vfs_does_file_exist(const char* pcFile)
490546

491547
// retrieve root directory in order to find expected file system
492548
char acDirectory[PL_VFS_MAX_PATH_LENGTH] = {0};
493-
pl_str_get_root_directory(pcFile, acDirectory, PL_VFS_MAX_PATH_LENGTH);
549+
pl__str_get_root_directory(pcFile, acDirectory, PL_VFS_MAX_PATH_LENGTH);
494550
size_t szRootLength = strnlen(acDirectory, PL_VFS_MAX_PATH_LENGTH);
495551

496552
// find file system
@@ -770,7 +826,6 @@ pl_vfs_file_size(const char* pcFile)
770826
plVfsFile* ptFile = pl__vfs_get_file(tHandle);
771827
if(ptFile == NULL)
772828
{
773-
PL_ASSERT(false && "file doesn't exist");
774829
return 0;
775830
}
776831
size_t szFileSize = 0;

libs/pl_string.h

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616

1717
// library version (format XYYZZ)
18-
#define PL_STRING_VERSION "1.1.1"
19-
#define PL_STRING_VERSION_NUM 10101
18+
#define PL_STRING_VERSION "1.1.2"
19+
#define PL_STRING_VERSION_NUM 10102
2020

2121
/*
2222
Index of this file:
@@ -54,7 +54,6 @@ const char* pl_str_get_file_extension(const char* pcFilePath, char* pcExtensionO
5454
const char* pl_str_get_file_name (const char* pcFilePath, char* pcFileOut, size_t szOutSize);
5555
bool pl_str_get_file_name_only(const char* pcFilePath, char* pcFileOut, size_t szOutSize);
5656
bool pl_str_get_directory (const char* pcFilePath, char* pcDirectoryOut, size_t szOutSize);
57-
bool pl_str_get_root_directory(const char* pcFilePath, char* pcDirectoryOut, size_t szOutSize);
5857

5958
// misc. opts
6059
bool pl_str_concatenate (const char* pcStr0, const char* pcStr1, char* pcStringOut, size_t szDataSize);
@@ -347,48 +346,6 @@ pl_str_get_directory(const char* pcFilePath, char* pcDirectoryOut, size_t szOutS
347346
return true;
348347
}
349348

350-
bool
351-
pl_str_get_root_directory(const char* pcFilePath, char* pcDirectoryOut, size_t szOutSize)
352-
{
353-
size_t szLen = strlen(pcFilePath);
354-
strncpy(pcDirectoryOut, pcFilePath, szOutSize);
355-
356-
if(szLen > szOutSize || szOutSize < 2)
357-
return false;
358-
359-
size_t szCurrentLocation = 0;
360-
int iHits = 2;
361-
while(szCurrentLocation < szLen)
362-
{
363-
if(pcDirectoryOut[szCurrentLocation] == '/' || pcDirectoryOut[szCurrentLocation] == '\\')
364-
{
365-
iHits--;
366-
}
367-
368-
if(iHits == 0)
369-
{
370-
pcDirectoryOut[szCurrentLocation + 1] = 0;
371-
break;
372-
}
373-
374-
szCurrentLocation++;
375-
}
376-
377-
if(iHits == 1)
378-
{
379-
pcDirectoryOut[0] = '/';
380-
pcDirectoryOut[1] = 0;
381-
pcDirectoryOut[2] = 0;
382-
}
383-
else if(szCurrentLocation == szLen)
384-
{
385-
pcDirectoryOut[0] = '.';
386-
pcDirectoryOut[1] = '/';
387-
pcDirectoryOut[2] = 0;
388-
}
389-
return true;
390-
}
391-
392349
bool
393350
pl_str_concatenate(const char* pcStr0, const char* pcStr1, char* pcStringOut, size_t szDataSize)
394351
{

tests/app_tests.c

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ Index of this file:
2525
#define PL_MATH_INCLUDE_FUNCTIONS
2626
#include "pl_math.h"
2727
#include "pl_json.h"
28+
#include "pl_string.h"
29+
30+
// stable extensions
31+
#include "pl_platform_ext.h"
2832

2933
// unstable extensions
3034
#include "pl_collision_ext.h"
@@ -44,6 +48,7 @@ const plDateTimeI* gptDateTime = NULL;
4448
const plVfsI* gptVfs = NULL;
4549
const plPakI* gptPak = NULL;
4650
const plCompressI* gptCompress = NULL;
51+
const plFileI* gptFile = NULL;
4752

4853
#define PL_ALLOC(x) gptMemory->tracked_realloc(NULL, (x), __FILE__, __LINE__)
4954
#define PL_REALLOC(x, y) gptMemory->tracked_realloc((x), (y), __FILE__, __LINE__)
@@ -70,6 +75,7 @@ typedef struct _plAppData
7075
void collision_only_tests_0(void*);
7176
void datetime_tests_0(void*);
7277
void vfs_tests_0(void*);
78+
void file_tests_0(void*);
7379

7480
//-----------------------------------------------------------------------------
7581
// [SECTION] pl_app_info
@@ -170,6 +176,7 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
170176
gptVfs = pl_get_api_latest(ptApiRegistry, plVfsI);
171177
gptPak = pl_get_api_latest(ptApiRegistry, plPakI);
172178
gptCompress = pl_get_api_latest(ptApiRegistry, plCompressI);
179+
gptFile = pl_get_api_latest(ptApiRegistry, plFileI);
173180

174181
// this path is taken only during first load, so we
175182
// allocate app memory here
@@ -179,6 +186,7 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
179186
// mount some directories
180187
gptVfs->mount_directory("/testing", "../out", PL_VFS_MOUNT_FLAGS_NONE);
181188
gptVfs->mount_memory("/ram", PL_VFS_MOUNT_FLAGS_NONE);
189+
gptVfs->mount_memory("/", PL_VFS_MOUNT_FLAGS_NONE);
182190

183191
plPakFile* ptPak = NULL;
184192
gptPak->begin_packing("../out/testing.pak", 1, &ptPak);
@@ -200,6 +208,8 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
200208
gptVfs->close_file(tHandle);
201209
tHandle = gptVfs->open_file("/ram/testing_compressed.json", PL_VFS_FILE_MODE_WRITE);
202210
szFileSize = gptVfs->write_file(tHandle, puBuffer, szFileSize);
211+
tHandle = gptVfs->open_file("/testing_compressed.json", PL_VFS_FILE_MODE_WRITE);
212+
szFileSize = gptVfs->write_file(tHandle, puBuffer, szFileSize);
203213
free(puBuffer);
204214

205215
// create
@@ -220,6 +230,9 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
220230
pl_test_register_test(vfs_tests_0, ptAppData);
221231
pl_test_run_suite("pl_vfs_ext.h");
222232

233+
pl_test_register_test(file_tests_0, ptAppData);
234+
pl_test_run_suite("pl_platform_ext.h (plFileI)");
235+
223236
return ptAppData;
224237
}
225238

@@ -379,16 +392,22 @@ vfs_tests_0(void* pAppData)
379392
gptVfs->close_file(tHandle);
380393

381394

382-
const char* acFiles[3] = {
395+
const char* acFiles[] = {
383396
"/ram/testing_compressed.json",
384397
"/data/testing_compressed.json",
385398
"/data/testing_uncompressed.json",
399+
"/testing_compressed.json",
386400
};
387401

388-
for(uint32_t i = 0; i < 3; i++)
402+
for(uint32_t i = 0; i < 4; i++)
389403
{
390404

391405
size_t szFileSize = gptVfs->get_file_size_str(acFiles[i]);
406+
if(szFileSize == 0)
407+
{
408+
pl_test_expect_true(false, NULL);
409+
continue;
410+
}
392411
tHandle = gptVfs->open_file(acFiles[i], PL_VFS_FILE_MODE_READ);
393412
char* pucBuffer = PL_ALLOC(szFileSize + 1);
394413
memset(pucBuffer, 0, szFileSize + 1);
@@ -492,6 +511,48 @@ vfs_tests_0(void* pAppData)
492511
}
493512
}
494513

514+
void
515+
file_tests_0(void* pAppData)
516+
{
517+
518+
const char** sbcFiles = NULL;
519+
pl_sb_push(sbcFiles, "pl_ds.h");
520+
pl_sb_push(sbcFiles, "pl_json.h");
521+
pl_sb_push(sbcFiles, "pl_log.h");
522+
pl_sb_push(sbcFiles, "pl_math.h");
523+
pl_sb_push(sbcFiles, "pl_memory.h");
524+
pl_sb_push(sbcFiles, "pl_profile.h");
525+
pl_sb_push(sbcFiles, "pl_stl.h");
526+
pl_sb_push(sbcFiles, "pl_string.h");
527+
pl_sb_push(sbcFiles, "pl_test.h");
528+
529+
plDirectoryInfo tInfo = {0};
530+
gptFile->get_directory_info("../libs", &tInfo);
531+
532+
pl_test_expect_uint32_equal(tInfo.uEntryCount, pl_sb_size(sbcFiles), NULL);
533+
534+
bool bFindMath = false;
535+
uint32_t uMathIndex = 0;
536+
for(uint32_t i = 0; i < tInfo.uEntryCount; i++)
537+
{
538+
539+
if(pl_str_equal(tInfo.sbtEntries[i].acName, "pl_math.h"))
540+
{
541+
bFindMath = true;
542+
uMathIndex = i;
543+
break;
544+
}
545+
}
546+
pl_test_expect_true(bFindMath, NULL);
547+
pl_test_expect_true(tInfo.sbtEntries[uMathIndex].tType == PL_DIRECTORY_ENTRY_TYPE_FILE, NULL);
548+
549+
gptFile->cleanup_directory_info(&tInfo);
550+
pl_sb_free(sbcFiles);
551+
552+
pl_test_expect_true(gptFile->directory_exists("../libs"), NULL);
553+
pl_test_expect_false(gptFile->directory_exists("../libs-offset"), NULL);
554+
}
555+
495556
//-----------------------------------------------------------------------------
496557
// [SECTION] unity build
497558
//-----------------------------------------------------------------------------
@@ -502,3 +563,6 @@ vfs_tests_0(void* pAppData)
502563

503564
#define PL_JSON_IMPLEMENTATION
504565
#include "pl_json.h"
566+
567+
#define PL_STRING_IMPLEMENTATION
568+
#include "pl_string.h"

tests/pl_string_tests.h

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ string_test_0(void* pData)
1111
const char* pcFilePath4 = "file1";
1212
const char* pcFilePath5 = "/tmp/dir2/file1.txt";
1313
const char* pcFilePath6 = "/tmp/file1.txt";
14+
const char* pcFilePath7 = "/file1.txt";
1415

1516
const char* pcExt0 = pl_str_get_file_extension(pcFilePath0, NULL, 0);
1617
const char* pcExt1 = pl_str_get_file_extension(pcFilePath1, NULL, 0);
@@ -60,6 +61,7 @@ string_test_0(void* pData)
6061
char acDirectory4[128] = {0};
6162
char acDirectory5[128] = {0};
6263
char acDirectory6[128] = {0};
64+
char acDirectory7[128] = {0};
6365

6466
pl_str_get_directory(pcFilePath0, acDirectory0, 128);
6567
pl_str_get_directory(pcFilePath1, acDirectory1, 128);
@@ -76,23 +78,6 @@ string_test_0(void* pData)
7678
pl_test_expect_string_equal(acDirectory4, "./", NULL);
7779
pl_test_expect_string_equal(acDirectory5, "/tmp/dir2/", NULL);
7880
pl_test_expect_string_equal(acDirectory6, "/tmp/", NULL);
79-
80-
pl_str_get_root_directory(pcFilePath0, acDirectory0, 128);
81-
pl_str_get_root_directory(pcFilePath1, acDirectory1, 128);
82-
pl_str_get_root_directory(pcFilePath2, acDirectory2, 128);
83-
pl_str_get_root_directory(pcFilePath3, acDirectory3, 128);
84-
pl_str_get_root_directory(pcFilePath4, acDirectory4, 128);
85-
pl_str_get_root_directory(pcFilePath5, acDirectory5, 128);
86-
pl_str_get_root_directory(pcFilePath6, acDirectory6, 128);
87-
88-
pl_test_expect_string_equal(acDirectory0, "C:/Users/", NULL);
89-
pl_test_expect_string_equal(acDirectory1, "C:\\Users\\", NULL);
90-
pl_test_expect_string_equal(acDirectory2, "C:\\Users/", NULL);
91-
pl_test_expect_string_equal(acDirectory3, "./", NULL);
92-
pl_test_expect_string_equal(acDirectory4, "./", NULL);
93-
pl_test_expect_string_equal(acDirectory5, "/tmp/", NULL);
94-
pl_test_expect_string_equal(acDirectory6, "/tmp/", NULL);
95-
9681
}
9782

9883
void

0 commit comments

Comments
 (0)