Skip to content

Commit dc0cee3

Browse files
Merge pull request #14 from dreamer-coding/main
Prep for release 0.1.5
2 parents b51dc23 + c62d258 commit dc0cee3

File tree

6 files changed

+445
-24
lines changed

6 files changed

+445
-24
lines changed

code/logic/fossil/io/input.h

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,68 @@ int fossil_io_validate_input_buffer(const char *buf, size_t size);
8383
*/
8484
char *fossil_io_gets_utf8(char *buf, size_t size, FILE *input_stream);
8585

86+
/**
87+
* @brief Validates if the input string is a valid integer.
88+
*
89+
* @param input The input string to validate.
90+
* @param output Pointer to an integer where the parsed value will be stored if valid.
91+
* @return true if the input is a valid integer, false otherwise.
92+
*/
93+
int fossil_io_validate_is_int(const char *input, int *output);
94+
95+
/**
96+
* @brief Validates if the input string is a valid float.
97+
*
98+
* @param input The input string to validate.
99+
* @param output Pointer to a float where the parsed value will be stored if valid.
100+
* @return true if the input is a valid float, false otherwise.
101+
*/
102+
int fossil_io_validate_is_float(const char *input, float *output);
103+
104+
/**
105+
* @brief Validates if the input string contains only alphanumeric characters.
106+
*
107+
* @param input The input string to validate.
108+
* @return true if the input is alphanumeric, false otherwise.
109+
*/
110+
int fossil_io_validate_is_alnum(const char *input);
111+
112+
/**
113+
* @brief Validates if the input string is a valid email address.
114+
*
115+
* @param input The input string to validate.
116+
* @return true if the input is a valid email address, false otherwise.
117+
*/
118+
int fossil_io_validate_is_email(const char *input);
119+
120+
/**
121+
* @brief Validates if the input string does not exceed the specified maximum length.
122+
*
123+
* @param input The input string to validate.
124+
* @param max_length The maximum allowed length of the input string.
125+
* @return true if the input length is within the specified limit, false otherwise.
126+
*/
127+
int fossil_io_validate_is_length(const char *input, size_t max_length);
128+
129+
/**
130+
* @brief Sanitizes the input string and stores the sanitized result in the output buffer.
131+
*
132+
* @param input The input string to sanitize.
133+
* @param output The buffer where the sanitized string will be stored.
134+
* @param output_size The size of the output buffer.
135+
* @return A fossil_io_validate_error_t indicating the result of the sanitization process.
136+
*/
137+
int fossil_io_validate_sanitize_string(const char *input, char *output, size_t output_size);
138+
139+
/**
140+
* @brief Reads a secure line of input into the provided buffer.
141+
*
142+
* @param buffer The buffer where the input will be stored.
143+
* @param buffer_size The size of the buffer.
144+
* @return A fossil_io_validate_error_t indicating the result of the input reading process.
145+
*/
146+
int fossil_io_validate_read_secure_line(char *buffer, size_t buffer_size);
147+
86148
#ifdef __cplusplus
87149
}
88150

@@ -149,6 +211,115 @@ namespace fossil {
149211
return fossil_io_gets_utf8(buf, size, input_stream);
150212
}
151213

214+
/**
215+
* Reads formatted input from the standard input stream.
216+
*
217+
* @param format The format string specifying how the input should be interpreted.
218+
* @param ... Additional arguments for storing the input values.
219+
* @return On success, the number of input items successfully matched and assigned is returned.
220+
* On failure, EOF is returned.
221+
*/
222+
static int scanf(const char *format, ...) {
223+
va_list args;
224+
va_start(args, format);
225+
int result = fossil_io_scanf(format, args);
226+
va_end(args);
227+
return result;
228+
}
229+
230+
/**
231+
* Reads formatted input from the specified input stream.
232+
*
233+
* @param input_stream Pointer to the input stream to read from.
234+
* @param format The format string specifying how the input should be interpreted.
235+
* @param ... Additional arguments for storing the input values.
236+
* @return On success, the number of input items successfully matched and assigned is returned.
237+
* On failure, EOF is returned.
238+
*/
239+
static int fscanf(FILE *input_stream, const char *format, ...) {
240+
va_list args;
241+
va_start(args, format);
242+
int result = fossil_io_fscanf(input_stream, format, args);
243+
va_end(args);
244+
return result;
245+
}
246+
247+
/**
248+
* @brief Validates if the input string is a valid integer.
249+
*
250+
* @param input The input string to validate.
251+
* @param output Pointer to an integer where the parsed value will be stored if valid.
252+
* @return true if the input is a valid integer, false otherwise.
253+
*/
254+
static int validate_is_int(const char *input, int *output) {
255+
return fossil_io_validate_is_int(input, output);
256+
}
257+
258+
/**
259+
* @brief Validates if the input string is a valid float.
260+
*
261+
* @param input The input string to validate.
262+
* @param output Pointer to a float where the parsed value will be stored if valid.
263+
* @return true if the input is a valid float, false otherwise.
264+
*/
265+
static int validate_is_float(const char *input, float *output) {
266+
return fossil_io_validate_is_float(input, output);
267+
}
268+
269+
/**
270+
* @brief Validates if the input string contains only alphanumeric characters.
271+
*
272+
* @param input The input string to validate.
273+
* @return true if the input is alphanumeric, false otherwise.
274+
*/
275+
static int validate_is_alnum(const char *input) {
276+
return fossil_io_validate_is_alnum(input);
277+
}
278+
279+
/**
280+
* @brief Validates if the input string is a valid email address.
281+
*
282+
* @param input The input string to validate.
283+
* @return true if the input is a valid email address, false otherwise.
284+
*/
285+
static int validate_is_email(const char *input) {
286+
return fossil_io_validate_is_email(input);
287+
}
288+
289+
/**
290+
* @brief Validates if the input string does not exceed the specified maximum length.
291+
*
292+
* @param input The input string to validate.
293+
* @param max_length The maximum allowed length of the input string.
294+
* @return true if the input length is within the specified limit, false otherwise.
295+
*/
296+
static int validate_is_length(const char *input, size_t max_length) {
297+
return fossil_io_validate_is_length(input, max_length);
298+
}
299+
300+
/**
301+
* @brief Sanitizes the input string and stores the sanitized result in the output buffer.
302+
*
303+
* @param input The input string to sanitize.
304+
* @param output The buffer where the sanitized string will be stored.
305+
* @param output_size The size of the output buffer.
306+
* @return A fossil_io_validate_error_t indicating the result of the sanitization process.
307+
*/
308+
static int validate_sanitize_string(const char *input, char *output, size_t output_size) {
309+
return fossil_io_validate_sanitize_string(input, output, output_size);
310+
}
311+
312+
/**
313+
* @brief Reads a secure line of input into the provided buffer.
314+
*
315+
* @param buffer The buffer where the input will be stored.
316+
* @param buffer_size The size of the buffer.
317+
* @return A fossil_io_validate_error_t indicating the result of the input reading process.
318+
*/
319+
static int validate_read_secure_line(char *buffer, size_t buffer_size) {
320+
return fossil_io_validate_read_secure_line(buffer, buffer_size);
321+
}
322+
152323
};
153324

