139 lines
2.9 KiB
C
139 lines
2.9 KiB
C
////////////////////////////////
|
|
//~ Path helpers
|
|
|
|
String F_GetFull(Arena *arena, String path)
|
|
{
|
|
return OS_GetFullPath(arena, path);
|
|
}
|
|
|
|
String F_GetFullCrossPlatform(Arena *arena, String path)
|
|
{
|
|
String result = OS_GetFullPath(arena, path);
|
|
for (u64 i = 0; i < result.len; ++i)
|
|
{
|
|
if (result.text[i] == '\\')
|
|
{
|
|
result.text[i] = '/';
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
String F_GetFileName(String path)
|
|
{
|
|
String result = path;
|
|
u64 start = path.len;
|
|
for (u64 i = path.len; i-- > 0;)
|
|
{
|
|
if (path.text[i] == '\\' || path.text[i] == '/')
|
|
{
|
|
start = i + 1;
|
|
break;
|
|
}
|
|
}
|
|
if (start < path.len)
|
|
{
|
|
result.text = &path.text[start];
|
|
result.len = path.len - start;
|
|
|
|
}
|
|
return result;
|
|
}
|
|
|
|
String F_GetParentDir(String path)
|
|
{
|
|
String result = ZI;
|
|
result.text = path.text;
|
|
u64 end = path.len;
|
|
for (u64 i = path.len; i-- > 0;)
|
|
{
|
|
if (path.text[i] == '\\' || path.text[i] == '/')
|
|
{
|
|
end = i + 1;
|
|
break;
|
|
}
|
|
}
|
|
if (end < path.len)
|
|
{
|
|
result.len = end;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
String F_ExtensionFromFile(String path)
|
|
{
|
|
String result = ZI;
|
|
u64 start = path.len;
|
|
for (u64 i = path.len; i-- > 0;)
|
|
{
|
|
if (path.text[i] == '.')
|
|
{
|
|
start = i + 1;
|
|
break;
|
|
}
|
|
}
|
|
if (start < path.len)
|
|
{
|
|
result.text = &path.text[start];
|
|
result.len = path.len - start;
|
|
|
|
}
|
|
return result;
|
|
}
|
|
|
|
////////////////////////////////
|
|
//~ File read operations
|
|
|
|
String F_DataFromFile(Arena *arena, String path)
|
|
{
|
|
String result = ZI;
|
|
OS_File file = OS_OpenFile(path, OS_FileFlag_Read);
|
|
result = OS_ReadEntireFile(arena, file);
|
|
OS_CloseFile(file);
|
|
return result;
|
|
}
|
|
|
|
////////////////////////////////
|
|
//~ File write operations
|
|
|
|
void F_ClearWrite(String path, String data)
|
|
{
|
|
OS_File file = OS_OpenFile(path, OS_FileFlag_Write | OS_FileFlag_Create);
|
|
OS_ClearWriteFile(file, data);
|
|
OS_CloseFile(file);
|
|
}
|
|
|
|
////////////////////////////////
|
|
//~ File attribute helpers
|
|
|
|
b32 F_IsFile(String path)
|
|
{
|
|
return OS_FileExists(path);
|
|
}
|
|
|
|
b32 F_IsDir(String path)
|
|
{
|
|
return OS_DirExists(path);
|
|
}
|
|
|
|
////////////////////////////////
|
|
//~ File iter operations
|
|
|
|
void F_FilesFromDir(Arena *arena, StringList *list, String dir, F_IterFlag flags)
|
|
{
|
|
TempArena scratch = BeginScratch(arena);
|
|
StringList tmp = ZI;
|
|
String dir_full_path = F_GetFull(scratch.arena, dir);
|
|
OS_DirContentsFromFullPath(scratch.arena, &tmp, dir_full_path);
|
|
for (StringListNode *n = tmp.first; n; n = n->next)
|
|
{
|
|
String file_joined_path = StringF(arena, "%F/%F", FmtString(dir), FmtString(n->s));
|
|
PushStringToList(arena, list, file_joined_path);
|
|
if (flags & F_IterFlag_Recurse && F_IsDir(file_joined_path))
|
|
{
|
|
F_FilesFromDir(arena, list, file_joined_path, flags);
|
|
}
|
|
}
|
|
EndScratch(scratch);
|
|
}
|