utils: Remove unnecessary casting and start passing values by ref across src

This commit is contained in:
Joel16 2021-12-05 23:26:43 -05:00
parent 3c38798681
commit 9e7fa1658a
15 changed files with 101 additions and 101 deletions

View File

@ -29,18 +29,18 @@ namespace FS {
std::string GetFileExt(const std::string &filename);
FileType GetFileType(const std::string &filename);
SceOff GetFileSize(const std::string &path);
char *GetFileTimestamp(SceIoStat *stat, FileTimestamp time);
char *GetFilePermission(SceIoStat *stat);
char *GetFileTimestamp(SceIoStat &stat, FileTimestamp time);
char *GetFilePermission(SceIoStat &stat);
int ReadFile(const std::string &path, void *buf, int size);
int WriteFile(const std::string &path, void *buf, int size);
int GetDirList(const std::string &path, std::vector<SceIoDirent> &entries);
int ChangeDirNext(const std::string &path, std::vector<SceIoDirent> &entries);
int ChangeDirPrev(std::vector<SceIoDirent> &entries);
std::string GetFilename(const std::string &path);
void Copy(SceIoDirent *entry, const std::string &path);
void Copy(SceIoDirent &entry, const std::string &path);
int Paste(void);
int Move(void);
int Delete(SceIoDirent *entry);
int Delete(SceIoDirent &entry);
std::string BuildPath(const std::string &path, const std::string &filename);
}

View File

