commit dc07691019816bdee2981422f11a7dedb438ca10
parent 2a2f1f4212456297668b588ab3dc02375ecf12c5
Author: vaplv <vaplv@free.fr>
Date: Sat, 11 Jan 2020 17:00:47 +0100
Add the txtrdr_set_line_num text_reader function
The line num now represents the number of read lines rather than the
index of the line.
Diffstat:
3 files changed, 23 insertions(+), 7 deletions(-)
diff --git a/src/test_text_reader.c b/src/test_text_reader.c
@@ -49,15 +49,17 @@ check_text_reader(struct txtrdr* txtrdr)
while(strcspn(text[iline], "#\n") == strspn(text[iline], " \t")) ++iline;
CHK(!strncmp(txtrdr_get_line(txtrdr), text[iline], strcspn(text[iline], "#\n")));
- CHK(txtrdr_get_line_num(txtrdr) == iline);
+ CHK(txtrdr_get_line_num(txtrdr) == iline+1);
CHK(txtrdr_read_line(txtrdr) == RES_OK);
++iline;
}
- CHK(txtrdr_get_line_num(txtrdr) == nlines-1);
+ CHK(txtrdr_get_line_num(txtrdr) == nlines);
CHK(txtrdr_read_line(txtrdr) == RES_OK);
CHK(txtrdr_get_line(txtrdr) == NULL);
- CHK(txtrdr_get_line_num(txtrdr) == nlines-1);
+ CHK(txtrdr_get_line_num(txtrdr) == nlines);
CHK(txtrdr_read_line(txtrdr) == RES_OK);
+ txtrdr_set_line_num(txtrdr, nlines/2);
+ CHK(txtrdr_get_line_num(txtrdr) == nlines/2);
}
int
diff --git a/src/text_reader.c b/src/text_reader.c
@@ -26,7 +26,7 @@
struct txtrdr {
FILE* stream; /* Stream of the text to read */
struct str name; /* Stream name */
- size_t iline; /* Line index currently read */
+ size_t nlines; /* # read lines */
struct darray_char line; /* Buffer storing the read line */
/* String of chars from which the remaining line chars are skipped */
@@ -82,7 +82,7 @@ txtrdr_stream
darray_char_init(allocator, &txtrdr->line);
txtrdr->allocator = allocator;
txtrdr->stream = stream;
- txtrdr->iline = (size_t)(-1);
+ txtrdr->nlines = 0;
txtrdr->reject[0] = comment;
txtrdr->reject[1] = '\n';
txtrdr->reject[2] = '\0'; /* Finalize the string */
@@ -188,7 +188,7 @@ txtrdr_read_line(struct txtrdr* txtrdr)
str = darray_char_data_get(&txtrdr->line);
str[strcspn(str, txtrdr->reject)] = '\0';
- ++txtrdr->iline; /* Increment the line index */
+ ++txtrdr->nlines; /* Increment the number of read lines */
} while(strspn(str, " \t") == strlen(str));/*Keep going if the line is Empty*/
@@ -218,7 +218,14 @@ size_t
txtrdr_get_line_num(const struct txtrdr* txtrdr)
{
ASSERT(txtrdr);
- return txtrdr->iline;
+ return txtrdr->nlines;
+}
+
+void
+txtrdr_set_line_num(struct txtrdr* txtrdr, const size_t nlines)
+{
+ ASSERT(txtrdr);
+ txtrdr->nlines = nlines;
}
const char*
diff --git a/src/text_reader.h b/src/text_reader.h
@@ -60,10 +60,17 @@ RSYS_API const char*
txtrdr_get_cline
(const struct txtrdr* txtrdr);
+/* Return the number of read lines */
RSYS_API size_t
txtrdr_get_line_num
(const struct txtrdr* txtrdr);
+/* Overwrite the internal line number */
+RSYS_API void
+txtrdr_set_line_num
+ (struct txtrdr* txtrdr,
+ const size_t nlines);
+
RSYS_API const char*
txtrdr_get_name
(const struct txtrdr* txtrdr);