summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bindings.c9
-rw-r--r--config.c11
-rw-r--r--io.c7
-rw-r--r--m68k_core.c4
-rw-r--r--util.c5
-rw-r--r--util.h2
6 files changed, 21 insertions, 17 deletions
diff --git a/bindings.c b/bindings.c
index 27b2383..cf5fb05 100644
--- a/bindings.c
+++ b/bindings.c
@@ -1,4 +1,5 @@
#include <string.h>
+#include <stdlib.h>
#include "render.h"
#include "system.h"
#include "io.h"
@@ -557,7 +558,7 @@ int parse_binding_target(int device_num, char * target, tern_node * padbuttons,
{
const int gpadslen = strlen("gamepads.");
const int mouselen = strlen("mouse.");
- if (!strncmp(target, "gamepads.", gpadslen)) {
+ if (startswith(target, "gamepads.")) {
int padnum = target[gpadslen] == 'n' ? device_num + 1 : target[gpadslen] - '0';
if (padnum >= 1 && padnum <= 8) {
int button = tern_find_int(padbuttons, target + gpadslen + 1, 0);
@@ -575,7 +576,7 @@ int parse_binding_target(int device_num, char * target, tern_node * padbuttons,
} else {
warning("Gamepad mapping string '%s' refers to an invalid gamepad number %c\n", target, target[gpadslen]);
}
- } else if(!strncmp(target, "mouse.", mouselen)) {
+ } else if(startswith(target, "mouse.")) {
int mousenum = target[mouselen] == 'n' ? device_num + 1 : target[mouselen] - '0';
if (mousenum >= 1 && mousenum <= 8) {
int button = tern_find_int(mousebuttons, target + mouselen + 1, 0);
@@ -593,7 +594,7 @@ int parse_binding_target(int device_num, char * target, tern_node * padbuttons,
} else {
warning("Gamepad mapping string '%s' refers to an invalid mouse number %c\n", target, target[mouselen]);
}
- } else if(!strncmp(target, "ui.", strlen("ui."))) {
+ } else if(startswith(target, "ui.")) {
if (!strcmp(target + 3, "vdp_debug_mode")) {
*subtype_a = UI_DEBUG_MODE_INC;
} else if(!strcmp(target + 3, "vdp_debug_pal")) {
@@ -603,7 +604,7 @@ int parse_binding_target(int device_num, char * target, tern_node * padbuttons,
*subtype_a = UI_ENTER_DEBUGGER;
} else if(!strcmp(target + 3, "save_state")) {
*subtype_a = UI_SAVE_STATE;
- } else if(!strncmp(target + 3, "set_speed.", strlen("set_speed."))) {
+ } else if(startswith(target + 3, "set_speed.")) {
*subtype_a = UI_SET_SPEED;
*subtype_b = atoi(target + 3 + strlen("set_speed."));
} else if(!strcmp(target + 3, "next_speed")) {
diff --git a/config.c b/config.c
index 351bff7..1407efb 100644
--- a/config.c
+++ b/config.c
@@ -49,11 +49,11 @@ static tern_node * parse_config_int(char **state, int started, int *line)
curline = strip_ws(curline);
int len = strlen(curline);
if (!len) {
- *line = *line + 1;
+ (*line)++;
continue;
}
if (curline[0] == '#') {
- *line = *line + 1;
+ (*line)++;
continue;
}
if (curline[0] == '}') {
@@ -67,7 +67,7 @@ static tern_node * parse_config_int(char **state, int started, int *line)
if (*end == '{') {
*end = 0;
curline = strip_ws(curline);
- *line = *line + 1;
+ (*line)++;
head = tern_insert_node(head, curline, parse_config_int(state, 1, line));
} else {
char * val = strip_ws(split_keyval(curline));
@@ -77,7 +77,7 @@ static tern_node * parse_config_int(char **state, int started, int *line)
} else {
fprintf(stderr, "Key %s is missing a value on line %d\n", key, *line);
}
- *line = *line + 1;
+ (*line)++;
}
}
return head;
@@ -174,11 +174,10 @@ tern_node *parse_config_file(char *config_path)
if (!config_size) {
goto config_empty;
}
- char * config_data = malloc(config_size+1);
+ char *config_data = calloc(config_size + 1, 1);
if (fread(config_data, 1, config_size, config_file) != config_size) {
goto config_read_fail;
}
- config_data[config_size] = '\0';
ret = parse_config(config_data);
config_read_fail:
diff --git a/io.c b/io.c
index ac30c56..4b571b0 100644
--- a/io.c
+++ b/io.c
@@ -219,8 +219,7 @@ void process_device(char * device_type, io_port * port)
}
const int gamepad_len = strlen("gamepad");
- const int mouse_len = strlen("mouse");
- if (!strncmp(device_type, "gamepad", gamepad_len))
+ if (startswith(device_type, "gamepad"))
{
if (
(device_type[gamepad_len] != '3' && device_type[gamepad_len] != '6' && device_type[gamepad_len] != '2')
@@ -236,10 +235,10 @@ void process_device(char * device_type, io_port * port)
port->device_type = IO_GAMEPAD6;
}
port->device.pad.gamepad_num = device_type[gamepad_len+2] - '0';
- } else if(!strncmp(device_type, "mouse", mouse_len)) {
+ } else if(startswith(device_type, "mouse")) {
if (port->device_type != IO_MOUSE) {
port->device_type = IO_MOUSE;
- port->device.mouse.mouse_num = device_type[mouse_len+1] - '0';
+ port->device.mouse.mouse_num = device_type[strlen("mouse")+1] - '0';
port->device.mouse.last_read_x = 0;
port->device.mouse.last_read_y = 0;
port->device.mouse.cur_x = 0;
diff --git a/m68k_core.c b/m68k_core.c
index 56013e7..ddd540a 100644
--- a/m68k_core.c
+++ b/m68k_core.c
@@ -1216,9 +1216,7 @@ void m68k_options_free(m68k_options *opts)
m68k_context * init_68k_context(m68k_options * opts, m68k_reset_handler reset_handler)
{
- size_t ctx_size = sizeof(m68k_context) + ram_size(&opts->gen) / (1 << opts->gen.ram_flags_shift) / 8;
- m68k_context * context = malloc(ctx_size);
- memset(context, 0, ctx_size);
+ m68k_context * context = calloc(1, sizeof(m68k_context) + ram_size(&opts->gen) / (1 << opts->gen.ram_flags_shift) / 8);
context->options = opts;
context->int_cycle = CYCLE_NEVER;
context->status = 0x27;
diff --git a/util.c b/util.c
index 50f79ee..88a6d4f 100644
--- a/util.c
+++ b/util.c
@@ -189,6 +189,11 @@ char * split_keyval(char * text)
return text+1;
}
+uint8_t startswith(const char *haystack, const char *prefix)
+{
+ return !strncmp(haystack, prefix, strlen(prefix));
+}
+
void bin_to_hex(uint8_t *output, uint8_t *input, uint64_t size)
{
while (size)
diff --git a/util.h b/util.h
index 34144b5..1b4c600 100644
--- a/util.h
+++ b/util.h
@@ -32,6 +32,8 @@ long file_size(FILE * f);
char * strip_ws(char * text);
//Inserts a null after the first word, returns a pointer to the second word
char * split_keyval(char * text);
+//Checks if haystack starts with prefix
+uint8_t startswith(const char *haystack, const char *prefix);
//Takes a binary byte buffer and produces a lowercase hex string
void bin_to_hex(uint8_t *output, uint8_t *input, uint64_t size);
//Takes an (optionally) null-terminated UTF16-BE string and converts a maximum of max_size code-units to UTF-8