File lister now handles .. in directory names

This commit is contained in:
Peter Slattery 2020-09-07 14:28:41 -07:00
parent 15a2cb080e
commit 6491a88d8a
2 changed files with 45 additions and 15 deletions

View File

@ -15,17 +15,23 @@ struct file_view_state
input_command* FileView_Commands = 0; input_command* FileView_Commands = 0;
s32 FileView_CommandsCount = 0; s32 FileView_CommandsCount = 0;
// TODO(pjs): 2 - On Change Dir function
// - clears the memory arena
// - enumerates the new directory in the memory arena
internal void internal void
FileViewUpdateWorkingDirectory(gs_const_string WorkingDirectory, file_view_state* State, context Context) FileViewUpdateWorkingDirectory(gs_const_string WorkingDirectory, file_view_state* State, context Context)
{ {
ClearArena(&State->FileNamesArena); ClearArena(&State->FileNamesArena);
gs_const_string SanitizedDirectory = WorkingDirectory;
u32 LastSlashIndex = FindLast(SanitizedDirectory, '\\');
gs_const_string LastDir = Substring(SanitizedDirectory, LastSlashIndex + 1, SanitizedDirectory.Length);
if (StringsEqual(LastDir, ConstString("..")))
{
u32 SecondLastSlashIndex = FindLast(SanitizedDirectory, LastSlashIndex - 1, '\\');
SanitizedDirectory = Substring(SanitizedDirectory, 0, SecondLastSlashIndex);
}
State->WorkingDirectory = PushString(&State->FileNamesArena, WorkingDirectory.Length + 2); State->WorkingDirectory = PushString(&State->FileNamesArena, WorkingDirectory.Length + 2);
PrintF(&State->WorkingDirectory, "%S", WorkingDirectory); PrintF(&State->WorkingDirectory, "%S", SanitizedDirectory);
if (State->WorkingDirectory.Str[State->WorkingDirectory.Length - 1] != '\\') if (State->WorkingDirectory.Str[State->WorkingDirectory.Length - 1] != '\\')
{ {
AppendPrintF(&State->WorkingDirectory, "\\"); AppendPrintF(&State->WorkingDirectory, "\\");
@ -80,7 +86,18 @@ FileView_Render(panel Panel, rect2 PanelBounds, render_command_buffer* RenderBuf
gs_file_info File = FileViewState->FileNames.Values[i]; gs_file_info File = FileViewState->FileNames.Values[i];
gs_string PathString = PushString(State->Transient, File.Path.Length); gs_string PathString = PushString(State->Transient, File.Path.Length);
PrintF(&PathString, "%S", File.Path); PrintF(&PathString, "%S", File.Path);
ui_LayoutDrawString(&State->Interface, &Layout, PathString, State->Interface.Style.TextColor); if (ui_LayoutListButton(&State->Interface, &Layout, PathString, i))
{
if (File.IsDirectory)
{
FileViewUpdateWorkingDirectory(File.Path, FileViewState, Context);
}
else
{
// TODO(pjs): Select the file
}
}
//ui_LayoutDrawString(&State->Interface, &Layout, PathString, State->Interface.Style.TextColor);
} }
} }

View File

@ -1366,7 +1366,7 @@ CharArrayLength (char* CS)
internal bool internal bool
IsNullTerminated(gs_const_string String) IsNullTerminated(gs_const_string String)
{ {
return (String.Str[String.Length - 1] != 0); return (String.Str[String.Length] == 0);
} }
internal bool internal bool
IsNullTerminated(gs_string String) IsNullTerminated(gs_string String)
@ -1455,13 +1455,21 @@ FindFirst(gs_const_string String, char C)
{ {
return FindFirst(String, 0, C); return FindFirst(String, 0, C);
} }
internal u64 internal u64
FindLast(gs_const_string String, char C) FindLast(gs_const_string String, u64 StartIndex, char C)
{ {
s64 Result = String.Length - 1; s64 Result = StartIndex;
for(; Result >= 0 && C != String.Str[Result]; Result--); for(; Result >= 0 && C != String.Str[Result]; Result--);
return (u64)Result; return (u64)Result;
} }
internal u64
FindLast(gs_const_string String, char C)
{
return FindLast(String, String.Length - 1, C);
}
internal u64 internal u64
FindFirstFromSet(gs_const_string String, char* SetArray) FindFirstFromSet(gs_const_string String, char* SetArray)
{ {
@ -2968,14 +2976,19 @@ CreateFileHandler(file_handler_get_file_info* GetFileInfo,
internal gs_const_string internal gs_const_string
GetNullTerminatedPath(gs_file_handler FileHandler, gs_const_string Path) GetNullTerminatedPath(gs_file_handler FileHandler, gs_const_string Path)
{ {
gs_const_string NullTermPath = Path; gs_const_string Result = {};
if (!IsNullTerminated(NullTermPath)) if (!IsNullTerminated(Path))
{ {
AssertMessage("need to allocate a new string, Path to it, and null terminate"); gs_string NullTermPath = PushString(FileHandler.Transient, Path.Length + 1);
// TODO(Peter): Probably want to have some sort of temp memory, PrintF(&NullTermPath, "%S", Path);
// or be passing in a thread context, etc. NullTerminate(&NullTermPath);
Result = NullTermPath.ConstString;
} }
return NullTermPath; else
{
Result = Path;
}
return Result;
} }
internal gs_file_info internal gs_file_info