From e5024f8a3b822e2dfd91e590c3b2f4fdceddb4d6 Mon Sep 17 00:00:00 2001 From: Xpl0itU Date: Wed, 8 Jun 2022 16:29:49 +0200 Subject: [PATCH] remove using namespace --- include/savemng.h | 8 +++--- include/string.hpp | 23 ++++++++-------- src/json.cpp | 15 ++++++----- src/main.cpp | 20 +++++++------- src/savemng.cpp | 66 +++++++++++++++++++++++----------------------- src/string.cpp | 6 ++--- 6 files changed, 67 insertions(+), 71 deletions(-) diff --git a/include/savemng.h b/include/savemng.h index 27dcccb..b0167b8 100644 --- a/include/savemng.h +++ b/include/savemng.h @@ -20,8 +20,6 @@ #include #include -using namespace std; - #define PATH_SIZE 0x400 extern VPADStatus vpad_status; @@ -70,11 +68,11 @@ extern uint8_t wiiuaccn, sdaccn; void console_print_pos(int x, int y, const char *format, ...); -bool promptConfirm(Style st, string question); +bool promptConfirm(Style st, std::string question); void promptError(const char *message, ...); -string getUserID(); +std::string getUserID(); void getAccountsWiiU(); @@ -118,7 +116,7 @@ int32_t loadFilePart(const char *fPath, uint32_t start, uint32_t size, uint8_t * int32_t loadTitleIcon(Title *title); -void show_file_operation(string file_name, string file_src, string file_dest); +void show_file_operation(std::string file_name, std::string file_src, std::string file_dest); void console_print_pos_multiline(int x, int y, char cdiv, const char *format, ...); diff --git a/include/string.hpp b/include/string.hpp index 7cf6667..332aac6 100644 --- a/include/string.hpp +++ b/include/string.hpp @@ -4,17 +4,16 @@ #include #include -using namespace std; +bool replace(std::string &str, const std::string &from, const std::string &to); +std::string decodeXMLEscapeLine(std::string xmlString); -bool replace(string &str, const string &from, const string &to); -string decodeXMLEscapeLine(string xmlString); - -template -auto string_format(const string &format, Args... args) -> string { - int size_s = snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0' - if (size_s <= 0) { throw runtime_error("Error during formatting."); } - auto size = static_cast(size_s); - unique_ptr buf(new char[size]); - snprintf(buf.get(), size, format.c_str(), args...); - return string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside +template +std::string string_format( const std::string& format, Args ... args ) +{ + int size_s = std::snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0' + if( size_s <= 0 ){ throw std::runtime_error( "Error during formatting." ); } + auto size = static_cast( size_s ); + std::unique_ptr buf( new char[ size ] ); + std::snprintf( buf.get(), size, format.c_str(), args ... ); + return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside } diff --git a/src/json.cpp b/src/json.cpp index 0bb7545..86ae863 100644 --- a/src/json.cpp +++ b/src/json.cpp @@ -1,6 +1,8 @@ #include "json.h" #include "string.hpp" +#define FS_ALIGN(x) ((x + 0x3F) & ~(0x3F)) + json_t *load_json(const char *text) { json_t *root; json_error_t error; @@ -27,7 +29,6 @@ auto doit(char *text) -> char * { auto dofile(char *filename) -> char * { FILE *f = nullptr; long len = 0; - char *data = nullptr; /* open in read binary mode */ f = fopen(filename, "rb"); @@ -36,7 +37,7 @@ auto dofile(char *filename) -> char * { len = ftell(f); fseek(f, 0, SEEK_SET); - data = (char *) malloc(len + 1); + char* data = (char *) aligned_alloc(0x40, FS_ALIGN(len + 1)); fread(data, 1, len, f); data[len] = '\0'; @@ -47,17 +48,17 @@ auto dofile(char *filename) -> char * { return stuff; } -string getSlotDate(uint32_t highID, uint32_t lowID, uint8_t slot) { - string path = string_format("sd:/wiiu/backups/%08x%08x/%u/savemiiMeta.json", highID, lowID, slot); +std::string getSlotDate(uint32_t highID, uint32_t lowID, uint8_t slot) { + std::string path = string_format("sd:/wiiu/backups/%08x%08x/%u/savemiiMeta.json", highID, lowID, slot); if (checkEntry(path.c_str()) != 0) { - string info = dofile((char *) path.c_str()); + std::string info = dofile((char *) path.c_str()); return info; } return ""; } -auto setSlotDate(uint32_t highID, uint32_t lowID, uint8_t slot, string date) -> bool { - string path = string_format("sd:/wiiu/backups/%08x%08x/%u/savemiiMeta.json", highID, lowID, slot); +auto setSlotDate(uint32_t highID, uint32_t lowID, uint8_t slot, std::string date) -> bool { + std::string path = string_format("sd:/wiiu/backups/%08x%08x/%u/savemiiMeta.json", highID, lowID, slot); json_t *config = json_object(); if (config == nullptr) diff --git a/src/main.cpp b/src/main.cpp index 7b24105..e3f136a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,8 +5,6 @@ #include "savemng.h" #include "string.hpp" -using namespace std; - #define VERSION_MAJOR 1 #define VERSION_MINOR 4 #define VERSION_MICRO 1 @@ -101,7 +99,7 @@ Title *loadWiiUTitles(int run) { int tNoSave = usable; for (int i = 0; i <= 1; i++) { for (uint8_t a = 0; a < 2; a++) { - string path = string_format("%s:/usr/save/%s", (i == 0) ? "usb" : "mlc", highIDs[a]); + std::string path = string_format("%s:/usr/save/%s", (i == 0) ? "usb" : "mlc", highIDs[a]); DIR *dir = opendir(path.c_str()); if (dir != nullptr) { struct dirent *data; @@ -140,7 +138,7 @@ Title *loadWiiUTitles(int run) { for (uint8_t a = 0; a < 2; a++) { for (int i = 0; i <= 1; i++) { - string path = string_format("%s:/usr/save/%s", (i == 0) ? "usb" : "mlc", highIDs[a]); + std::string path = string_format("%s:/usr/save/%s", (i == 0) ? "usb" : "mlc", highIDs[a]); DIR *dir = opendir(path.c_str()); if (dir != nullptr) { struct dirent *data; @@ -184,7 +182,7 @@ Title *loadWiiUTitles(int run) { uint32_t lowID = saves[i].lowID; bool isTitleOnUSB = saves[i].dev == 0u; - const string path = string_format("%s:/usr/%s/%08x/%08x/meta/meta.xml", isTitleOnUSB ? "usb" : "mlc", + const std::string path = string_format("%s:/usr/%s/%08x/%08x/meta/meta.xml", isTitleOnUSB ? "usb" : "mlc", saves[i].found ? "title" : "save", highID, lowID); titles[titleswiiu].saveInit = !saves[i].found; @@ -199,15 +197,15 @@ Title *loadWiiUTitles(int run) { if (strcspn(cptr, "<") == 0) cptr = strchr(strstr(xmlBuf, "shortname_ja"), '>') + 1; - decodeXMLEscapeLine(string(cptr)); - strlcpy(titles[titleswiiu].shortName, decodeXMLEscapeLine(string(cptr)).c_str(), strcspn(decodeXMLEscapeLine(string(cptr)).c_str(), "<") + 1); + decodeXMLEscapeLine(std::string(cptr)); + strlcpy(titles[titleswiiu].shortName, decodeXMLEscapeLine(std::string(cptr)).c_str(), strcspn(decodeXMLEscapeLine(std::string(cptr)).c_str(), "<") + 1); cptr = strchr(strstr(xmlBuf, "longname_en"), '>') + 1; memset(titles[i].longName, 0, sizeof(titles[i].longName)); if (strcspn(cptr, "<") == 0) cptr = strchr(strstr(xmlBuf, "longname_ja"), '>') + 1; - strlcpy(titles[titleswiiu].longName, decodeXMLEscapeLine(string(cptr)).c_str(), strcspn(decodeXMLEscapeLine(string(cptr)).c_str(), "<") + 1); + strlcpy(titles[titleswiiu].longName, decodeXMLEscapeLine(std::string(cptr)).c_str(), strcspn(decodeXMLEscapeLine(std::string(cptr)).c_str(), "<") + 1); free(xmlBuf); } @@ -257,7 +255,7 @@ Title *loadWiiTitles() { {0x00010001, 0x48424344}, {0x00010001, 0x554E454F}}; - string pathW; + std::string pathW; for (int k = 0; k < 3; k++) { pathW = string_format("slc:/title/%s", highIDs[k]); DIR *dir = opendir(pathW.c_str()); @@ -311,7 +309,7 @@ Title *loadWiiTitles() { continue; } - const string path = string_format("slc:/title/%s/%s/data/banner.bin", highIDs[k], data->d_name); + const std::string path = string_format("slc:/title/%s/%s/data/banner.bin", highIDs[k], data->d_name); FILE *file = fopen(path.c_str(), "rb"); if (file != nullptr) { fseek(file, 0x20, SEEK_SET); @@ -980,7 +978,7 @@ int main(void) { continue; } } - string path = string_format("%s:/usr/title/000%x/%x/code/fw.img", + std::string path = string_format("%s:/usr/title/000%x/%x/code/fw.img", (titles[targ].isTitleOnUSB) ? "usb" : "mlc", titles[targ].highID, titles[targ].lowID); if ((mode == 0) && (checkEntry(path.c_str()) != 0)) diff --git a/src/savemng.cpp b/src/savemng.cpp index 156f11c..30cefe2 100644 --- a/src/savemng.cpp +++ b/src/savemng.cpp @@ -17,7 +17,7 @@ VPADReadError vpad_error; KPADStatus kpad[4], kpad_status; -static string newlibToFSA(string path) { +static std::string newlibToFSA(std::string path) { if (path[3] == ':') { switch (path[0]) { case 'u': @@ -38,7 +38,7 @@ void setFSAFD(int fd) { fsaFd = fd; } -void show_file_operation(string file_name, string file_src, string file_dest) { +void show_file_operation(std::string file_name, std::string file_src, std::string file_dest) { console_print_pos(-2, 0, "Copying file: %s", file_name.c_str()); console_print_pos_multiline(-2, 2, '/', "From: %s", file_src.c_str()); console_print_pos_multiline(-2, 8, '/', "To: %s", file_dest.c_str()); @@ -102,7 +102,7 @@ int32_t loadTitleIcon(Title *title) { uint32_t lowID = title->lowID; bool isUSB = title->isTitleOnUSB; bool isWii = ((highID & 0xFFFFFFF0) == 0x00010000); - string path; + std::string path; if (isWii) { if (title->saveInit) { @@ -147,7 +147,7 @@ int folderEmpty(const char *fPath) { } int createFolder(const char *fPath) { //Adapted from mkdir_p made by JonathonReinhart - string _path; + std::string _path; char *p; int found = 0; @@ -177,7 +177,7 @@ void console_print_pos_aligned(int y, uint16_t offset, uint8_t align, const char int x = 0; va_list va; va_start(va, format); - string tmp = string_format(format, va); + std::string tmp = string_format(format, va); if (!tmp.empty()) { switch (align) { case 0: @@ -239,7 +239,7 @@ void console_print_pos_multiline(int x, int y, char cdiv, const char *format, .. q[l1] = o; l1--; } - string buf; + std::string buf; buf.assign(p); p = (char *) string_format("\n%s", buf.c_str()).c_str(); p++; @@ -253,12 +253,12 @@ void console_print_pos_multiline(int x, int y, char cdiv, const char *format, .. free(tmp); } -bool promptConfirm(Style st, string question) { +bool promptConfirm(Style st, std::string question) { clearBuffers(); WHBLogFreetypeDraw(); - const string msg1 = "\ue000 Yes - \ue001 No"; - const string msg2 = "\ue000 Confirm - \ue001 Cancel"; - string msg; + const std::string msg1 = "\ue000 Yes - \ue001 No"; + const std::string msg2 = "\ue000 Confirm - \ue001 Cancel"; + std::string msg; switch (st & 0x0F) { case ST_YES_NO: msg = msg1; @@ -378,7 +378,7 @@ void getAccountsSD(Title *title, uint8_t slot) { if (sdacc != nullptr) free(sdacc); - string path = string_format("sd:/wiiu/backups/%08x%08x/%u", highID, lowID, slot); + std::string path = string_format("sd:/wiiu/backups/%08x%08x/%u", highID, lowID, slot); DIR *dir = opendir(path.c_str()); if (dir != nullptr) { struct dirent *data; @@ -407,7 +407,7 @@ void getAccountsSD(Title *title, uint8_t slot) { } } -int DumpFile(string pPath, string oPath) { +int DumpFile(std::string pPath, std::string oPath) { FILE *source = fopen(pPath.c_str(), "rb"); if (source == nullptr) return -1; @@ -475,7 +475,7 @@ int DumpFile(string pPath, string oPath) { return 0; } -int DumpDir(string pPath, string tPath) { // Source: ft2sd +int DumpDir(std::string pPath, std::string tPath) { // Source: ft2sd DIR *dir = opendir(pPath.c_str()); if (dir == nullptr) { return -1; @@ -491,7 +491,7 @@ int DumpDir(string pPath, string tPath) { // Source: ft2sd continue; } - string targetPath = string_format("%s/%s", tPath.c_str(), data->d_name); + std::string targetPath = string_format("%s/%s", tPath.c_str(), data->d_name); if ((data->d_type & DT_DIR) != 0) { mkdir(targetPath.c_str(), DEFFILEMODE); @@ -555,14 +555,14 @@ int DeleteDir(char *pPath) { return 0; } -string getUserID() { // Source: loadiine_gx2 +std::string getUserID() { // Source: loadiine_gx2 /* get persistent ID - thanks to Maschell */ nn::act::Initialize(); unsigned char slotno = nn::act::GetSlotNo(); unsigned int persistentID = nn::act::GetPersistentIdEx(slotno); nn::act::Finalize(); - string out = string_format("%08X", persistentID); + std::string out = string_format("%08X", persistentID); return out; } @@ -631,7 +631,7 @@ int getLoadiineUserDir(char *out, const char *fullSavePath, const char *userID) } bool isSlotEmpty(uint32_t highID, uint32_t lowID, uint8_t slot) { - string path; + std::string path; if (((highID & 0xFFFFFFF0) == 0x00010000) && (slot == 255)) path = string_format("sd:/savegames/%08x%08x", highID, lowID); else @@ -709,7 +709,7 @@ bool hasCommonSave(Title *title, bool inSD, bool iine, uint8_t slot, int version if (isWii) return false; - string srcPath; + std::string srcPath; if (!inSD) { const char *path = (isUSB ? "usb:/usr/save" : "mlc:/usr/save"); srcPath = string_format("%s/%08x/%08x/%s/common", path, highID, lowID, "user"); @@ -747,10 +747,10 @@ void copySavedata(Title *title, Title *titleb, int8_t allusers, int8_t allusers_ promptError("Backup done. Now copying Savedata."); } - string path = (isUSB ? "usb:/usr/save" : "mlc:/usr/save"); - string pathb = (isUSBb ? "usb:/usr/save" : "mlc:/usr/save"); - string srcPath = string_format("%s/%08x/%08x/%s", path.c_str(), highID, lowID, "user"); - string dstPath = string_format("%s/%08x/%08x/%s", pathb.c_str(), highIDb, lowIDb, "user"); + std::string path = (isUSB ? "usb:/usr/save" : "mlc:/usr/save"); + std::string pathb = (isUSBb ? "usb:/usr/save" : "mlc:/usr/save"); + std::string srcPath = string_format("%s/%08x/%08x/%s", path.c_str(), highID, lowID, "user"); + std::string dstPath = string_format("%s/%08x/%08x/%s", pathb.c_str(), highIDb, lowIDb, "user"); createFolder(dstPath.c_str()); if (allusers > -1) @@ -776,7 +776,7 @@ void backupAllSave(Title *titles, int count, OSCalendarTime *date) { dateTime.tm_mon++; } - string datetime = string_format("%04d-%02d-%02dT%02d%02d%02d", dateTime.tm_year, dateTime.tm_mon, dateTime.tm_mday, + std::string datetime = string_format("%04d-%02d-%02dT%02d%02d%02d", dateTime.tm_year, dateTime.tm_mon, dateTime.tm_mday, dateTime.tm_hour, dateTime.tm_min, dateTime.tm_sec); for (int i = 0; i < count; i++) { if (titles[i].highID == 0 || titles[i].lowID == 0 || !titles[i].saveInit) @@ -786,9 +786,9 @@ void backupAllSave(Title *titles, int count, OSCalendarTime *date) { uint32_t lowID = titles[i].lowID; bool isUSB = titles[i].isTitleOnUSB; bool isWii = ((highID & 0xFFFFFFF0) == 0x00010000); - const string path = (isWii ? "slc:/title" : (isUSB ? "usb:/usr/save" : "mlc:/usr/save")); - string srcPath = string_format("%s/%08x/%08x/%s", path.c_str(), highID, lowID, isWii ? "data" : "user"); - string dstPath = string_format("sd:/wiiu/backups/batch/%s/%08x%08x", datetime.c_str(), highID, lowID); + const std::string path = (isWii ? "slc:/title" : (isUSB ? "usb:/usr/save" : "mlc:/usr/save")); + std::string srcPath = string_format("%s/%08x/%08x/%s", path.c_str(), highID, lowID, isWii ? "data" : "user"); + std::string dstPath = string_format("sd:/wiiu/backups/batch/%s/%08x%08x", datetime.c_str(), highID, lowID); createFolder(dstPath.c_str()); if (DumpDir(srcPath, dstPath) != 0) @@ -805,9 +805,9 @@ void backupSavedata(Title *title, uint8_t slot, int8_t allusers, bool common) { uint32_t lowID = title->lowID; bool isUSB = title->isTitleOnUSB; bool isWii = ((highID & 0xFFFFFFF0) == 0x00010000); - const string path = (isWii ? "slc:/title" : (isUSB ? "usb:/usr/save" : "mlc:/usr/save")); - string srcPath = string_format("%s/%08x/%08x/%s", path.c_str(), highID, lowID, isWii ? "data" : "user"); - string dstPath; + const std::string path = (isWii ? "slc:/title" : (isUSB ? "usb:/usr/save" : "mlc:/usr/save")); + std::string srcPath = string_format("%s/%08x/%08x/%s", path.c_str(), highID, lowID, isWii ? "data" : "user"); + std::string dstPath; if (isWii && (slot == 255)) dstPath = string_format("sd:/savegames/%08x%08x", highID, lowID); else @@ -832,7 +832,7 @@ void backupSavedata(Title *title, uint8_t slot, int8_t allusers, bool common) { promptError("Backup failed. DO NOT restore from this slot."); OSCalendarTime now; OSTicksToCalendarTime(OSGetTime(), &now); - string date = string_format("%02d/%02d/%d %02d:%02d", now.tm_mday, now.tm_mon, now.tm_year, now.tm_hour, now.tm_min); + std::string date = string_format("%02d/%02d/%d %02d:%02d", now.tm_mday, now.tm_mon, now.tm_year, now.tm_hour, now.tm_min); setSlotDate(title->highID, title->lowID, slot, date); } @@ -850,13 +850,13 @@ void restoreSavedata(Title *title, uint8_t slot, int8_t sdusers, int8_t allusers uint32_t lowID = title->lowID; bool isUSB = title->isTitleOnUSB; bool isWii = ((highID & 0xFFFFFFF0) == 0x00010000); - string srcPath; - const string path = (isWii ? "slc:/title" : (isUSB ? "usb:/usr/save" : "mlc:/usr/save")); + std::string srcPath; + const std::string path = (isWii ? "slc:/title" : (isUSB ? "usb:/usr/save" : "mlc:/usr/save")); if (isWii && (slot == 255)) srcPath = string_format("sd:/savegames/%08x%08x", highID, lowID); else srcPath = string_format("sd:/wiiu/backups/%08x%08x/%u", highID, lowID, slot); - string dstPath = string_format("%s/%08x/%08x/%s", path.c_str(), highID, lowID, isWii ? "data" : "user"); + std::string dstPath = string_format("%s/%08x/%08x/%s", path.c_str(), highID, lowID, isWii ? "data" : "user"); createFolder(dstPath.c_str()); if ((sdusers > -1) && !isWii) { diff --git a/src/string.cpp b/src/string.cpp index a7ae089..49d2844 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -1,14 +1,14 @@ #include "string.hpp" -bool replace(string &str, const string &from, const string &to) { +bool replace(std::string &str, const std::string &from, const std::string &to) { size_t start_pos = str.find(from); - if (start_pos == string::npos) + if (start_pos == std::string::npos) return false; str.replace(start_pos, from.length(), to); return true; } -string decodeXMLEscapeLine(string xmlString) { +std::string decodeXMLEscapeLine(std::string xmlString) { replace(xmlString, """, "\""); replace(xmlString, "'", "'"); replace(xmlString, "<", "<");