1
0
mirror of https://github.com/libretro/RetroArch synced 2024-07-05 09:48:42 +00:00

CLeanups/fixes

This commit is contained in:
twinaphex 2015-09-17 20:24:49 +02:00
parent 483a4264e5
commit 82e15d1377
5 changed files with 46 additions and 40 deletions

View File

@ -39,6 +39,7 @@
#include <boolean.h>
#include <retro_dirent.h>
#include <retro_inline.h>
#include <retro_file.h>
#include <retro_log.h>
#include <compat/strl.h>
#include <rhash.h>
@ -49,25 +50,6 @@
#include "../../general.h"
#include "platform_linux.h"
static bool load_generic_file(ssize_t *len, const char *path, char *buf, size_t buflen)
{
const int fd = open(path, O_RDONLY);
if (fd < 0)
return false;
do
{
*len = read(fd, buf, buflen);
}while(*len < 0 && errno == EINTR);
close(fd);
if (*len < 0)
return false;
buf[*len] = '\0'; /* null-terminate the string. */
return true;
}
static bool cpu_inited_once;
static cpu_family g_cpuFamily;
static uint64_t g_cpuFeatures;
@ -299,7 +281,7 @@ static void cpulist_read_from(CpuList* list, const char* filename)
list->mask = 0;
if (!load_generic_file(&filelen, filename, file, sizeof(file)))
if (!retro_fmemcpy(filename, file, sizeof(file), &filelen))
{
RARCH_ERR("Could not read %s: %s\n", filename, strerror(errno));
return;
@ -339,9 +321,7 @@ static void linux_cpu_init(void)
g_cpuFeatures = 0;
g_cpuCount = 1;
load_generic_file(&cpuinfo_len, "/proc/cpuinfo", cpuinfo, sizeof cpuinfo);
if (cpuinfo_len < 0)
if (!retro_fmemcpy("/proc/cpuinfo", cpuinfo, sizeof(cpuinfo), &cpuinfo_len))
return;
/* Count the CPU cores, the value may be 0 for single-core CPUs */
@ -1005,11 +985,11 @@ static void check_proc_acpi_battery(const char * node, bool * have_battery,
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "state");
if (!load_generic_file(&buflen, path, state, sizeof (state)))
if (!retro_fmemcpy(path, state, sizeof(state), &buflen))
return;
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "info");
if (!load_generic_file(&buflen, path, info, sizeof (info)))
if (!retro_fmemcpy(path, info, sizeof(info), &buflen))
return;
ptr = &state[0];
@ -1120,7 +1100,7 @@ static void check_proc_acpi_sysfs_battery(const char * node, bool * have_battery
return;
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "status");
if (!load_generic_file(&buflen, path, state, sizeof (state)))
if (!retro_fmemcpy(path, state, sizeof (state), &buflen))
return;
if (strstr(state, "Discharging"))
@ -1129,7 +1109,7 @@ static void check_proc_acpi_sysfs_battery(const char * node, bool * have_battery
*have_battery = true;
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "capacity");
if (!load_generic_file(&buflen, path, state, sizeof (state)))
if (!retro_fmemcpy(path, state, sizeof (state), &buflen))
return;
capacity = atoi(state);
@ -1148,7 +1128,7 @@ static void check_proc_acpi_ac_adapter(const char * node, bool *have_ac)
ssize_t buflen = 0;
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "state");
if (!load_generic_file(&buflen, path, state, sizeof (state)))
if (!retro_fmemcpy(path, state, sizeof(state), &buflen))
return;
ptr = &state[0];
@ -1170,7 +1150,7 @@ static void check_proc_acpi_sysfs_ac_adapter(const char * node, bool *have_ac)
const char *base = proc_acpi_sysfs_ac_adapter_path;
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "online");
if (!load_generic_file(&buflen, path, state, sizeof (state)))
if (!retro_fmemcpy(path, state, sizeof(state), &buflen))
return;
if (strstr(state, "1"))
@ -1220,7 +1200,7 @@ static bool frontend_linux_powerstate_check_apm(
ssize_t buflen = 0;
char *str = NULL;
if (!load_generic_file(&buflen, proc_apm_path, buf, sizeof(buf)))
if (!retro_fmemcpy(proc_apm_path, buf, sizeof(buf), &buflen))
return false;
ptr = &buf[0];

View File

@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#if defined(_WIN32)
# include <compat/posix_string.h>
@ -37,10 +38,10 @@
#include <fcntl.h>
#endif
typedef struct RFILE
struct RFILE
{
int fd;
} RFILE;
};
RFILE *retro_fopen(const char *path, unsigned mode, ssize_t len)
{
@ -99,3 +100,24 @@ void retro_fclose(RFILE *stream)
close(stream->fd);
free(stream);
}
bool retro_fmemcpy(const char *path, char *s, size_t len, ssize_t *bytes_written)
{
RFILE *stream = retro_fopen(path, RFILE_MODE_READ, -1);
if (!stream)
return false;
do
{
*bytes_written = retro_fread(stream, s, len);
}while(*bytes_written < 0 && errno == EINTR);
retro_fclose(stream);
if (*bytes_written < 0)
return false;
/* NULL-terminate the string. */
s[*bytes_written] = '\0';
return true;
}

View File

@ -28,6 +28,8 @@
#include <sys/types.h>
#include <boolean.h>
#ifdef __cplusplus
extern "C" {
#endif
@ -51,6 +53,8 @@ ssize_t retro_fwrite(RFILE *stream, const void *s, size_t len);
void retro_fclose(RFILE *stream);
bool retro_fmemcpy(const char *path, char *s, size_t len, ssize_t *bytes_written);
#ifdef __cplusplus
}
#endif

View File

@ -14,12 +14,12 @@ struct bintree_node
struct bintree_node *right;
};
typedef struct bintree
struct bintree
{
struct bintree_node *root;
bintree_cmp_func cmp;
void *ctx;
} bintree_t;
};
static void *NIL_NODE = &NIL_NODE;

View File

@ -31,21 +31,21 @@ struct node_iter_ctx
libretrodb_index_t *idx;
};
typedef struct libretrodb
struct libretrodb
{
RFILE *fd;
uint64_t root;
uint64_t count;
uint64_t first_index_offset;
char path[1024];
} libretrodb_t;
};
typedef struct libretrodb_index
struct libretrodb_index
{
char name[50];
uint64_t key_size;
uint64_t next;
} libretrodb_index_t;
};
typedef struct libretrodb_metadata
{
@ -58,14 +58,14 @@ typedef struct libretrodb_header
uint64_t metadata_offset;
} libretrodb_header_t;
typedef struct libretrodb_cursor
struct libretrodb_cursor
{
int is_valid;
RFILE *fd;
int eof;
libretrodb_query_t *query;
libretrodb_t *db;
} libretrodb_cursor_t;
};
static struct rmsgpack_dom_value sentinal;