summaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c140
1 files changed, 96 insertions, 44 deletions
diff --git a/src/main.c b/src/main.c
index 1050741..352c531 100644
--- a/src/main.c
+++ b/src/main.c
@@ -17,14 +17,18 @@ static char randfile[] = "/dev/urandom";
static struct settings settings = {0};
-static struct stream file_in = {.prev_idx = -1, .fd = -1};
-static struct stream file_out = {.prev_idx = -1, .fd = -1, .out = 1};
-
static int parseargs(int argc, char **argv, struct settings * settings);
-int prime_io_functions(struct settings const * const s, struct stream * const in);
+int load_io_functions(struct settings const * const s, struct stream * const in);
void printhelp(const char * const name);
-static int stub_get(struct stream * const restrict in, ssize_t idx, struct entry_l * const data);
-static int stub_put(struct stream * const restrict in, ssize_t idx, struct entry_l const * const data);
+struct entry_l *stub_getnext(struct stream * const restrict in);
+static int stub_put(struct stream * const restrict in, struct entry_l const * const data);
+static int stub_split(struct stream * const in, struct stream * const a, struct stream * const b);
+static int stub_flush(struct stream * const in);
+
+/* these need to go AFTER the stub declarations */
+static struct stream file_in = {stream_blank};
+static struct stream file_out = {stream_blank, .out = 1};
+static struct stream file_tmp = {stream_blank, .out = 2};
int main(int argc, char **argv)
{
@@ -37,21 +41,58 @@ int main(int argc, char **argv)
file_in.name = randfile;
file_in.out = -1;
file_in.n = settings.to;
+ file_in.cached = 1;
+ file_out.cached = 1;
} else {
file_in.name = settings.filein;
}
- ret = prime_io_functions(&settings, &file_in);
- ret = prime_io_functions(&settings, &file_out);
+ load_io_functions(&settings, &file_in);
file_out.name = settings.fileout ? settings.fileout : settings.filein;
try_s((ret = stream_open(&file_in, &settings)), out);
- file_out.n = file_in.n;
+ file_out.n = file_in.n - settings.ss;
try_s((ret = stream_open(&file_out, &settings)), out);
-out:
+ load_io_functions(&settings, &file_out);
+
+ if (settings.access == cached) {
+ try_s((ret = cache_create(&file_in, &settings)), out);
+ try_s((ret = cache_populate(&file_in)), out);
+
+ switch (settings.opmode) {
+ case mode_fetch:
+ if (settings.format == array) {
+ try_s((ret = cache_block_copy(&file_in, &file_out, &settings)), out);
+ } else { /* settings.format == list */
+ try_s((ret = cache_list_copy(&file_in, &file_out, &settings)), out);
+ }
+ break;
+ case mode_generate:
+ try_s((ret = cache_transfer(&file_in, &file_out)), out);
+ break;
+ case mode_normal:
+ /* TODO */
+ ;
+ }
+
+ try_s((ret = cache_flush(&file_out)), out);
+
+ } else { /* uncached */
+ /* TODO */
+ }
+
stream_close(&file_in);
stream_close(&file_out);
+
+ while (0) {
+out:
+ stream_close(&file_in);
+ file_out.out = 2; /* in case of error-exit, just close, don't link */
+ stream_close(&file_out);
+ stream_close(&file_tmp);
+ }
+
early_out:
return ret;
}
@@ -89,7 +130,7 @@ static int parseargs(int argc, char **argv, struct settings * settings)
}
} else if (!(strncmp(argv[i], "--num=", 6))) {
if (strlen(argv[i]) > 6) {
- s.to = strtoul(argv[i] + 11, NULL, 10);
+ s.to = strtoul(argv[i] + 6, NULL, 10);
} else {
goto err;
}
@@ -139,45 +180,29 @@ err:
return ret;
}
-int prime_io_functions(struct settings const * const s, struct stream * const in)
+int load_io_functions(struct settings const * const s, struct stream * const in)
{
int ret = 0;
if (in->out == 1) {
if (s->format == array) {
- in->get_element = stub_get;
- in->put_element = stub_put;
- in->get_element_cache = cached_get_array;
- in->put_element_cache = cached_put_array;
+ /* TODO */
} else {
- in->get_element = stub_get;
- in->put_element = stub_put;
- in->get_element_cache = cached_get_list;
- in->put_element_cache = cached_put_list;
+ /* TODO */
}
} else if (!in->out) { /* reading streams do not support dumping */
if (s->format == array) {
- in->get_element = stub_get;
- in->put_element = stub_put;
- in->get_element_cache = stub_get;
- in->put_element_cache = stub_put;
+ /* TODO */
} else {
- in->get_element = stub_get;
- in->put_element = stub_put;
- in->get_element_cache = stub_get;
- in->put_element_cache = stub_put;
+ /* TODO */
}
} else { /* data generation streams do not support dumping nor any cache I/O beyond initial data generation */
if (s->format == array) {
- in->get_element = gen_get_array;
- in->put_element = stub_put;
- in->get_element_cache = stub_get;
- in->put_element_cache = stub_put;
+ in->get_next_element_direct = gen_get_array;
+ in->place_next_element_cache = cached_put_array;
} else {
- in->get_element = gen_get_list;
- in->put_element = stub_put;
- in->get_element_cache = stub_get;
- in->put_element_cache = stub_put;
+ in->get_next_element_direct = gen_get_list;
+ in->place_next_element_cache = cached_put_list;
}
}
@@ -188,7 +213,7 @@ void printhelp(const char * const name)
{
printf( "This is a mergesort program and such\n"
"\n"
- "usage:\t%s [options]\n FILE"
+ "usage:\t%s [OPTION]... FILE"
"\n"
"Options:\n"
" --sort sort mode (default)\n"
@@ -207,22 +232,49 @@ void printhelp(const char * const name)
return;
}
-static int stub_get(struct stream * const restrict in, ssize_t idx, struct entry_l * const data)
+struct entry_l *stub_getnext(struct stream * const restrict in)
{
+ struct entry_l *ret = NULL;
+
(void) in;
- (void) idx;
- (void) data;
rin_warn("stub!");
- return ENOSYS;
+
+ return ret;
}
-static int stub_put(struct stream * const restrict in, ssize_t idx, struct entry_l const * const data)
+static int stub_put(struct stream * const restrict in, struct entry_l const * const data)
{
+ int ret = ENOSYS;
+
(void) in;
- (void) idx;
(void) data;
rin_warn("stub!");
- return ENOSYS;
+
+ return ret;
+}
+
+static int stub_split(struct stream * const in, struct stream * const a, struct stream * const b)
+{
+ int ret = ENOSYS;
+
+ (void) in;
+ (void) a;
+ (void) b;
+
+ rin_warn("stub!");
+
+ return ret;
+}
+
+static int stub_flush(struct stream * const in)
+{
+ int ret = ENOSYS;
+
+ (void) in;
+
+ rin_warn("stub!");
+
+ return ret;
}