Lectura directorio escindida main

This commit is contained in:
OugonNoHane 2022-10-27 17:01:21 +02:00
commit 564f840936

109
main.cpp
View file

@ -7,7 +7,10 @@
#define GL_SILENCE_DEPRECATION #define GL_SILENCE_DEPRECATION
#include "imgui/misc/single_file/imgui_single_file.h" #include "imgui/misc/single_file/imgui_single_file.h"
#include <iostream> #include <iostream>
#include <vector>
#include <Windows.h> #include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
// [Win32] Our example includes a copy of glfw3.lib pre-compiled with VS2010 to maximize ease of testing and compatibility with old VS compilers. // [Win32] Our example includes a copy of glfw3.lib pre-compiled with VS2010 to maximize ease of testing and compatibility with old VS compilers.
// To link with VS2010-era libraries, VS2015+ requires linking with legacy_stdio_definitions.lib, which we do using this pragma. // To link with VS2010-era libraries, VS2015+ requires linking with legacy_stdio_definitions.lib, which we do using this pragma.
@ -20,6 +23,52 @@ static void glfw_error_callback(int error, const char* description)
fprintf(stderr, "Glfw Error %d: %s\n", error, description); fprintf(stderr, "Glfw Error %d: %s\n", error, description);
} }
bool retrieveCurrentDirectory(char** currentPath){
if(GetCurrentDirectory(MAX_PATH, *currentPath)) return true;
return false;
}
int listDirectory(std::string path, std::vector<char*> *directoryContents){
HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA ffd;
LARGE_INTEGER filesize;
int numFiles = 0;
if (path.length() > (MAX_PATH - 3)) return -1;
path = path + "\\*";
hFind = FindFirstFile(path.c_str(), &ffd);
if (INVALID_HANDLE_VALUE == hFind) return -2;
do {
char* itemPath;
if (directoryContents->size() <= numFiles){
itemPath = (char*)calloc(1, MAX_PATH * sizeof(WCHAR));
directoryContents->push_back(itemPath);
} else {
itemPath = directoryContents->at(numFiles);
}
uint64_t idx = 0;
do {
itemPath[idx] = ffd.cFileName[idx];
idx++;
} while (ffd.cFileName[idx]);
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
strcat(itemPath, " <DIR>");
} else {
filesize.QuadPart = ((ffd.nFileSizeHigh * (MAXDWORD+1)) + ffd.nFileSizeLow);
char buf[16]; buf[0] = ' ';
itoa(filesize.QuadPart, buf + 1 , 10 );
strcat(itemPath, buf);
}
numFiles++;
} while (FindNextFile(hFind, &ffd) != 0);
FindClose(hFind);
return numFiles;
}
int main(int, char**) int main(int, char**)
@ -94,7 +143,7 @@ int main(int, char**)
bool show_demo_window = true; bool show_demo_window = true;
bool show_another_window = false; bool show_another_window = false;
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
// Main loop // Main loop
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
// Poll and handle events (inputs, window resize, etc.) // Poll and handle events (inputs, window resize, etc.)
@ -139,52 +188,42 @@ int main(int, char**)
{ {
ImGui::Begin("File Picker in 4K"); ImGui::Begin("File Picker in 4K");
//TODO Win32 //cursed C momento
HANDLE hFind = INVALID_HANDLE_VALUE; static char currentPath[MAX_PATH];
WIN32_FIND_DATA ffd; char* ptr = &currentPath[0];
LARGE_INTEGER filesize;
static char* debug32;
//
static int currentItemIdx = 0; static int currentItemIdx = 0;
ImGui::Text("Select a file:"); std::vector<char*> directoryContents;
hFind = FindFirstFile("C:\\Users\\Fabio\\Downloads\\Street Fighter 6 - Closed Beta\\*", &ffd);
if (INVALID_HANDLE_VALUE != hFind) { if(!retrieveCurrentDirectory(&ptr)) goto filepickerFailure;
if (ImGui::BeginListBox("fpLb", ImVec2(FLT_MIN, 25 * ImGui::GetTextLineHeightWithSpacing()))){
for (int n = 0; FindNextFile(hFind, &ffd) != 0; n++) { if(int numFiles = (listDirectory(".", &directoryContents))){
static std::string seleciable; ImGui::Text("Select a file:");
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (ImGui::BeginListBox("fpLb", ImVec2(-FLT_MIN, 25 * ImGui::GetTextLineHeightWithSpacing()))){
seleciable = " " + std::string(ffd.cFileName) + "<DIR>"; for (int i = 0; i < numFiles; i++) {
ImGui::Selectable(seleciable.c_str(), false); // ImGui::Selectable(directoryContents.at(i), false);
} else {
filesize.QuadPart = (long long)(ffd.nFileSizeHigh * (MAXDWORD+1)) + ffd.nFileSizeLow; const bool isSelected = (currentItemIdx == i);
seleciable = " " + std::string(ffd.cFileName) + " " + std::to_string(filesize.QuadPart); if (ImGui::Selectable(directoryContents.at(i), isSelected))
ImGui::Selectable(seleciable.c_str(), false); currentItemIdx = i;
}
//const bool isSelected = (currentItemIdx == n);
//if (ImGui::Selectable(items[n], is_selected))
// item_current_idx = n;
// Set the initial focus when opening the combo (scrolling + keyboard navigation focus) //Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
//if (is_selected) if (isSelected)
// ImGui::SetItemDefaultFocus(); ImGui::SetItemDefaultFocus();
// Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
//if (is_selected) ImGui::SetItemDefaultFocus();
} }
FindClose(hFind);
ImGui::EndListBox(); ImGui::EndListBox();
} }
} else { } else {
ImGui::Text("cagaste"); ImGui::Text("cagaste");
} }
//ImGui::TreePop(); //ImGui::TreePop();
filepickerFailure:
ImGui::End(); ImGui::End();
} }
// 3. Show another simple window. // 3. Show another simple window.
if (show_another_window) if (show_another_window)
{ {