Skip to content

Commit

Permalink
Add freqent helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
btrkeks committed May 26, 2024
1 parent 54f3b70 commit db10a43
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
8 changes: 1 addition & 7 deletions include/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,13 @@
// An opaque struct
typedef struct database_s database_t;

typedef struct {
s8 word;
s8 reading;
u32 frequency;
} frequency_entry;

database_t *_nonnull_ db_open(char *dbpath, bool readonly);
void _nonnull_ db_close(database_t *db);

void _nonnull_ db_put_dictent(database_t *db, s8 headword, dictentry de);
void _nonnull_ db_get_dictents(const database_t *db, s8 headword, dictentry *dict[static 1]);

void _nonnull_ db_put_freq(const database_t *db, frequency_entry fe);
void _nonnull_ db_put_freq(const database_t *db, freqentry fe);

/*
* Checks if there exists a database in the provided path
Expand Down
10 changes: 9 additions & 1 deletion include/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define DP_UTIL_H

#include "buf.h" // growable buffer implementation
#include <dirent.h> // DIR
#include <stdio.h> // FILE
#include <unistd.h> // close()

Expand Down Expand Up @@ -169,6 +168,15 @@ void _nonnull_ dictionary_free(dictentry **dict);
dictentry dictentry_at_index(dictentry *dict, size index);
/* --------------------- End dictentry ------------------------ */

typedef struct {
s8 word;
s8 reading;
u32 frequency;
} freqentry;

freqentry freqentry_dup(freqentry fe);
void freqentry_free(freqentry *fe);

size_t _printf_(3, 4) snprintf_safe(char *buf, size_t len, const char *fmt, ...);

void nuke_whitespace(s8 *z);
Expand Down
2 changes: 1 addition & 1 deletion src/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ static u32 *getids(const database_t *db, s8 word, size_t *num) {
return ret;
}

void db_put_freq(const database_t *db, frequency_entry fe) {
void db_put_freq(const database_t *db, freqentry fe) {
die_on(db->readonly, "Cannot put frequency into db in readonly mode.");

s8 key = concat(fe.word, S("\0"), fe.reading);
Expand Down
5 changes: 5 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ s8 news8(size len) {
}

s8 s8dup(s8 src) {
#ifdef UNIT_TEST
if (!src.s)
return (s8){0};
#endif

s8 r = news8(src.len);
s8copy(r, src);
return r;
Expand Down

0 comments on commit db10a43

Please sign in to comment.