Skip to content

Commit

Permalink
Return null if there is no jppron db
Browse files Browse the repository at this point in the history
  • Loading branch information
btrkeks committed Aug 23, 2024
1 parent fc547fa commit b127184
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 47 deletions.
3 changes: 1 addition & 2 deletions include/jppron/jppron.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ void free_pronfile_buffer(Pronfile *pronfiles);
DEFINE_DROP_FUNC_PTR(Pronfile, free_pronfile)
DEFINE_DROP_FUNC(Pronfile *, free_pronfile_buffer)

void jppron(Word word, char *audio_folders_path);
/* _deallocator_(free_pronfile_buffer) */ // TODO: Check why this gives compiler errors
_deallocator_(free_pronfile_buffer) // TODO: Check why this gives compiler errors
Pronfile *get_pronfiles_for(Word word);

void jppron_create_index(const char *audio_folders_path);
Expand Down
2 changes: 1 addition & 1 deletion include/jppron/jppron_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

typedef struct _PronDatabase PronDatabase;

PronDatabase *jppron_open_db(char *path, bool readonly);
PronDatabase *jppron_open_db(bool readonly);
void jppron_close_db(PronDatabase *);
/*
* Add to database, allowing duplicates if they are added directly after another
Expand Down
40 changes: 12 additions & 28 deletions src/jppron/jppron.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ static s8 get_default_database_path(void) {
return buildpath(fromcstr_((char *)get_user_data_dir()), S("jppron"));
}

static void process_audio_subdirectory(const char *audio_dir_path, char *subdir_name, PronDatabase *db) {
_drop_(frees8) s8 curdir = buildpath(fromcstr_((char*)audio_dir_path), fromcstr_(subdir_name));
static void process_audio_subdirectory(const char *audio_dir_path, char *subdir_name,
PronDatabase *db) {
_drop_(frees8) s8 curdir = buildpath(fromcstr_((char *)audio_dir_path), fromcstr_(subdir_name));
_drop_(frees8) s8 index_path = buildpath(curdir, S("index.json"));
dbg("Processing path: %.*s", (int)curdir.len, (char *)curdir.s);

Expand All @@ -48,7 +49,7 @@ static void jppron_create_database(const char *audio_dir_path, s8 dbpth) {
jdb_remove(dbpth);
createdir(dbpth);

_drop_(jppron_close_db) PronDatabase *db = jppron_open_db((char *)dbpth.s, false);
_drop_(jppron_close_db) PronDatabase *db = jppron_open_db(false);

_drop_(closedir) DIR *audio_dir = opendir(audio_dir_path);
err_ret_on(!audio_dir, "Failed to open audio directory '%s': %s", audio_dir_path,
Expand Down Expand Up @@ -91,9 +92,11 @@ static FileInfo *get_fileinfo_for_array(PronDatabase *db, s8 *files) {
return fi;
}

static void get_all_files_and_fileinfo_for(s8 kanji, s8 db_path, s8 *files[static 1],
FileInfo *fileinfo[static 1]) {
_drop_(jppron_close_db) PronDatabase *db = jppron_open_db((char *)db_path.s, true);
static void _nonnull_ get_all_files_and_fileinfo_for(s8 kanji, s8 *files[static 1],
FileInfo *fileinfo[static 1]) {
_drop_(jppron_close_db) PronDatabase *db = jppron_open_db(true);
if (!db)
return;

*files = jdb_get_files(db, kanji);
if (!*files)
Expand All @@ -103,10 +106,9 @@ static void get_all_files_and_fileinfo_for(s8 kanji, s8 db_path, s8 *files[stati
}

Pronfile *get_pronfiles_for(Word word) {
_drop_(frees8) s8 db_path = get_default_database_path();
_drop_(s8_buf_free) s8Buf files = 0;
_drop_(free) FileInfo *fileinfo = 0;
get_all_files_and_fileinfo_for(word.kanji, db_path, &files, &fileinfo);
get_all_files_and_fileinfo_for(word.kanji, &files, &fileinfo);

if (!files)
return NULL;
Expand Down Expand Up @@ -144,32 +146,14 @@ Pronfile *get_pronfiles_for(Word word) {
return pronfiles;
}

static void play_pronfiles(Pronfile *pronfiles) {
for (size_t i = 0; i < buf_size(pronfiles); i++) {
play_audio_sync(pronfiles[i].filepath);
}
}

void jppron_create_index(const char *audio_folders_path) {
_drop_(frees8) s8 dbpath = get_default_database_path();

if (audio_folders_path) {
msg("Indexing files..");
jppron_create_database(audio_folders_path, dbpath); // TODO: エラー処理
msg("Index completed.");
}
else {
} else {
err("No path provided.");
}
}

void jppron(Word word, char *audio_folders_path) {
/* _drop_(free_pronfile_buffer) pronfile_s *pronfiles = get_pronfiles_for(word, reading,
* dbpath); */
Pronfile *pronfiles = get_pronfiles_for(word);
if (pronfiles) {
play_pronfiles(pronfiles);
free_pronfile_buffer(pronfiles);
} else
msg("No pronunciation found.");
}
}
63 changes: 47 additions & 16 deletions src/jppron/jppron_database.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "utils/util.h"

