Skip to content

Commit b47a538

Browse files
committed
Some cleanup
1 parent cb2e4ff commit b47a538

File tree

10 files changed

+75
-81
lines changed

10 files changed

+75
-81
lines changed

include/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ s8 s8striputf8chr(s8 s);
101101
/*
102102
* Returns the length in bytes of the utf8 encoded char pointed to by @p
103103
*/
104-
#define utf8_chr_len(p) utf8_chr_len_data[(p)[0] >> 3]
104+
#define utf8_chr_len(p) utf8_chr_len_data[(p)[0] >> 4]
105105
extern u8 const utf8_chr_len_data[];
106106

107107
/*

src/ankiconnectc.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,18 @@ void ankicard_free(ankicard ac) {
209209
g_strfreev(ac.tags);
210210
}
211211

212+
/*
213+
* Fetch api url and cache the result
214+
*/
215+
static const char *get_api_url(void) {
216+
static const char *api_url = NULL;
217+
if (!api_url) {
218+
const char *env = getenv(AC_API_URL_EVAR);
219+
api_url = env && *env ? env : DEFAULT_AC_API_URL;
220+
}
221+
return api_url;
222+
}
223+
212224
static retval_s sendRequest(s8 request, ResponseFunc response_checker) {
213225
CURL *curl = curl_easy_init();
214226
if (!curl)
@@ -218,12 +230,7 @@ static retval_s sendRequest(s8 request, ResponseFunc response_checker) {
218230
headers = curl_slist_append(headers, "Accept: application/json");
219231
headers = curl_slist_append(headers, "Content-Type: application/json");
220232

221-
const char *env_url = getenv(AC_API_URL_EVAR);
222-
if (env_url)
223-
curl_easy_setopt(curl, CURLOPT_URL, env_url);
224-
else
225-
curl_easy_setopt(curl, CURLOPT_URL, DEFAULT_AC_API_URL);
226-
233+
curl_easy_setopt(curl, CURLOPT_URL, get_api_url());
227234
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
228235
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
229236
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, request.len);
@@ -237,10 +244,12 @@ static retval_s sendRequest(s8 request, ResponseFunc response_checker) {
237244
}
238245

239246
CURLcode res = curl_easy_perform(curl);
240-
if (res != CURLE_OK)
241-
return (retval_s){.data.string = "Could not connect to AnkiConnect. Is Anki running?",
242-
.ok = false};
247+
if (res != CURLE_OK) {
248+
ret = (retval_s){.data.string = "Could not connect to AnkiConnect. Is Anki running?",
249+
.ok = false};
250+
}
243251

252+
curl_slist_free_all(headers);
244253
curl_easy_cleanup(curl);
245254
return ret;
246255
}
@@ -263,7 +272,7 @@ static size_t search_checker(char *ptr, size_t len, size_t nmemb, void *userdata
263272
*
264273
* Returns: Error msg on error, null otherwise
265274
*/
266-
retval_s ac_search(_Bool include_suspended, char *deck, char *field, char *entry) {
275+
retval_s ac_search(bool include_suspended, char *deck, char *field, char *entry) {
267276

268277
stringbuilder_s sb;
269278
sb_init(&sb, 200);

src/cli.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ int main(int argc, char **argv) {
99
dictentry_print(dict[i]);
1010
putchar('\n');
1111
}
12+
13+
dictionary_free(&dict);
1214
}

src/database.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ s8 *getfiles(database db, s8 key) {
129129
if (rc != MDB_NOTFOUND)
130130
MDB_CHECK(rc);
131131

132+
mdb_cursor_close(cursor);
132133
mdb_txn_abort(db.txn);
133134
return ret;
134135
}

src/db.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ void db_put_freq(database_t *db, s8 word, s8 reading, u32 freq) {
180180
}
181181

182182
int db_get_freq(database_t *db, s8 word, s8 reading) {
183-
s8 key = concat(word, S("\0"), reading);
183+
_drop_(frees8) s8 key = concat(word, S("\0"), reading);
184184
MDB_val key_m = (MDB_val){.mv_data = key.s, .mv_size = (size_t)key.len};
185185
MDB_val val_m = {0};
186186

src/deinflector.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ s8 kanji2hira(s8 input) {
148148
}
149149

150150
mecab_destroy(mecab);
151-
assert(input.s[input.len] == '\0');
152151
return sb_gets8(sb);
153152
}
154153

src/jppron.c

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,28 @@
2323
#define access _access
2424
#endif
2525

26+
const char json_typename[][16] = {
27+
[JSON_ERROR] = "ERROR", [JSON_DONE] = "DONE", [JSON_OBJECT] = "OBJECT",
28+
[JSON_OBJECT_END] = "OBJECT_END", [JSON_ARRAY] = "ARRAY", [JSON_ARRAY_END] = "ARRAY_END",
29+
[JSON_STRING] = "STRING", [JSON_NUMBER] = "NUMBER", [JSON_TRUE] = "TRUE",
30+
[JSON_FALSE] = "FALSE", [JSON_NULL] = "NULL",
31+
};
32+
2633
typedef struct {
2734
s8 origin;
2835
s8 hira_reading;
2936
s8 pitch_number;
3037
s8 pitch_pattern;
3138
} fileinfo;
3239

33-
const char json_typename[][16] = {
34-
[JSON_ERROR] = "ERROR", [JSON_DONE] = "DONE", [JSON_OBJECT] = "OBJECT",
35-
[JSON_OBJECT_END] = "OBJECT_END", [JSON_ARRAY] = "ARRAY", [JSON_ARRAY_END] = "ARRAY_END",
36-
[JSON_STRING] = "STRING", [JSON_NUMBER] = "NUMBER", [JSON_TRUE] = "TRUE",
37-
[JSON_FALSE] = "FALSE", [JSON_NULL] = "NULL",
38-
};
40+
static void freefileinfo(fileinfo fi[static 1]) {
41+
frees8(&fi->origin);
42+
frees8(&fi->hira_reading);
43+
frees8(&fi->pitch_number);
44+
frees8(&fi->pitch_pattern);
45+
}
46+
47+
DEFINE_DROP_FUNC_PTR(fileinfo, freefileinfo)
3948

4049
static void print_fileinfo(fileinfo fi) {
4150
printf("Source: %.*s\nReading: %.*s\nPitch number: %.*s\nPitch pattern: "
@@ -45,13 +54,6 @@ static void print_fileinfo(fileinfo fi) {
4554
(int)fi.pitch_pattern.len, (char *)fi.pitch_pattern.s);
4655
}
4756

48-
static void freefileinfo(fileinfo fi[static 1]) {
49-
/* frees8(&fi->origin); */
50-
frees8(&fi->hira_reading);
51-
frees8(&fi->pitch_number);
52-
frees8(&fi->pitch_pattern);
53-
}
54-
5557
/* static void */
5658
/* prints8(s8 z) */
5759
/* { */
@@ -66,9 +68,9 @@ static void add_filename(database db, s8 headw, s8 fullpth) {
6668

6769
static void add_fileinfo(database db, s8 fullpth, fileinfo fi) {
6870
s8 sep = S("\0");
69-
s8 data = concat(fi.origin, sep, fi.hira_reading, sep, fi.pitch_number, sep, fi.pitch_pattern);
71+
_drop_(frees8) s8 data =
72+
concat(fi.origin, sep, fi.hira_reading, sep, fi.pitch_number, sep, fi.pitch_pattern);
7073
addtodb2(db, fullpth, data);
71-
frees8(&data);
7274
}
7375

7476
// wrapper for json api
@@ -312,7 +314,10 @@ static void add_from_index(database db, const char *index_path, s8 curdir) {
312314
json_skip_until(s, JSON_OBJECT_END);
313315

314316
add_fileinfo(db, fullpth, fi);
315-
freefileinfo(&fi);
317+
318+
frees8(&fi.hira_reading);
319+
frees8(&fi.pitch_number);
320+
frees8(&fi.pitch_pattern);
316321
frees8(&fullpth);
317322
} else if (reading_files) {
318323
dbg("Skipping entry of type '%s' while reading files.", json_typename[type]);
@@ -391,7 +396,7 @@ static fileinfo getfileinfo(database db, s8 fn) {
391396
}
392397

393398
static void play_word(s8 word, s8 reading, s8 database_path) {
394-
s8 normread = normalize_reading(reading);
399+
_drop_(frees8) s8 normread = normalize_reading(reading);
395400

396401
database db = opendb((char *)database_path.s, true);
397402
s8 *files = getfiles(db, word);
@@ -407,15 +412,14 @@ static void play_word(s8 word, s8 reading, s8 database_path) {
407412
if (reading.len) {
408413
bool match = false;
409414
for (size_t i = 0; i < buf_size(files); i++) {
410-
fileinfo fi = getfileinfo(db, files[i]);
415+
_drop_(freefileinfo) fileinfo fi = getfileinfo(db, files[i]);
411416

412417
if (s8equals(normread, fi.hira_reading)) {
413418
printf("File path: %.*s\n", (int)files[i].len, (char *)files[i].s);
414419
print_fileinfo(fi);
415420
play_audio(files[i]);
416421
match = true;
417422
}
418-
freefileinfo(&fi);
419423
}
420424
if (!match) {
421425
dbg("Could not find an audio file with corresponding reading. "
@@ -426,10 +430,9 @@ static void play_word(s8 word, s8 reading, s8 database_path) {
426430
if (!reading.len) {
427431
// Play all
428432
for (size_t i = 0; i < buf_size(files); i++) {
429-
fileinfo fi = getfileinfo(db, files[i]);
433+
_drop_(freefileinfo) fileinfo fi = getfileinfo(db, files[i]);
430434
printf("\nFile path: %.*s\n", (int)files[i].len, (char *)files[i].s);
431435
print_fileinfo(fi);
432-
freefileinfo(&fi);
433436

434437
play_audio(files[i]);
435438
}

src/platformdep.c

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ s8 get_sentence(void) {
6969

7070
s8 get_windowname(void) {
7171
#ifdef HAVEX11
72-
XTextProperty text_prop;
73-
7472
Display *dpy = XOpenDisplay(NULL);
7573
if (!dpy) {
7674
dbg("Can't open X display for retrieving the window title. Are you "
@@ -82,7 +80,6 @@ s8 get_windowname(void) {
8280
int revert_to;
8381
XGetInputFocus(dpy, &focused_win, &revert_to);
8482

85-
/* --------------- */
8683
Atom props[] = {XInternAtom(dpy, "_NET_WM_NAME", False), XA_WM_NAME};
8784
Atom utf8_string = XInternAtom(dpy, "UTF8_STRING", False);
8885
Atom actual_type;
@@ -95,19 +92,12 @@ s8 get_windowname(void) {
9592
(props[i] == XA_WM_NAME) ? AnyPropertyType : utf8_string,
9693
&actual_type, &format, &nr_items, &bytes_after, &prop) == Success &&
9794
prop) {
98-
return fromcstr_((char *)prop);
95+
break;
9996
}
10097
}
101-
return (s8){0};
102-
/* --------------- */
103-
/* if (!XGetWMName(dpy, focused_win, &text_prop)) */
104-
/* { */
105-
/* dbg("Could not obtain window name."); */
106-
/* return (s8){0}; */
107-
/* } */
108-
/* XCloseDisplay(dpy); */
109-
110-
return fromcstr_((char *)text_prop.value);
98+
99+
XCloseDisplay(dpy);
100+
return fromcstr_((char *)prop);
111101
#else
112102
// Not implemented
113103
return (s8){0};

src/tests.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ static int test_deinflections(void) {
118118
/* -- katakana -- */
119119
DEINFLECT("きれい", "キレイ");
120120
DEINFLECT("おもしろい", "オモシロイ");
121+
DEINFLECT("ضüщшерт1234asdfあいんしゅたいんhjkl6789уиопö",
122+
"ضüщшерт1234asdfアインシュタインhjkl6789уиопö");
121123

122124
/* -- ?? -- */
123125
DEINFLECT("目する", "目した");
@@ -166,10 +168,11 @@ static i32 test_dictionary_lookup(void) {
166168

167169
#define TEST(call) \
168170
do { \
169-
if ((call) == 0) { \
171+
int _rc = call; \
172+
if (_rc == 0) { \
170173
exit(EXIT_FAILURE); \
171174
} else { \
172-
printf("%s: Success\n", #call); \
175+
printf("%s: Success\n", #call); \
173176
} \
174177
} while (0)
175178

src/util.c

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88

99
#include "util.h"
1010

11-
u8 const utf8_chr_len_data[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12-
0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 4, 0};
11+
const u8 utf8_chr_len_data[] = {
12+
/* 0XXX */ 1, 1, 1, 1, 1, 1, 1, 1,
13+
/* 10XX */ 1, 1, 1, 1, /* invalid */
14+
/* 110X */ 2, 2,
15+
/* 1110 */ 3,
16+
/* 1111 */ 4, /* maybe, but also could be invalid */
17+
};
1318

1419
#define likely(x) __builtin_expect(!!(x), 1)
1520
#define expect(x) \
@@ -46,24 +51,6 @@ void *xrealloc(void *ptr, size_t nbytes) {
4651
}
4752
return p;
4853
}
49-
/* --------------- Start arena ----------------- */
50-
51-
/* typedef struct { */
52-
/* byte *beg; */
53-
/* byte *end; */
54-
/* } arena; */
55-
56-
/* arena */
57-
/* newarena_(void) */
58-
/* { */
59-
/* arena a = {0}; */
60-
/* size cap = 1<<22; */
61-
/* a.beg = (byte*)xmalloc(cap); */
62-
/* a.end = a.beg + cap; */
63-
/* return a; */
64-
/* } */
65-
66-
/* --------------------------------------------- */
6754

6855
/* --------------- Start s8 utils -------------- */
6956
void u8copy(u8 *restrict dst, const u8 *restrict src, size n) {
@@ -341,22 +328,22 @@ size_t snprintf_safe(char *buf, size_t len, const char *fmt, ...) {
341328
return (size_t)needed;
342329
}
343330

344-
// TODO: Rewrite these
345-
static void _nonnull_ remove_substr(char *str, s8 sub) {
346-
char *s = str, *e = str;
331+
static void strremove(char *str, const s8 sub) {
332+
assert(sub.s[sub.len] == '\0');
347333

348-
do {
349-
while (strncmp(e, (char *)sub.s, sub.len) == 0) {
350-
e += sub.len;
334+
if (sub.len > 0) {
335+
char *p = str;
336+
while ((p = strstr(p, (char *)sub.s)) != NULL) {
337+
memmove(p, p + sub.len, strlen(p + sub.len) + 1);
351338
}
352-
} while ((*s++ = *e++));
339+
}
353340
}
354341

355342
s8 nuke_whitespace(s8 z) {
356-
remove_substr((char *)z.s, S("\n"));
357-
remove_substr((char *)z.s, S("\t"));
358-
remove_substr((char *)z.s, S(" "));
359-
remove_substr((char *)z.s, S(" "));
343+
strremove((char *)z.s, S("\n"));
344+
strremove((char *)z.s, S("\t"));
345+
strremove((char *)z.s, S(" "));
346+
strremove((char *)z.s, S(" "));
360347

361348
return fromcstr_((char *)z.s);
362349
}

0 commit comments

Comments
 (0)