diff --git a/main.c b/main.c index 9f625f0..5088493 100644 --- a/main.c +++ b/main.c @@ -29,6 +29,7 @@ #include #include #include +#include // Prints the program license to the user. int printLicense(int full){ @@ -49,7 +50,7 @@ int printLicense(int full){ } // Cleans a directory. -void cleanDir(const char* path, unsigned* objectCount){ +void cleanDir(const char* path, unsigned* objectCount, unsigned long* totalDeletedBytes){ const char* validPath = path; //char validPath[sizeof(path) + 1]; //strcpy(validPath, path); @@ -85,7 +86,7 @@ void cleanDir(const char* path, unsigned* objectCount){ // If the item is a directory, call cleanDir on that directory. if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){ - cleanDir(fullPath, objectCount); + cleanDir(fullPath, objectCount, totalDeletedBytes); /*int shellReturnCode = shellRemoveDir(fullPath); if (shellReturnCode != 0){ printf(" %s\n", STR_FAILED_DIR); @@ -95,6 +96,21 @@ void cleanDir(const char* path, unsigned* objectCount){ continue; } + // Get the length of the file before deleting it. + HANDLE fileHandle = CreateFile(fullPath, GENERIC_READ, + FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, NULL); + + LARGE_INTEGER size; + unsigned long length = 0; + + if (fileHandle != INVALID_HANDLE_VALUE){ + if (GetFileSizeEx(fileHandle, &size)){ + length = size.QuadPart; + } + CloseHandle(fileHandle); + } + if (DeleteFile(fullPath) == 0){ int lastError = GetLastError(); printf(" %s\n", STR_FAILED_FILE); @@ -104,6 +120,7 @@ void cleanDir(const char* path, unsigned* objectCount){ printf(" %s\n", STR_DELETED_FILE); // Update the deleted object count if the process was successful. (*objectCount)++; + (*totalDeletedBytes) += length; } } while(FindNextFile(hFind, &data)); } else { @@ -114,6 +131,7 @@ void cleanDir(const char* path, unsigned* objectCount){ // Gets the user defined directories void getAdditionalDirs(bool safeMode){ unsigned* objectCount = 0; + unsigned long* totalDeletedBytes = 0; printf("\n%s\n", STR_FETCHING_ADD_DIRS); int dirs; dirs = GetPrivateProfileInt("Pastas", "Pastas", 3, PATH_CFG); @@ -192,7 +210,7 @@ void getAdditionalDirs(bool safeMode){ printf("%s [%s] %s\n", STR_FOUND_DIR, dirKey, buffer); if (!safeMode) - cleanDir(buffer, &objectCount); + cleanDir(buffer, &objectCount, &totalDeletedBytes); free(buffer); free(varBuffer); continue; @@ -201,9 +219,24 @@ void getAdditionalDirs(bool safeMode){ printf("%s [%s] %s\n", STR_FOUND_DIR, dirKey, currentPath); if (!safeMode) - cleanDir(currentPath, &objectCount); + cleanDir(currentPath, &objectCount, &totalDeletedBytes); } printf(FMT_STR_OBJECT_COUNT, objectCount); + // Determine whether we should output the size as bytes, KiB, MiB or GiB. + + if (totalDeletedBytes < 1024){ + printf(FMT_STR_TOTAL_DELETED_BYTES, totalDeletedBytes); + } else if (totalDeletedBytes < (unsigned long) pow(1024, 2)){ + float kib = (unsigned long) totalDeletedBytes / pow(1024, 1); + printf(FMT_STR_TOTAL_DELETED_KIBIBYTES, kib); + } else if (totalDeletedBytes < (unsigned long) pow(1024, 3)){ + float mib = (unsigned long) totalDeletedBytes / pow(1024, 2);// / (float) 1024 / (float) 1024; + printf("%lu\n", totalDeletedBytes); + printf(FMT_STR_TOTAL_DELETED_MEBIBYTES, mib); + } else { + float gib = (unsigned long) totalDeletedBytes / pow(1024, 3); + printf(FMT_STR_TOTAL_DELETED_GIBIBYTES, gib); + } } int main(void){ diff --git a/str.h b/str.h index d3dd9de..58b2c6c 100644 --- a/str.h +++ b/str.h @@ -22,7 +22,7 @@ #define STR_LN "--------------------------------------" -#define STR_USE_WARNING "Aviso: é recomendado fechar todos os programas abertos antes de utilizar este programa para poder remover mais arquivos temporários. Deseja continuar? (S/n)" +#define STR_USE_WARNING "Aviso: é recomendado fechar todos os programas abertos antes de utilizar este programa para poder remover mais arquivos temporários. Deseja continuar? (s/n)" #define STR_FETCHING_ADD_DIRS "Recolhendo pastas adicionais..." @@ -38,6 +38,10 @@ #define STR_FAILED_DIR "[Pasta mantida] Não foi possível apagar esta pasta pelo seguinte erro:" #define FMT_STR_OBJECT_COUNT "[Relatório] %u objetos foram apagados com sucesso.\n" +#define FMT_STR_TOTAL_DELETED_BYTES "[Relatório] %lu baites ou mais foram liberados." +#define FMT_STR_TOTAL_DELETED_KIBIBYTES "[Relatório] %.2f KiB ou mais foram liberados." +#define FMT_STR_TOTAL_DELETED_MEBIBYTES "[Relatório] %.2f MiB ou mais foram liberados." +#define FMT_STR_TOTAL_DELETED_GIBIBYTES "[Relatório] %.2f GiB ou mais foram liberados." #define STR_MEM_ERR "[Erro fatal] Não foi possível alocar memória. Tente fechar alguns programas abertos e tente novamente."