diff --git a/src/database.c b/src/database.c index 6d325bf..d9e5a3f 100644 --- a/src/database.c +++ b/src/database.c @@ -224,6 +224,32 @@ simdb_write(simdb_t *db, int start, int records, simdb_urec_t *data) { return records; } +int +simdb_record_del(simdb_t *db, int num) { + simdb_urec_t *rec; + int ret = 0; + + assert(db != NULL); + + if (num < 1) + return SIMDB_ERR_USAGE; + + if (!(db->flags & SIMDB_FLAG_WRITE)) + return SIMDB_ERR_READONLY; + + if ((ret = simdb_read(db, num, 1, &rec)) < 1) + return ret; + + rec->used = 0x0; + + if ((ret = simdb_write(db, num, 1, rec)) < 1) + num = ret; + + FREE(rec); + + return num; +} + int simdb_records_count(simdb_t * const db) { struct stat st; diff --git a/src/simdb-write.c b/src/simdb-write.c index fb3d3a5..d0f5631 100644 --- a/src/simdb-write.c +++ b/src/simdb-write.c @@ -93,7 +93,7 @@ int main(int argc, char **argv) } break; case del : - if ((err = simdb_write(db, num, 1, (void *) &rec)) < 1) { + if ((err = simdb_record_del(db, num)) < 0) { fprintf(stderr, "%s\n", simdb_error(err)); exit(EXIT_FAILURE); } diff --git a/src/simdb.h b/src/simdb.h index 313f0f3..5e3d726 100644 --- a/src/simdb.h +++ b/src/simdb.h @@ -111,6 +111,16 @@ int simdb_search(simdb_t * const db, int num, */ bool simdb_record_create(void * rec, const char *path); +/** + * @brief Delete a record from database by num + * @param db Database handle + * @param num Number of record to delete + * @retval <0 on error + * @retval 0 no record not exists + * @retval >0 as on success + */ +int simdb_record_del(simdb_t *db, int num); + /** * @brief Get database capacity */