-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_utils.c
More file actions
166 lines (137 loc) · 4.72 KB
/
parser_utils.c
File metadata and controls
166 lines (137 loc) · 4.72 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "parser_utils.h"
#include "defs.h"
// Helper Function to Remove Trailing Zeros from Fractional Part
void remove_trailing_zeros(char* num_str) {
char* decimal_ptr = strchr(num_str, DECIMAL_POINT);
if (decimal_ptr == NULL) {
return; // No decimal point, nothing to do
}
char* end_ptr = num_str + strlen(num_str) - 1;
// Remove trailing zeros
while (end_ptr > decimal_ptr && *end_ptr == '0') {
*end_ptr = END_STRING_CHAR;
end_ptr--;
}
// If the last character is a decimal point, remove it
if (end_ptr == decimal_ptr) {
*end_ptr = END_STRING_CHAR;
}
}
char* extract_function(const char* function_str) {
if (function_str == NULL) {
return NULL;
}
char* function_copy = strdup(function_str);
if (function_copy == NULL) {
return NULL;
}
return function_copy;
}
// Implementation of filename validation
bool is_valid_filename(const char* filename) {
if (filename == NULL || strlen(filename) == 0) {
return false;
}
const char* invalid_chars = "<>:\"/\\|?*";
for (size_t i = 0; i < strlen(filename); i++) {
if (strchr(invalid_chars, filename[i]) != NULL) {
return false;
}
}
return true;
}
// Refactored extract_output_file function
char* extract_output_file(const char* output_file_str) {
// Check if the output file string is NULL or empty
if (output_file_str == NULL || strlen(output_file_str) == 0) {
printf("[DEBUG]: Output file string is NULL or empty.\n");
return NULL;
}
// Validate the filename
if (!is_valid_filename(output_file_str)) {
printf("[DEBUG]: Output file name contains invalid characters: %s\n", output_file_str);
return NULL;
}
// Attempt to open the file for writing to verify creation and write permissions
FILE* file = fopen(output_file_str, "w");
if (file == NULL) {
perror("[DEBUG]: Failed to create or open the output file"); // Use perror for more detailed error
return NULL;
}
// Successfully opened the file, now close it
fclose(file);
// Duplicate the file string to store in params
char* output_file_copy = strdup(output_file_str);
if (output_file_copy == NULL) {
printf("[DEBUG]: Memory allocation error for output_file_copy.\n");
return NULL;
}
return output_file_copy;
}
bool parse_limits(const char* limits_str, double* x_min, double* x_max, double* y_min, double* y_max) {
if (limits_str == NULL || x_min == NULL || x_max == NULL || y_min == NULL || y_max == NULL) {
return false;
}
char* limits_copy = strdup(limits_str);
if (limits_copy == NULL) {
return false;
}
char* token;
char* rest = limits_copy;
token = strtok(rest, DELIMITER);
if (token == NULL || sscanf(token, "%lf", x_min) != 1) {
free(limits_copy);
return false;
}
token = strtok(NULL, DELIMITER);
if (token == NULL || sscanf(token, "%lf", x_max) != 1) {
free(limits_copy);
return false;
}
token = strtok(NULL, DELIMITER);
if (token == NULL || sscanf(token, "%lf", y_min) != 1) {
free(limits_copy);
return false;
}
token = strtok(NULL, DELIMITER);
if (token == NULL || sscanf(token, "%lf", y_max) != 1) {
free(limits_copy);
return false;
}
if (strtok(NULL, DELIMITER) != NULL) {
free(limits_copy);
return false;
}
free(limits_copy);
return true;
}
// Function to set default limits
void set_default_limits(input_params_t* params) {
if (params) {
printf("[DEBUG]: Setting default limits\n");
params->x_min = DEFAULT_MIN;
params->x_max = DEFAULT_MAX;
params->y_min = DEFAULT_MIN;
params->y_max = DEFAULT_MAX;
params->has_limits = false;
}
}
// Check if a string is a supported function
bool is_supported_function(const char* func) {
if (func == NULL) {
return false;
}
return strcmp(func, FUNC_ABS) == 0 || strcmp(func, FUNC_EXP) == 0 || strcmp(func, FUNC_LN) == 0 ||
strcmp(func, FUNC_LOG) == 0 || strcmp(func, FUNC_SIN) == 0 || strcmp(func, FUNC_COS) == 0 ||
strcmp(func, FUNC_TAN) == 0 || strcmp(func, FUNC_ASIN) == 0 || strcmp(func, FUNC_ACOS) == 0 ||
strcmp(func, FUNC_ATAN) == 0 || strcmp(func, FUNC_SINH) == 0 || strcmp(func, FUNC_COSH) == 0 ||
strcmp(func, FUNC_TANH) == 0;
}
// Checking if a symbol is a binary operator
bool is_binary_operator(char op) {
return op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_MULTIPLY || op == OPERATOR_DIVIDE || op == OPERATOR_POWER;
}