Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
* Typedef dictentry array to Dict to reduce coupling
* Choose better function names
* Split utils into multiple files to make code more readable
* Clang format

Sorry for huge commit, lol
  • Loading branch information
btrkeks committed Jul 18, 2024
1 parent 4473195 commit fc175ac
Show file tree
Hide file tree
Showing 50 changed files with 1,105 additions and 803 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
BinPackArguments: true
BreakStringLiterals: false
BreakStringLiterals: true
ColumnLimit: 100
IndentCaseLabels: true
IndentPPDirectives: BeforeHash
Expand Down
21 changes: 11 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ set(JPPRON_SRCS
src/jppron/jppron.c
src/jppron/database.c
src/jppron/ajt_audio_index_parser.c
src/utils/yyjson.c)
src/utils/yyjson.c
src/objects/dict.c
include/objects/freqentry.h)

# ##############################################################################
# dictpopup
Expand All @@ -46,11 +48,13 @@ add_executable(dictpopup ${JPPRON_SRCS}
src/settings.c
src/utils/util.c
src/utils/utf8.c
src/utils/str.c
src/platformdep/audio.c
src/platformdep/notifications.c
src/platformdep/clipboard.c
src/platformdep/file_operations.c
src/platformdep/windowtitle.c)
src/platformdep/windowtitle.c
src/objects/freqentry.c)
target_compile_definitions(dictpopup PUBLIC NOTIFICATIONS CLIPBOARD)
target_include_directories(
dictpopup PRIVATE ${GTK3_INCLUDE_DIRS} ${NOTIFY_INCLUDE_DIRS}
Expand Down Expand Up @@ -121,12 +125,8 @@ add_executable(dictpopup-cli EXCLUDE_FROM_ALL src/frontends/cli.c
)
target_include_directories(
dictpopup-cli PRIVATE ${GTK3_INCLUDE_DIRS} ${CURL_INCLUDE_DIRS} include/)
target_link_directories(dictpopup-cli PRIVATE ${GTK3_LIBRARY_DIRS}
target_link_directories(cli PRIVATE ${GTK3_LIBRARY_DIRS}
${NOTIFY_LIBRARY_DIRS} ${CURL_LIBRARY_DIRS})
target_link_libraries(dictpopup-cli PRIVATE ${GTK3_LIBRARIES} ${NOTIFY_LIBRARIES}
target_link_libraries(cli PRIVATE ${GTK3_LIBRARIES} ${NOTIFY_LIBRARIES}
${CURL_LIBRARIES} -lmecab -llmdb)
target_link_directories(dictpopup-cli PRIVATE ${GTK3_LIBRARY_DIRS})
target_link_libraries(dictpopup-cli PRIVATE ${GTK3_LIBRARIES} ${NOTIFY_LIBRARIES})

# ##############################################################################
# Tests
Expand All @@ -146,8 +146,9 @@ if (BUILD_TESTING)
tests/jppron_tests/ajt_audio_index_parser_tests.c
tests/utils_tests/enclose_word_in_string_tests.c
tests/dictpopup_tests.c
src/utils/utf8.c)
target_compile_definitions(c_tests PRIVATE UNIT_TEST)
src/utils/utf8.c
tests/jppron_tests/jppron_tests.c)
target_compile_definitions(c_tests PRIVATE UNIT_TEST CLIPBOARD)
target_include_directories(c_tests PRIVATE ${GTK3_INCLUDE_DIRS} ${CGREEN_INCLUDE_DIRS} include/ src/ src/jppron)
target_link_libraries(
c_tests
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ cli: $(SRC) $(SRC_H) $(SDIR)/frontends/cli.c
$(CC) $(CFLAGS) $(DEBUG_CFLAGS) $(CPPFLAGS) -o $@ $(SDIR)/frontends/cli.c $(SRCS) $(LDLIBS)

deinflector: $(SRC) $(SRC_H) $(SDIR)/deinflector.c
$(CC) $(CFLAGS) $(DEBUG_CFLAGS) $(CPPFLAGS) -DDEINFLECTOR_MAIN -o $@ $(SDIR)/deinflector.c $(SDIR)/util.c $(LDLIBS)
$(CC) $(CFLAGS) $(DEBUG_CFLAGS) $(CPPFLAGS) -DDEINFLECTOR_MAIN -o $@ $(SDIR)/deinflector.c $(SDIR)/utils/util.c $(SDIR)/utils/utf8.c $(LDLIBS)

release:
version=$$(git describe); prefix=dictpopup-$${version#v}; \
Expand Down
2 changes: 2 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## dictpopup
- Write a "Yomichan search"-like window application
- Check if sentence is mapped to some field and only prompt for it in that case
- Cache dot indicator result for kanji/reading pairs and lookup for every dict entry
- Disable pronunciation button if nothing found

Mid priority
- Implement the reading label as a text view to allow for editing -> initiate new search on Enter press
Expand Down
3 changes: 2 additions & 1 deletion include/ankiconnectc.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "utils/util.h"
#include <stdbool.h>
#include <utils/str.h>

typedef struct {
char *deck;
Expand All @@ -21,7 +22,7 @@ s8 *ac_get_decks(char **error);
s8 *ac_get_notetypes(char **error);
int ac_check_exists(char *deck, char *field, char *entry, char **error);
void ac_gui_search(const char *deck, const char *field, const char *entry, char **error);
void ac_addNote(ankicard ac, char **error);
void _nonnull_ ac_addNote(ankicard ac, char **error);

/*
* Stores the file at @path in the Anki media collection under the name
Expand Down
8 changes: 5 additions & 3 deletions include/db.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef DP_DB_H
#define DP_DB_H

#include "objects/dict.h"
#include "objects/freqentry.h"
#include "utils/util.h"
#include <stdbool.h>

Expand All @@ -11,14 +12,15 @@ __attribute__((returns_nonnull)) database_t *_nonnull_ db_open(char *dbpath, boo
void _nonnull_ db_close(database_t *db);

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

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

/*
* Checks if there exists a database in the provided path
*/
i32 db_check_exists(s8 dbpath);
bool db_check_exists(s8 dbpath);
void db_remove(s8 dbpath);

DEFINE_DROP_FUNC(database_t *, db_close)
Expand Down
2 changes: 2 additions & 0 deletions include/deinflector.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "utils/util.h"

#include <utils/str.h>

/*
* @word: The japanese word to be deinflected
*
Expand Down
23 changes: 4 additions & 19 deletions include/dictpopup.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#ifndef DP_DICTPOPUP_H
#define DP_DICTPOPUP_H

#include "utils/util.h"

#include <settings.h>
#include "utils/util.h"
#include "objects/dict.h"

typedef dictentry* Dict;
/*
* Should only be called once and as early as possible
*/
Expand All @@ -14,24 +15,8 @@ void dictpopup_init(void);
* Looks up @lookup in the database and returns all corresponding dictentries in
* a buffer (see include/buf.h)
*/
dictentry *_nonnull_ create_dictionary(s8 *word);
Dict _nonnull_ create_dictionary(s8 *word, Config config);

void create_ankicard(s8 lookup, dictentry de, Config config);

#define POSSIBLE_ENTRIES_S_NMEMB 9
typedef struct possible_entries_s {
s8 copiedsent;
s8 boldsent;
s8 dictkanji;
s8 dictreading;
s8 dictdefinition;
s8 furigana;
s8 windowname;
s8 dictname;
} possible_entries_s;

struct dictpopup_s {
possible_entries_s pe;
};

#endif
3 changes: 1 addition & 2 deletions include/jppron/ajt_audio_index_parser.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#ifndef AUDIO_INDEX_PARSER_H
#define AUDIO_INDEX_PARSER_H

#include <stddef.h>
#include "utils/util.h"
#include "utils/str.h"

typedef struct {
s8 origin;
Expand Down
1 change: 1 addition & 0 deletions include/jppron/database.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "ajt_audio_index_parser.h"
#include "utils/util.h"
#include <stdbool.h>
#include <utils/str.h>

typedef struct database_s database;

Expand Down
6 changes: 3 additions & 3 deletions include/jppron/jppron.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ typedef struct {
fileinfo_s fileinfo;
} pronfile_s;

void _nonnull_ free_pronfile(pronfile_s *pronfile);
void _nonnull_ free_pronfile_buffer(pronfile_s **pronfiles);
void free_pronfile(pronfile_s pronfile[static 1]);
void free_pronfile_buffer(pronfile_s *pronfiles);
DEFINE_DROP_FUNC_PTR(pronfile_s, free_pronfile)
DEFINE_DROP_FUNC_PTR(pronfile_s *, free_pronfile_buffer)
DEFINE_DROP_FUNC(pronfile_s *, free_pronfile_buffer)

void jppron(s8 word, s8 reading, char *audio_folders_path);
/* _deallocator_(free_pronfile_buffer) */ // TODO: Check why this gives compiler errors
Expand Down
32 changes: 32 additions & 0 deletions include/objects/dict.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef DICT_H
#define DICT_H
#include "utils/str.h"
#include "utils/util.h"

typedef struct {
s8 dictname;
s8 kanji;
s8 reading;
s8 definition;
u32 frequency;
bool is_deinflection;
} dictentry;

dictentry dictentry_dup(dictentry de);
void _nonnull_ dictentry_free(dictentry de);
void dictentry_print(dictentry de);

typedef dictentry *Dict;
Dict newDict(void);
bool isEmpty(Dict dict);
void _nonnull_ dictionary_add(Dict *dict, dictentry de);
isize dictLen(Dict dict);

// Sorts @dict in place
void dictSort(Dict dict, int (*dictentryComparer)(const dictentry *a, const dictentry *b));

void _nonnull_ dictionary_free(Dict *dict);
dictentry dictentry_at_index(Dict dict, isize index);
dictentry *pointer_to_entry_at(Dict dict, isize index);

#endif // DICT_H
14 changes: 14 additions & 0 deletions include/objects/freqentry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef FREQENTRY_H
#define FREQENTRY_H
#include <utils/str.h>

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

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

#endif // FREQENTRY_H
4 changes: 2 additions & 2 deletions include/platformdep/audio.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef AUDIO_H
#define AUDIO_H

#include "utils/util.h"
#include <utils/str.h>

void play_audio(s8 filepath);

#endif //AUDIO_H
#endif // AUDIO_H
8 changes: 4 additions & 4 deletions include/platformdep/clipboard.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef CLIPBOARD_H
#define CLIPBOARD_H

#include "utils/util.h"
#include <utils/str.h>

s8 get_selection(void);
s8 get_clipboard(void);
s8 get_next_clipboard(void);
char *get_clipboard(void);
char *get_next_clipboard(void);

#endif //CLIPBOARD_H
#endif // CLIPBOARD_H
4 changes: 2 additions & 2 deletions include/platformdep/file_operations.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef FILE_OPERATIONS_H
#define FILE_OPERATIONS_H

#include <stdbool.h>
#include "utils/util.h"
#include <stdbool.h>

void _nonnull_ createdir(char *dirpath);
const char *get_user_data_dir(void);
Expand All @@ -11,4 +11,4 @@ void rem(char *filepath);
void _nonnull_ file_copy_sync(const char *source, const char *dest);
void _nonnull_ file_copy_async(const char *source, const char *dest);

#endif //FILE_OPERATIONS_H
#endif // FILE_OPERATIONS_H
22 changes: 17 additions & 5 deletions include/platformdep/file_paths.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,23 @@

#ifdef _WIN32
// TODO: fill
#define DEFAULT_DATABASE_LOCATIONS (char*[]){""}
#define DEFAULT_SETTINGS_LOCATIONS (char*[]){"", ""}
#define DEFAULT_DATABASE_LOCATIONS \
(char *[]) { \
"" \
}
#define DEFAULT_SETTINGS_LOCATIONS \
(char *[]) { \
"", "" \
}
#else
#define DEFAULT_DATABASE_LOCATIONS (char*[]){"/usr/share/dictpopup/data.mdb", "/usr/local/share/dictpopup/data.mdb"}
#define DEFAULT_SETTINGS_LOCATIONS (char*[]){"/usr/share/dictpopup/config.ini", "/usr/local/share/dictpopup/config.ini"}
#define DEFAULT_DATABASE_LOCATIONS \
(char *[]) { \
"/usr/share/dictpopup/data.mdb", "/usr/local/share/dictpopup/data.mdb" \
}
#define DEFAULT_SETTINGS_LOCATIONS \
(char *[]) { \
"/usr/share/dictpopup/config.ini", "/usr/local/share/dictpopup/config.ini" \
}
#endif

#endif //FILE_PATHS_H
#endif // FILE_PATHS_H
2 changes: 1 addition & 1 deletion include/platformdep/notifications.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

void notify(_Bool urgent, char const *fmt, ...);

#endif //NOTIFICATIONS_H
#endif // NOTIFICATIONS_H
3 changes: 2 additions & 1 deletion include/platformdep/windowtitle.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#ifndef WINDOWTITLE_H
#define WINDOWTITLE_H

#include "utils/util.h"
#include <utils/str.h>

s8 get_windowname(void);

#endif //WINDOWTITLE_H
4 changes: 2 additions & 2 deletions include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

typedef struct {
struct {
char *dbpth;
bool sort;
char *dbDir;
bool sortDictEntries;
char **dictSortOrder;
bool nukeWhitespaceLookup;
bool mecab;
Expand Down
File renamed without changes.
Loading

0 comments on commit fc175ac

Please sign in to comment.