Browse Source

+ simdb_record_del()

master
Alex 'AdUser' Z 8 years ago
parent
commit
1cdeacdc54
  1. 26
      src/database.c
  2. 2
      src/simdb-write.c
  3. 10
      src/simdb.h

26
src/database.c

@ -224,6 +224,32 @@ simdb_write(simdb_t *db, int start, int records, simdb_urec_t *data) {
return records; 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 int
simdb_records_count(simdb_t * const db) { simdb_records_count(simdb_t * const db) {
struct stat st; struct stat st;

2
src/simdb-write.c

@ -93,7 +93,7 @@ int main(int argc, char **argv)
} }
break; break;
case del : 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)); fprintf(stderr, "%s\n", simdb_error(err));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }

10
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); 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 * @brief Get database capacity
*/ */

Loading…
Cancel
Save