Skip to content
Permalink
Browse files

UtilRetro.cpp: Cleanup

  • Loading branch information
retro-wertz
retro-wertz committed Jul 27, 2019
1 parent 855db11 commit bc80ecdb398fe8b4ac1924e5cb89c44290c828c6
Showing with 56 additions and 70 deletions.
  1. +56 −70 src/libretro/UtilRetro.cpp
@@ -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)
IMAGE_TYPE utilFindType(const char* file)
{
if (buffer != file) // allows conversion in place
strcpy(buffer, file);
}
if (utilIsGBAImage(file))
return IMAGE_GBA;

static bool utilIsImage(const char* file)
{
return utilIsGBAImage(file) || utilIsGBImage(file);
}
if (utilIsGBImage(file))
return IMAGE_GB;

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;
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;

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);

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;
}
}

FREAD_UNCHECKED(image, 1, size, fp); // read into buffer
fclose(fp);
return image;
FILE *fp = NULL;

fp = fopen(file,"rb");
if (!fp)
{
log("Failed to open file %s", file);
return NULL;
}
fseek(fp, 0, SEEK_END); //go to end

size = ftell(fp); // get position at end (length)
rewind(fp);

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)
{
int i = 0;

(void)lcd;

switch (systemColorDepth) {
case 16: {
for (int i = 0; i < 0x10000; i++) {
case 16:
for (i = 0; i < 0x10000; i++)
systemColorMap16[i] = ((i & 0x1f) << systemRedShift) | (((i & 0x3e0) >> 5) << systemGreenShift) | (((i & 0x7c00) >> 10) << systemBlueShift);
}
} break;
break;
case 24:
case 32: {
for (int i = 0; i < 0x10000; i++) {
case 32:
for (i = 0; i < 0x10000; i++)
systemColorMap32[i] = ((i & 0x1f) << systemRedShift) | (((i & 0x3e0) >> 5) << systemGreenShift) | (((i & 0x7c00) >> 10) << systemBlueShift);
}
} break;
}
}

// 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;
break;
}
}

0 comments on commit bc80ecd

Please sign in to comment.
You can’t perform that action at this time.