Skip to content

Commit 2ee7215

Browse files
committed
Restructured functionality in anticipation of testing.
1 parent 46ede3d commit 2ee7215

9 files changed

Lines changed: 444 additions & 432 deletions

File tree

src/config.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include "config.h"
2+
3+
#include <stdlib.h>
4+
5+
#include "flags.h"
6+
#include "log.h"
7+
8+
// Fills the fields of a given Config struct with default values.
9+
void initialise_config(struct Config *cfg) {
10+
cfg->file_path = DEFAULT_FILE_PATH;
11+
cfg->new_file_name = DEFAULT_NEW_FILE_NAME;
12+
cfg->exclude_headers = DEFAULT_EXCLUDE_HEADERS;
13+
cfg->include_remainders = DEFAULT_INCLUDE_REMAINDERS;
14+
cfg->delimiter = DEFAULT_DELIMITER;
15+
cfg->line_count = DEFAULT_LINE_COUNT;
16+
cfg->remove_columns_l = 0;
17+
cfg->remove_columns = NULL;
18+
}
19+
20+
// Parses the given command line arguments into the given config struct.
21+
void parse_config(struct Config *cfg, const int argc, const char **argv) {
22+
LOG("Parsing config.\n");
23+
for (size_t i = 1; i < argc;) {
24+
i = parse_arg(cfg, argc, argv, i);
25+
}
26+
if (!cfg->file_path) {
27+
fprintf(HELP_OUT, "Expected input file. Use --help to learn more.\n");
28+
exit(PARSE_ERR);
29+
}
30+
}
31+
32+
// Prints a string to stderr and adds spaces to the right such that the total
33+
// length is len.
34+
static void print_right_padded(const char *str, size_t len) {
35+
fprintf(HELP_OUT, "%s", str);
36+
for (size_t i = 0; i < len - strlen(str); i++) {
37+
putc(' ', HELP_OUT);
38+
}
39+
}
40+
41+
// Prints a given Flag struct as required by the --help flag.
42+
static void print_option(struct Flag *f, size_t max) {
43+
fprintf(HELP_OUT, "\t-%c, --%s ", f->short_id, f->long_id);
44+
print_right_padded(f->arg, max - strlen(f->long_id));
45+
fprintf(HELP_OUT, "\t%s\n", f->desc);
46+
}
47+
48+
// Prints useful information about the program as required by the --help flag.
49+
void print_help(void) {
50+
fprintf(HELP_OUT, "%s\n\n", TITLE);
51+
fprintf(HELP_OUT, "USAGE:\n\t%s\n\n", USAGE);
52+
fprintf(HELP_OUT, "OPTIONS:\n");
53+
size_t max = max_flag_length();
54+
for (int i = 0; i < FLAG_COUNT; i++) {
55+
print_option(FLAGS + i, max);
56+
}
57+
}

src/config.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef CONFIG_H
2+
#define CONFIG_H
3+
4+
#include <stdio.h>
5+
#include <string.h>
6+
7+
struct Config {
8+
/* Strings */
9+
const char *file_path;
10+
const char *new_file_name;
11+
/* Booleans */
12+
char exclude_headers;
13+
char include_remainders;
14+
/* Other values */
15+
char delimiter;
16+
size_t line_count;
17+
size_t remove_columns_l;
18+
char **remove_columns;
19+
};
20+
21+
// Function to assign default values to cfg fields.
22+
void initialise_config(struct Config *cfg);
23+
24+
// Function to parse given command line arguments to cfg fields.
25+
void parse_config(struct Config *cfg, const int argc, const char **argv);
26+
27+
void print_help(void);
28+
29+
#endif

src/cli.c renamed to src/flags.c

Lines changed: 32 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,24 @@
1-
#include "cli.h"
1+
#include "flags.h"
22

33
#include <stdio.h>
44
#include <stdlib.h>
55
#include <string.h>
66

