changeset 1783:eda8df5bc74c

Minor cleanup
author Michael Pavone <pavone@retrodev.com>
date Mon, 11 Mar 2019 00:04:48 -0700
parents b2bffd98063d
children 72540af9c90a
files bindings.c config.c io.c m68k_core.c util.c util.h
diffstat 6 files changed, 21 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/bindings.c	Sun Mar 10 23:10:43 2019 -0700
+++ b/bindings.c	Mon Mar 11 00:04:48 2019 -0700
@@ -1,4 +1,5 @@
 #include <string.h>
+#include <stdlib.h>
 #include "render.h"
 #include "system.h"
 #include "io.h"
@@ -557,7 +558,7 @@
 {
 	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 @@
 		} 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 @@
 		} 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 @@
 			*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")) {
--- a/config.c	Sun Mar 10 23:10:43 2019 -0700
+++ b/config.c	Mon Mar 11 00:04:48 2019 -0700
@@ -49,11 +49,11 @@
 		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 @@
 		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 @@
 			} 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 @@
 	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:
--- a/io.c	Sun Mar 10 23:10:43 2019 -0700
+++ b/io.c	Mon Mar 11 00:04:48 2019 -0700
@@ -219,8 +219,7 @@
 	}
 
 	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 @@
 			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;
--- a/m68k_core.c	Sun Mar 10 23:10:43 2019 -0700
+++ b/m68k_core.c	Mon Mar 11 00:04:48 2019 -0700
@@ -1216,9 +1216,7 @@
 
 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;
--- a/util.c	Sun Mar 10 23:10:43 2019 -0700
+++ b/util.c	Mon Mar 11 00:04:48 2019 -0700
@@ -189,6 +189,11 @@
 	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)
--- a/util.h	Sun Mar 10 23:10:43 2019 -0700
+++ b/util.h	Mon Mar 11 00:04:48 2019 -0700
@@ -32,6 +32,8 @@
 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