summaryrefslogtreecommitdiff
path: root/src/string_vec.c
diff options
context:
space:
mode:
authorPhil Jones <philj56@gmail.com>2022-06-11 10:14:53 +0100
committerPhil Jones <philj56@gmail.com>2022-06-11 10:14:53 +0100
commitc0cd4cdf78886040528b16fad084a14165a16384 (patch)
tree82788f09527c91060cea22246f1af9a2572ce655 /src/string_vec.c
parent7c31982244f179cb2f51bb1d81b21ad4efd649ba (diff)
Add compgen caching.
A list of commands is now stored in $XDG_CACHE_HOME/.cache/tofi-compgen, and regenerated as necessary.
Diffstat (limited to 'src/string_vec.c')
-rw-r--r--src/string_vec.c34
1 files changed, 32 insertions, 2 deletions
diff --git a/src/string_vec.c b/src/string_vec.c
index 0bcd7f2..e55dea4 100644
--- a/src/string_vec.c
+++ b/src/string_vec.c
@@ -1,7 +1,8 @@
-#define _GNU_SOURCE /* Required for strcasecmp */
#include <stdint.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/mman.h>
#include "string_vec.h"
#include "xmalloc.h"
@@ -99,9 +100,38 @@ struct string_vec string_vec_filter(
{
struct string_vec filt = string_vec_create();
for (size_t i = 0; i < vec->count; i++) {
- if (strcasestr(vec->buf[i], substr) != NULL) {
+ if (strstr(vec->buf[i], substr) != NULL) {
string_vec_add(&filt, vec->buf[i]);
}
}
return filt;
}
+
+struct string_vec string_vec_load(FILE *file)
+{
+ struct string_vec vec = string_vec_create();
+ if (file == NULL) {
+ return vec;
+ }
+
+ ssize_t bytes_read;
+ char *line = NULL;
+ size_t len;
+ while ((bytes_read = getline(&line, &len, file)) != -1) {
+ if (line[bytes_read - 1] == '\n') {
+ line[bytes_read - 1] = '\0';
+ }
+ string_vec_add(&vec, line);
+ }
+ free(line);
+
+ return vec;
+}
+
+void string_vec_save(struct string_vec *restrict vec, FILE *restrict file)
+{
+ for (size_t i = 0; i < vec->count; i++) {
+ fputs(vec->buf[i], file);
+ fputc('\n', file);
+ }
+}