UtilRetro.cpp: Cleanup

This commit is contained in:
retro-wertz 2019-07-27 11:29:39 +08:00
parent 855db11d98
commit bc80ecdb39
1 changed files with 57 additions and 71 deletions

View File

@ -2,6 +2,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <libretro.h>
#include "NLS.h" #include "NLS.h"
#include "System.h" #include "System.h"
#include "Util.h" #include "Util.h"
@ -111,26 +113,15 @@ bool utilIsGBImage(const char* file)
return false; return false;
} }
// strip .gz or .z off end
void utilStripDoubleExtension(const char* file, char* buffer)
{
if (buffer != file) // allows conversion in place
strcpy(buffer, file);
}
static bool utilIsImage(const char* file)
{
return utilIsGBAImage(file) || utilIsGBImage(file);
}
IMAGE_TYPE utilFindType(const char* file) IMAGE_TYPE utilFindType(const char* file)
{ {
//char buffer[2048]; if (utilIsGBAImage(file))
if (!utilIsImage(file)) // TODO: utilIsArchive() instead? return IMAGE_GBA;
{
if (utilIsGBImage(file))
return IMAGE_GB;
return IMAGE_UNKNOWN; return IMAGE_UNKNOWN;
}
return utilIsGBAImage(file) ? IMAGE_GBA : IMAGE_GB;
} }
static int utilGetSize(int size) static int utilGetSize(int size)
@ -144,28 +135,35 @@ static int utilGetSize(int size)
uint8_t *utilLoad(const char *file, bool (*accept)(const char *), uint8_t *data, int &size) uint8_t *utilLoad(const char *file, bool (*accept)(const char *), uint8_t *data, int &size)
{ {
FILE *fp = NULL; FILE *fp = NULL;
//char *buf = NULL;
fp = fopen(file,"rb"); fp = fopen(file,"rb");
if(!fp) return NULL; if (!fp)
{
log("Failed to open file %s", file);
return NULL;
}
fseek(fp, 0, SEEK_END); //go to end fseek(fp, 0, SEEK_END); //go to end
size = ftell(fp); // get position at end (length) size = ftell(fp); // get position at end (length)
rewind(fp); rewind(fp);
uint8_t *image = data; uint8_t *image = data;
if(image == NULL) if(image == NULL)
{ {
//allocate buffer memory if none was passed to the function
image = (uint8_t *)malloc(utilGetSize(size)); image = (uint8_t *)malloc(utilGetSize(size));
if(image == NULL) if(image == NULL)
{ {
systemMessage(MSG_OUT_OF_MEMORY, N_("Failed to allocate memory for %s"), log("Failed to allocate memory for %s", file);
"data");
return NULL; return NULL;
} }
} }
FREAD_UNCHECKED(image, 1, size, fp); // read into buffer if (fread(image, 1, size, fp) != size) {
log("Failed to read from %s", file);
fclose(fp);
return NULL;
}
fclose(fp); fclose(fp);
return image; return image;
} }
@ -220,12 +218,10 @@ void utilGBAFindSave(const int size)
p++; p++;
} }
// if no matches found, then set it to NONE // if no matches found, then set it to NONE
if (detectedSaveType == 0) { if (detectedSaveType == 0)
detectedSaveType = 5; detectedSaveType = 5;
} if (detectedSaveType == 4)
if (detectedSaveType == 4) {
detectedSaveType = 3; detectedSaveType = 3;
}
cpuSaveType = detectedSaveType; cpuSaveType = detectedSaveType;
rtcEnabled = rtcFound_; rtcEnabled = rtcFound_;
@ -234,30 +230,20 @@ void utilGBAFindSave(const int size)
void utilUpdateSystemColorMaps(bool lcd) void utilUpdateSystemColorMaps(bool lcd)
{ {
switch (systemColorDepth) { int i = 0;
case 16: {
for (int i = 0; i < 0x10000; i++) {
systemColorMap16[i] = ((i & 0x1f) << systemRedShift) | (((i & 0x3e0) >> 5) << systemGreenShift) | (((i & 0x7c00) >> 10) << systemBlueShift);
}
} break;
case 24:
case 32: {
for (int i = 0; i < 0x10000; i++) {
systemColorMap32[i] = ((i & 0x1f) << systemRedShift) | (((i & 0x3e0) >> 5) << systemGreenShift) | (((i & 0x7c00) >> 10) << systemBlueShift);
}
} break;
}
}
// Check for existence of file. (void)lcd;
bool utilFileExists(const char* filename)
{ switch (systemColorDepth) {
FILE* f = fopen(filename, "r"); case 16:
if (f == NULL) { for (i = 0; i < 0x10000; i++)
return false; systemColorMap16[i] = ((i & 0x1f) << systemRedShift) | (((i & 0x3e0) >> 5) << systemGreenShift) | (((i & 0x7c00) >> 10) << systemBlueShift);
} else { break;
fclose(f); case 24:
return true; case 32:
for (i = 0; i < 0x10000; i++)
systemColorMap32[i] = ((i & 0x1f) << systemRedShift) | (((i & 0x3e0) >> 5) << systemGreenShift) | (((i & 0x7c00) >> 10) << systemBlueShift);
break;
} }
} }