7-
// Fills the fields of a given Config struct with default values.
8-
void initialise_config(struct Config *cfg) {
9-
cfg->file_path = DEFAULT_FILE_PATH;
10-
cfg->new_file_name = DEFAULT_NEW_FILE_NAME;
11-
cfg->exclude_headers = DEFAULT_EXCLUDE_HEADERS;
12-
cfg->include_remainders = DEFAULT_INCLUDE_REMAINDERS;
13-
cfg->delimiter = DEFAULT_DELIMITER;
14-
cfg->line_count = DEFAULT_LINE_COUNT;
15-
cfg->remove_columns_l = 0;
16-
cfg->remove_columns = NULL;
17-
}
7+
#include "config.h"
8+
#include "log.h"
189

1910
// Returns 1 iff the arguments first character is a '-'
20-
int is_flag(const char *arg) {
11+
static int is_flag(const char *arg) {
2112
return arg[0] == '-';
2213
}
2314

2415
// Returns 1 iff the arguments second character is a '-'
25-
int is_long_flag(const char *arg) {
16+
static int is_long_flag(const char *arg) {
2617
return arg[1] == '-';
2718
}
2819

2920
// Returns 1 iff the argument only contains numerical digits.
30-
int is_natural(const char *arg) {
21+
static char is_natural(const char *arg) {
3122
size_t i = 0;
3223
while (arg[i]) {
3324
if (arg[i] < 48 || arg[i] > 57) {
@@ -40,7 +31,7 @@ int is_natural(const char *arg) {
4031

4132
// Given a path to a file, the columns the user wants to exclude are parsed and
4233
// saved in the config struct.
43-
int parse_remove_columns(struct Config *cfg, const char *file) {
34+
static int parse_remove_columns(struct Config *cfg, const char *file) {
4435
ERR_LOG("Removing columns has not yet been implemented.\n");
4536
return 0;
4637
}
@@ -52,12 +43,8 @@ int parse_remove_columns(struct Config *cfg, const char *file) {
5243
// found.
5344
//
5445
// Exits if a PARSE_ERR is encountered.
55-
size_t parse_flag_by_index(
56-
struct Config *cfg,
57-
const int argc,
58-
const char **argv,
59-
size_t at,
60-
size_t flag) {
46+
static size_t parse_flag_by_index(
47+
struct Config *cfg, const int argc, const char **argv, size_t at, size_t flag) {
6148
const char *arg;
6249
switch (flag) {
6350
case NEW_FILE_NAME:
@@ -86,8 +73,7 @@ size_t parse_flag_by_index(
8673
LOG("Found line count: %lu\n", cfg->line_count);
8774
return at + 2; // Read extra argument.
8875
case DELIMITER:
89-
if (at + 1 == argc ||
90-
strlen(argv[at + 1]) > 1) { // Expected single character.
76+
if (at + 1 == argc || strlen(argv[at + 1]) > 1) { // Expected single character.
9177
ERR_LOG("Expected delimiter. Use --help to learn more.\n");
9278
exit(PARSE_ERR);
9379
}
@@ -118,7 +104,7 @@ size_t parse_flag_by_index(
118104
// found.
119105
//
120106
// Exits if a PARSE_ERR is encountered.
121-
int parse_short_flag(
107+
static int parse_short_flag(
122108
struct Config *cfg, const int argc, const char **argv, size_t at) {
123109
size_t subflag = 1; // First character is -, so first flag char is at 1.
124110
size_t new_at;
@@ -152,7 +138,7 @@ int parse_short_flag(
152138
// found.
153139
//
154140
// Exits if a PARSE_ERR is encountered.
155-
size_t parse_long_flag(
141+
static size_t parse_long_flag(
156142
struct Config *cfg, const int argc, const char **argv, size_t at) {
157143
for (struct Flag *f = FLAGS; f < FLAGS + FLAG_COUNT; f++) {
158144
if (!strcmp(argv[at] + 2, f->long_id)) {
@@ -163,14 +149,32 @@ size_t parse_long_flag(
163149
exit(PARSE_ERR);
164150
}
165151

152+
// Given a Flag, this function returns the length this flag will occupy in the help output
153+
// just by its long style flag and the corresponding argument(s)
154+
static size_t flag_length(struct Flag *f) {
155+
return 3 + strlen(f->long_id) + strlen(f->arg);
156+
}
157+
158+
// Finds the maximum length that any flag requires for printing its long-id and
159+
// arg.
160+
size_t max_flag_length() {
161+
size_t max = flag_length(FLAGS);
162+
size_t cur;
163+
for (struct Flag *p = FLAGS; p < FLAGS + FLAG_COUNT; p++) {
164+
if ((cur = flag_length(p)) > max) {
165+
cur = max;
166+
}
167+
}
168+
return max;
169+
}
170+
166171
// Parses the next argument in the argument array.
167172
//
168173
// Returns the index of the argument array where the next argument will be
169174
// found.
170175
//
171176
// Exits if a PARSE_ERR is encountered.
172-
int parse_arg(
173-
struct Config *cfg, const int argc, const char **argv, size_t at) {
177+
int parse_arg(struct Config *cfg, const int argc, const char **argv, size_t at) {
174178
LOG("Parsing argument at %lu\n", at);
175179
if (at < 1 || at >= argc) {
176180
return argc;
@@ -187,61 +191,3 @@ int parse_arg(
187191
LOG("Found long style flag\n");
188192
return parse_long_flag(cfg, argc, argv, at);
189193
}
190-
191-
size_t flag_length(struct Flag *f) {
192-
return 3 + strlen(f->long_id) + strlen(f->arg);
193-
}
194-
195-
// Finds the maximum length that any flag requires for printing its long-id and
196-
// arg.
197-
size_t max_flag_length() {
198-
size_t max = flag_length(FLAGS);
199-
size_t cur;
200-
for (struct Flag *p = FLAGS; p < FLAGS + FLAG_COUNT; p++) {
201-
if ((cur = flag_length(p)) > max) {
202-
cur = max;
203-
}
204-
}
205-
return max;
206-
}
207-
208-
#define HELP_OUT stderr
209-
210-
// Prints a string to stderr and adds spaces to the right such that the total
211-
// length is len.
212-
void print_right_padded(const char *str, size_t len) {
213-
fprintf(HELP_OUT, "%s", str);
214-
for (size_t i = 0; i < len - strlen(str); i++) {
215-
putc(' ', HELP_OUT);
216-
}
217-
}
218-
219-
// Prints a given Flag struct as required by the --help flag.
220-
void print_option(struct Flag *f, size_t max) {
221-
fprintf(HELP_OUT, "\t-%c, --%s ", f->short_id, f->long_id);
222-
print_right_padded(f->arg, max - strlen(f->long_id));
223-
fprintf(HELP_OUT, "\t%s\n", f->desc);
224-
}
225-
226-
// Prints useful information about the program as required by the --help flag.
227-
void print_help(void) {
228-
fprintf(HELP_OUT, "%s\n\n", TITLE);
229-
fprintf(HELP_OUT, "USAGE:\n\t%s\n\n", USAGE);
230-
fprintf(HELP_OUT, "OPTIONS:\n");
231-
size_t max = max_flag_length();
232-
for (int i = 0; i < FLAG_COUNT; i++) {
233-
print_option(FLAGS + i, max);
234-
}
235-
}
236-
237-
// Parses the given command line arguments into the given config struct.
238-
void parse_config(struct Config *cfg, const int argc, const char **argv) {
239-
LOG("Parsing config.\n");
240-
for (size_t i = 1; i < argc;) {
241-
i = parse_arg(cfg, argc, argv, i);
242-
}
243-
if (!cfg->file_path) {
244-
fprintf(HELP_OUT, "Expected input file. Use --help to learn more.\n");
245-
exit(PARSE_ERR);
246-
}
247-
}

src/cli.h renamed to src/flags.h

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,25 @@
1+
#ifndef FLAGS_H
2+
#define FLAGS_H
13

2-
#include <stdlib.h>
4+
#include <stdio.h>
35

4-
#include "log.h"
5-
6-
#ifndef CLI_H
7-
#define CLI_H
8-
9-
#define ARG_ERR -1
10-
#define PARSE_ERR -2
11-
12-
#define DEFAULT_FILE_PATH NULL
13-
#define DEFAULT_NEW_FILE_NAME "split"
14-
#define DEFAULT_EXCLUDE_HEADERS 0
15-
#define DEFAULT_LINE_COUNT 1
16-
#define DEFAULT_DELIMITER ','
17-
#define DEFAULT_INCLUDE_REMAINDERS 0
18-
19-
struct Config {
20-
/* Strings */
21-
const char *file_path;
22-
const char *new_file_name;
23-
/* Boleans */
24-
char exclude_headers;
25-
char include_remainders;
26-
/* Other values */
27-
char delimiter;
28-
size_t line_count;
29-
size_t remove_columns_l;
30-
char **remove_columns;
31-
};
6+
#include "config.h"
327

8+
// Represents an input option for the CLI.
9+
//
10+
// Has
11+
// - SHORT: a single character flag
12+
// - LONG: a verbose string flag
13+
// - ARG: a string containing the arguments required as printed by --help
14+
// - DESC: a description of what this does as printed by --help
3315
struct Flag {
3416
const char short_id;
3517
const char *long_id;
3618
const char *arg;
3719
const char *desc;
3820
};
3921

22+
// Different indices to access the corresponding Flag in the FLAGS array.
4023
enum CLI_FLAG_TYPE {
4124
NEW_FILE_NAME = 0,
4225
EXCLUDE_HEADERS,
@@ -47,6 +30,13 @@ enum CLI_FLAG_TYPE {
4730
HELP
4831
};
4932

33+
#define DEFAULT_FILE_PATH NULL
34+
#define DEFAULT_NEW_FILE_NAME "split"
35+
#define DEFAULT_EXCLUDE_HEADERS 0
36+
#define DEFAULT_LINE_COUNT 1
37+
#define DEFAULT_DELIMITER ','
38+
#define DEFAULT_INCLUDE_REMAINDERS 0
39+
5040
#define FLAG(s, l, a, d) \
5141
{ s, l, a, d }
5242

@@ -63,11 +53,7 @@ enum CLI_FLAG_TYPE {
6353
"number (default: \"split\")")
6454

6555
#define FLAG_EXCLUDE_HEADERS \
66-
FLAG( \
67-
'e', \
68-
"exclude-headers", \
69-
"", \
70-
"Exclude headers in new files (default: false)")
56+
FLAG('e', "exclude-headers", "", "Exclude headers in new files (default: false)")
7157

7258
#define FLAG_LINE_COUNT \
7359
FLAG('l', "line-count", "<COUNT>", "Number of lines per file (default: 1)")
@@ -105,13 +91,8 @@ static struct Flag FLAGS[] = {FLAG_NEW_FILE_NAME,
10591
FLAG_INCLUDE_REMAINDERS,
10692
FLAG_HELP};
10793

108-
// Function to assign default values to cfg fields.
109-
void initialise_config(struct Config *cfg);
110-
111-
// Function to parse given command line arguments to cfg fields.
112-
void parse_config(struct Config *cfg, const int argc, const char **argv);
94+
int parse_arg(struct Config *config, const int argc, const char **argv, size_t at);
11395

114-
// Prints helpful information about this program
115-
void print_help(void);
96+
size_t max_flag_length();
11697

11798
#endif

src/log.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
#include <stdio.h>
55

6+
#define ARG_ERR -1
7+
#define PARSE_ERR -2
8+
#define HELP_OUT stderr
69
#define ERR_LOG(FMT, ...) fprintf(stderr, FMT, ##__VA_ARGS__)
710

811
#ifdef DEBUG

0 commit comments

Comments
 (0)