Listar volumenes en consola

This commit is contained in:
OugonNoHane 2022-10-29 20:51:45 +02:00 committed by Hane
commit a7a8ba613f

View file

@ -97,24 +97,47 @@ int listDirectory(std::string path, std::vector<char*> *directoryContents){
if (nextOcurrence == NULL) return 0;
*/
int explodePaths(TCHAR* volumePaths, int volumePathsBufferSize, char separator, std::vector<char*> *dest){
static int lmao = 0;
int explodePaths(TCHAR* volumePaths, int volumePathsBufferSize, char separator, std::vector<char*> *dest, int depth = 0){
//Por alguna razon esta puta mierda que dice acabar con 2 NULL acaba con 3; de locos.
//https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getvolumepathnamesforvolumenamew
static int lmao = 0;
char* nextOcurrence = strchr(volumePaths, separator);
//TODO Test de cadena original
//TODO Test de cadena original;
// if (debug && lmao) {
// std::cout << std::to_string(lmao) + ":lmao " << std::endl;
// for (int i = 0; i < volumePathsBufferSize; i++){
// std::cout << std::to_string(i) + ": ";
// std::cout << volumePaths[i];
// std::cout << " | ";
// }
// std::cout << std::endl << "I IA" << std::endl;
// exit(1);
// }
// if (debug && volumePaths[0] == 'E') lmao = 1;
char* itemPath;
itemPath = (char*)calloc(1, volumePathsBufferSize);
//for (int i = 0; &volumePaths[i] == nextOcurrence; i++) {
for (int i = 0; volumePaths[i] != nextOcurrence[0]; i++) {
itemPath[i] = volumePaths[i];
int pathLengthIdx = 0;
for (; volumePaths[pathLengthIdx] != nextOcurrence[0]; pathLengthIdx++) {
if (debug) { std::cout << volumePaths[0]; std::cout << " ENTERS " + std::to_string(pathLengthIdx) << std::endl; }
itemPath[pathLengthIdx] = volumePaths[pathLengthIdx];
}
dest->push_back(itemPath);
if (debug) {
std::cout << dest->at(dest->size() - 1);
std::cout << " EXPLODEPATH" << std::endl;
}
//TODO Test de recursividad
//if (debug) { std::cout << dest->at(0) << std::endl; exit(1); }
if (debug && lmao == 3) { std::cout << volumePaths << std::endl; exit(1); }
lmao++;
if (nextOcurrence[0] == nextOcurrence[sizeof(TCHAR)]) return 0;
return explodePaths(nextOcurrence, volumePathsBufferSize, separator, dest);
//if (debug && lmao == 3) { std::cout << volumePaths << std::endl; exit(1); }
//lmao++;
depth++;
if (nextOcurrence[0] == nextOcurrence[1]) return depth;
return explodePaths(nextOcurrence + sizeof(TCHAR), (volumePathsBufferSize - pathLengthIdx), separator, dest);
}
@ -131,17 +154,10 @@ int listVolumes(std::vector<char*> *onPresentPaths){
int volumePathsBufferSize = (MAX_PATH + 1) * sizeof(TCHAR);
unsigned long volumePathLength;
TCHAR volumePaths[volumePathsBufferSize];
//TODO BORRAR
std::fill(volumePaths, volumePaths + volumePathsBufferSize, '4');
for (int i = 0; i < volumePathsBufferSize; i++){
std::cout << std::to_string(i) + ": ";
std::cout << volumePaths[i];
std::cout << " | ";
}
std::cout << std::endl << "I IA" << std::endl;
//TODO necesario? di
std::fill(volumePaths, volumePaths + volumePathsBufferSize, '\0');
hFind = FindFirstVolume(volumeName, volumeNameSize);
//if (debug) std::cout << volumeNameSize << std::endl;
hFind = FindFirstVolume(volumeName, volumeNameSize); //if (debug) std::cout << volumeNameSize << std::endl;
if (INVALID_HANDLE_VALUE == hFind) return -2;
do {
//if (debug) std::cout << volumeName << std::endl;
@ -150,20 +166,19 @@ int listVolumes(std::vector<char*> *onPresentPaths){
if (volumePathLength == 1) { if (debug) std::cout << "Skill Issue" << std::endl; continue; }
if (debug) std::cout << volumeName;
if (debug) {
if (debug && volumePaths[0] == 'E') {
std::cout << volumePathLength << std::endl;
/*exit(1);*/
for (int i = 0; i < volumePathsBufferSize; i++){
std::cout << std::to_string(i) + ": ";
std::cout << volumePaths[i];
std::cout << " | ";
}
std::cout << std::endl << "I IA" << std::endl;
exit(1);
}
if (debug) {
// if (debug && volumePaths[0] == 'E') {
// std::cout << volumePathLength << std::endl;
// /*exit(1);*/
// for (int i = 0; i < volumePathsBufferSize; i++){
// std::cout << std::to_string(i) + ": ";
// std::cout << volumePaths[i];
// std::cout << " | ";
// }
// std::cout << std::endl << "I IA" << std::endl;
// exit(1);
// }
if(explodePaths(volumePaths, volumePathsBufferSize, separator, onPresentPaths)){
if (debug) std::cout << volumePaths << std::endl;
for (int i = 0; i < onPresentPaths->size(); i++){
std::cout << onPresentPaths->at(i) << std::endl;
}
@ -178,7 +193,8 @@ int listVolumes(std::vector<char*> *onPresentPaths){
}
numVolumes++;
//TODO necesario? di CAMBIAR 4 A NUL
std::fill(volumePaths, volumePaths + volumePathsBufferSize, '4');
} while (FindNextVolume(hFind, volumeName, volumeNameSize) != 0);
FindVolumeClose(hFind);
@ -309,7 +325,7 @@ int main(int, char**)
static bool isListVolumesAdequate = true;
if (debug && isListVolumesAdequate) {
isListVolumesAdequate = false;
std::cout << isListVolumesAdequate << std::endl;
//std::cout << isListVolumesAdequate << std::endl;
std::vector<char*> onPresentPaths;
listVolumes(&onPresentPaths);
}