Skip to content

Commit c8808c2

Browse files
add get from io stream
1 parent a3ca67a commit c8808c2

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

code/logic/fossil/io/input.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,22 @@
1515
#define FOSSIL_IO_INPUT_H
1616

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

1920
#ifdef __cplusplus
2021
extern "C" {
2122
#endif
2223

24+
/**
25+
* Reads a line from the input stream and stores it into the buffer pointed to by 'buf'.
26+
*
27+
* @param buf Pointer to the buffer where the line will be stored.
28+
* @param size Maximum number of characters to be read, including the null terminator.
29+
* @param input_stream Pointer to the input stream to read from.
30+
* @return On success, the function returns 'buf'. If the end-of-file is reached or an error occurs, it returns NULL.
31+
*/
32+
char *fossil_io_gets_from_stream(char *buf, size_t size, FILE *input_stream);
33+
2334
/**
2435
* Reads a line from the input stream and stores it into the buffer pointed to by 'buf'.
2536
*

code/logic/input.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,16 @@
1515
#include "fossil/io/soap.h"
1616
#include "fossil/io/output.h"
1717

18-
#include <stdio.h>
1918
#include <stdlib.h>
2019
#include <string.h>
2120

22-
// Function to get a sanitized line of input
23-
char *fossil_io_gets(char *buf, size_t size) {
24-
if (buf == NULL || size == 0) {
21+
// Function to get a sanitized line of input from a provided stream (or stdin by default)
22+
char *fossil_io_gets_from_stream(char *buf, size_t size, FILE *input_stream) {
23+
if (buf == NULL || size == 0 || input_stream == NULL) {
2524
return NULL;
2625
}
2726

28-
if (fgets(buf, size, stdin) == NULL) {
27+
if (fgets(buf, size, input_stream) == NULL) {
2928
return NULL;
3029
}
3130

@@ -40,6 +39,11 @@ char *fossil_io_gets(char *buf, size_t size) {
4039
return buf;
4140
}
4241

42+
// Default version that uses stdin
43+
char *fossil_io_gets(char *buf, size_t size) {
44+
return fossil_io_gets_from_stream(buf, size, stdin);
45+
}
46+
4347
// Function to get a sanitized line of input with a dialog prompt
4448
char *fossil_io_gets_with_dialog(char *buf, size_t size, const char *dialog)
4549
{

0 commit comments

Comments
 (0)