Skip to content

Commit 5496ef0

Browse files
add C++ class for input
1 parent a51e4af commit 5496ef0

File tree

1 file changed

+114
-12
lines changed

1 file changed

+114
-12
lines changed

code/logic/fossil/io/input.h

Lines changed: 114 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define FOSSIL_IO_INPUT_H
1616

1717
#include <stddef.h>
18+
#include <stdarg.h>
1819
#include <stdio.h>
1920

2021
#ifdef __cplusplus
@@ -32,27 +33,128 @@ extern "C" {
3233
char *fossil_io_gets_from_stream(char *buf, size_t size, FILE *input_stream);
3334

3435
/**
35-
* Reads a line from the input stream and stores it into the buffer pointed to by 'buf'.
36+
* Reads a line from the input stream with error reporting.
37+
*
38+
* @param buf Pointer to the buffer where the line will be stored.
39+
* @param size Maximum number of characters to be read, including the null terminator.
40+
* @param input_stream Pointer to the input stream to read from.
41+
* @param error_code Pointer to an integer to store the error code (e.g., EOF, input error).
42+
* @return On success, the function returns 'buf'. If the end-of-file is reached or an error occurs, it returns NULL.
43+
*/
44+
char *fossil_io_gets_from_stream_ex(char *buf, size_t size, FILE *input_stream, int *error_code);
45+
46+
/**
47+
* Reads formatted input from the standard input stream.
3648
*
37-
* @param buf Pointer to the buffer where the line will be stored.
38-
* @param size Maximum number of characters to be read, including the null terminator.
39-
* @return On success, the function returns 'buf'. If the end-of-file is reached or an error occurs, it returns NULL.
49+
* @param format The format string specifying how the input should be interpreted.
50+
* @param ... Additional arguments for storing the input values.
51+
* @return On success, the number of input items successfully matched and assigned is returned.
52+
* On failure, EOF is returned.
4053
*/
41-
char *fossil_io_gets(char *buf, size_t size);
54+
int fossil_io_scanf(const char *format, ...);
4255

4356
/**
44-
* Reads a line from the input stream and stores it into the buffer pointed to by 'buf',
45-
* while displaying a dialog message to the user.
57+
* Reads formatted input from the specified input stream.
4658
*
47-
* @param buf Pointer to the buffer where the line will be stored.
48-
* @param size Maximum number of characters to be read, including the null terminator.
49-
* @param dialog Dialog message to be displayed to the user.
50-
* @return On success, the function returns 'buf'. If the end-of-file is reached or an error occurs, it returns NULL.
59+
* @param input_stream Pointer to the input stream to read from.
60+
* @param format The format string specifying how the input should be interpreted.
61+
* @param ... Additional arguments for storing the input values.
62+
* @return On success, the number of input items successfully matched and assigned is returned.
63+
* On failure, EOF is returned.
64+
*/
65+
int fossil_io_fscanf(FILE *input_stream, const char *format, ...);
66+
67+
/**
68+
* Validates the input buffer and size before reading.
69+
*
70+
* @param buf Pointer to the buffer where the input will be stored.
71+
* @param size Size of the buffer.
72+
* @return 1 if the buffer and size are valid; 0 otherwise.
5173
*/
52-
char *fossil_io_gets_with_dialog(char *buf, size_t size, const char *dialog);
74+
int fossil_io_validate_input_buffer(const char *buf, size_t size);
75+
76+
/**
77+
* Reads a UTF-8 encoded line from the input stream.
78+
*
79+
* @param buf Pointer to the buffer where the line will be stored.
80+
* @param size Maximum number of characters to be read, including the null terminator.
81+
* @param input_stream Pointer to the input stream to read from.
82+
* @return On success, the function returns 'buf'. If the end-of-file is reached or an error occurs, it returns NULL.
83+
*/
84+
char *fossil_io_gets_utf8(char *buf, size_t size, FILE *input_stream);
5385

5486
#ifdef __cplusplus
5587
}
88+
89+
/**
90+
* Namespace for the Fossil Logic I/O library.
91+
*/
92+
namespace fossil {
93+
94+
/**
95+
* Namespace for the I/O utilities.
96+
*/
97+
namespace io {
98+
99+
/**
100+
* Class for handling input operations.
101+
*/
102+
class Input {
103+
public:
104+
/**
105+
* Reads a line from the input stream and stores it into the buffer pointed to by 'buf'.
106+
*
107+
* @param buf Pointer to the buffer where the line will be stored.
108+
* @param size Maximum number of characters to be read, including the null terminator.
109+
* @param input_stream Pointer to the input stream to read from.
110+
* @return On success, the function returns 'buf'. If the end-of-file is reached or an error occurs, it returns NULL.
111+
*/
112+
static char *gets_from_stream(char *buf, size_t size, FILE *input_stream) {
113+
return fossil_io_gets_from_stream(buf, size, input_stream);
114+
}
115+
116+
/**
117+
* Reads a line from the input stream with error reporting.
118+
*
119+
* @param buf Pointer to the buffer where the line will be stored.
120+
* @param size Maximum number of characters to be read, including the null terminator.
121+
* @param input_stream Pointer to the input stream to read from.
122+
* @param error_code Pointer to an integer to store the error code (e.g., EOF, input error).
123+
* @return On success, the function returns 'buf'. If the end-of-file is reached or an error occurs, it returns NULL.
124+
*/
125+
static char *gets_from_stream_ex(char *buf, size_t size, FILE *input_stream, int *error_code) {
126+
return fossil_io_gets_from_stream_ex(buf, size, input_stream, error_code);
127+
}
128+
129+
/**
130+
* Validates the input buffer and size before reading.
131+
*
132+
* @param buf Pointer to the buffer where the input will be stored.
133+
* @param size Size of the buffer.
134+
* @return 1 if the buffer and size are valid; 0 otherwise.
135+
*/
136+
static int validate_input_buffer(const char *buf, size_t size) {
137+
return fossil_io_validate_input_buffer(buf, size);
138+
}
139+
140+
/**
141+
* Reads a UTF-8 encoded line from the input stream.
142+
*
143+
* @param buf Pointer to the buffer where the line will be stored.
144+
* @param size Maximum number of characters to be read, including the null terminator.
145+
* @param input_stream Pointer to the input stream to read from.
146+
* @return On success, the function returns 'buf'. If the end-of-file is reached or an error occurs, it returns NULL.
147+
*/
148+
static char *gets_utf8(char *buf, size_t size, FILE *input_stream) {
149+
return fossil_io_gets_utf8(buf, size, input_stream);
150+
}
151+
152+
};
153+
154+
}
155+
156+
}
157+
56158
#endif
57159

58160
#endif /* FOSSIL_IO_FRAMEWORK_H */

0 commit comments

Comments
 (0)