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