From 74b018bf1c5b89a77447d25b7bbdb1ba7cbc5dd4 Mon Sep 17 00:00:00 2001 From: Anthony Wang Date: Tue, 19 Apr 2022 08:23:47 -0500 Subject: Implement C arg parsing with getopt_long --- README.md | 2 +- cd.c | 37 ++++++++++++++++++++++++++++++++++++- segmenttree.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7dcbffc..819b73d 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ C port of [SD](https://git.exozy.me/Ta180m/SD), a very efficient flash cards app Flash cards are stored in the `cards` table of a SQLite database. There are four columns: `idx INTEGER PRIMARY KEY, weight INTEGER, key STRING, val STRING`. The `idx` is a unique index for each card, starting at 0. The weight is how often the card should come up. The key and value are the front and reverse sides of the card. You can use the `sqlite3` CLI to create a card deck. -Now build this project with `gcc cd.c -o cd -O2 -march=native` and run `./cd` to enjoy a fast flash cards experience! The program will display the `key` of a randomly selected card. Press any key to show the `val` of the card. Now press either `y` or `n` depending on whether you got the card correct, and the program adjusts that card's weight. +Now build this project with `gcc cd.c -o cd -l sqlite3 -O2 -march=native` and run `./cd` to enjoy a fast flash cards experience! The program will display the `key` of a randomly selected card. Press any key to show the `val` of the card. Now press either `y` or `n` depending on whether you got the card correct, and the program adjusts that card's weight. If you're wondering where the name came from, this is the C port of [SD](https://git.exozy.me/Ta180m/SD). diff --git a/cd.c b/cd.c index 617cdaf..14a9105 100644 --- a/cd.c +++ b/cd.c @@ -1,7 +1,42 @@ #include +#include +#include +#include +#include +#include #include #include "segmenttree.h" int main(int argc, char* argv[]) { - printf("Hello!"); + char *file = "cards"; + bool verbose = false; + + static struct option long_options[] = { + {"file", required_argument, 0, 'f'}, + {"verbose", no_argument, 0, 'v'} + }; + while (1) { + int option_index = 0; + int c = getopt_long(argc, argv, "f:v", long_options, &option_index); + if (c == -1) break; + switch (c) { + case 'f': file = strdup(optarg); break; + case 'v': verbose = true; break; + default: abort(); + } + } + + printf("%s", file); + + sqlite3 *db; + int rc = sqlite3_open(file, &db); + if (rc) { + fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); + abort(); + } + fprintf(stderr, "Opened database successfully\n"); + + //int N = sqlite3_exec(db, "SELECT COUNT(*) FROM cards", callback) + + sqlite3_close(db); } diff --git a/segmenttree.c b/segmenttree.c index e69de29..1bf0fcd 100644 --- a/segmenttree.c +++ b/segmenttree.c @@ -0,0 +1,46 @@ +struct SegmentTree { + int N; + int * seg; +} + +// Build segment tree +void Build(int l, int r, int n) { + if (l == r) { + + } + int m = l + r >> 1; + Build(l, m, n << 1); + Build(m + 1, r, n << 1 | 1); + seg[n] = seg[n << 1] + seg[n << 1 | 1]; +} + +// Update value at index x +void Update(int x, int v, int l, int r, int n) { + if (l == r) { + seg[n] += v; + return; + } + int m = l + r >> 1; + if (x <= m) { + Update(x, v, l, m, n<<1); + } + else { + Update(x, v, m+1, r, n<<1|1); + } + seg[n] = seg[n << 1] + seg[n << 1 | 1]; +} + +// Find element with prefix sum v +int Query(int v, int l, int r, int n) { + if (l == r) { + return s.seg[n], l; + } + int m = l + r >> 1; + if (seg[n << 1] >= v) { + return Query(v, l, m, n << 1); + } + else { + return Query(v - s.seg[n << 1], m + 1, r, n << 1 | 1); + } +} + -- cgit v1.2.3-70-g09d2