Error checking and "find" command

This commit is contained in:
Casey 2024-04-30 12:17:02 +03:00
parent 470fd27fbb
commit 58010c8a07
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
1 changed files with 49 additions and 3 deletions

View File

@ -151,6 +151,8 @@ void ipc_loop(void) {
if (params != NULL) {
*params = '\0';
params++;
} else {
params = "";
}
execute_command((struct msg_target) { 0, ipc.sock_fd, sa_client, sl_client }, command, params);
SOFT_EXPECT(sendto(ipc.sock_fd, "", 0, 0, sa_client, sl_client), == 0,);
@ -195,8 +197,16 @@ void execute_command(struct msg_target tgt, const char *command, const char *par
send_txt((struct msg_target){ stdout, 0, 0, 0 }, "INF: Received '%s' with '%s'\n", command, params);
if (0 == strcmp(command, "load")) {
if (strlen(params) == 0) {
send_txt(tgt, "ERR: argument required: KEY\n");
return;
}
const char *key = params;
char *path = strchr(params, ' ');
if (path == NULL) {
send_txt(tgt, "ERR: argument required: PATH\n");
return;
}
*path = '\0';
path++;
@ -216,9 +226,13 @@ void execute_command(struct msg_target tgt, const char *command, const char *par
send_txt(tgt, "OK: Loaded as %08x\n", adler32(key, strlen(key)));
}
} else if (0 == strcmp(command, "play")) {
if (strlen(params) == 0) {
send_txt(tgt, "ERR: argument required: KEY\n");
return;
}
struct sfx_pool_item *sound = sfx_pool_lookup(params);
if (!sound) {
send_txt(tgt, "No such sound: '%s'\n", params);
send_txt(tgt, "ERR: No such sound: '%s'\n", params);
return;
}
@ -229,6 +243,10 @@ void execute_command(struct msg_target tgt, const char *command, const char *par
ma_sound_set_volume(sfx, sound->volume);
ma_sound_start(sfx);
} else if (0 == strcmp(command, "play:rs")) {
if (strlen(params) == 0) {
send_txt(tgt, "ERR: argument required: KEY\n");
return;
}
struct sfx_pool_item *sound = sfx_pool_lookup(params);
if (!sound) {
send_txt(tgt, "ERR: No such sound: '%s'\n", params);
@ -245,7 +263,11 @@ void execute_command(struct msg_target tgt, const char *command, const char *par
} else if (0 == strcmp(command, "set:pitch")) {
float min, max;
char *key;
sscanf(params, "%ms %f %f", &key, &min, &max);
int retval = sscanf(params, "%ms %f %f", &key, &min, &max);
if (retval != 3) {
send_txt(tgt, "ERR: not enough arguments: KEY MIN MAX needed\n");
return;
}
struct sfx_pool_item *sound = sfx_pool_lookup(key);
if (!sound) {
@ -271,7 +293,11 @@ void execute_command(struct msg_target tgt, const char *command, const char *par
} else if (0 == strcmp(command, "set:volume")) {
float vol;
char *key;
sscanf(params, "%ms %f", &key, &vol);
int retval = sscanf(params, "%ms %f", &key, &vol);
if (retval != 2) {
send_txt(tgt, "ERR: not enough arguments: KEY VOLUME\n");
return;
}
struct sfx_pool_item *sound = sfx_pool_lookup(key);
if (!sound) {
@ -284,7 +310,23 @@ void execute_command(struct msg_target tgt, const char *command, const char *par
}
sound->volume = vol;
} else if (0 == strcmp(command, "find")) {
if (strlen(params) == 0) {
send_txt(tgt, "ERR: argument required: QUERY\n");
return;
}
for (int i = 0; i < sounds_pool.cap; i++) {
struct sfx_pool_item item = sounds_pool.sounds[i];
if (item.key[0] != '\0' && strcasestr(item.key, params)) {
send_txt(tgt, "%s\n", item.key);
}
}
} else if (0 == strcmp(command, "source")) {
if (strlen(params) == 0) {
send_txt(tgt, "ERR: argument required: PATH\n");
return;
}
execute_file(tgt, params, 0);
} else if (0 == strcmp(command, "help")) {
send_help(tgt);
@ -439,6 +481,7 @@ void sfx_pool_grow(int size) {
struct sfx_pool_item *sfx_pool_lookup(const char *key) {
if (key == NULL) return NULL;
if (strlen(key) == 0) return NULL;
uint32_t hash = adler32(key, strlen(key));
global_counters.pool_read++;
for (int offset = 0; offset < sounds_pool.cap; offset++) {
@ -457,6 +500,8 @@ struct sfx_pool_item *sfx_pool_lookup(const char *key) {
}
struct sfx_pool_item *sfx_pool_find_place_for(const char *key) {
if (key == NULL) return NULL;
if (strlen(key) == 0) return NULL;
uint32_t hash = adler32(key, strlen(key));
global_counters.pool_write++;
for (int offset = 0; offset < sounds_pool.cap; offset++) {
@ -476,6 +521,7 @@ struct sfx_pool_item *sfx_pool_find_place_for(const char *key) {
struct sfx_pool_item *sfx_pool_load(const char *key, const char *path) {
if (key == NULL || path == NULL) return NULL;
if (strlen(key) == 0 || strlen(path) == 0) return NULL;
struct sfx_pool_item *sound = sfx_pool_find_place_for(key);
SOFT_EXPECT(sound == NULL, == false, NULL);