Skip to content

Commit 57cf401

Browse files
committed
Reduce gotos in command.c/core_backup.c
1 parent 3aa8db2 commit 57cf401

File tree

2 files changed

+174
-198
lines changed

2 files changed

+174
-198
lines changed

command.c

Lines changed: 80 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -294,42 +294,42 @@ static void stdin_command_free(command_t *handle)
294294

295295
static void command_stdin_poll(command_t *handle)
296296
{
297-
ptrdiff_t msg_len;
298-
char *last_newline = NULL;
299297
command_stdin_t *stdincmd = (command_stdin_t*)handle->userptr;
300298
ssize_t ret = read_stdin(
301299
stdincmd->stdin_buf + stdincmd->stdin_buf_ptr,
302300
CMD_BUF_SIZE - stdincmd->stdin_buf_ptr - 1);
303301

304-
if (ret == 0)
305-
return;
306-
307-
stdincmd->stdin_buf_ptr += ret;
308-
stdincmd->stdin_buf[stdincmd->stdin_buf_ptr] = '\0';
302+
if (ret != 0)
303+
{
304+
char *last_newline = NULL;
305+
stdincmd->stdin_buf_ptr += ret;
306+
stdincmd->stdin_buf[stdincmd->stdin_buf_ptr] = '\0';
309307

310-
last_newline = strrchr(stdincmd->stdin_buf, '\n');
308+
last_newline = strrchr(stdincmd->stdin_buf, '\n');
311309

312-
if (!last_newline)
313-
{
314-
/* We're receiving bogus data in pipe
315-
* (no terminating newline), flush out the buffer. */
316-
if (stdincmd->stdin_buf_ptr + 1 >= CMD_BUF_SIZE)
310+
if (!last_newline)
317311
{
318-
stdincmd->stdin_buf_ptr = 0;
319-
stdincmd->stdin_buf[0] = '\0';
312+
/* We're receiving bogus data in pipe
313+
* (no terminating newline), flush out the buffer. */
314+
if (stdincmd->stdin_buf_ptr + 1 >= CMD_BUF_SIZE)
315+
{
316+
stdincmd->stdin_buf_ptr = 0;
317+
stdincmd->stdin_buf[0] = '\0';
318+
}
320319
}
320+
else
321+
{
322+
ptrdiff_t msg_len;
323+
*last_newline++ = '\0';
324+
msg_len = last_newline - stdincmd->stdin_buf;
321325

322-
return;
323-
}
324-
325-
*last_newline++ = '\0';
326-
msg_len = last_newline - stdincmd->stdin_buf;
327-
328-
command_parse_msg(handle, stdincmd->stdin_buf);
326+
command_parse_msg(handle, stdincmd->stdin_buf);
329327

330-
memmove(stdincmd->stdin_buf, last_newline,
331-
stdincmd->stdin_buf_ptr - msg_len);
332-
stdincmd->stdin_buf_ptr -= msg_len;
328+
memmove(stdincmd->stdin_buf, last_newline,
329+
stdincmd->stdin_buf_ptr - msg_len);
330+
stdincmd->stdin_buf_ptr -= msg_len;
331+
}
332+
}
333333
}
334334

335335
command_t* command_stdin_new(void)
@@ -386,9 +386,8 @@ static void command_emscripten_poll(command_t *handle)
386386
{
387387
command_emscripten_t *emscriptencmd = (command_emscripten_t*)handle->userptr;
388388
ptrdiff_t msg_len = platform_emscripten_command_read((char **)(&emscriptencmd->command_buf), CMD_BUF_SIZE);
389-
if (msg_len == 0)
390-
return;
391-
command_parse_msg(handle, emscriptencmd->command_buf);
389+
if (msg_len != 0)
390+
command_parse_msg(handle, emscriptencmd->command_buf);
392391
}
393392