#include <jppron/jppron_database.h>
#include <platformdep/file_operations.h>

DEFINE_DROP_FUNC(MDB_cursor *, mdb_cursor_close)

Expand All @@ -31,38 +32,68 @@ struct _PronDatabase {
bool readonly;
};

PronDatabase *jppron_open_db(char *dbpath, bool readonly) {
dbg("Opening database in directory: '%s'", dbpath);
static s8 pron_db_get_path(void) {
static s8 dbpath = (s8){0};

if (!dbpath.len) {
dbpath = buildpath(fromcstr_((char *)get_user_data_dir()), S("jppron"));
}

return dbpath;
}

static PronDatabase *pron_db_open_readonly(s8 dbpath) {
PronDatabase *db = new (PronDatabase, 1);
db->readonly = readonly;
db->readonly = true;

MDB_CHECK(mdb_env_create(&db->env));
mdb_env_set_maxdbs(db->env, 2);

if (db->readonly) {
die_on(!jdb_check_exists(fromcstr_(dbpath)), "There is no jppron database in '%s'.",
dbpath);
int rc = mdb_env_open(db->env, (char *)dbpath.s, MDB_RDONLY | MDB_NOLOCK | MDB_NORDAHEAD, 0664);
if (rc != MDB_SUCCESS) {
dbg("Could not open pron database: %s", mdb_strerror(rc));
free(db);
return NULL;
}
return db;
}

MDB_CHECK(mdb_env_open(db->env, dbpath, MDB_RDONLY | MDB_NOLOCK | MDB_NORDAHEAD, 0664));
static PronDatabase *pron_db_open_read_write(s8 dbpath) {
PronDatabase *db = new (PronDatabase, 1);
db->readonly = true;

} else {
unsigned int mapsize = 2147483648; // 2 Gb max
MDB_CHECK(mdb_env_set_mapsize(db->env, mapsize));
MDB_CHECK(mdb_env_create(&db->env));
mdb_env_set_maxdbs(db->env, 2);

MDB_CHECK(mdb_env_open(db->env, dbpath, 0, 0664));
MDB_CHECK(mdb_txn_begin(db->env, NULL, 0, &db->txn));
MDB_CHECK(
mdb_dbi_open(db->txn, "dbi1", MDB_DUPSORT | MDB_CREATE, &db->db_headword_to_filepath));
MDB_CHECK(mdb_dbi_open(db->txn, "dbi2", MDB_CREATE, &db->db_filepath_to_fileinfo));
unsigned int mapsize = 2147483648; // 2 Gb max
MDB_CHECK(mdb_env_set_mapsize(db->env, mapsize));

db->lastval = sb_init(200);
int rc = mdb_env_open(db->env, (char *)dbpath.s, 0, 0664);
if (rc != MDB_SUCCESS) {
dbg("Could not open pron database: %s", mdb_strerror(rc));
free(db);
return NULL;
}

MDB_CHECK(mdb_txn_begin(db->env, NULL, 0, &db->txn));
MDB_CHECK(
mdb_dbi_open(db->txn, "dbi1", MDB_DUPSORT | MDB_CREATE, &db->db_headword_to_filepath));
MDB_CHECK(mdb_dbi_open(db->txn, "dbi2", MDB_CREATE, &db->db_filepath_to_fileinfo));

db->lastval = sb_init(200);
return db;
}

PronDatabase *jppron_open_db(bool readonly) {
s8 dbpath = pron_db_get_path();
dbg("Opening jppron database in directory: '%s'", (char *)dbpath.s);
return readonly ? pron_db_open_readonly(dbpath) : pron_db_open_read_write(dbpath);
}

void jppron_close_db(PronDatabase *db) {
if (!db)
return;

if (!db->readonly) {
MDB_CHECK(mdb_txn_commit(db->txn));
mdb_dbi_close(db->env, db->db_headword_to_filepath);
Expand Down

0 comments on commit b127184

Please sign in to comment.