@ -24,15 +24,15 @@ extern enum PspCtrlButtons PSP_CTRL_ENTER, PSP_CTRL_CANCEL;
extern BROWSE_STATE device;
namespace Utils {
void SetBounds(int *set, int min, int max);
void SetMax(int *set, int value, int max);
void SetMin(int *set, int value, int min);
void SetBounds(int &set, int min, int max);
void SetMax(int &set, int value, int max);
void SetMin(int &set, int value, int min);
void GetSizeString(char *string, double size);
void InitKernelDrivers(void);
void TermKernelDrivers(void);
void HandleUSB(void);
bool IsModelPSPGo(void);
int IsMemCardInserted(bool *is_inserted);
int IsMemCardInserted(bool &is_inserted);
bool IsInternalStorage(void);
int LaunchEboot(const char *path);
u64 GetTotalStorage(void);
@ -43,8 +43,8 @@ namespace Utils {
int IsButtonHeld(enum PspCtrlButtons buttons);
int IsKButtonPressed(enum PspCtrlButtons buttons);
int IsKButtonHeld(enum PspCtrlButtons buttons);
int GetEnterButton(void);
int GetCancelButton(void);
enum PspCtrlButtons GetEnterButton(void);
enum PspCtrlButtons GetCancelButton(void);
float GetAnalogX(void);
float GetAnalogY(void);
bool IsCancelButtonPressed(void);

View File

@ -25,10 +25,10 @@ namespace Config {
return 0;
}
static void SetDefault(config_t *config) {
config->sort = 0;
config->dark_theme = false;
config->dev_options = false;
static void SetDefault(config_t &config) {
config.sort = 0;
config.dark_theme = false;
config.dev_options = false;
}
int Load(void) {
@ -43,7 +43,7 @@ namespace Config {
device = BROWSE_STATE_EXTERNAL;
if (!FS::FileExists("config.json")) {
Config::SetDefault(&cfg);
Config::SetDefault(cfg);
return Save(cfg);
}
@ -74,9 +74,9 @@ namespace Config {
delete[] buf;
// delete[] config file if config file is updated. This will rarely happen.
if (config_version_holder < config_version) {
if (config_version_holder < config_version) {
sceIoRemove("config.json");
Config::SetDefault(&cfg);
Config::SetDefault(cfg);
return Config::Save(cfg);
}

View File

@ -117,36 +117,36 @@ namespace FS {
return stat.st_size;
}
char *GetFileTimestamp(SceIoStat *stat, FileTimestamp time) {
char *GetFileTimestamp(SceIoStat &stat, FileTimestamp time) {
static char timestamp[30];
switch(time) {
case FileCreatedTime:
snprintf(timestamp, 30, "%d/%d/%d %2i:%02i", stat->st_ctime.year, stat->st_ctime.month, stat->st_ctime.day, stat->st_ctime.hour,
stat->st_ctime.minute);
snprintf(timestamp, 30, "%d/%d/%d %2i:%02i", stat.st_ctime.year, stat.st_ctime.month, stat.st_ctime.day, stat.st_ctime.hour,
stat.st_ctime.minute);
break;
case FileAccessedTime:
snprintf(timestamp, 30, "%d/%d/%d %2i:%02i", stat->st_atime.year, stat->st_atime.month, stat->st_atime.day, stat->st_atime.hour,
stat->st_atime.minute);
snprintf(timestamp, 30, "%d/%d/%d %2i:%02i", stat.st_atime.year, stat.st_atime.month, stat.st_atime.day, stat.st_atime.hour,
stat.st_atime.minute);
break;
case FileModifiedTime:
snprintf(timestamp, 30, "%d/%d/%d %2i:%02i", stat->st_mtime.year, stat->st_mtime.month, stat->st_mtime.day, stat->st_mtime.hour,
stat->st_mtime.minute);
snprintf(timestamp, 30, "%d/%d/%d %2i:%02i", stat.st_mtime.year, stat.st_mtime.month, stat.st_mtime.day, stat.st_mtime.hour,
stat.st_mtime.minute);
break;
}
return timestamp;
}
char *GetFilePermission(SceIoStat *stat) {
char *GetFilePermission(SceIoStat &stat) {
static char perms[11];
snprintf(perms, 11, "%s%s%s%s%s%s%s%s%s%s", (FIO_S_ISDIR(stat->st_mode)) ? "d" : "-", (stat->st_mode & FIO_S_IRUSR) ? "r" : "-",
(stat->st_mode & FIO_S_IWUSR) ? "w" : "-", (stat->st_mode & FIO_S_IXUSR) ? "x" : "-", (stat->st_mode & FIO_S_IRGRP) ? "r" : "-",
(stat->st_mode & FIO_S_IWGRP) ? "w" : "-", (stat->st_mode & FIO_S_IXGRP) ? "x" : "-", (stat->st_mode & FIO_S_IROTH) ? "r" : "-",
(stat->st_mode & FIO_S_IWOTH) ? "w" : "-", (stat->st_mode & FIO_S_IXOTH) ? "x" : "-");
snprintf(perms, 11, "%s%s%s%s%s%s%s%s%s%s", (FIO_S_ISDIR(stat.st_mode)) ? "d" : "-", (stat.st_mode & FIO_S_IRUSR) ? "r" : "-",
(stat.st_mode & FIO_S_IWUSR) ? "w" : "-", (stat.st_mode & FIO_S_IXUSR) ? "x" : "-", (stat.st_mode & FIO_S_IRGRP) ? "r" : "-",
(stat.st_mode & FIO_S_IWGRP) ? "w" : "-", (stat.st_mode & FIO_S_IXGRP) ? "x" : "-", (stat.st_mode & FIO_S_IROTH) ? "r" : "-",
(stat.st_mode & FIO_S_IWOTH) ? "w" : "-", (stat.st_mode & FIO_S_IXOTH) ? "x" : "-");
return perms;
}
@ -418,12 +418,12 @@ namespace FS {
fs_copy_entry.is_dir = false;
}
void Copy(SceIoDirent *entry, const std::string &path) {
void Copy(SceIoDirent &entry, const std::string &path) {
FS::ClearCopyData();
fs_copy_entry.copy_path = FS::BuildPath(path, entry->d_name);
fs_copy_entry.copy_filename.append(entry->d_name);
fs_copy_entry.copy_path = FS::BuildPath(path, entry.d_name);
fs_copy_entry.copy_filename.append(entry.d_name);
if (FIO_S_ISDIR(entry->d_stat.st_mode))
if (FIO_S_ISDIR(entry.d_stat.st_mode))
fs_copy_entry.is_dir = true;
}
@ -586,11 +586,11 @@ namespace FS {
return 1;
}
int Delete(SceIoDirent *entry) {
int Delete(SceIoDirent &entry) {
int ret = 0;
std::string path = FS::BuildPath(cfg.cwd, entry->d_name);
std::string path = FS::BuildPath(cfg.cwd, entry.d_name);
if (FIO_S_ISDIR(entry->d_stat.st_mode)) {
if (FIO_S_ISDIR(entry.d_stat.st_mode)) {
if (R_FAILED(ret = FS::DeleteDirectoryRecursive(path))) {
Log::Error("FS::DeleteDirectoryRecursive(%s) failed: 0x%x\n", path.c_str(), ret);
return ret;

View File

@ -13,7 +13,7 @@ namespace Options {
if ((item->checked_count > 1) && (!item->checked_cwd.compare(cfg.cwd))) {
for (u32 i = 0; i < item->checked.size(); i++) {
if (item->checked.at(i)) {
if (R_FAILED(ret = FS::Delete(&item->entries[i]))) {
if (R_FAILED(ret = FS::Delete(item->entries[i]))) {
FS::GetDirList(cfg.cwd, item->entries);
GUI::ResetCheckbox(item);
break;
@ -22,7 +22,7 @@ namespace Options {
}
}
else
ret = FS::Delete(&item->entries[item->selected]);
ret = FS::Delete(item->entries[item->selected]);
if (R_SUCCEEDED(ret)) {
FS::GetDirList(cfg.cwd, item->entries);
@ -78,6 +78,6 @@ namespace GUI {
else if (Utils::IsButtonPressed(PSP_CTRL_CANCEL))
item->state = MENU_STATE_OPTIONS;
Utils::SetBounds(&selection, 0, 1);
Utils::SetBounds(selection, 0, 1);
}
}

View File

@ -61,7 +61,7 @@ namespace GUI {
void ControlFileBrowser(MenuItem *item, int *ctrl) {
u32 size = (item->entries.size() - 1);
Utils::SetBounds(&item->selected, 0, size);
Utils::SetBounds(item->selected, 0, size);
if (*ctrl & PSP_CTRL_UP) {
item->selected--;

View File

@ -607,7 +607,7 @@ namespace GameLauncher {
else if (ctrl & PSP_CTRL_RIGHT)
selection++;
Utils::SetBounds(&selection, 0, 4);
Utils::SetBounds(selection, 0, 4);
if (Utils::IsButtonPressed(PSP_CTRL_ENTER))
Utils::LaunchEboot(path.c_str());

View File

@ -118,16 +118,16 @@ namespace GUI {
}
if ((degrees == 0) || (degrees == 180)) {
Utils::SetMax(&pos_x, horizantal_flip? -width : width, horizantal_flip? -width : width);
Utils::SetMin(&pos_x, horizantal_flip? width : -width, horizantal_flip? width : -width);
Utils::SetMax(&pos_y, vertical_flip? -height : height, vertical_flip? -height : height);
Utils::SetMin(&pos_y, vertical_flip? height : -height, vertical_flip? height : -height);
Utils::SetMax(pos_x, horizantal_flip? -width : width, horizantal_flip? -width : width);
Utils::SetMin(pos_x, horizantal_flip? width : -width, horizantal_flip? width : -width);
Utils::SetMax(pos_y, vertical_flip? -height : height, vertical_flip? -height : height);
Utils::SetMin(pos_y, vertical_flip? height : -height, vertical_flip? height : -height);
}
else {
Utils::SetMax(&pos_x, vertical_flip? -height : height, vertical_flip? -height : height);
Utils::SetMin(&pos_x, vertical_flip? height : -height, vertical_flip? height : -height);
Utils::SetMax(&pos_y, horizantal_flip? -width : width, horizantal_flip? -width : width);
Utils::SetMin(&pos_y, horizantal_flip? width : -width, horizantal_flip? width : -width);
Utils::SetMax(pos_x, vertical_flip? -height : height, vertical_flip? -height : height);
Utils::SetMin(pos_x, vertical_flip? height : -height, vertical_flip? height : -height);
Utils::SetMax(pos_y, horizantal_flip? -width : width, horizantal_flip? -width : width);
Utils::SetMin(pos_y, horizantal_flip? width : -width, horizantal_flip? width : -width);
}
if (Utils::IsButtonPressed(PSP_CTRL_CANCEL)) {

View File

@ -98,17 +98,17 @@ namespace GUI {
if (is_psp_go) {
if (cfg.dev_options) {
Utils::SetMax(&selection, 0, is_ms_inserted? 5 : 4);
Utils::SetMin(&selection, is_ms_inserted? 5 : 4, 0);
Utils::SetMax(selection, 0, is_ms_inserted? 5 : 4);
Utils::SetMin(selection, is_ms_inserted? 5 : 4, 0);
}
else {
Utils::SetMax(&selection, 0, is_ms_inserted? 1 : 0);
Utils::SetMin(&selection, is_ms_inserted? 1 : 0, 0);
Utils::SetMax(selection, 0, is_ms_inserted? 1 : 0);
Utils::SetMin(selection, is_ms_inserted? 1 : 0, 0);
}
}
else {
Utils::SetMax(&selection, 0, cfg.dev_options? 5 : 0);
Utils::SetMin(&selection, cfg.dev_options? 5 : 0, 0);
Utils::SetMax(selection, 0, cfg.dev_options? 5 : 0);
Utils::SetMin(selection, cfg.dev_options? 5 : 0, 0);
}
if (Utils::IsButtonPressed(PSP_CTRL_ENTER)) {

View File

@ -26,7 +26,7 @@ namespace Options {
for (u32 i = 0; i < item->checked_copy.size(); i++) {
if (item->checked_copy.at(i)) {
FS::Copy(&entries[i], item->checked_cwd);
FS::Copy(entries[i], item->checked_cwd);
if (R_FAILED((*func)())) {
FS::GetDirList(cfg.cwd, item->entries);
GUI::ResetCheckbox(item);
@ -78,7 +78,7 @@ namespace Options {
if ((item->checked_count >= 1) && (item->checked_cwd.compare(cfg.cwd) != 0))
GUI::ResetCheckbox(item);
if (item->checked_count <= 1)
FS::Copy(&item->entries[item->selected], cfg.cwd);
FS::Copy(item->entries[item->selected], cfg.cwd);
copy = !copy;
item->state = MENU_STATE_FILEBROWSER;
@ -105,7 +105,7 @@ namespace Options {
GUI::ResetCheckbox(item);
if (item->checked_count <= 1)
FS::Copy(&item->entries[item->selected], cfg.cwd);
FS::Copy(item->entries[item->selected], cfg.cwd);
}
else {
if ((item->checked_count > 1) && (item->checked_cwd.compare(cfg.cwd) != 0))
@ -177,16 +177,16 @@ namespace GUI {
column--;
if (!options_more) {
Utils::SetBounds(&row, 0, 1);
Utils::SetBounds(&column, 0, 3);
Utils::SetBounds(row, 0, 1);
Utils::SetBounds(column, 0, 3);
}
else {
Utils::SetBounds(&column, 0, 2);
Utils::SetBounds(column, 0, 2);
if (column == 1)
Utils::SetBounds(&row, 0, 0);
Utils::SetBounds(row, 0, 0);
else
Utils::SetBounds(&row, 0, 1);
Utils::SetBounds(row, 0, 1);
}
if (Utils::IsButtonPressed(PSP_CTRL_ENTER)) {

View File

@ -27,16 +27,16 @@ namespace GUI {
char size[16];
Utils::GetSizeString(size, item->entries[item->selected].d_stat.st_size);
intraFontPrintf(font, 140, 92, "Size: %s", size);
intraFontPrintf(font, 140, 110, "Created: %s", FS::GetFileTimestamp(&item->entries[item->selected].d_stat, FileCreatedTime));
intraFontPrintf(font, 140, 128, "Accessed: %s", FS::GetFileTimestamp(&item->entries[item->selected].d_stat, FileAccessedTime));
intraFontPrintf(font, 140, 146, "Modified: %s", FS::GetFileTimestamp(&item->entries[item->selected].d_stat, FileModifiedTime));
intraFontPrintf(font, 140, 164, "Perms: %s", FS::GetFilePermission(&item->entries[item->selected].d_stat));
intraFontPrintf(font, 140, 110, "Created: %s", FS::GetFileTimestamp(item->entries[item->selected].d_stat, FileCreatedTime));
intraFontPrintf(font, 140, 128, "Accessed: %s", FS::GetFileTimestamp(item->entries[item->selected].d_stat, FileAccessedTime));
intraFontPrintf(font, 140, 146, "Modified: %s", FS::GetFileTimestamp(item->entries[item->selected].d_stat, FileModifiedTime));
intraFontPrintf(font, 140, 164, "Perms: %s", FS::GetFilePermission(item->entries[item->selected].d_stat));
}
else {
intraFontPrintf(font, 140, 92, "Created: %s", FS::GetFileTimestamp(&item->entries[item->selected].d_stat, FileCreatedTime));
intraFontPrintf(font, 140, 110, "Accessed: %s", FS::GetFileTimestamp(&item->entries[item->selected].d_stat, FileAccessedTime));
intraFontPrintf(font, 140, 128, "Modified: %s", FS::GetFileTimestamp(&item->entries[item->selected].d_stat, FileModifiedTime));
intraFontPrintf(font, 140, 146, "Perms: %s", FS::GetFilePermission(&item->entries[item->selected].d_stat));
intraFontPrintf(font, 140, 92, "Created: %s", FS::GetFileTimestamp(item->entries[item->selected].d_stat, FileCreatedTime));
intraFontPrintf(font, 140, 110, "Accessed: %s", FS::GetFileTimestamp(item->entries[item->selected].d_stat, FileAccessedTime));
intraFontPrintf(font, 140, 128, "Modified: %s", FS::GetFileTimestamp(item->entries[item->selected].d_stat, FileModifiedTime));
intraFontPrintf(font, 140, 146, "Perms: %s", FS::GetFilePermission(item->entries[item->selected].d_stat));
}
}

View File

@ -248,7 +248,7 @@ namespace GUI {
settings_state = GENERAL_SETTINGS;
}
Utils::SetBounds(&selection, 0, 0);
Utils::SetBounds(selection, 0, 0);
}
static void DisplaySortSettings(void) {
@ -284,7 +284,7 @@ namespace GUI {
settings_state = GENERAL_SETTINGS;
}
Utils::SetBounds(&selection, 0, 3);
Utils::SetBounds(selection, 0, 3);
}
static void DisplayAboutSettings(void) {
@ -310,7 +310,7 @@ namespace GUI {
if ((Utils::IsButtonPressed(PSP_CTRL_ENTER)) || (Utils::IsButtonPressed(PSP_CTRL_CANCEL)))
settings_state = GENERAL_SETTINGS;
Utils::SetBounds(&selection, 4, 4);
Utils::SetBounds(selection, 4, 4);
}
static void DisplayGeneralSettings(void) {
@ -380,7 +380,7 @@ namespace GUI {
item->state = MENU_STATE_FILEBROWSER;
}
Utils::SetBounds(&selection, 0, 4);
Utils::SetBounds(selection, 0, 4);
}
void DisplaySettings(MenuItem *item) {

View File

@ -384,7 +384,7 @@ namespace TextViewer {
break;
}
Utils::SetBounds(&selection, 0, 1);
Utils::SetBounds(selection, 0, 1);
}
else {
if (ctrl & PSP_CTRL_UP) {

View File

@ -47,11 +47,11 @@ namespace Services {
font_size_cache[i] = intraFontMeasureText(font, character);
}
Utils::IsMemCardInserted(&is_ms_inserted);
Utils::IsMemCardInserted(is_ms_inserted);
is_psp_go = Utils::IsModelPSPGo();
PSP_CTRL_ENTER = static_cast<enum PspCtrlButtons>(Utils::GetEnterButton());
PSP_CTRL_CANCEL = static_cast<enum PspCtrlButtons>(Utils::GetCancelButton());
PSP_CTRL_ENTER = Utils::GetEnterButton();
PSP_CTRL_CANCEL = Utils::GetCancelButton();
return 0;
}

View File

@ -63,21 +63,21 @@ namespace Utils {
SystemDevCtl *pdevinf;
} SystemDevCommand;
void SetBounds(int *set, int min, int max) {
if (*set > max)
*set = min;
else if (*set < min)
*set = max;
void SetBounds(int &set, int min, int max) {
if (set > max)
set = min;
else if (set < min)
set = max;
}
void SetMax(int *set, int value, int max) {
if (*set > max)
*set = value;
void SetMax(int &set, int value, int max) {
if (set > max)
set = value;
}
void SetMin(int *set, int value, int min) {
if (*set < min)
*set = value;
void SetMin(int &set, int value, int min) {
if (set < min)
set = value;
}
void GetSizeString(char *string, double size) {
@ -244,15 +244,15 @@ namespace Utils {
return (kuKernelGetModel() == 4);
}
int IsMemCardInserted(bool *is_inserted) {
int IsMemCardInserted(bool &is_inserted) {
int status = 0, ret = 0;
if (R_FAILED(ret = sceIoDevctl("mscmhc0:", 0x02025806, 0, 0, &status, sizeof(status))))
return ret;
if (status != 1)
*is_inserted = false;
is_inserted = false;
else
*is_inserted = true;
is_inserted = true;
return 0;
}
@ -389,30 +389,30 @@ namespace Utils {
return kernel_pad.Buttons & buttons;
}
int GetEnterButton(void) {
enum PspCtrlButtons GetEnterButton(void) {
unsigned int button = 0;
if (R_SUCCEEDED(GetRegistryValue("/CONFIG/SYSTEM/XMB", "button_assign", &button))) {
if (button == 0)
return 0x002000; // PSP_CTRL_CIRCLE
return PSP_CTRL_CIRCLE; // PSP_CTRL_CIRCLE
else
return 0x004000; // PSP_CTRL_CROSS
return PSP_CTRL_CROSS; // PSP_CTRL_CROSS
}
return 0x004000; // By default return PSP_CTRL_CROSS
return PSP_CTRL_CROSS; // By default return PSP_CTRL_CROSS
}
// Basically the opposite of GetEnterButton()
int GetCancelButton(void) {
enum PspCtrlButtons GetCancelButton(void) {
unsigned int button = 0;
if (R_SUCCEEDED(GetRegistryValue("/CONFIG/SYSTEM/XMB", "button_assign", &button))) {
if (button == 0)
return 0x004000; // PSP_CTRL_CROSS
return PSP_CTRL_CROSS; // PSP_CTRL_CROSS
else
return 0x002000; // PSP_CTRL_CIRCLE
return PSP_CTRL_CIRCLE; // PSP_CTRL_CIRCLE
}
return 0x002000; // By default return PSP_CTRL_CIRCLE
return PSP_CTRL_CIRCLE; // By default return PSP_CTRL_CIRCLE
}
float GetAnalogX(void) {