fix(core): a lot of bug fixes, optimization improves and some code cleanup
This commit is contained in:
parent
7d97948e74
commit
20ae688a31
@ -1,10 +1,11 @@
|
||||
#include <stdint.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "core_func.h"
|
||||
|
||||
const Tanzaku HOLE_TANZAKU = {HOLE_ID};
|
||||
const Tanzaku HOLE_TANZAKU = {HOLE_ID, 0, 0, NULL, NULL};
|
||||
|
||||
// Sappyou file signature: 七夕冊表
|
||||
const uint16_t SAPPYOU_SIG[4] = {L'七', L'夕', L'冊', L'表'};
|
||||
@ -21,80 +22,110 @@ int sappyou_init(Sappyou *sappyou) {
|
||||
}
|
||||
|
||||
int sappyou_free(Sappyou *sappyou) {
|
||||
for (uint64_t i = 0; i < sappyou->size; i++) {
|
||||
free(sappyou->database[i].name);
|
||||
free(sappyou->database[i].description);
|
||||
sappyou->created_ts = 0;
|
||||
sappyou->modified_ts = 0;
|
||||
sappyou->size = 0;
|
||||
sappyou->hole_cnt = 0;
|
||||
if (sappyou->database != NULL) {
|
||||
for (Tanzaku *current_tanzaku = sappyou->database + sappyou->size - 1;
|
||||
current_tanzaku >= sappyou->database; current_tanzaku--) {
|
||||
free(current_tanzaku->name);
|
||||
free(current_tanzaku->description);
|
||||
}
|
||||
free(sappyou->database);
|
||||
sappyou->database = NULL;
|
||||
}
|
||||
free(sappyou->database);
|
||||
free(sappyou->holes);
|
||||
sappyou->holes = NULL;
|
||||
if (sappyou->file != NULL) {
|
||||
fclose(sappyou->file);
|
||||
sappyou->file = NULL;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sappyou_load(Sappyou *sappyou) {
|
||||
if (sappyou->file == NULL ||
|
||||
(sappyou->file = freopen(NULL, "rb", sappyou->file)) == 0) {
|
||||
(sappyou->file = freopen(NULL, "rb", sappyou->file)) == NULL) {
|
||||
return 1;
|
||||
}
|
||||
Sappyou temp;
|
||||
sappyou_init(&temp);
|
||||
temp.file = sappyou->file;
|
||||
uint16_t signature[4];
|
||||
if (fread(signature, 2, 4, sappyou->file) < 4 ||
|
||||
if (fread(signature, 2, 4, temp.file) != 4 ||
|
||||
memcmp(signature, SAPPYOU_SIG, 8) != 0 ||
|
||||
fread(&sappyou->created_ts, 8, 1, sappyou->file) == 0 ||
|
||||
fread(&sappyou->modified_ts, 8, 1, sappyou->file) == 0 ||
|
||||
fread(&sappyou->size, 8, 1, sappyou->file) == 0 ||
|
||||
fread(&sappyou->hole_cnt, 8, 1, sappyou->file) == 0) {
|
||||
fread(&temp.created_ts, 8, 1, temp.file) != 1 ||
|
||||
fread(&temp.modified_ts, 8, 1, temp.file) != 1 ||
|
||||
fread(&temp.size, 8, 1, temp.file) != 1 ||
|
||||
fread(&temp.hole_cnt, 8, 1, temp.file) != 1) {
|
||||
return 1;
|
||||
}
|
||||
sappyou->database = malloc(sappyou->size * sizeof(Tanzaku));
|
||||
sappyou->holes = malloc(sappyou->hole_cnt * sizeof(Tanzaku *));
|
||||
temp.database = calloc(temp.size, sizeof(Tanzaku));
|
||||
temp.holes = calloc(temp.hole_cnt, sizeof(Tanzaku *));
|
||||
size_t max_string_len = SIZE_MAX;
|
||||
for (uint64_t i = 0, r = sappyou->hole_cnt; i < sappyou->size; i++) {
|
||||
if (fgetc(sappyou->file) != 0) {
|
||||
sappyou->database[i].id = i;
|
||||
if (fread(&sappyou->database[i].created_ts, 8, 1, sappyou->file) == 0 ||
|
||||
fread(&sappyou->database[i].modified_ts, 8, 1, sappyou->file) == 0 ||
|
||||
getdelim(&sappyou->database[i].name, &max_string_len, 0, sappyou->file) == -1 ||
|
||||
getdelim(&sappyou->database[i].description, &max_string_len, 0, sappyou->file) == -1) {
|
||||
Tanzaku *current_tanzaku = temp.database;
|
||||
for (uint64_t i = 0, r = temp.hole_cnt; i < temp.size; i++) {
|
||||
if (fgetc(temp.file) != 0) {
|
||||
current_tanzaku->id = i;
|
||||
if (fread(¤t_tanzaku->created_ts, 8, 1, temp.file) != 1 ||
|
||||
fread(¤t_tanzaku->modified_ts, 8, 1, temp.file) != 1 ||
|
||||
getdelim(¤t_tanzaku->name, &max_string_len, 0, temp.file) == -1 ||
|
||||
getdelim(¤t_tanzaku->description, &max_string_len, 0, temp.file) == -1) {
|
||||
temp.file = NULL;
|
||||
sappyou_free(&temp);
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
sappyou->database[i].id = HOLE_ID;
|
||||
current_tanzaku->id = HOLE_ID;
|
||||
if (r == 0) {
|
||||
temp.file = NULL;
|
||||
sappyou_free(&temp);
|
||||
return 1;
|
||||
}
|
||||
r--;
|
||||
sappyou->holes[r] = sappyou->database + i;
|
||||
temp.holes[r] = current_tanzaku;
|
||||
}
|
||||
current_tanzaku++;
|
||||
}
|
||||
return fflush(sappyou->file);
|
||||
if (fflush(temp.file) == 0) {
|
||||
sappyou->file = NULL;
|
||||
sappyou_free(sappyou);
|
||||
*sappyou = temp;
|
||||
return 0;
|
||||
}
|
||||
temp.file = NULL;
|
||||
sappyou_free(&temp);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sappyou_save(Sappyou *sappyou) {
|
||||
if (sappyou->file == NULL ||
|
||||
(sappyou->file = freopen(NULL, "wb", sappyou->file)) == NULL ||
|
||||
fwrite(SAPPYOU_SIG, 2, 4, sappyou->file) < 4 ||
|
||||
fwrite(&sappyou->created_ts, 8, 1, sappyou->file) == 0 ||
|
||||
fwrite(&sappyou->modified_ts, 8, 1, sappyou->file) == 0 ||
|
||||
fwrite(&sappyou->size, 8, 1, sappyou->file) == 0 ||
|
||||
fwrite(&sappyou->hole_cnt, 8, 1, sappyou->file) == 0 ||
|
||||
fwrite(SAPPYOU_SIG, 2, 4, sappyou->file) != 4 ||
|
||||
fwrite(&sappyou->created_ts, 8, 1, sappyou->file) != 1 ||
|
||||
fwrite(&sappyou->modified_ts, 8, 1, sappyou->file) != 1 ||
|
||||
fwrite(&sappyou->size, 8, 1, sappyou->file) != 1 ||
|
||||
fwrite(&sappyou->hole_cnt, 8, 1, sappyou->file) != 1 ||
|
||||
fflush(sappyou->file) != 0) {
|
||||
return 1;
|
||||
}
|
||||
Tanzaku *current_tanzaku = sappyou->database;
|
||||
for (uint64_t i = 0; i < sappyou->size; i++) {
|
||||
if (sappyou->database[i].id != HOLE_ID) {
|
||||
if (fputc(-1, sappyou->file) == EOF ||
|
||||
fwrite(&sappyou->database[i].created_ts, 8, 1, sappyou->file) == 0 ||
|
||||
fwrite(&sappyou->database[i].modified_ts, 8, 1, sappyou->file) == 0 ||
|
||||
fputs(sappyou->database[i].name, sappyou->file) == EOF ||
|
||||
if (current_tanzaku->id != HOLE_ID) {
|
||||
if (fputc(0xff, sappyou->file) == EOF ||
|
||||
fwrite(¤t_tanzaku->created_ts, 8, 1, sappyou->file) != 1 ||
|
||||
fwrite(¤t_tanzaku->modified_ts, 8, 1, sappyou->file) != 1 ||
|
||||
fputs(current_tanzaku->name, sappyou->file) == EOF ||
|
||||
fputc(0, sappyou->file) == EOF ||
|
||||
fputs(sappyou->database[i].description, sappyou->file) == EOF ||
|
||||
fputs(current_tanzaku->description, sappyou->file) == EOF ||
|
||||
fputc(0, sappyou->file) == EOF) {
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
if (fputc(0, sappyou->file) == EOF) {
|
||||
return 1;
|
||||
}
|
||||
} else if (fputc(0, sappyou->file) == EOF) {
|
||||
return 1;
|
||||
}
|
||||
current_tanzaku++;
|
||||
}
|
||||
return fflush(sappyou->file);
|
||||
}
|
||||
@ -103,8 +134,8 @@ int sappyou_open(Sappyou *sappyou, const char *path) {
|
||||
if (path == NULL) {
|
||||
return 1;
|
||||
}
|
||||
sappyou->file = fopen(path, "rb");
|
||||
if (sappyou->file == NULL) {
|
||||
if (sappyou->file == NULL && (sappyou->file = fopen(path, "rb")) == NULL ||
|
||||
sappyou->file != NULL && (sappyou->file = freopen(path, "rb", sappyou->file)) == NULL) {
|
||||
return 1;
|
||||
}
|
||||
return sappyou_load(sappyou);
|
||||
@ -114,8 +145,8 @@ int sappyou_dump(Sappyou *sappyou, const char *path) {
|
||||
if (path == NULL) {
|
||||
return 1;
|
||||
}
|
||||
sappyou->file = fopen(path, "wb");
|
||||
if (sappyou->file == NULL) {
|
||||
if (sappyou->file == NULL && (sappyou->file = fopen(path, "wb")) == NULL ||
|
||||
sappyou->file != NULL && (sappyou->file = freopen(path, "wb", sappyou->file)) == NULL) {
|
||||
return 1;
|
||||
}
|
||||
return sappyou_save(sappyou);
|
||||
@ -128,22 +159,20 @@ int tanzaku_add(Sappyou *sappyou, const char *name, const char *description) {
|
||||
Tanzaku newbie;
|
||||
newbie.created_ts = time(NULL);
|
||||
newbie.modified_ts = newbie.created_ts;
|
||||
size_t name_size = strlen(name),
|
||||
description_size = strlen(description);
|
||||
newbie.name = malloc(name_size + 1);
|
||||
newbie.name = malloc(strlen(name) + 1);
|
||||
strcpy(newbie.name, name);
|
||||
newbie.description = malloc(description_size + 1);
|
||||
newbie.description = malloc(strlen(description) + 1);
|
||||
strcpy(newbie.description, description);
|
||||
if (sappyou->hole_cnt > 0) {
|
||||
sappyou->hole_cnt--;
|
||||
Tanzaku **hole_ptr = sappyou->holes + sappyou->hole_cnt;
|
||||
newbie.id = *hole_ptr - sappyou->database;
|
||||
**hole_ptr = newbie;
|
||||
sappyou->holes = realloc(sappyou->holes, sappyou->hole_cnt * sizeof(Tanzaku *));
|
||||
sappyou->holes = reallocarray(sappyou->holes, sappyou->hole_cnt, sizeof(Tanzaku *));
|
||||
} else {
|
||||
newbie.id = sappyou->size;
|
||||
sappyou->size++;
|
||||
sappyou->database = realloc(sappyou->database, sappyou->size * sizeof(Tanzaku));
|
||||
sappyou->database = reallocarray(sappyou->database, sappyou->size, sizeof(Tanzaku));
|
||||
sappyou->database[newbie.id] = newbie;
|
||||
}
|
||||
sappyou->modified_ts = newbie.created_ts;
|
||||
@ -154,38 +183,42 @@ int tanzaku_rem(Sappyou *sappyou, uint64_t tanzaku_id) {
|
||||
if (tanzaku_id == HOLE_ID || tanzaku_id >= sappyou->size) {
|
||||
return 1;
|
||||
}
|
||||
if (sappyou->database[tanzaku_id].id == HOLE_ID) {
|
||||
return 0;
|
||||
Tanzaku *current_tanzaku = sappyou->database + tanzaku_id;
|
||||
if (current_tanzaku->id == HOLE_ID) {
|
||||
return 1;
|
||||
}
|
||||
sappyou->database[tanzaku_id].id = HOLE_ID;
|
||||
current_tanzaku->id = HOLE_ID;
|
||||
free(current_tanzaku->name);
|
||||
free(current_tanzaku->description);
|
||||
if (tanzaku_id == sappyou->size - 1) {
|
||||
sappyou->size--;
|
||||
sappyou->database = realloc(sappyou->database, sappyou->size * sizeof(Tanzaku));
|
||||
sappyou->database = reallocarray(sappyou->database, sappyou->size, sizeof(Tanzaku));
|
||||
} else {
|
||||
sappyou->hole_cnt++;
|
||||
sappyou->holes = realloc(sappyou->holes, sappyou->hole_cnt);
|
||||
sappyou->holes[sappyou->hole_cnt - 1] = sappyou->database + tanzaku_id;
|
||||
sappyou->holes = reallocarray(sappyou->holes, sappyou->hole_cnt, sizeof(Tanzaku *));
|
||||
sappyou->holes[sappyou->hole_cnt - 1] = current_tanzaku;
|
||||
}
|
||||
sappyou->modified_ts = time(NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int tanzaku_upd(Sappyou *sappyou, uint64_t tanzaku_id, const char *name, const char *description) {
|
||||
if (tanzaku_id == HOLE_ID || tanzaku_id >= sappyou->size) {
|
||||
if (tanzaku_id == HOLE_ID || tanzaku_id >= sappyou->size || name == NULL && description == NULL) {
|
||||
return 1;
|
||||
}
|
||||
Tanzaku *current_tanzaku = sappyou->database + tanzaku_id;
|
||||
if (current_tanzaku->id == HOLE_ID) {
|
||||
return 1;
|
||||
}
|
||||
_Bool changed = 0;
|
||||
if (name != NULL) {
|
||||
strcpy(sappyou->database[tanzaku_id].name, name);
|
||||
changed = 1;
|
||||
current_tanzaku->name = realloc(current_tanzaku->name, strlen(name) + 1);
|
||||
strcpy(current_tanzaku->name, name);
|
||||
}
|
||||
if (description != NULL) {
|
||||
strcpy(sappyou->database[tanzaku_id].description, description);
|
||||
changed = 1;
|
||||
}
|
||||
if (changed) {
|
||||
sappyou->modified_ts = time(NULL);
|
||||
sappyou->database[tanzaku_id].modified_ts = sappyou->modified_ts;
|
||||
current_tanzaku->description = realloc(current_tanzaku->description, strlen(description) + 1);
|
||||
strcpy(current_tanzaku->description, description);
|
||||
}
|
||||
sappyou->modified_ts = time(NULL);
|
||||
current_tanzaku->modified_ts = sappyou->modified_ts;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
#include <stdint.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "core_func.h"
|
||||
|
||||
const Sasa HOLE_SASA = {HOLE_ID};
|
||||
const Sasa HOLE_SASA = {HOLE_ID, 0, NULL};
|
||||
|
||||
// Sasahyou file signature: 七夕笹表
|
||||
const uint16_t SASAHYOU_SIG[4] = {L'七', L'夕', L'笹', L'表'};
|
||||
@ -22,77 +22,104 @@ int sasahyou_init(Sasahyou *sasahyou) {
|
||||
}
|
||||
|
||||
int sasahyou_free(Sasahyou *sasahyou) {
|
||||
for (uint64_t i = 0; i < sasahyou->size; i++) {
|
||||
free(sasahyou->database[i].path);
|
||||
sasahyou->created_ts = 0;
|
||||
sasahyou->modified_ts = 0;
|
||||
sasahyou->size = 0;
|
||||
sasahyou->hole_cnt = 0;
|
||||
if (sasahyou->database != NULL) {
|
||||
for (Sasa *current_sasa = sasahyou->database + sasahyou->size - 1;
|
||||
current_sasa >= sasahyou->database; current_sasa--) {
|
||||
free(current_sasa->path);
|
||||
}
|
||||
free(sasahyou->database);
|
||||
sasahyou->database = NULL;
|
||||
}
|
||||
free(sasahyou->database);
|
||||
free(sasahyou->holes);
|
||||
sasahyou->holes = NULL;
|
||||
if (sasahyou->file != NULL) {
|
||||
fclose(sasahyou->file);
|
||||
sasahyou->file = NULL;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sasahyou_load(Sasahyou *sasahyou) {
|
||||
if (sasahyou->file == NULL ||
|
||||
(sasahyou->file = freopen(NULL, "rb", sasahyou->file)) == 0) {
|
||||
(sasahyou->file = freopen(NULL, "rb", sasahyou->file)) == NULL) {
|
||||
return 1;
|
||||
}
|
||||
Sasahyou temp;
|
||||
sasahyou_init(&temp);
|
||||
temp.file = sasahyou->file;
|
||||
uint16_t signature[4];
|
||||
if (fread(signature, 2, 4, sasahyou->file) < 4 ||
|
||||
if (fread(signature, 2, 4, temp.file) != 4 ||
|
||||
memcmp(signature, SASAHYOU_SIG, 8) != 0 ||
|
||||
fread(&sasahyou->created_ts, 8, 1, sasahyou->file) == 0 ||
|
||||
fread(&sasahyou->modified_ts, 8, 1, sasahyou->file) == 0 ||
|
||||
fread(&sasahyou->size, 8, 1, sasahyou->file) == 0 ||
|
||||
fread(&sasahyou->hole_cnt, 8, 1, sasahyou->file) == 0) {
|
||||
fread(&temp.created_ts, 8, 1, temp.file) != 1 ||
|
||||
fread(&temp.modified_ts, 8, 1, temp.file) != 1 ||
|
||||
fread(&temp.size, 8, 1, temp.file) != 1 ||
|
||||
fread(&temp.hole_cnt, 8, 1, temp.file) != 1) {
|
||||
return 1;
|
||||
}
|
||||
sasahyou->database = malloc(sasahyou->size * sizeof(Sasa));
|
||||
sasahyou->holes = malloc(sasahyou->hole_cnt * sizeof(Sasa *));
|
||||
temp.database = calloc(temp.size, sizeof(Sasa));
|
||||
temp.holes = calloc(temp.hole_cnt, sizeof(Sasa *));
|
||||
size_t max_path_len = SIZE_MAX;
|
||||
for (uint64_t i = 0, r = sasahyou->hole_cnt; i < sasahyou->size; i++) {
|
||||
if (fgetc(sasahyou->file) != 0) {
|
||||
sasahyou->database[i].id = i;
|
||||
if (fread(&sasahyou->database[i].created_ts, 8, 1, sasahyou->file) == 0 ||
|
||||
getdelim(&sasahyou->database[i].path, &max_path_len, 0, sasahyou->file) == -1) {
|
||||
Sasa *current_sasa = temp.database;
|
||||
for (uint64_t i = 0, r = temp.hole_cnt; i < temp.size; i++) {
|
||||
if (fgetc(temp.file) != 0) {
|
||||
current_sasa->id = i;
|
||||
if (fread(¤t_sasa->created_ts, 8, 1, temp.file) != 1 ||
|
||||
getdelim(¤t_sasa->path, &max_path_len, 0, temp.file) == -1) {
|
||||
temp.file = NULL;
|
||||
sasahyou_free(&temp);
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
sasahyou->database[i].id = HOLE_ID;
|
||||
current_sasa->id = HOLE_ID;
|
||||
if (r == 0) {
|
||||
temp.file = NULL;
|
||||
sasahyou_free(&temp);
|
||||
return 1;
|
||||
}
|
||||
r--;
|
||||
sasahyou->holes[r] = sasahyou->database + i;
|
||||
temp.holes[r] = current_sasa;
|
||||
}
|
||||
current_sasa++;
|
||||
}
|
||||
return fflush(sasahyou->file);
|
||||
if (fflush(temp.file) == 0) {
|
||||
sasahyou->file = NULL;
|
||||
sasahyou_free(sasahyou);
|
||||
*sasahyou = temp;
|
||||
return 0;
|
||||
}
|
||||
temp.file = NULL;
|
||||
sasahyou_free(&temp);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int sasahyou_save(Sasahyou *sasahyou) {
|
||||
if (sasahyou->file == NULL ||
|
||||
(sasahyou->file = freopen(NULL, "wb", sasahyou->file)) == NULL ||
|
||||
fwrite(SASAHYOU_SIG, 2, 4, sasahyou->file) < 4 ||
|
||||
fwrite(&sasahyou->created_ts, 8, 1, sasahyou->file) == 0 ||
|
||||
fwrite(&sasahyou->modified_ts, 8, 1, sasahyou->file) == 0 ||
|
||||
fwrite(&sasahyou->size, 8, 1, sasahyou->file) == 0 ||
|
||||
fwrite(&sasahyou->hole_cnt, 8, 1, sasahyou->file) == 0 ||
|
||||
fwrite(SASAHYOU_SIG, 2, 4, sasahyou->file) != 4 ||
|
||||
fwrite(&sasahyou->created_ts, 8, 1, sasahyou->file) != 1 ||
|
||||
fwrite(&sasahyou->modified_ts, 8, 1, sasahyou->file) != 1 ||
|
||||
fwrite(&sasahyou->size, 8, 1, sasahyou->file) != 1 ||
|
||||
fwrite(&sasahyou->hole_cnt, 8, 1, sasahyou->file) != 1 ||
|
||||
fflush(sasahyou->file) != 0) {
|
||||
return 1;
|
||||
}
|
||||
Sasa *current_sasa = sasahyou->database;
|
||||
for (uint64_t i = 0; i < sasahyou->size; i++) {
|
||||
if (feof(sasahyou->file) != 0 || ferror(sasahyou->file) != 0) {
|
||||
return 1;
|
||||
}
|
||||
if (sasahyou->database[i].id != HOLE_ID) {
|
||||
if (fputc(-1, sasahyou->file) == EOF ||
|
||||
fwrite(&sasahyou->database[i].created_ts, 8, 1, sasahyou->file) == 0 ||
|
||||
fputs(sasahyou->database[i].path, sasahyou->file) == EOF ||
|
||||
if (current_sasa->id != HOLE_ID) {
|
||||
if (fputc(0xff, sasahyou->file) == EOF ||
|
||||
fwrite(¤t_sasa->created_ts, 8, 1, sasahyou->file) != 1 ||
|
||||
fputs(current_sasa->path, sasahyou->file) == EOF ||
|
||||
fputc(0, sasahyou->file) == EOF) {
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
if (fputc(0, sasahyou->file) == EOF) {
|
||||
return 1;
|
||||
}
|
||||
} else if (fputc(0, sasahyou->file) == EOF) {
|
||||
return 1;
|
||||
}
|
||||
current_sasa++;
|
||||
}
|
||||
return fflush(sasahyou->file);
|
||||
}
|
||||
@ -101,8 +128,8 @@ int sasahyou_open(Sasahyou *sasahyou, const char *path) {
|
||||
if (path == NULL) {
|
||||
return 1;
|
||||
}
|
||||
sasahyou->file = fopen(path, "rb");
|
||||
if (sasahyou->file == NULL) {
|
||||
if (sasahyou->file == NULL && (sasahyou->file = fopen(path, "rb")) == NULL ||
|
||||
sasahyou->file != NULL && (sasahyou->file = freopen(path, "rb", sasahyou->file)) == NULL) {
|
||||
return 1;
|
||||
}
|
||||
return sasahyou_load(sasahyou);
|
||||
@ -112,8 +139,8 @@ int sasahyou_dump(Sasahyou *sasahyou, const char *path) {
|
||||
if (path == NULL) {
|
||||
return 1;
|
||||
}
|
||||
sasahyou->file = fopen(path, "wb");
|
||||
if (sasahyou->file == NULL) {
|
||||
if (sasahyou->file == NULL && (sasahyou->file = fopen(path, "wb")) == NULL ||
|
||||
sasahyou->file != NULL && (sasahyou->file = freopen(path, "wb", sasahyou->file)) == NULL) {
|
||||
return 1;
|
||||
}
|
||||
return sasahyou_save(sasahyou);
|
||||
@ -125,19 +152,18 @@ int sasa_add(Sasahyou *sasahyou, const char *path) {
|
||||
}
|
||||
Sasa newbie;
|
||||
newbie.created_ts = time(NULL);
|
||||
size_t path_size = strlen(path);
|
||||
newbie.path = malloc(path_size + 1);
|
||||
newbie.path = malloc(strlen(path) + 1);
|
||||
strcpy(newbie.path, path);
|
||||
if (sasahyou->hole_cnt > 0) {
|
||||
sasahyou->hole_cnt--;
|
||||
Sasa **hole_ptr = sasahyou->holes + sasahyou->hole_cnt;
|
||||
newbie.id = *hole_ptr - sasahyou->database;
|
||||
**hole_ptr = newbie;
|
||||
sasahyou->holes = realloc(sasahyou->holes, sasahyou->hole_cnt * sizeof(Sasa *));
|
||||
sasahyou->holes = reallocarray(sasahyou->holes, sasahyou->hole_cnt, sizeof(Sasa *));
|
||||
} else {
|
||||
newbie.id = sasahyou->size;
|
||||
sasahyou->size++;
|
||||
sasahyou->database = realloc(sasahyou->database, sasahyou->size * sizeof(Sasa));
|
||||
sasahyou->database = reallocarray(sasahyou->database, sasahyou->size, sizeof(Sasa));
|
||||
sasahyou->database[newbie.id] = newbie;
|
||||
}
|
||||
sasahyou->modified_ts = newbie.created_ts;
|
||||
@ -148,29 +174,35 @@ int sasa_rem(Sasahyou *sasahyou, uint64_t sasa_id) {
|
||||
if (sasa_id == HOLE_ID || sasa_id >= sasahyou->size) {
|
||||
return 1;
|
||||
}
|
||||
if (sasahyou->database[sasa_id].id == HOLE_ID) {
|
||||
return 0;
|
||||
Sasa *current_sasa = sasahyou->database + sasa_id;
|
||||
if (current_sasa->id == HOLE_ID) {
|
||||
return 1;
|
||||
}
|
||||
sasahyou->database[sasa_id].id = HOLE_ID;
|
||||
current_sasa->id = HOLE_ID;
|
||||
free(current_sasa->path);
|
||||
current_sasa->path = NULL;
|
||||
if (sasa_id == sasahyou->size - 1) {
|
||||
sasahyou->size--;
|
||||
sasahyou->database = realloc(sasahyou->database, sasahyou->size * sizeof(Sasa));
|
||||
sasahyou->database = reallocarray(sasahyou->database, sasahyou->size, sizeof(Sasa));
|
||||
} else {
|
||||
sasahyou->hole_cnt++;
|
||||
sasahyou->holes = realloc(sasahyou->holes, sasahyou->hole_cnt * sizeof(Sasa *));
|
||||
sasahyou->holes[sasahyou->hole_cnt - 1] = sasahyou->database + sasa_id;
|
||||
sasahyou->holes = reallocarray(sasahyou->holes, sasahyou->hole_cnt, sizeof(Sasa *));
|
||||
sasahyou->holes[sasahyou->hole_cnt - 1] = current_sasa;
|
||||
}
|
||||
sasahyou->modified_ts = time(NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int sasa_upd(Sasahyou *sasahyou, uint64_t sasa_id, const char *path) {
|
||||
if (sasa_id == HOLE_ID || sasa_id >= sasahyou->size) {
|
||||
if (sasa_id == HOLE_ID || sasa_id >= sasahyou->size || path == NULL) {
|
||||
return 1;
|
||||
}
|
||||
if (path != NULL) {
|
||||
strcpy(sasahyou->database[sasa_id].path, path);
|
||||
sasahyou->modified_ts = time(NULL);
|
||||
Sasa *current_sasa = sasahyou->database + sasa_id;
|
||||
if (current_sasa->id == HOLE_ID) {
|
||||
return 1;
|
||||
}
|
||||
current_sasa->path = realloc(current_sasa->path, strlen(path) + 1);
|
||||
strcpy(current_sasa->path, path);
|
||||
sasahyou->modified_ts = time(NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -1,10 +1,11 @@
|
||||
#include <stdint.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "core_func.h"
|
||||
|
||||
const Kazari HOLE_KAZARI = {HOLE_ID};
|
||||
const Kazari HOLE_KAZARI = {HOLE_ID, HOLE_ID, 0};
|
||||
|
||||
// Shoppyou file signature: 七夕飾表
|
||||
static const uint16_t SHOPPYOU_SIG[4] = {L'七', L'夕', L'飾', L'表'};
|
||||
@ -21,61 +22,83 @@ int shoppyou_init(Shoppyou *shoppyou) {
|
||||
}
|
||||
|
||||
int shoppyou_free(Shoppyou *shoppyou) {
|
||||
shoppyou->created_ts = 0;
|
||||
shoppyou->modified_ts = 0;
|
||||
shoppyou->size = 0;
|
||||
shoppyou->hole_cnt = 0;
|
||||
free(shoppyou->database);
|
||||
shoppyou->database = NULL;
|
||||
free(shoppyou->holes);
|
||||
shoppyou->holes = NULL;
|
||||
if (shoppyou->file != NULL) {
|
||||
fclose(shoppyou->file);
|
||||
shoppyou->file = NULL;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int shoppyou_load(Shoppyou *shoppyou) {
|
||||
if (shoppyou->file == NULL ||
|
||||
(shoppyou->file = freopen(NULL, "rb", shoppyou->file)) == 0) {
|
||||
(shoppyou->file = freopen(NULL, "rb", shoppyou->file)) == NULL) {
|
||||
return 1;
|
||||
}
|
||||
Shoppyou temp;
|
||||
shoppyou_init(&temp);
|
||||
temp.file = shoppyou->file;
|
||||
uint16_t signature[4];
|
||||
if (fread(signature, 2, 4, shoppyou->file) < 4 ||
|
||||
if (fread(signature, 2, 4, temp.file) != 4 ||
|
||||
memcmp(signature, SHOPPYOU_SIG, 8) != 0 ||
|
||||
fread(&shoppyou->created_ts, 8, 1, shoppyou->file) == 0 ||
|
||||
fread(&shoppyou->modified_ts, 8, 1, shoppyou->file) == 0 ||
|
||||
fread(&shoppyou->size, 8, 1, shoppyou->file) == 0) {
|
||||
fread(&temp.created_ts, 8, 1, temp.file) != 1 ||
|
||||
fread(&temp.modified_ts, 8, 1, temp.file) != 1 ||
|
||||
fread(&temp.size, 8, 1, temp.file) != 1) {
|
||||
return 1;
|
||||
}
|
||||
shoppyou->hole_cnt = 0;
|
||||
free(shoppyou->holes);
|
||||
shoppyou->database = malloc(shoppyou->size * sizeof(Kazari));
|
||||
for (uint64_t i = 0; i < shoppyou->size; i++) {
|
||||
if (fread(&shoppyou->database[i].created_ts, 8, 1, shoppyou->file) == 0 ||
|
||||
fread(&shoppyou->database[i].sasa_id, 8, 1, shoppyou->file) == 0 ||
|
||||
fread(&shoppyou->database[i].tanzaku_id, 8, 1, shoppyou->file) == 0) {
|
||||
temp.database = calloc(temp.size, sizeof(Kazari));
|
||||
Kazari *current_kazari = temp.database;
|
||||
for (uint64_t i = 0; i < temp.size; i++) {
|
||||
if (fread(¤t_kazari->created_ts, 8, 1, temp.file) != 1 ||
|
||||
fread(¤t_kazari->sasa_id, 8, 1, temp.file) != 1 ||
|
||||
fread(¤t_kazari->tanzaku_id, 8, 1, temp.file) != 1) {
|
||||
temp.file = NULL;
|
||||
shoppyou_free(&temp);
|
||||
return 1;
|
||||
}
|
||||
current_kazari++;
|
||||
}
|
||||
return fflush(shoppyou->file);
|
||||
if (fflush(temp.file) == 0) {
|
||||
shoppyou->file = NULL;
|
||||
shoppyou_free(shoppyou);
|
||||
*shoppyou = temp;
|
||||
return 0;
|
||||
}
|
||||
temp.file = NULL;
|
||||
shoppyou_free(&temp);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int shoppyou_save(Shoppyou *shoppyou) {
|
||||
if (shoppyou->file == NULL ||
|
||||
(shoppyou->file = freopen(NULL, "wb", shoppyou->file)) == NULL ||
|
||||
fwrite(SHOPPYOU_SIG, 2, 4, shoppyou->file) < 4 ||
|
||||
fwrite(&shoppyou->created_ts, 8, 1, shoppyou->file) == 0 ||
|
||||
fwrite(&shoppyou->modified_ts, 8, 1, shoppyou->file) == 0) {
|
||||
fwrite(SHOPPYOU_SIG, 2, 4, shoppyou->file) != 4 ||
|
||||
fwrite(&shoppyou->created_ts, 8, 1, shoppyou->file) != 1 ||
|
||||
fwrite(&shoppyou->modified_ts, 8, 1, shoppyou->file) != 1) {
|
||||
return 1;
|
||||
}
|
||||
uint64_t size = shoppyou->size - shoppyou->hole_cnt;
|
||||
if (fwrite(&size, 8, 1, shoppyou->file) == 0 ||
|
||||
if (fwrite(&size, 8, 1, shoppyou->file) != 1 ||
|
||||
fflush(shoppyou->file) != 0) {
|
||||
return 1;
|
||||
}
|
||||
Kazari *current_kazari = shoppyou->database;
|
||||
for (uint64_t i = 0; i < shoppyou->size; i++) {
|
||||
if (shoppyou->database[i].sasa_id != HOLE_ID && shoppyou->database[i].tanzaku_id != HOLE_ID) {
|
||||
if (fwrite(&shoppyou->database[i].created_ts, 8, 1, shoppyou->file) == 0 ||
|
||||
fwrite(&shoppyou->database[i].sasa_id, 8, 1, shoppyou->file) == 0 ||
|
||||
fwrite(&shoppyou->database[i].tanzaku_id, 8, 1, shoppyou->file) == 0) {
|
||||
if (fwrite(¤t_kazari->created_ts, 8, 1, shoppyou->file) != 1 ||
|
||||
fwrite(¤t_kazari->sasa_id, 8, 1, shoppyou->file) != 1 ||
|
||||
fwrite(¤t_kazari->tanzaku_id, 8, 1, shoppyou->file) != 1) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
current_kazari++;
|
||||
}
|
||||
return fflush(shoppyou->file);
|
||||
}
|
||||
@ -84,11 +107,10 @@ int shoppyou_open(Shoppyou *shoppyou, const char *path) {
|
||||
if (path == NULL) {
|
||||
return 1;
|
||||
}
|
||||
shoppyou->file = fopen(path, "rb");
|
||||
if (shoppyou->file == NULL) {
|
||||
if (shoppyou->file == NULL && (shoppyou->file = fopen(path, "rb")) == NULL ||
|
||||
shoppyou->file != NULL && (shoppyou->file = freopen(path, "rb", shoppyou->file)) == NULL) {
|
||||
return 1;
|
||||
}
|
||||
shoppyou->holes = NULL;
|
||||
return shoppyou_load(shoppyou);
|
||||
}
|
||||
|
||||
@ -96,8 +118,8 @@ int shoppyou_dump(Shoppyou *shoppyou, const char *path) {
|
||||
if (path == NULL) {
|
||||
return 1;
|
||||
}
|
||||
shoppyou->file = fopen(path, "wb");
|
||||
if (shoppyou->file == NULL) {
|
||||
if (shoppyou->file == NULL && (shoppyou->file = fopen(path, "wb")) == NULL ||
|
||||
shoppyou->file != NULL && (shoppyou->file = freopen(path, "wb", shoppyou->file)) == NULL) {
|
||||
return 1;
|
||||
}
|
||||
return shoppyou_save(shoppyou);
|
||||
@ -114,10 +136,10 @@ int kazari_add(Shoppyou *shoppyou, uint64_t sasa_id, uint64_t tanzaku_id) {
|
||||
if (shoppyou->hole_cnt > 0) {
|
||||
shoppyou->hole_cnt--;
|
||||
**(shoppyou->holes + shoppyou->hole_cnt) = newbie;
|
||||
shoppyou->holes = realloc(shoppyou->holes, shoppyou->hole_cnt * sizeof(Kazari *));
|
||||
shoppyou->holes = reallocarray(shoppyou->holes, shoppyou->hole_cnt, sizeof(Kazari *));
|
||||
} else {
|
||||
shoppyou->size++;
|
||||
shoppyou->database = realloc(shoppyou->database, shoppyou->size * sizeof(Kazari));
|
||||
shoppyou->database = reallocarray(shoppyou->database, shoppyou->size, sizeof(Kazari));
|
||||
shoppyou->database[shoppyou->size - 1] = newbie;
|
||||
}
|
||||
shoppyou->modified_ts = newbie.created_ts;
|
||||
@ -128,55 +150,66 @@ int kazari_rem(Shoppyou *shoppyou, uint64_t sasa_id, uint64_t tanzaku_id) {
|
||||
if (sasa_id == HOLE_ID || tanzaku_id == HOLE_ID) {
|
||||
return 1;
|
||||
}
|
||||
Kazari *current_kazari = shoppyou->database;
|
||||
for (uint64_t i = 0; i < shoppyou->size; i++) {
|
||||
if (shoppyou->database[i].sasa_id == sasa_id && shoppyou->database[i].tanzaku_id == tanzaku_id) {
|
||||
shoppyou->database[i].sasa_id = HOLE_ID;
|
||||
if (current_kazari->sasa_id == sasa_id && current_kazari->tanzaku_id == tanzaku_id) {
|
||||
current_kazari->sasa_id = HOLE_ID;
|
||||
current_kazari->tanzaku_id = HOLE_ID;
|
||||
shoppyou->hole_cnt++;
|
||||
shoppyou->holes = realloc(shoppyou->holes, shoppyou->hole_cnt * sizeof(Kazari *));
|
||||
shoppyou->holes[shoppyou->hole_cnt - 1] = shoppyou->database + i;
|
||||
shoppyou->holes = reallocarray(shoppyou->holes, shoppyou->hole_cnt, sizeof(Kazari *));
|
||||
shoppyou->holes[shoppyou->hole_cnt - 1] = current_kazari;
|
||||
shoppyou->modified_ts = time(NULL);
|
||||
return 0;
|
||||
}
|
||||
current_kazari++;
|
||||
}
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int kazari_rem_by_sasa(Shoppyou *shoppyou, uint64_t sasa_id) {
|
||||
if (sasa_id == HOLE_ID) {
|
||||
return 1;
|
||||
}
|
||||
Kazari *current_kazari = shoppyou->database;
|
||||
_Bool changed = 0;
|
||||
for (uint64_t i = 0; i < shoppyou->size; i++) {
|
||||
if (shoppyou->database[i].sasa_id == sasa_id) {
|
||||
shoppyou->database[i].sasa_id = HOLE_ID;
|
||||
if (current_kazari->sasa_id == sasa_id) {
|
||||
current_kazari->sasa_id = HOLE_ID;
|
||||
current_kazari->tanzaku_id = HOLE_ID;
|
||||
shoppyou->hole_cnt++;
|
||||
shoppyou->holes = realloc(shoppyou->holes, shoppyou->hole_cnt * sizeof(Kazari *));
|
||||
shoppyou->holes[shoppyou->hole_cnt - 1] = shoppyou->database + i;
|
||||
shoppyou->holes = reallocarray(shoppyou->holes, shoppyou->hole_cnt, sizeof(Kazari *));
|
||||
shoppyou->holes[shoppyou->hole_cnt - 1] = current_kazari;
|
||||
changed = 1;
|
||||
}
|
||||
current_kazari++;
|
||||
}
|
||||
if (changed) {
|
||||
shoppyou->modified_ts = time(NULL);
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int kazari_rem_by_tanzaku(Shoppyou *shoppyou, uint64_t tanzaku_id) {
|
||||
if (tanzaku_id == HOLE_ID) {
|
||||
return 1;
|
||||
}
|
||||
Kazari *current_kazari = shoppyou->database;
|
||||
_Bool changed = 0;
|
||||
for (uint64_t i = 0; i < shoppyou->size; i++) {
|
||||
if (shoppyou->database[i].tanzaku_id == tanzaku_id) {
|
||||
shoppyou->database[i].tanzaku_id = HOLE_ID;
|
||||
if (current_kazari->tanzaku_id == tanzaku_id) {
|
||||
current_kazari->sasa_id = HOLE_ID;
|
||||
current_kazari->tanzaku_id = HOLE_ID;
|
||||
shoppyou->hole_cnt++;
|
||||
shoppyou->holes = realloc(shoppyou->holes, shoppyou->hole_cnt * sizeof(Kazari *));
|
||||
shoppyou->holes[shoppyou->hole_cnt - 1] = shoppyou->database + i;
|
||||
shoppyou->holes = reallocarray(shoppyou->holes, shoppyou->hole_cnt, sizeof(Kazari *));
|
||||
shoppyou->holes[shoppyou->hole_cnt - 1] = current_kazari;
|
||||
changed = 1;
|
||||
}
|
||||
current_kazari++;
|
||||
}
|
||||
if (changed) {
|
||||
shoppyou->modified_ts = time(NULL);
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user