impl historial y adminrigh error

This commit is contained in:
OugonNoHane 2022-11-06 22:58:45 +01:00
commit 8eea8336ee

188
main.cpp
View file

@ -6,6 +6,7 @@
#define IMGUI_IMPLEMENTATION #define IMGUI_IMPLEMENTATION
#define GL_SILENCE_DEPRECATION #define GL_SILENCE_DEPRECATION
//DEBUG MACRO
#define log_debugc(str, ...) do { if (debug) fprintf(stdout, "[DEBUG] (%s:%d): " (str) "\n", __FILE__, __LINE__, ##__VA_ARGS__); } while (0) #define log_debugc(str, ...) do { if (debug) fprintf(stdout, "[DEBUG] (%s:%d): " (str) "\n", __FILE__, __LINE__, ##__VA_ARGS__); } while (0)
#define log_debugcpp(str) do { \ #define log_debugcpp(str) do { \
if (debug) std::cout << "[DEBUG]" << "(" << __FILE__ << ":" << __LINE__ << "): " << str << std::endl; \ if (debug) std::cout << "[DEBUG]" << "(" << __FILE__ << ":" << __LINE__ << "): " << str << std::endl; \
@ -14,10 +15,14 @@
#define log_volume(str) if(debugVerbosity & DEBUG_VOLUME) log_debugcpp((str)) #define log_volume(str) if(debugVerbosity & DEBUG_VOLUME) log_debugcpp((str))
#define log_extension(str) if(debugVerbosity & DEBUG_EXTENSION) log_debugcpp((str)) #define log_extension(str) if(debugVerbosity & DEBUG_EXTENSION) log_debugcpp((str))
//PATH MACRO
#define MAX_LISTDIR_PATH_LENGTH (MAX_PATH - 3) #define MAX_LISTDIR_PATH_LENGTH (MAX_PATH - 3)
#define MAX_ERRORSTR_LEN 512 #define MAX_ERRORSTR_LEN 512
//HISTORY HANDLING MACRO
#define MIN_HISTORY_POS history->historyTraversalPos <= 0
#define MAX_HISTORY_POS history->historyTraversalPos >= history->historyDepth
#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 <vector>
@ -61,20 +66,49 @@ typedef struct {
} directoriesInfo; } directoriesInfo;
struct ErrorMessages{ struct History{
const std::string addrBarError = "Failed to open folder: invalid path"; std::vector<char*> previousPaths;
const std::string tableElementError = "Failed to open folder: access restricted to non-admin users"; //TODO C++ david momento
const std::string listDirectoryError = "Failed to open folder: access denied"; //int historyDepth = -1;
const std::string moveUpError = "Failed to move up: reached volume root?"; int historyDepth;
int historyTraversalPos;
bool isAdditionTime;
History(){
historyTraversalPos = historyDepth = -1;
isAdditionTime = true;
}
};
enum HistoryMovement {
HISTORY_FORWARD = 1,
HISTORY_BACKWARD = 0
};
enum ListDirectoryError {
DIRECTORY_ERROR_PATH_TOO_LONG = -1,
DIRECTORY_ERROR_ACCESSING_CONTENT = -2
};
enum agnosticDirError {
AGDIR_ERROR_ACCESS_DENIED = 1
}; };
// struct ErrorMessages{ // struct ErrorMessages{
// static constexpr char addrBarError[] = "Failed to open folder: invalid path"; // const std::string addrBarError = "Failed to open folder: invalid path";
// static constexpr char tableElementError[] = "Failed to open folder: access restricted to non-admin users"; // const std::string tableElementError = "Failed to open folder: access restricted to non-admin users";
// static constexpr char listDirectoryError[] = "Failed to open folder: access denied"; // const std::string listDirectoryError = "Failed to open folder: access denied";
// static constexpr char moveUpError[] = "Failed to move up: reached volume root?"; // const std::string moveUpError = "Failed to move up: reached volume root?";
// }; // };
namespace ErrorMessages {
char addrBarError[] = "Failed to open folder: invalid path";
char tableElementError[] = "Failed to open folder: access restricted to non-admin users";
char accessDeniedError[] = "Failed to open folder: access denied";
char listDirectoryError[] = "Failed to open folder: unknown error";
char moveUpError[] = "Failed to move up: reached volume root?";
};
//TODO UNICORDEO //TODO UNICORDEO
/* FILE PICKER MOMENTO */ /* FILE PICKER MOMENTO */
@ -99,10 +133,10 @@ int listDirectory(std::string path, std::vector<directoriesInfo*> *directoryCont
LARGE_INTEGER filesize; LARGE_INTEGER filesize;
int numFiles = 0; int numFiles = 0;
if (path.length() > (MAX_LISTDIR_PATH_LENGTH)) return -1; if (path.length() > (MAX_LISTDIR_PATH_LENGTH)) return DIRECTORY_ERROR_PATH_TOO_LONG;
path = path + "\\*"; path = path + "\\*";
hFind = FindFirstFile(path.c_str(), &ffd); hFind = FindFirstFile(path.c_str(), &ffd);
if (INVALID_HANDLE_VALUE == hFind) return -2; if (INVALID_HANDLE_VALUE == hFind) return DIRECTORY_ERROR_ACCESSING_CONTENT;
do { do {
if(!strcmp(ffd.cFileName, ".") || !strcmp(ffd.cFileName, "..")) continue; if(!strcmp(ffd.cFileName, ".") || !strcmp(ffd.cFileName, "..")) continue;
log_debugcpp("BUCLE listDirectory iteracion " + std::to_string(numFiles)); log_debugcpp("BUCLE listDirectory iteracion " + std::to_string(numFiles));
@ -141,6 +175,18 @@ int listDirectory(std::string path, std::vector<directoriesInfo*> *directoryCont
return numFiles; return numFiles;
} }
long getLastDirectorySystemError(){
long winLastError = GetLastError();
switch(winLastError){
case ERROR_ACCESS_DENIED:
return AGDIR_ERROR_ACCESS_DENIED;
break;
default:
return -1;
break;
}
}
/* VOLUMES */ /* VOLUMES */
//new char*[CharCount * sizeof(WCHAR) //new char*[CharCount * sizeof(WCHAR)
@ -166,7 +212,7 @@ int explodePaths(TCHAR* volumePaths, int volumePathsBufferSize, char separator,
} }
itemPath[pathLengthIdx + 1] = '\0'; itemPath[pathLengthIdx + 1] = '\0';
dest->push_back(itemPath); dest->push_back(itemPath);
//TODO concat de puto C
log_debugcpp(dest->at(dest->size() - 1) << " EXPLODEPATH"); log_debugcpp(dest->at(dest->size() - 1) << " EXPLODEPATH");
if (nextOcurrence[0] == nextOcurrence[1]) return depth; if (nextOcurrence[0] == nextOcurrence[1]) return depth;
@ -198,7 +244,7 @@ int listVolumes(std::vector<char*> *onPresentPaths){
if (volumePathLength == 1) { if (debug) std::cout << "Skill Issue" << std::endl; continue; } if (volumePathLength == 1) { if (debug) std::cout << "Skill Issue" << std::endl; continue; }
log_debugcpp(volumeName); log_debugcpp(volumeName);
//pathSchecker //DEBUG: pathSchecker
// if (debug && volumePaths[0] == 'E') { // if (debug && volumePaths[0] == 'E') {
// std::cout << volumePathLength << std::endl; // std::cout << volumePathLength << std::endl;
// /*exit(1);*/ // /*exit(1);*/
@ -260,18 +306,54 @@ bool inputMove(bool* error, char** str){
return true; return true;
} }
void handleFolderAccessResult(bool isListDirectoriesAdequate, char* currentPath, char* addrBarVal, char* errorDest, const char* errorContent){ void handleFolderAccessResult(bool isListDirectoriesAdequate, char* currentPath, \
if (isListDirectoriesAdequate) strcpy(currentPath, addrBarVal); char* addrBarVal, char* errorDest, const char* errorContent, History* history){
if (isListDirectoriesAdequate) { strcpy(currentPath, addrBarVal); history->isAdditionTime = true; }
else strcpy(addrBarVal, currentPath); else strcpy(addrBarVal, currentPath);
strcpy(errorDest, errorContent); strcpy(errorDest, errorContent);
} }
/* HISTORIAL */
void addPathToHistory(History *history, char* pathToAdd){
char* path;
int ajjj = history->previousPaths.size();
if (history->historyTraversalPos + 1 >= ajjj){
path = (char*)calloc(1, MAX_PATH);
history->previousPaths.push_back(path);
} else {
path = history->previousPaths.at(history->historyTraversalPos + 1);
}
strncpy(path, pathToAdd, MAX_PATH);
history->historyTraversalPos++;
history->historyDepth = history->historyTraversalPos;
history->isAdditionTime = false;
}
char** moveThroughHistory(History *history, HistoryMovement movement){
switch(movement){
case HISTORY_BACKWARD:
if(MIN_HISTORY_POS)
return &history->previousPaths.at(history->historyTraversalPos);
history->historyTraversalPos--;
return &history->previousPaths.at(history->historyTraversalPos);
break;
case HISTORY_FORWARD:
if(MAX_HISTORY_POS)
return &history->previousPaths.at(history->historyDepth);
history->historyTraversalPos++;
return &history->previousPaths.at(history->historyTraversalPos);
break;
default:
return &history->previousPaths.at(history->historyDepth);
}
}
/* FIN FILE PICKER MOMENTO */ /* FIN FILE PICKER MOMENTO */
int main(int, char**) int main(int, char**) {
{
// Setup window // Setup window
glfwSetErrorCallback(glfw_error_callback); glfwSetErrorCallback(glfw_error_callback);
if (!glfwInit()) if (!glfwInit())
@ -387,9 +469,10 @@ int main(int, char**)
{ {
ImGui::Begin("File Picker in 4K"); ImGui::Begin("File Picker in 4K");
//TODO is dis bector in hipp?
static History* history = new History();
static bool showError = false; static bool showError = false;
static char error[MAX_ERRORSTR_LEN]; static char error[MAX_ERRORSTR_LEN];
static ErrorMessages eMsg;
static bool isListVolumesAdequate = true; static bool isListVolumesAdequate = true;
static bool isListDirectoriesAdequate = true; static bool isListDirectoriesAdequate = true;
@ -432,18 +515,46 @@ int main(int, char**)
numFiles = listDirectory(std::string(currentPath), &directoryContents); numFiles = listDirectory(std::string(currentPath), &directoryContents);
//std::cout << numFiles; //std::cout << numFiles;
if (numFiles < 0) { if (numFiles < 0) {
//TODO quitar GetLastError() de aqui
long directoryErrorCode;
log_debugcpp("FALLO LA FUNCION, VALOR NEGATIVO " + std::to_string(numFiles)); log_debugcpp("FALLO LA FUNCION, VALOR NEGATIVO " + std::to_string(numFiles));
if (numFiles == DIRECTORY_ERROR_ACCESSING_CONTENT) directoryErrorCode = getLastDirectorySystemError();
showError = true; showError = true;
strcpy(error, eMsg.listDirectoryError.c_str()); switch(directoryErrorCode) {
case AGDIR_ERROR_ACCESS_DENIED:
strcpy(error, ErrorMessages::accessDeniedError);
strcpy(addrBarVal, history->previousPaths.at(history->historyTraversalPos));
strcpy(currentPath, history->previousPaths.at(history->historyTraversalPos));
numFiles = listDirectory(history->previousPaths.at(history->historyTraversalPos), &directoryContents);
break;
default:
strcpy(error, ErrorMessages::listDirectoryError);
strcpy(currentPath, addrBarVal); strcpy(currentPath, addrBarVal);
numFiles = listDirectory(std::string(currentPath), &directoryContents); numFiles = listDirectory(std::string(currentPath), &directoryContents);
history->isAdditionTime = false;
break;
}
history->isAdditionTime = false;
} else { } else {
showError = false; showError = false;
strcpy(addrBarVal, currentPath); strcpy(addrBarVal, currentPath);
if(history->isAdditionTime) addPathToHistory(history, addrBarVal);
} }
} }
//It's renderin' time /*
It's renderin' time
*/
static bool showHidden = false; static bool showHidden = false;
static bool filterByExtension = false; static bool filterByExtension = false;
static std::vector<std::string> extensions; static std::vector<std::string> extensions;
@ -454,32 +565,54 @@ int main(int, char**)
ImGui::Text("Select a file:"); ImGui::Text("Select a file:");
//Permanentes e increiblemente utilitarios botones en Cuatro K: MOVE UP //Permanentes e increiblemente utilitarios botones en Cuatro K: BACK
ImGui::BeginDisabled(MIN_HISTORY_POS);
if(ImGui::Button("Back")) {
char** interfaceMovementButtonsPath = moveThroughHistory(history, HISTORY_BACKWARD);
isListDirectoriesAdequate = inputMove(&showError, interfaceMovementButtonsPath);
//TODO: ERROR
strcpy(error, ErrorMessages::moveUpError);
}
ImGui::EndDisabled();
//FORWARD
ImGui::SameLine();
ImGui::BeginDisabled(MAX_HISTORY_POS);
if(ImGui::Button("Forward")) {
char** interfaceMovementButtonsPath = moveThroughHistory(history, HISTORY_FORWARD);
isListDirectoriesAdequate = inputMove(&showError, interfaceMovementButtonsPath);
strcpy(error, ErrorMessages::moveUpError);
}
ImGui::EndDisabled();
//MOVE UP
ImGui::SameLine();
//c pervirtio con unicode std::wstring s(L"←→↑↓"); //c pervirtio con unicode std::wstring s(L"←→↑↓");
static char moveUp[] = ".."; static char moveUp[] = "..";
static char* moveUpPtr = &moveUp[0]; static char* moveUpPtr = &moveUp[0];
if(ImGui::Button("Move Up")) { if(ImGui::Button("Move Up")) {
isListDirectoriesAdequate = inputMove(&showError, (&moveUpPtr)); isListDirectoriesAdequate = inputMove(&showError, (&moveUpPtr));
strcpy(error, eMsg.moveUpError.c_str()); strcpy(error, ErrorMessages::moveUpError);
history->isAdditionTime = true;
} }
ImGui::SameLine(); ImGui::SameLine();
//BARRA DE DIRECCIONES //BARRA DE DIRECCIONES
//TODO a point
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x - (ImGui::CalcTextSize("Enter").x + ImGui::GetStyle().ItemSpacing.x * 2) ); ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x - (ImGui::CalcTextSize("Enter").x + ImGui::GetStyle().ItemSpacing.x * 2) );
if(ImGui::InputText("##addrbar", addrBarVal, IM_ARRAYSIZE(addrBarVal), ImGuiInputTextFlags_EnterReturnsTrue )){ if(ImGui::InputText("##addrbar", addrBarVal, IM_ARRAYSIZE(addrBarVal), ImGuiInputTextFlags_EnterReturnsTrue )){
log_debugcpp(currentPath <<" ADDRBAR INTENTO"); log_debugcpp(currentPath <<" ADDRBAR INTENTO");
isListDirectoriesAdequate = inputMove(&showError, &addrBarValPtr); isListDirectoriesAdequate = inputMove(&showError, &addrBarValPtr);
handleFolderAccessResult(isListDirectoriesAdequate, currentPath, addrBarVal, error, eMsg.addrBarError.c_str()); handleFolderAccessResult(isListDirectoriesAdequate, currentPath, addrBarVal, error, ErrorMessages::addrBarError, history);
}; };
ImGui::PopItemWidth(); ImGui::PopItemWidth();
ImGui::SameLine(); ImGui::SameLine();
if(ImGui::Button("Enter")){ if(ImGui::Button("Enter")){
log_debugcpp(currentPath <<" ADDRBAR INTENTO"); log_debugcpp(currentPath <<" ADDRBAR INTENTO");
isListDirectoriesAdequate = inputMove(&showError, &addrBarValPtr); isListDirectoriesAdequate = inputMove(&showError, &addrBarValPtr);
handleFolderAccessResult(isListDirectoriesAdequate, currentPath, addrBarVal, error, eMsg.addrBarError.c_str()); handleFolderAccessResult(isListDirectoriesAdequate, currentPath, addrBarVal, error, ErrorMessages::addrBarError, history);
} }
//ImGui::Text("%s TEMPADDRBAR", currentPath); //ImGui::Text("%s TEMPADDRBAR", currentPath);
log_debugcpp(currentPath <<" LEMISIIMIA");
//LA GRAN TABLACIÓN: ACTOR EN LAS SOMBRAS //LA GRAN TABLACIÓN: ACTOR EN LAS SOMBRAS
static ImGuiTableFlags splitterTableFlags = ImGuiTableFlags_Resizable | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV; static ImGuiTableFlags splitterTableFlags = ImGuiTableFlags_Resizable | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV;
if (ImGui::BeginTable("##splitterTable", 2, splitterTableFlags, ImVec2(-FLT_MIN, 0.8f * ImGui::GetTextLineHeightWithSpacing()))){ if (ImGui::BeginTable("##splitterTable", 2, splitterTableFlags, ImVec2(-FLT_MIN, 0.8f * ImGui::GetTextLineHeightWithSpacing()))){
@ -504,6 +637,7 @@ int main(int, char**)
ImGui::PushStyleColor(ImGuiCol_ButtonActive, (ImVec4)ImColor::HSV(i / 7.0f, 0.8f, 0.8f)); ImGui::PushStyleColor(ImGuiCol_ButtonActive, (ImVec4)ImColor::HSV(i / 7.0f, 0.8f, 0.8f));
if(ImGui::Button(onPresentPaths.at(i))){ if(ImGui::Button(onPresentPaths.at(i))){
isListDirectoriesAdequate = inputMove(&showError, &onPresentPaths.at(i)); isListDirectoriesAdequate = inputMove(&showError, &onPresentPaths.at(i));
history->isAdditionTime = true;
} }
ImGui::PopStyleColor(3); ImGui::PopStyleColor(3);
if (i != onPresentPaths.size() - 1) ImGui::TableNextRow(); if (i != onPresentPaths.size() - 1) ImGui::TableNextRow();
@ -545,7 +679,7 @@ int main(int, char**)
strcat(addrBarVal, "\\"); strcat(addrBarVal, "\\");
strcat(addrBarVal, directoryContents.at(idx)->name); strcat(addrBarVal, directoryContents.at(idx)->name);
isListDirectoriesAdequate = inputMove(&showError, &addrBarValPtr); isListDirectoriesAdequate = inputMove(&showError, &addrBarValPtr);
handleFolderAccessResult(isListDirectoriesAdequate, currentPath, addrBarVal, error, eMsg.tableElementError.c_str()); handleFolderAccessResult(isListDirectoriesAdequate, currentPath, addrBarVal, error, ErrorMessages::tableElementError, history);
//currentItemIdx = -1; //currentItemIdx = -1;
} }
ImGui::TableNextColumn(); ImGui::TableNextColumn();