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- }
0 commit comments