154325
}

code/logic/input.c

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,19 @@
1515
#include "fossil/io/soap.h"
1616
#include "fossil/io/output.h"
1717

18-
#include <stdlib.h>
18+
#include <ctype.h>
19+
#include <stdio.h>
1920
#include <string.h>
21+
#include <stdlib.h>
22+
#include <stdarg.h>
23+
#include <limits.h>
24+
25+
#ifdef __WIN32
26+
#include <windows.h>
27+
#else
28+
#include <termios.h>
29+
#include <unistd.h>
30+
#endif
2031

2132
// Function to trim leading and trailing spaces from a string
2233
void fossil_io_trim(char *str) {
@@ -155,3 +166,111 @@ char *fossil_io_gets_utf8(char *buf, size_t size, FILE *input_stream) {
155166

156167
return buf;
157168
}
169+
170+
int fossil_io_validate_is_int(const char *input, int *output) {
171+
if (input == NULL || output == NULL) {
172+
return 0;
173+
}
174+
175+
char *endptr;
176+
long value = strtol(input, &endptr, 10);
177+
178+
if (*endptr != '\0' || value < INT_MIN || value > INT_MAX) {
179+
return 0;
180+
}
181+
182+
*output = (int)value;
183+
return 1;
184+
}
185+
186+
int fossil_io_validate_is_float(const char *input, float *output) {
187+
if (input == NULL || output == NULL) {
188+
return 0;
189+
}
190+
191+
char *endptr;
192+
float value = strtof(input, &endptr);
193+
194+
if (*endptr != '\0') {
195+
return 0;
196+
}
197+
198+
*output = value;
199+
return 1;
200+
}
201+
202+
int fossil_io_validate_is_alnum(const char *input) {
203+
if (input == NULL) {
204+
return 0;
205+
}
206+
207+
while (*input != '\0') {
208+
if (!isalnum(*input)) {
209+
return 0;
210+
}
211+
input++;
212+
}
213+
214+
return 1;
215+
}
216+
217+
int fossil_io_validate_is_email(const char *input) {
218+
if (input == NULL) {
219+
return 0;
220+
}
221+
222+
// Check for the presence of an '@' character
223+
const char *at = strchr(input, '@');
224+
if (at == NULL) {
225+
return 0;
226+
}
227+
228+
// Check for the presence of a '.' character after the '@' character
229+
const char *dot = strchr(at, '.');
230+
if (dot == NULL) {
231+
return 0;
232+
}
233+
234+
return 1;
235+
}
236+
237+
int fossil_io_validate_is_length(const char *input, size_t max_length) {
238+
if (input == NULL) {
239+
return 0;
240+
}
241+
242+
return strlen(input) <= max_length;
243+
}
244+
245+
int fossil_io_validate_sanitize_string(const char *input, char *output, size_t output_size) {
246+
if (input == NULL || output == NULL || output_size == 0) {
247+
return 0;
248+
}
249+
250+
// Copy the input string to the output buffer
251+
strncpy(output, input, output_size);
252+
253+
// Sanitize the output buffer
254+
fossil_soap_sanitize(output);
255+
256+
return 1;
257+
}
258+
259+
int fossil_io_validate_read_secure_line(char *buffer, size_t buffer_size) {
260+
if (buffer == NULL || buffer_size == 0) {
261+
return 0;
262+
}
263+
264+
// Read a line of input from the user
265+
if (fgets(buffer, buffer_size, stdin) == NULL) {
266+
return 0;
267+
}
268+
269+
// Remove the newline character from the input
270+
size_t len = strlen(buffer);
271+
if (len > 0 && buffer[len - 1] == '\n') {
272+
buffer[len - 1] = '\0';
273+
}
274+
275+
return 1;
276+
}

0 commit comments

Comments
 (0)