-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_input.c
More file actions
131 lines (116 loc) · 3.82 KB
/
parse_input.c
File metadata and controls
131 lines (116 loc) · 3.82 KB
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "function_normalizer.h"
#include "parser_utils.h"
#include "parse_input.h"
// Function to check argument count
bool check_arg_count(int argc) {
if (argc < 3 || argc > 4) {
printf("[DEBUG]: Incorrect number of arguments: %d\n", argc);
return false;
}
return true;
}
// Function to allocate and initialize input_params_t
input_params_t* allocate_params() {
input_params_t* params = malloc(sizeof(input_params_t));
if (!params) {
printf("[DEBUG]: Memory allocation error for params\n");
return NULL;
}
memset(params, 0, sizeof(input_params_t)); // Initialize all fields to zero
return params;
}
// Function to extract the mathematical function string
bool extract_function_param(input_params_t* params, const char* arg) {
if (params == NULL || arg == NULL) {
return false;
}
params->function_str = extract_function(arg);
if (!params->function_str) {
printf("[DEBUG]: Failed to extract function string from argv[1]\n");
return false;
}
printf("[DEBUG]: Extracted function: %s\n", params->function_str);
return true;
}
// Function to extract the output file name
bool extract_output_file_param(input_params_t* params, const char* arg) {
if (params == NULL || arg == NULL) {
return false;
}
params->output_file_str = extract_output_file(arg);
if (!params->output_file_str) {
printf("[DEBUG]: Failed to extract output file name from argv[2]\n");
return false;
}
printf("[DEBUG]: Extracted output file name: %s\n", params->output_file_str);
return true;
}
// Function to parse limits
bool parse_limits_param(input_params_t* params, const char* arg) {
if (params == NULL || arg == NULL) {
return false;
}
printf("[DEBUG]: Parsing limits from argv[3]: %s\n", arg);
if (!parse_limits(arg, ¶ms->x_min, ¶ms->x_max, ¶ms->y_min, ¶ms->y_max)) {
printf("[DEBUG]: Failed to parse limits\n");
return false;
}
printf("[DEBUG]: Successfully parsed limits: x_min=%f, x_max=%f, y_min=%f, y_max=%f\n",
params->x_min, params->x_max, params->y_min, params->y_max);
params->has_limits = true;
return true;
}
// Function to normalize the function string
bool normalize_function_param(input_params_t* params) {
if (params == NULL) {
return false;
}
char* normalized_function = remove_spaces(params->function_str);
if (!normalized_function) {
printf("[DEBUG]: Failed to normalize function string\n");
return false;
}
free(params->function_str);
params->function_str = normalized_function;
printf("[DEBUG]: Normalized function: %s\n", params->function_str);
return true;
}
// Function to validate the mathematical expression
bool validate_expression_param(input_params_t* params) {
if (params == NULL) {
return false;
}
if (!is_valid_expression(params->function_str)) {
printf("[DEBUG]: Function contains invalid characters or is incorrect\n");
return false;
}
printf("[DEBUG]: Function is valid\n");
return true;
}
// Function to check if limits are valid
bool check_limits_valid(input_params_t* params) {
if (params == NULL) {
return false;
}
if (params->x_min >= params->x_max || params->y_min >= params->y_max) {
printf("[DEBUG]: Limits are invalid\n");
return false;
}
printf("[DEBUG]: Limits are valid\n");
return true;
}
// Let it stay for now, maybe it will be needed
void free_input_params(input_params_t *params) {
if (params) {
if (params->function_str) {
free(params->function_str);
}
if (params->output_file_str) {
free(params->output_file_str);
}
free(params);
}
}