summaryrefslogtreecommitdiffstats
path: root/src/main.c
blob: 7cc0c5e0555bb590ca49895152e4c1637904217f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <unistd.h>
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "io.h"

static struct settings_s {
	size_t fetchpos;
	size_t fetchto;
	char *filein;
	char *fileout;
	int mode;
} settings = {0};

static struct stream file_in = {.fd = -1};
static struct stream file_out = {.fd = -1};

static int parseargs(int argc, char **argv, struct settings_s * settings);
void printhelp(const char * const name);

int main(int argc, char **argv)
{
	int ret = 0;

	if ((ret = parseargs(argc, argv, &settings))) {
		goto out;
	}

	file_in.name = settings.filein;
	file_in.stride = 0; /* TODO */

	if ((ret = openstream(&file_in))) {
		goto out;
	}

	file_out.name = settings.fileout ? settings.fileout : settings.filein;
	file_out.out = 1;
	file_out.n = file_in.n;
	file_out.stride = 0; /* TODO */

	if ((ret = openstream(&file_out))) {
		goto out;
	}

out:
	close(file_in.fd);
	close(file_out.fd);
	return ret;
}

static int parseargs(int argc, char **argv, struct settings_s * settings)
{
	struct settings_s s = {0};
	ssize_t i;
	int ret = 0;

	if (argc < 2) {
		goto err;
	}

	for (i = 1; i < argc - 1; ++i) {
		if (!(strcmp(argv[i], "--sort"))) {
			s.mode &= ~1;
		} else if (!(strcmp(argv[i], "--fetch"))) {
			s.mode |= 1;
		} else if (!(strncmp(argv[i], "--position=", 11))) {
			if (strlen(argv[i]) > 11) {
				s.fetchpos = strtoul(argv[i] + 11, NULL, 10);
			} else {
				goto err;
			}
		} else if (!(strncmp(argv[i], "--num=", 6))) {
			if (strlen(argv[i]) > 6) {
				s.fetchto = strtoul(argv[i] + 11, NULL, 10);
			} else {
				goto err;
			}
		} else if (!(strncmp(argv[i], "--out=", 6))) {
			if (strlen(argv[i]) > 6) {
				s.fileout = argv[i] + 6;
			} else {
				goto err;
			}
		} else {
			goto err;
		}
	}

	if (!strncmp(argv[i], "--", 2)) {
		goto err;
	} else {
		s.filein = argv[i];
	}

	if (!s.fileout) {
		s.fileout = s.filein;
	}

	s.fetchto = s.fetchpos + s.fetchto;

	*settings = s;

	while (0) {
err:
		ret = EINVAL;
		printhelp(*argv);
	}

	return ret;
}

void printhelp(const char * const name)
{
	printf(	"This is a mergesort program and such\n"
		"\n"
		"usage:\t%s [options]\n FILE"
		"\n"
		"Options:\n"
		" --sort              sort mode (default)\n"
		" --fetch             fetch element\n"
		" --position=<num>    fetch element from position <num>\n"
		" --num=<num>         <num> of elements to fetch\n"
		" --out=<name>        write output to <name> file\n"
		"\n"
		"In case more than one same option is provided, the last one take precedence\n"
		"\n",
			name);
	return;
}