diff --git a/liberty-proto.c b/liberty-proto.c index cf84586..ff97d2b 100644 --- a/liberty-proto.c +++ b/liberty-proto.c @@ -29,7 +29,7 @@ struct irc_message struct str_map tags; ///< IRC 3.2 message tags char *prefix; ///< Message prefix char *command; ///< IRC command - struct str_vector params; ///< Command parameters + struct strv params; ///< Command parameters }; static char * @@ -64,8 +64,8 @@ irc_unescape_message_tag (const char *value) static void irc_parse_message_tags (const char *tags, struct str_map *out) { - struct str_vector v; - str_vector_init (&v); + struct strv v; + strv_init (&v); cstr_split (tags, ";", true, &v); for (size_t i = 0; i < v.len; i++) @@ -80,7 +80,7 @@ irc_parse_message_tags (const char *tags, struct str_map *out) str_map_set (out, key, xstrdup ("")); } - str_vector_free (&v); + strv_free (&v); } static void @@ -91,7 +91,7 @@ irc_parse_message (struct irc_message *msg, const char *line) msg->prefix = NULL; msg->command = NULL; - str_vector_init (&msg->params); + strv_init (&msg->params); // IRC 3.2 message tags if (*line == '@') @@ -132,7 +132,7 @@ irc_parse_message (struct irc_message *msg, const char *line) if (*line == ':') { - str_vector_add (&msg->params, ++line); + strv_add (&msg->params, ++line); break; } @@ -140,7 +140,7 @@ irc_parse_message (struct irc_message *msg, const char *line) if (!param_len) break; - str_vector_add_owned (&msg->params, xstrndup (line, param_len)); + strv_add_owned (&msg->params, xstrndup (line, param_len)); line += param_len; } } @@ -151,7 +151,7 @@ irc_free_message (struct irc_message *msg) str_map_free (&msg->tags); free (msg->prefix); free (msg->command); - str_vector_free (&msg->params); + strv_free (&msg->params); } static void @@ -1360,7 +1360,7 @@ struct mpd_response /// Task completion callback typedef void (*mpd_client_task_cb) (const struct mpd_response *response, - const struct str_vector *data, void *user_data); + const struct strv *data, void *user_data); struct mpd_client_task { @@ -1396,7 +1396,7 @@ struct mpd_client struct mpd_client_task *tasks; ///< Task queue struct mpd_client_task *tasks_tail; ///< Tail of task queue - struct str_vector data; ///< Data from last command + struct strv data; ///< Data from last command // User configuration: @@ -1431,7 +1431,7 @@ mpd_client_init (struct mpd_client *self, struct poller *poller) str_init (&self->read_buffer); str_init (&self->write_buffer); - str_vector_init (&self->data); + strv_init (&self->data); poller_fd_init (&self->socket_event, poller, -1); poller_timer_init (&self->timeout_timer, poller); @@ -1446,7 +1446,7 @@ mpd_client_free (struct mpd_client *self) str_free (&self->read_buffer); str_free (&self->write_buffer); - str_vector_free (&self->data); + strv_free (&self->data); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1469,7 +1469,7 @@ mpd_client_reset (struct mpd_client *self) str_reset (&self->read_buffer); str_reset (&self->write_buffer); - str_vector_reset (&self->data); + strv_reset (&self->data); self->got_hello = false; self->idling = false; @@ -1539,7 +1539,7 @@ mpd_client_dispatch (struct mpd_client *self, struct mpd_response *response) if (task->callback) task->callback (response, &self->data, task->user_data); - str_vector_reset (&self->data); + strv_reset (&self->data); LIST_UNLINK_WITH_TAIL (self->tasks, self->tasks_tail, task); free (task); @@ -1572,7 +1572,7 @@ mpd_client_parse_line (struct mpd_client *self, const char *line) struct mpd_response response; memset (&response, 0, sizeof response); if (!strcmp (line, "list_OK")) - str_vector_add_owned (&self->data, NULL); + strv_add_owned (&self->data, NULL); else if (mpd_client_parse_response (line, &response)) { mpd_client_dispatch (self, &response); @@ -1580,7 +1580,7 @@ mpd_client_parse_line (struct mpd_client *self, const char *line) free (response.message_text); } else - str_vector_add (&self->data, line); + strv_add (&self->data, line); return true; } @@ -1735,17 +1735,17 @@ mpd_client_send_commandv (struct mpd_client *self, char **commands) static void mpd_client_send_command (struct mpd_client *self, const char *command, ...) { - struct str_vector v; - str_vector_init (&v); + struct strv v; + strv_init (&v); va_list ap; va_start (ap, command); for (; command; command = va_arg (ap, const char *)) - str_vector_add (&v, command); + strv_add (&v, command); va_end (ap); mpd_client_send_commandv (self, v.vector); - str_vector_free (&v); + strv_free (&v); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1793,7 +1793,7 @@ mpd_resolve_subsystem (const char *name, unsigned *output) static void mpd_client_on_idle_return (const struct mpd_response *response, - const struct str_vector *data, void *user_data) + const struct strv *data, void *user_data) { (void) response; @@ -1836,16 +1836,16 @@ mpd_client_idle (struct mpd_client *self, unsigned subsystems) { hard_assert (!self->in_list); - struct str_vector v; - str_vector_init (&v); + struct strv v; + strv_init (&v); - str_vector_add (&v, "idle"); + strv_add (&v, "idle"); for (size_t i = 0; i < N_ELEMENTS (mpd_subsystem_names); i++) if (subsystems & (1 << i)) - str_vector_add (&v, mpd_subsystem_names[i]); + strv_add (&v, mpd_subsystem_names[i]); mpd_client_send_commandv (self, v.vector); - str_vector_free (&v); + strv_free (&v); self->timeout_timer.dispatcher = mpd_client_on_timeout; self->timeout_timer.user_data = self; diff --git a/liberty-tui.c b/liberty-tui.c index c6e70e9..4d48c2a 100644 --- a/liberty-tui.c +++ b/liberty-tui.c @@ -62,8 +62,8 @@ struct attrs static struct attrs attrs_decode (const char *value) { - struct str_vector v; - str_vector_init (&v); + struct strv v; + strv_init (&v); cstr_split (value, " ", true, &v); int colors = 0; @@ -87,7 +87,7 @@ attrs_decode (const char *value) else if (!strcmp (*it, "italic")) attrs.attrs |= A_ITALIC; #endif // A_ITALIC } - str_vector_free (&v); + strv_free (&v); return attrs; } diff --git a/liberty.c b/liberty.c index c564bfd..9a0d2cc 100644 --- a/liberty.c +++ b/liberty.c @@ -377,7 +377,7 @@ xstrndup (const char *s, size_t n) // --- Dynamically allocated string array -------------------------------------- -struct str_vector +struct strv { char **vector; size_t len; @@ -385,7 +385,7 @@ struct str_vector }; static void -str_vector_init (struct str_vector *self) +strv_init (struct strv *self) { self->alloc = 4; self->len = 0; @@ -393,7 +393,7 @@ str_vector_init (struct str_vector *self) } static void -str_vector_free (struct str_vector *self) +strv_free (struct strv *self) { unsigned i; for (i = 0; i < self->len; i++) @@ -404,14 +404,14 @@ str_vector_free (struct str_vector *self) } static void -str_vector_reset (struct str_vector *self) +strv_reset (struct strv *self) { - str_vector_free (self); - str_vector_init (self); + strv_free (self); + strv_init (self); } static void -str_vector_add_owned (struct str_vector *self, char *s) +strv_add_owned (struct strv *self, char *s) { self->vector[self->len] = s; if (++self->len >= self->alloc) @@ -421,38 +421,38 @@ str_vector_add_owned (struct str_vector *self, char *s) } static void -str_vector_add (struct str_vector *self, const char *s) +strv_add (struct strv *self, const char *s) { - str_vector_add_owned (self, xstrdup (s)); + strv_add_owned (self, xstrdup (s)); } static void -str_vector_add_args (struct str_vector *self, const char *s, ...) +strv_add_args (struct strv *self, const char *s, ...) ATTRIBUTE_SENTINEL; static void -str_vector_add_args (struct str_vector *self, const char *s, ...) +strv_add_args (struct strv *self, const char *s, ...) { va_list ap; va_start (ap, s); while (s) { - str_vector_add (self, s); + strv_add (self, s); s = va_arg (ap, const char *); } va_end (ap); } static void -str_vector_add_vector (struct str_vector *self, char **vector) +strv_add_vector (struct strv *self, char **vector) { while (*vector) - str_vector_add (self, *vector++); + strv_add (self, *vector++); } static char * -str_vector_steal (struct str_vector *self, size_t i) +strv_steal (struct strv *self, size_t i) { hard_assert (i < self->len); char *tmp = self->vector[i]; @@ -462,9 +462,9 @@ str_vector_steal (struct str_vector *self, size_t i) } static void -str_vector_remove (struct str_vector *self, size_t i) +strv_remove (struct strv *self, size_t i) { - free (str_vector_steal (self, i)); + free (strv_steal (self, i)); } // --- Dynamically allocated strings ------------------------------------------- @@ -2915,17 +2915,17 @@ base64_encode (const void *data, size_t len, struct str *output) static void cstr_split (const char *s, const char *delimiters, bool ignore_empty, - struct str_vector *out) + struct strv *out) { const char *begin = s, *end; while ((end = strpbrk (begin, delimiters))) { if (!ignore_empty || begin != end) - str_vector_add_owned (out, xstrndup (begin, end - begin)); + strv_add_owned (out, xstrndup (begin, end - begin)); begin = ++end; } if (!ignore_empty || *begin) - str_vector_add (out, begin); + strv_add (out, begin); } static char * @@ -2957,7 +2957,7 @@ cstr_cut_until (const char *s, const char *alphabet) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - static char * -join_str_vector (const struct str_vector *v, char delimiter) +join_strv (const struct strv *v, char delimiter) { if (!v->len) return xstrdup (""); @@ -3188,7 +3188,7 @@ get_xdg_home_dir (struct str *output, const char *var, const char *def) static char * resolve_relative_filename_generic - (struct str_vector *paths, const char *tail, const char *filename) + (struct strv *paths, const char *tail, const char *filename) { for (unsigned i = 0; i < paths->len; i++) { @@ -3208,12 +3208,12 @@ resolve_relative_filename_generic } static void -get_xdg_config_dirs (struct str_vector *out) +get_xdg_config_dirs (struct strv *out) { struct str config_home; str_init (&config_home); get_xdg_home_dir (&config_home, "XDG_CONFIG_HOME", ".config"); - str_vector_add (out, config_home.str); + strv_add (out, config_home.str); str_free (&config_home); const char *xdg_config_dirs; @@ -3225,22 +3225,22 @@ get_xdg_config_dirs (struct str_vector *out) static char * resolve_relative_config_filename (const char *filename) { - struct str_vector paths; - str_vector_init (&paths); + struct strv paths; + strv_init (&paths); get_xdg_config_dirs (&paths); char *result = resolve_relative_filename_generic (&paths, PROGRAM_NAME "/", filename); - str_vector_free (&paths); + strv_free (&paths); return result; } static void -get_xdg_data_dirs (struct str_vector *out) +get_xdg_data_dirs (struct strv *out) { struct str data_home; str_init (&data_home); get_xdg_home_dir (&data_home, "XDG_DATA_HOME", ".local/share"); - str_vector_add (out, data_home.str); + strv_add (out, data_home.str); str_free (&data_home); const char *xdg_data_dirs; @@ -3252,12 +3252,12 @@ get_xdg_data_dirs (struct str_vector *out) static char * resolve_relative_data_filename (const char *filename) { - struct str_vector paths; - str_vector_init (&paths); + struct strv paths; + strv_init (&paths); get_xdg_data_dirs (&paths); char *result = resolve_relative_filename_generic (&paths, PROGRAM_NAME "/", filename); - str_vector_free (&paths); + strv_free (&paths); return result; } @@ -3406,7 +3406,7 @@ regex_free (void *regex) // The cost of hashing a string is likely to be significantly smaller than that // of compiling the whole regular expression anew, so here is a simple cache. // Adding basic support for subgroups is easy: check `re_nsub' and output into -// a `struct str_vector' (if all we want is the substrings). +// a `struct strv' (if all we want is the substrings). static void regex_cache_init (struct str_map *cache) @@ -4630,8 +4630,8 @@ config_item_get (struct config_item *self, const char *path, struct error **e) { hard_assert (self->type == CONFIG_ITEM_OBJECT); - struct str_vector v; - str_vector_init (&v); + struct strv v; + strv_init (&v); cstr_split (path, ".", false, &v); struct config_item *result = NULL; @@ -4651,7 +4651,7 @@ config_item_get (struct config_item *self, const char *path, struct error **e) continue; break; } - str_vector_free (&v); + strv_free (&v); return result; } diff --git a/tests/liberty.c b/tests/liberty.c index 0925b05..b46ef87 100644 --- a/tests/liberty.c +++ b/tests/liberty.c @@ -157,31 +157,31 @@ test_list_with_tail (void) // --- Strings ----------------------------------------------------------------- static void -test_str_vector (void) +test_strv (void) { - struct str_vector v; - str_vector_init (&v); + struct strv v; + strv_init (&v); - str_vector_add_owned (&v, xstrdup ("xkcd")); - str_vector_reset (&v); + strv_add_owned (&v, xstrdup ("xkcd")); + strv_reset (&v); const char *a[] = { "123", "456", "a", "bc", "def", "ghij", "klmno", "pqrstu" }; // Add the first two items via another vector - struct str_vector w; - str_vector_init (&w); - str_vector_add_args (&w, a[0], a[1], NULL); - str_vector_add_vector (&v, w.vector); - str_vector_free (&w); + struct strv w; + strv_init (&w); + strv_add_args (&w, a[0], a[1], NULL); + strv_add_vector (&v, w.vector); + strv_free (&w); // Add an item and delete it right after - str_vector_add (&v, "test"); - str_vector_remove (&v, v.len - 1); + strv_add (&v, "test"); + strv_remove (&v, v.len - 1); // Add the rest of the list properly for (int i = 2; i < (int) N_ELEMENTS (a); i++) - str_vector_add (&v, a[i]); + strv_add (&v, a[i]); // Check the contents soft_assert (v.len == N_ELEMENTS (a)); @@ -189,7 +189,7 @@ test_str_vector (void) soft_assert (!strcmp (v.vector[i], a[i])); soft_assert (v.vector[v.len] == NULL); - str_vector_free (&v); + strv_free (&v); } static void @@ -629,7 +629,7 @@ main (int argc, char *argv[]) test_add_simple (&test, "/memory", NULL, test_memory); test_add_simple (&test, "/list", NULL, test_list); test_add_simple (&test, "/list-with-tail", NULL, test_list_with_tail); - test_add_simple (&test, "/str-vector", NULL, test_str_vector); + test_add_simple (&test, "/strv", NULL, test_strv); test_add_simple (&test, "/str", NULL, test_str); test_add_simple (&test, "/error", NULL, test_error); test_add_simple (&test, "/str-map", NULL, test_str_map);