mirror of
https://gitlab.com/80486DX2-66/gists
synced 2025-01-10 17:32:05 +05:30
freadln.*: implement reading lines from arbitrary file
This commit is contained in:
parent
7d223427b6
commit
e8d66db238
@ -10,13 +10,12 @@
|
|||||||
* TODO: Figure out potential problems
|
* TODO: Figure out potential problems
|
||||||
* TODO: Add 'flushing' of STDIN (while there are characters, read them) before
|
* TODO: Add 'flushing' of STDIN (while there are characters, read them) before
|
||||||
* the input reading loop to avoid input queues
|
* the input reading loop to avoid input queues
|
||||||
* TODO: Add the feature of reading from arbitrary file, then extract a macro:
|
* FIXME: Handle EOF in a special way to indicate EOF
|
||||||
* `#define finreadln(output, length_out) freadln(stdin, output, length_out)`
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "freadln.h"
|
#include "freadln.h"
|
||||||
|
|
||||||
int freadln(char** output, size_t* length_out) {
|
int freadln(FILE* f, char** output, size_t* length_out) {
|
||||||
/*
|
/*
|
||||||
* The length of STDIN line is counted without any terminating characters.
|
* The length of STDIN line is counted without any terminating characters.
|
||||||
*
|
*
|
||||||
@ -24,7 +23,7 @@ int freadln(char** output, size_t* length_out) {
|
|||||||
* freadln_OK: no errors, the length of STDIN line has been stored in
|
* freadln_OK: no errors, the length of STDIN line has been stored in
|
||||||
* `length_out`
|
* `length_out`
|
||||||
* freadln_ERROR: an error occurred (see errno)
|
* freadln_ERROR: an error occurred (see errno)
|
||||||
* >= 0: length of stdin line
|
* >= 0: length of read line
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (output == NULL)
|
if (output == NULL)
|
||||||
@ -41,7 +40,7 @@ int freadln(char** output, size_t* length_out) {
|
|||||||
return freadln_ERROR;
|
return freadln_ERROR;
|
||||||
|
|
||||||
int character;
|
int character;
|
||||||
while ((character = fgetc(stdin)) != EOF
|
while ((character = fgetc(f)) != EOF
|
||||||
/* stop on a newline character: */ && character != '\n') {
|
/* stop on a newline character: */ && character != '\n') {
|
||||||
(*output)[length] = (char) character;
|
(*output)[length] = (char) character;
|
||||||
|
|
||||||
@ -71,13 +70,40 @@ int freadln(char** output, size_t* length_out) {
|
|||||||
|
|
||||||
#ifdef TEST
|
#ifdef TEST
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
// stdin test
|
||||||
printf("Type something> ");
|
printf("Type something> ");
|
||||||
char* line;
|
char* line;
|
||||||
if (freadln(&line, NULL) != freadln_OK) {
|
if (finreadln(&line, NULL) != freadln_OK) {
|
||||||
perror("freadln");
|
perror("freadln");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
printf("Input string: '%s'\n", line);
|
printf("Input string: '%s'\n", line);
|
||||||
|
|
||||||
|
// file test
|
||||||
|
#define TEST_FILE "freadln_test.txt"
|
||||||
|
FILE* f = fopen(TEST_FILE, "w");
|
||||||
|
if (f == NULL) {
|
||||||
|
perror("fopen");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
fprintf(f, "Hello, world!\nAnother line\n\n");
|
||||||
|
FILE* new_f = freopen(TEST_FILE, "r", f);
|
||||||
|
if (new_f == NULL) {
|
||||||
|
perror("freopen");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
f = new_f;
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
size_t line_length;
|
||||||
|
if (freadln(f, &line, &line_length) != freadln_OK) {
|
||||||
|
perror("freadln");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
printf("File, line #%d: '%s' (%zu characters)\n", i + 1, line,
|
||||||
|
line_length);
|
||||||
|
}
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -26,6 +26,7 @@ enum freadln_status {
|
|||||||
return freadln_OK; \
|
return freadln_OK; \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
int freadln(char** output, size_t* length_out);
|
int freadln(FILE* f, char** output, size_t* length_out);
|
||||||
|
#define finreadln(output, length_out) freadln(stdin, output, length_out)
|
||||||
|
|
||||||
#endif /* _FREADLN_H */
|
#endif /* _FREADLN_H */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user