refactor(core): change hole ID to -1

This commit is contained in:
Masahiko AMANO 2022-12-23 14:01:16 +03:00
parent 99d6a2a95c
commit cee515da52
3 changed files with 11 additions and 13 deletions

View File

@ -16,7 +16,7 @@ extern "C" {
// ==================== CONSTANTS ==================== // // ==================== CONSTANTS ==================== //
// ID of hole - an invalid record // ID of hole - an invalid record
#define HOLE_ID (0) #define HOLE_ID (-1)
// ==================== STRUCTS AND TYPEDEFS ==================== // // ==================== STRUCTS AND TYPEDEFS ==================== //

View File

@ -52,7 +52,7 @@ int sappyou_load(Sappyou *sappyou) {
size_t max_string_len = SIZE_MAX; size_t max_string_len = SIZE_MAX;
for (uint64_t i = 0, r = sappyou->hole_cnt; i < sappyou->size; i++) { for (uint64_t i = 0, r = sappyou->hole_cnt; i < sappyou->size; i++) {
if (fgetc(sappyou->file) != 0) { if (fgetc(sappyou->file) != 0) {
sappyou->content[i].id = i + 1; sappyou->content[i].id = i;
fread(&sappyou->content[i].created_ts, 8, 1, sappyou->file); fread(&sappyou->content[i].created_ts, 8, 1, sappyou->file);
fread(&sappyou->content[i].modified_ts, 8, 1, sappyou->file); fread(&sappyou->content[i].modified_ts, 8, 1, sappyou->file);
getdelim(&sappyou->content[i].name, &max_string_len, 0, sappyou->file); getdelim(&sappyou->content[i].name, &max_string_len, 0, sappyou->file);
@ -139,14 +139,14 @@ int tanzaku_add(Sappyou *sappyou, const char *name, const char *alias, const cha
if (sappyou->hole_cnt > 0) { if (sappyou->hole_cnt > 0) {
sappyou->hole_cnt--; sappyou->hole_cnt--;
Tanzaku **hole_ptr = sappyou->holes + sappyou->hole_cnt; Tanzaku **hole_ptr = sappyou->holes + sappyou->hole_cnt;
newbie.id = *hole_ptr - sappyou->content + 1; newbie.id = *hole_ptr - sappyou->content;
**hole_ptr = newbie; **hole_ptr = newbie;
sappyou->holes = realloc(sappyou->holes, sappyou->hole_cnt * sizeof(Tanzaku *)); sappyou->holes = realloc(sappyou->holes, sappyou->hole_cnt * sizeof(Tanzaku *));
} else { } else {
sappyou->size++;
newbie.id = sappyou->size; newbie.id = sappyou->size;
sappyou->size++;
sappyou->content = realloc(sappyou->content, sappyou->size * sizeof(Tanzaku)); sappyou->content = realloc(sappyou->content, sappyou->size * sizeof(Tanzaku));
sappyou->content[sappyou->size - 1] = newbie; sappyou->content[newbie.id] = newbie;
} }
sappyou->modified_ts = newbie.created_ts; sappyou->modified_ts = newbie.created_ts;
return 0; return 0;
@ -157,11 +157,10 @@ int tanzaku_rem_by_id(Sappyou *sappyou, uint64_t tanzaku_id) {
fprintf(stderr, "Failed to remove tanzaku: got hole ID\n"); fprintf(stderr, "Failed to remove tanzaku: got hole ID\n");
return 1; return 1;
} }
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;
} }
tanzaku_id--;
if (sappyou->content[tanzaku_id].id == HOLE_ID) { if (sappyou->content[tanzaku_id].id == HOLE_ID) {
fprintf(stderr, "Failed to remove tanzaku: target tanzaku is already removed\n"); fprintf(stderr, "Failed to remove tanzaku: target tanzaku is already removed\n");
return 1; return 1;

View File

@ -52,7 +52,7 @@ int sasahyou_load(Sasahyou *sasahyou) {
size_t max_path_len = SIZE_MAX; size_t max_path_len = SIZE_MAX;
for (uint64_t i = 0, r = sasahyou->hole_cnt; i < sasahyou->size; i++) { for (uint64_t i = 0, r = sasahyou->hole_cnt; i < sasahyou->size; i++) {
if (fgetc(sasahyou->file) != 0) { if (fgetc(sasahyou->file) != 0) {
sasahyou->content[i].id = i + 1; sasahyou->content[i].id = i;
fread(&sasahyou->content[i].created_ts, 8, 1, sasahyou->file); fread(&sasahyou->content[i].created_ts, 8, 1, sasahyou->file);
getdelim(&sasahyou->content[i].path, &max_path_len, 0, sasahyou->file); getdelim(&sasahyou->content[i].path, &max_path_len, 0, sasahyou->file);
} else { } else {
@ -122,14 +122,14 @@ int sasa_add(Sasahyou *sasahyou, const char *path) {
if (sasahyou->hole_cnt > 0) { if (sasahyou->hole_cnt > 0) {
sasahyou->hole_cnt--; sasahyou->hole_cnt--;
Sasa **hole_ptr = sasahyou->holes + sasahyou->hole_cnt; Sasa **hole_ptr = sasahyou->holes + sasahyou->hole_cnt;
newbie.id = *hole_ptr - sasahyou->content + 1; newbie.id = *hole_ptr - sasahyou->content;
**hole_ptr = newbie; **hole_ptr = newbie;
sasahyou->holes = realloc(sasahyou->holes, sasahyou->hole_cnt * sizeof(Sasa *)); sasahyou->holes = realloc(sasahyou->holes, sasahyou->hole_cnt * sizeof(Sasa *));
} else { } else {
sasahyou->size++;
newbie.id = sasahyou->size; newbie.id = sasahyou->size;
sasahyou->size++;
sasahyou->content = realloc(sasahyou->content, sasahyou->size * sizeof(Sasa)); sasahyou->content = realloc(sasahyou->content, sasahyou->size * sizeof(Sasa));
sasahyou->content[sasahyou->size - 1] = newbie; sasahyou->content[newbie.id] = newbie;
} }
sasahyou->modified_ts = newbie.created_ts; sasahyou->modified_ts = newbie.created_ts;
return 0; return 0;
@ -140,11 +140,10 @@ int sasa_rem_by_id(Sasahyou *sasahyou, uint64_t sasa_id) {
fprintf(stderr, "Failed to remove sasa: got hole ID\n"); fprintf(stderr, "Failed to remove sasa: got hole ID\n");
return 1; return 1;
} }
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;
} }
sasa_id--;
if (sasahyou->content[sasa_id].id == HOLE_ID) { if (sasahyou->content[sasa_id].id == HOLE_ID) {
fprintf(stderr, "Failed to remove sasa: target sasa is already removed\n"); fprintf(stderr, "Failed to remove sasa: target sasa is already removed\n");
return 1; return 1;