Use scope-based memory management for malloc, free, open, close, etc.

If we are using C++, we might as well use scope-based memory management
for these things, cleaner and less error prone. Also removed
unnecessary gotos.
This commit is contained in:
Eric Curtin
2023-05-08 23:49:26 +01:00
committed by Caleb Connolly
parent 843aa92266
commit 9d7600df51
5 changed files with 331 additions and 347 deletions

View File

@@ -41,17 +41,23 @@ bool isslotnum(const char* str)
return strspn(str, "01") == strlen(str);
}
static void unexpectedSlot(const char *arg)
{
fprintf(stderr, "Expected slot not '%s'\n", arg);
exit(1);
}
unsigned parseSlot(const char* arg)
{
char *end;
int slot;
if (!isslot(arg)) {
goto fail;
}
if (!isslot(arg))
unexpectedSlot(arg);
if (isslotnum(arg)) {
slot = (int)strtol(arg, &end, 10);
if (end == arg)
goto fail;
unexpectedSlot(arg);
} else {
switch (arg[0]) {
case 'a':
@@ -63,17 +69,11 @@ unsigned parseSlot(const char* arg)
slot = 1;
break;
default:
goto fail;
unexpectedSlot(arg);
}
}
return (unsigned)slot;
fail:
fprintf(stderr,
"Expected slot not '%s'\n",
arg);
exit(1);
}
int usage()