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:
Masahiko AMANO 2022-12-21 17:55:35 +03:00
parent 587415dc78
commit 6f02cdae10
3 changed files with 70 additions and 7 deletions

View File

@ -19,7 +19,7 @@ extern "C" {
typedef struct sasa { typedef struct sasa {
uint64_t id; // Sasa ID uint64_t id; // Sasa ID
uint64_t created_ts; // Sasa creation timestamp uint64_t created_ts; // Sasa creation timestamp
char *path; // Path to file char *path; // File path
} Sasa; } Sasa;
// Tanzaku (短冊) - a tag record // Tanzaku (短冊) - a tag record
@ -104,8 +104,11 @@ int sasahyou_dump(Sasahyou *sasahyou, const char *path);
// Add sasa to sasahyou // Add sasa to sasahyou
int sasa_add(Sasahyou *sasahyou, const char *path); int sasa_add(Sasahyou *sasahyou, const char *path);
// Remove sasa from sasahyou // Remove sasa from sasahyou by ID
int sasa_rem(Sasahyou *sasahyou, uint64_t sasa_id); int sasa_rem_by_id(Sasahyou *sasahyou, uint64_t sasa_id);
// Remove sasa from sasahyou by file path
int sasa_rem_by_path(Sasahyou *sasahyou, const char *path);
// ==================== SAPPYOU SECTION ==================== // // ==================== SAPPYOU SECTION ==================== //
@ -133,8 +136,14 @@ int sappyou_dump(Sappyou *sappyou, const char *path);
// Add new tanzaku to sappyou // Add new tanzaku to sappyou
int tanzaku_add(Sappyou *sappyou, const char *name, const char *alias, const char *description); int tanzaku_add(Sappyou *sappyou, const char *name, const char *alias, const char *description);
// Remove tanzaku from sappyou // Remove tanzaku from sappyou by ID
int tanzaku_rem(Sappyou *sappyou, uint64_t tanzaku_id); int tanzaku_rem_by_id(Sappyou *sappyou, uint64_t tanzaku_id);
// Remove tanzaku from sappyou by name
int tanzaku_rem_by_name(Sappyou *sappyou, const char *name);
// Remove tanzaku from sappyou by alias
int tanzaku_rem_by_alias(Sappyou *sappyou, const char *alias);
// ==================== SHOPPYOU SECTION ==================== // // ==================== SHOPPYOU SECTION ==================== //

View File

@ -160,7 +160,7 @@ int tanzaku_add(Sappyou *sappyou, const char *name, const char *alias, const cha
return 0; 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) { if (tanzaku_id > sappyou->size) {
fprintf(stderr, "Failed to remove tanzaku: target tanzaku does not exist\n"); fprintf(stderr, "Failed to remove tanzaku: target tanzaku does not exist\n");
return 1; return 1;
@ -175,3 +175,39 @@ int tanzaku_rem(Sappyou *sappyou, uint64_t tanzaku_id) {
return 0; 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;
}

View File

@ -149,7 +149,7 @@ int sasa_add(Sasahyou *sasahyou, const char *path) {
return 0; 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) { if (sasa_id > sasahyou->size) {
fprintf(stderr, "Failed to remove sasa: target sasa does not exist\n"); fprintf(stderr, "Failed to remove sasa: target sasa does not exist\n");
return 1; return 1;
@ -164,3 +164,21 @@ int sasa_rem(Sasahyou *sasahyou, uint64_t sasa_id) {
return 0; 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;
}