feat(core): add new remove functions

Now sasa can be removed not only by ID, but also by file path, tanzaku can also be removed by name and alias.
This commit is contained in:
2022-12-21 17:55:35 +03:00
parent 587415dc78
commit 6f02cdae10
3 changed files with 70 additions and 7 deletions
+37 -1
View File
@@ -160,7 +160,7 @@ int tanzaku_add(Sappyou *sappyou, const char *name, const char *alias, const cha
return 0;
}
int tanzaku_rem(Sappyou *sappyou, uint64_t tanzaku_id) {
int tanzaku_rem_by_id(Sappyou *sappyou, uint64_t tanzaku_id) {
if (tanzaku_id > sappyou->size) {
fprintf(stderr, "Failed to remove tanzaku: target tanzaku does not exist\n");
return 1;
@@ -175,3 +175,39 @@ int tanzaku_rem(Sappyou *sappyou, uint64_t tanzaku_id) {
return 0;
}
int tanzaku_rem_by_name(Sappyou *sappyou, const char *name) {
for (uint64_t i = 0; i < sappyou->size; i++) {
if (strcmp(sappyou->contents[i].name, name) == 0) {
if (sappyou->contents[i].id != 0) {
sappyou->modified_ts = time(NULL);
sappyou->contents[i].id = 0;
sappyou->removed_cnt++;
return 0;
} else {
fprintf(stderr, "Failed to remove tanzaku: target tanzaku is already removed\n");
return 1;
}
}
}
fprintf(stderr, "Failed to remove tanzaku: target tanzaku does not exist\n");
return 1;
}
int tanzaku_rem_by_alias(Sappyou *sappyou, const char *alias) {
for (uint64_t i = 0; i < sappyou->size; i++) {
if (strcmp(sappyou->contents[i].alias, alias) == 0) {
if (sappyou->contents[i].id != 0) {
sappyou->modified_ts = time(NULL);
sappyou->contents[i].id = 0;
sappyou->removed_cnt++;
return 0;
} else {
fprintf(stderr, "Failed to remove tanzaku: target tanzaku is already removed\n");
return 1;
}
}
}
fprintf(stderr, "Failed to remove tanzaku: target tanzaku does not exist\n");
return 1;
}
+19 -1
View File
@@ -149,7 +149,7 @@ int sasa_add(Sasahyou *sasahyou, const char *path) {
return 0;
}
int sasa_rem(Sasahyou *sasahyou, uint64_t sasa_id) {
int sasa_rem_by_id(Sasahyou *sasahyou, uint64_t sasa_id) {
if (sasa_id > sasahyou->size) {
fprintf(stderr, "Failed to remove sasa: target sasa does not exist\n");
return 1;
@@ -164,3 +164,21 @@ int sasa_rem(Sasahyou *sasahyou, uint64_t sasa_id) {
return 0;
}
int sasa_rem_by_path(Sasahyou *sasahyou, const char *path) {
for (uint64_t i = 0; i < sasahyou->size; i++) {
if (strcmp(sasahyou->contents[i].path, path) == 0) {
if (sasahyou->contents[i].id != 0) {
sasahyou->modified_ts = time(NULL);
sasahyou->contents[i].id = 0;
sasahyou->removed_cnt++;
return 0;
} else {
fprintf(stderr, "Failed to remove sasa: target sasa is already removed\n");
return 1;
}
}
}
fprintf(stderr, "Failed to remove sasa: target sasa does not exist\n");
return 1;
}