123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- /*
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- * Author: hakanai
- */
- #include <stdio.h>
- #include <assert.h>
- #include <string.h>
- #include <libsdb.h>
- #include <unistd.h>
- #define TFILE "/tmp/d23rnst39ax.sdb"
- #define CATS "./cat_names.txt"
- struct record_t {
- int num;
- char str[48];
- };
- static void populate_db(sdb_t *db) {
- struct record_t r = { 0 };
- uint16_t size = sizeof(struct record_t);
- FILE *fd = fopen(CATS, "r");
- char name[48];
- assert(fd != NULL);
- while (fgets(name, 48, fd)) {
- memset(&r, 0, size);
- strncpy(r.str, name, (strlen(name) - 1));
- assert(sdb_append(db, &r, size) == SDB_OK);
- }
- fclose(fd);
- }
- static void print_cats(sdb_t *db) {
- struct record_t r = { 0 };
- int size = sdb_size(db);
- for (int i = 0; i < size; i++) {
- assert(sdb_read(db, i, &r) == SDB_OK);
- }
- }
- static void remove_all_W(sdb_t *db) {
- struct record_t r = { 0 };
- int size = sdb_size(db);
- int i;
- for (i = 0; i < size; i++) {
- assert(sdb_read(db, i, &r) == SDB_OK);
- if (r.str[0] == 'W') {
- puts(r.str);
- assert(sdb_delete(db, i) == SDB_OK);
- size--;
- i--;
- }
- }
- strncpy(r.str, "None", 4);
- assert(sdb_update(db, i, &r) == SDB_OK);
- }
- static int test_cats() {
- struct record_t r, b;
- uint16_t size = sizeof(struct record_t);
- sdb_t db;
- assert(sdb_open(TFILE, &db) == SDB_OK);
- populate_db(&db);
- print_cats(&db);
- remove_all_W(&db);
- sdb_close(&db);
- unlink(TFILE);
- return 0;
- }
- int main() {
- unlink(TFILE);
- assert(!test_cats());
- return 0;
- }
|