-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram_init.c
154 lines (123 loc) · 4.04 KB
/
program_init.c
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* Name: program_init.c
*
* Purpose: Perform initial checks and initialization to get required data
* from data file and set up the program's data structure for the
* budget.
*
* Author: jjones4
*
* Copyright (c) 2024 Jerad Jones
*
* This file is part of c_budget. c_budget may be freely distributed under the
* MIT license. For all details and documentation, see:
*
* https://github.com/jjones4/c_budget
*/
#include <stdlib.h>
#include <string.h>
#include "program_init.h"
#include "return_codes.h"
Bool arg_count_within_range(int argument_count);
Bool string_length_within_range(const char *s, int maximum_string_length);
Bool is_default_data_file_used(int argument_count);
Bool is_valid_data_file_name_length(Bool default_data_file_used,
char **arg_vector);
void get_data_file_name(Bool default_data_file_used, char *default_data_file,
char **arg_vector);
/*
* Return 0 to main function if all initializations and checks succeed. Return
* applicable error code, otherwise (see return_codes.h).
*/
int program_init(int argument_count, char **argument_vector,
char **data_file_name) {
char *data_file = NULL;
/*
* Ensure number of command-line arguments given does not exceed maximum
* number of allowable arguments.
*/
if(arg_count_within_range(argument_count) == FALSE) {
printf("\nUp to one argument allowed after program name.\n");
return ARGUMENT_ERROR;
}
data_file = malloc(FILE_NAME_LENGTH + 1);
if(data_file == NULL) {
printf("\nMemory could not be allocated.\n");
return MEMORY_ERROR;
}
/*
* Make sure the data file name length is within the valid range. Based on
* the number of command-line arguments provided, it is determined whether
* the user is entering a name for the data file or is using the default
* data file.
*/
if(is_valid_data_file_name_length(is_default_data_file_used(argument_count),
argument_vector) == FALSE) {
printf("\nData file name exceeds maximum number of %d",
FILE_NAME_LENGTH);
printf(" allowable characters.\n");
return FILE_ERROR;
}
/*
* If get_data_file_name is successful, data_file will point to the data
* file name, which can be used to open the file later. Based on the number
* of command-line arguments provided, it is determined whether the user is
* entering a name for the data file or is using the default data file.
*/
(void) get_data_file_name(is_default_data_file_used(argument_count), data_file,
argument_vector);
(void) strncpy(*data_file_name, data_file, FILE_NAME_LENGTH - 1);
free(data_file);
return 0;
}
Bool is_default_data_file_used(int argument_count) {
if(argument_count > 1) {
return FALSE;
}
else {
return TRUE;
}
}
Bool arg_count_within_range(int argument_count) {
if(argument_count > MAX_ARG_COUNT || argument_count < MIN_ARG_COUNT) {
return FALSE;
}
else {
return TRUE;
}
}
Bool string_length_within_range(const char *s, int maximum_string_length)
{
int i = 0;
while(*s) {
s++;
i++;
if(i > maximum_string_length) {
return FALSE;
}
}
return TRUE;
}
Bool is_valid_data_file_name_length(Bool default_data_file_used,
char **arg_vector) {
if(default_data_file_used == TRUE) {
if(!string_length_within_range(DEFAULT_DATA_FILE, FILE_NAME_LENGTH)) {
return FALSE;
}
}
else {
if(!string_length_within_range(*++arg_vector, FILE_NAME_LENGTH)) {
return FALSE;
}
}
return TRUE;
}
void get_data_file_name(Bool default_data_file_used, char *data_file_name,
char **arg_vector) {
if(default_data_file_used == TRUE) {
(void) strncpy(data_file_name, DEFAULT_DATA_FILE, FILE_NAME_LENGTH -1);
}
else {
(void) strncpy(data_file_name, *++arg_vector, FILE_NAME_LENGTH - 1);
}
}