394393
command_t* command_emscripten_new(void)
@@ -419,9 +418,9 @@ bool command_get_config_param(command_t *cmd, const char* arg)
419418
{
420419
size_t _len;
421420
char reply[8192];
422-
#ifdef HAVE_BSV_MOVIE
421+
#ifdef HAVE_BSV_MOVIE
423422
char value_dynamic[256];
424-
#endif
423+
#endif
425424
const char *value = "unsupported";
426425
settings_t *settings = config_get_ptr();
427426
bool video_fullscreen = settings->bools.video_fullscreen;
@@ -590,13 +589,8 @@ command_t* command_uds_new(void)
590589
strcpy(&addr.sun_path[1], "retroarch/cmd");
591590

592591
if ( bind(fd, (struct sockaddr*)&addr, addrsz) < 0
593-
|| listen(fd, MAX_USER_CONNECTIONS) < 0)
594-
{
595-
socket_close(fd);
596-
return NULL;
597-
}
598-
599-
if (!socket_nonblock(fd))
592+
|| listen(fd, MAX_USER_CONNECTIONS) < 0
593+
|| !socket_nonblock(fd))
600594
{
601595
socket_close(fd);
602596
return NULL;
@@ -624,8 +618,7 @@ command_t* command_uds_new(void)
624618
#ifdef HAVE_NETWORK_CMD
625619
static bool command_verify(const char *cmd)
626620
{
627-
unsigned i;
628-
621+
size_t i;
629622
if (command_get_arg(cmd, NULL, NULL))
630623
return true;
631624

@@ -842,38 +835,37 @@ bool command_load_savefiles(command_t *cmd, const char* arg)
842835
#if defined(HAVE_CHEEVOS)
843836
bool command_read_ram(command_t *cmd, const char *arg)
844837
{
845-
unsigned i;
846-
char *reply = NULL;
847-
const uint8_t *data = NULL;
848-
char *reply_at = NULL;
849-
unsigned int nbytes = 0;
850-
unsigned int alloc_size = 0;
851-
unsigned int addr = -1;
852-
size_t _len = 0;
838+
unsigned int nbytes = 0;
839+
unsigned int addr = -1;
853840

854-
if (sscanf(arg, "%x %u", &addr, &nbytes) != 2)
855-
return true;
856-
/* We allocate more than needed, saving 20 bytes is not really relevant */
857-
alloc_size = 40 + nbytes * 3;
858-
reply = (char*)malloc(alloc_size);
859-
reply[0] = '\0';
860-
reply_at = reply + snprintf(
861-
reply, alloc_size - 1, "READ_CORE_RAM" " %x", addr);
862-
863-
if ((data = rcheevos_patch_address(addr)))
841+
if (sscanf(arg, "%x %u", &addr, &nbytes) == 2)
864842
{
865-
for (i = 0; i < nbytes; i++)
866-
snprintf(reply_at + 3 * i, 4, " %.2X", data[i]);
867-
reply_at[3 * nbytes] = '\n';
868-
_len = reply_at + 3 * nbytes + 1 - reply;
869-
}
870-
else
871-
{
872-
strlcpy(reply_at, " -1\n", sizeof(reply) - strlen(reply));
873-
_len = reply_at + STRLEN_CONST(" -1\n") - reply;
843+
size_t _len = 0;
844+
char *reply_at = NULL;
845+
const uint8_t *data = NULL;
846+
/* We allocate more than needed, saving 20 bytes is not really relevant */
847+
unsigned int alloc_size = 40 + nbytes * 3;
848+
char *reply = (char*)malloc(alloc_size);
849+
reply[0] = '\0';
850+
reply_at = reply + snprintf(
851+
reply, alloc_size - 1, "READ_CORE_RAM" " %x", addr);
852+
853+
if ((data = rcheevos_patch_address(addr)))
854+
{
855+
size_t i;
856+
for (i = 0; i < nbytes; i++)
857+
snprintf(reply_at + 3 * i, 4, " %.2X", data[i]);
858+
reply_at[3 * nbytes] = '\n';
859+
_len = reply_at + 3 * nbytes + 1 - reply;
860+
}
861+
else
862+
{
863+
strlcpy(reply_at, " -1\n", sizeof(reply) - strlen(reply));
864+
_len = reply_at + STRLEN_CONST(" -1\n") - reply;
865+
}
866+
cmd->replier(cmd, reply, _len);
867+
free(reply);
874868
}
875-
cmd->replier(cmd, reply, _len);
876-
free(reply);
877869
return true;
878870
}
879871

@@ -1080,7 +1072,8 @@ bool command_write_memory(command_t *cmd, const char *arg)
10801072
const rarch_system_info_t
10811073
*sys_info = &runloop_st->system;
10821074
char *reply_at = reply + snprintf(reply, sizeof(reply) - 1, "WRITE_CORE_MEMORY %x", address);
1083-
uint8_t *data = command_memory_get_pointer(sys_info, address, &max_bytes, 1, reply_at, sizeof(reply) - strlen(reply) - 1);
1075+
uint8_t *data = command_memory_get_pointer(sys_info, address, &max_bytes, 1,
1076+
reply_at, sizeof(reply) - strlen(reply) - 1);
10841077

10851078
if (data)
10861079
{
@@ -1123,15 +1116,10 @@ void command_event_set_volume(
11231116
configuration_set_float(settings, settings->floats.audio_volume, new_volume);
11241117
_len = strlcpy(msg, msg_hash_to_str(MSG_AUDIO_VOLUME),
11251118
sizeof(msg));
1126-
msg[_len ] = ':';
1127-
msg[++_len] = ' ';
1128-
msg[++_len] = '\0';
1119+
_len += strlcpy(msg + _len, ": ", sizeof(msg) - _len);
11291120
_len += snprintf(msg + _len, sizeof(msg) - _len, "%.1f",
11301121
new_volume);
1131-
msg[_len ] = ' ';
1132-
msg[++_len] = 'd';
1133-
msg[++_len] = 'B';
1134-
msg[++_len] = '\0';
1122+
_len += strlcpy(msg + _len, " dB", sizeof(msg) - _len);
11351123

11361124
#if defined(HAVE_GFX_WIDGETS)
11371125
if (widgets_active)
@@ -1166,15 +1154,10 @@ void command_event_set_mixer_volume(
11661154
configuration_set_float(settings, settings->floats.audio_mixer_volume, new_volume);
11671155
_len = strlcpy(msg, msg_hash_to_str(MSG_AUDIO_VOLUME),
11681156
sizeof(msg));
1169-
msg[_len ] = ':';
1170-
msg[++_len] = ' ';
1171-
msg[++_len] = '\0';
1157+
_len += strlcpy(msg + _len, ": ", sizeof(msg) - _len);
11721158
_len += snprintf(msg + _len, sizeof(msg) - _len, "%.1f",
11731159
new_volume);
1174-
msg[_len ] = ' ';
1175-
msg[++_len] = 'd';
1176-
msg[++_len] = 'B';
1177-
msg[++_len] = '\0';
1160+
_len += strlcpy(msg + _len, " dB", sizeof(msg) - _len);
11781161
runloop_msg_queue_push(msg, _len, 1, 180, true, NULL,
11791162
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
11801163

@@ -1191,7 +1174,7 @@ void command_event_init_controllers(rarch_system_info_t *sys_info,
11911174

11921175
for (port = 0; port < num_core_ports; port++)
11931176
{
1194-
unsigned i;
1177+
size_t i;
11951178
retro_ctx_controller_info_t pad;
11961179
unsigned device = RETRO_DEVICE_NONE;
11971180
const struct retro_controller_description *desc = NULL;
@@ -1314,8 +1297,8 @@ static size_t command_event_undo_load_state(char *s, size_t len)
13141297
bool command_event_resize_windowed_scale(settings_t *settings,
13151298
unsigned window_scale)
13161299
{
1317-
unsigned idx = 0;
1318-
bool video_fullscreen = settings->bools.video_fullscreen;
1300+
unsigned idx = 0;
1301+
bool video_fullscreen = settings->bools.video_fullscreen;
13191302

13201303
if (window_scale == 0)
13211304
return false;
@@ -1702,20 +1685,20 @@ void command_event_set_savestate_auto_index(settings_t *settings)
17021685
{
17031686
unsigned max_idx = 0;
17041687
bool savestate_auto_index = settings->bools.savestate_auto_index;
1705-
if (!savestate_auto_index)
1688+
if (savestate_auto_index)
17061689
{
1690+
command_scan_states(
1691+
settings->bools.show_hidden_files,
1692+
settings->uints.savestate_max_keep,
1693+
settings->ints.state_slot, &max_idx, NULL);
1694+
configuration_set_int(settings, settings->ints.state_slot, max_idx);
1695+
RARCH_LOG("[State] %s: #%d.\n",
1696+
msg_hash_to_str(MSG_FOUND_LAST_STATE_SLOT),
1697+
max_idx);
1698+
}
1699+
else
17071700
/* Reset savestate index to 0 when loading content. */
17081701
configuration_set_int(settings, settings->ints.state_slot, 0);
1709-
return;
1710-
}
1711-
command_scan_states(
1712-
settings->bools.show_hidden_files,
1713-
settings->uints.savestate_max_keep,
1714-
settings->ints.state_slot, &max_idx, NULL);
1715-
configuration_set_int(settings, settings->ints.state_slot, max_idx);
1716-
RARCH_LOG("[State] %s: #%d.\n",
1717-
msg_hash_to_str(MSG_FOUND_LAST_STATE_SLOT),
1718-
max_idx);
17191702
}
17201703

17211704
/**
@@ -2154,8 +2137,6 @@ bool command_event_main_state(unsigned cmd)
21542137
then and now (not yet implemented); if the state is not part of
21552138
the replay, do nothing and log a warning.
21562139
*/
2157-
2158-
21592140
if (savestates_enabled)
21602141
{
21612142
switch (cmd)

0 commit comments

Comments
 (0)