#include #include #include #include #include <3ds.h> #include "util.h" #include "../ui/error.h" #include "../ui/section/task/task.h" extern void cleanup(); static int util_get_line_length(PrintConsole* console, const char* str) { int lineLength = 0; while(*str != 0) { if(*str == '\n') { break; } lineLength++; if(lineLength >= console->consoleWidth - 1) { break; } str++; } return lineLength; } static int util_get_lines(PrintConsole* console, const char* str) { int lines = 1; int lineLength = 0; while(*str != 0) { if(*str == '\n') { lines++; lineLength = 0; } else { lineLength++; if(lineLength >= console->consoleWidth - 1) { lines++; lineLength = 0; } } str++; } return lines; } void util_panic(const char* s, ...) { va_list list; va_start(list, s); char buf[1024]; vsnprintf(buf, 1024, s, list); va_end(list); gspWaitForVBlank(); u16 width; u16 height; for(int i = 0; i < 2; i++) { memset(gfxGetFramebuffer(GFX_TOP, GFX_LEFT, &width, &height), 0, (size_t) (width * height * 3)); memset(gfxGetFramebuffer(GFX_TOP, GFX_RIGHT, &width, &height), 0, (size_t) (width * height * 3)); memset(gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, &width, &height), 0, (size_t) (width * height * 3)); gfxSwapBuffers(); } PrintConsole* console = consoleInit(GFX_TOP, NULL); const char* header = "FBI has encountered a fatal error!"; const char* footer = "Press any button to exit."; printf("\x1b[0;0H"); for(int i = 0; i < console->consoleWidth; i++) { printf("-"); } printf("\x1b[%d;0H", console->consoleHeight - 1); for(int i = 0; i < console->consoleWidth; i++) { printf("-"); } printf("\x1b[0;%dH%s", (console->consoleWidth - util_get_line_length(console, header)) / 2, header); printf("\x1b[%d;%dH%s", console->consoleHeight - 1, (console->consoleWidth - util_get_line_length(console, footer)) / 2, footer); int bufRow = (console->consoleHeight - util_get_lines(console, buf)) / 2; char* str = buf; while(*str != 0) { if(*str == '\n') { bufRow++; str++; continue; } else { int lineLength = util_get_line_length(console, str); char old = *(str + lineLength); *(str + lineLength) = '\0'; printf("\x1b[%d;%dH%s", bufRow, (console->consoleWidth - lineLength) / 2, str); *(str + lineLength) = old; bufRow++; str += lineLength; } } gfxFlushBuffers(); gspWaitForVBlank(); while(aptMainLoop()) { hidScanInput(); if(hidKeysDown() & ~KEY_TOUCH) { break; } gspWaitForVBlank(); } cleanup(); exit(1); } FS_Path* util_make_path_utf8(const char* path) { size_t len = strlen(path); u16* utf16 = (u16*) calloc(len + 1, sizeof(u16)); if(utf16 == NULL) { return NULL; } ssize_t utf16Len = utf8_to_utf16(utf16, (const uint8_t*) path, len); FS_Path* fsPath = (FS_Path*) calloc(1, sizeof(FS_Path)); if(fsPath == NULL) { free(utf16); return NULL; } fsPath->type = PATH_UTF16; fsPath->size = (utf16Len + 1) * sizeof(u16); fsPath->data = utf16; return fsPath; } void util_free_path_utf8(FS_Path* path) { free((void*) path->data); free(path); } FS_Path util_make_binary_path(const void* data, u32 size) { FS_Path path = {PATH_BINARY, size, data}; return path; } bool util_is_dir(FS_Archive archive, const char* path) { Result res = 0; FS_Path* fsPath = util_make_path_utf8(path); if(fsPath != NULL) { Handle dirHandle = 0; if(R_SUCCEEDED(res = FSUSER_OpenDirectory(&dirHandle, archive, *fsPath))) { FSDIR_Close(dirHandle); } util_free_path_utf8(fsPath); } else { res = R_FBI_OUT_OF_MEMORY; } return R_SUCCEEDED(res); } Result util_ensure_dir(FS_Archive archive, const char* path) { Result res = 0; FS_Path* fsPath = util_make_path_utf8(path); if(fsPath != NULL) { Handle dirHandle = 0; if(R_SUCCEEDED(FSUSER_OpenDirectory(&dirHandle, archive, *fsPath))) { FSDIR_Close(dirHandle); } else { FSUSER_DeleteFile(archive, *fsPath); res = FSUSER_CreateDirectory(archive, *fsPath, 0); } util_free_path_utf8(fsPath); } else { res = R_FBI_OUT_OF_MEMORY; } return res; } void util_get_path_file(char* out, const char* path, u32 size) { const char* start = NULL; const char* end = NULL; const char* curr = path - 1; while((curr = strchr(curr + 1, '/')) != NULL) { start = end != NULL ? end : path; end = curr; } if(end != path + strlen(path) - 1) { start = end; end = path + strlen(path); } if(end - start == 0) { strncpy(out, "/", size); } else { u32 terminatorPos = end - start - 1 < size - 1 ? end - start - 1 : size - 1; strncpy(out, start + 1, terminatorPos); out[terminatorPos] = '\0'; } } void util_get_parent_path(char* out, const char* path, u32 size) { size_t pathLen = strlen(path); const char* start = NULL; const char* end = NULL; const char* curr = path - 1; while((curr = strchr(curr + 1, '/')) != NULL && (start == NULL || curr != path + pathLen - 1)) { start = end != NULL ? end : path; end = curr; } u32 terminatorPos = end - path + 1 < size - 1 ? end - path + 1 : size - 1; strncpy(out, path, terminatorPos); out[terminatorPos] = '\0'; }