Add remove config

This commit is contained in:
Nikita Ivanov 2022-06-10 13:25:11 +05:00
parent a44cc26cef
commit f850b4b116
No known key found for this signature in database
GPG Key ID: 6E656AC5B97B5133
3 changed files with 49 additions and 5 deletions

View File

@ -71,6 +71,21 @@ static int add_priority(char *name, int priority)
return found ? OK : ERR; return found ? OK : ERR;
} }
static int remove_preview(char *name)
{
int found = 0;
for (size_t i = 0; i < previews->len; i++) {
if (strcmp(previews->buf[i].name, name) != 0)
continue;
vectorPreview_remove(previews, i);
found = 1;
}
return found ? OK : ERR;
}
static inline void next_token(void) static inline void next_token(void)
{ {
token = lexer_get_token(lexer); token = lexer_get_token(lexer);
@ -142,7 +157,7 @@ static int preview_type(char **type, char **subtype, char **ext)
return preview_type_mime(type, subtype); return preview_type_mime(type, subtype);
} }
static int new_preview(void) static int cmd_preview(void)
{ {
Token name = token; Token name = token;
EXPECT(TOK_STR); EXPECT(TOK_STR);
@ -164,7 +179,7 @@ static int new_preview(void)
return STAT_OK; return STAT_OK;
} }
static int priority(Token tok) static int cmd_priority(Token tok)
{ {
Token name = token; Token name = token;
EXPECT(TOK_STR); EXPECT(TOK_STR);
@ -181,6 +196,20 @@ static int priority(Token tok)
return STAT_OK; return STAT_OK;
} }
static int cmd_remove(Token tok)
{
Token name = token;
EXPECT(TOK_STR);
char *name_str = get_str(name);
if (remove_preview(name_str) != OK) {
PARSEERROR(name, "preview '%s' not found", name_str);
return STAT_ERR;
}
return STAT_OK;
}
static int command(void) static int command(void)
{ {
Token cmd = token; Token cmd = token;
@ -188,9 +217,11 @@ static int command(void)
char *cmd_str = get_str(cmd); char *cmd_str = get_str(cmd);
if (strcmp(cmd_str, "preview") == 0) if (strcmp(cmd_str, "preview") == 0)
return new_preview(); return cmd_preview();
else if (strcmp(cmd_str, "priority") == 0) else if (strcmp(cmd_str, "priority") == 0)
return priority(cmd); return cmd_priority(cmd);
else if (strcmp(cmd_str, "remove") == 0)
return cmd_remove(cmd);
PARSEERROR(cmd, "unknown command: %s", cmd_str); PARSEERROR(cmd, "unknown command: %s", cmd_str);
return STAT_ERR; return STAT_ERR;

View File

@ -86,3 +86,9 @@ void vector_resize(Vector *vec, size_t len)
vec->len = len; vec->len = len;
} }
void vector_remove(Vector *vec, size_t i)
{
memcpy(vec->buf + i * vec->size, vec->buf + (--vec->len) * vec->size,
vec->size);
}

View File

@ -22,6 +22,7 @@
#define VECTOR_SIGN_APPEND(name, type) VECTOR_SIGN_V(name, type, append, size_t, type val) #define VECTOR_SIGN_APPEND(name, type) VECTOR_SIGN_V(name, type, append, size_t, type val)
#define VECTOR_SIGN_GET(name, type) VECTOR_SIGN_V(name, type, get, type, size_t i) #define VECTOR_SIGN_GET(name, type) VECTOR_SIGN_V(name, type, get, type, size_t i)
#define VECTOR_SIGN_RESIZE(name, type) VECTOR_SIGN_V(name, type, resize, void, size_t len) #define VECTOR_SIGN_RESIZE(name, type) VECTOR_SIGN_V(name, type, resize, void, size_t len)
#define VECTOR_SIGN_REMOVE(name, type) VECTOR_SIGN_V(name, type, remove, void, size_t i)
#define VECTOR_GEN_SOURCE_(name, type, spec) \ #define VECTOR_GEN_SOURCE_(name, type, spec) \
inline spec VECTOR_SIGN_NEW(name, type) \ inline spec VECTOR_SIGN_NEW(name, type) \
@ -47,6 +48,10 @@
inline spec VECTOR_SIGN_RESIZE(name, type) \ inline spec VECTOR_SIGN_RESIZE(name, type) \
{ \ { \
vector_resize((Vector *)vec, len); \ vector_resize((Vector *)vec, len); \
} \
inline spec VECTOR_SIGN_REMOVE(name, type) \
{ \
vector_remove((Vector *)vec, i); \
} }
#define VECTOR_GEN_SOURCE(name, type) VECTOR_GEN_SOURCE_(name, type, ) #define VECTOR_GEN_SOURCE(name, type) VECTOR_GEN_SOURCE_(name, type, )
@ -61,7 +66,8 @@
VECTOR_SIGN_APPEND_ARR(name, type); \ VECTOR_SIGN_APPEND_ARR(name, type); \
VECTOR_SIGN_APPEND(name, type); \ VECTOR_SIGN_APPEND(name, type); \
VECTOR_SIGN_GET(name, type); \ VECTOR_SIGN_GET(name, type); \
VECTOR_SIGN_RESIZE(name, type); VECTOR_SIGN_RESIZE(name, type); \
VECTOR_SIGN_REMOVE(name, type);
VECTOR_TYPE(, void); VECTOR_TYPE(, void);
@ -71,6 +77,7 @@ size_t vector_append_arr(Vector *vec, void *arr, size_t len);
size_t vector_append(Vector *vec, void *arr); size_t vector_append(Vector *vec, void *arr);
void *vector_get(Vector *vec, size_t i); void *vector_get(Vector *vec, size_t i);
void vector_resize(Vector *vec, size_t len); void vector_resize(Vector *vec, size_t len);
void vector_remove(Vector *vec, size_t i);
VECTOR_GEN_HEADER(Char, char) VECTOR_GEN_HEADER(Char, char)