Skip to content

Commit 68695b1

Browse files
committed
feat(io): Initial implementation
Implement `io_readline` and `io_fill` operations and introduce IO API extension to the library.
1 parent 74aac44 commit 68695b1

5 files changed

Lines changed: 115 additions & 2 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ message(VERBOSE "YAVL: C symbol namespace: ${YAVL_C_NAMESPACE}")
5050

5151
## 2. List of auto-generated headers
5252
set("${PROJECT_NAME}_GEN_HEADERS"
53-
"vec.h")
53+
"vec.h" "io.h")
5454

5555
# Target list
5656
# ===========
@@ -126,7 +126,7 @@ endif()
126126

127127
# Header generation and inclusion to API
128128
# ======================================
129-
foreach(header "${${PROJECT_NAME}_GEN_HEADERS}")
129+
foreach(header IN LISTS "${PROJECT_NAME}_GEN_HEADERS")
130130
configure_file(
131131
"${CMAKE_CURRENT_SOURCE_DIR}/template/include/YAVL/${header}.in"
132132
"${${PROJECT_NAME}_GEN_HEADERS_OUTPUT_DIRECTORY}/YAVL/${header}"

src/io.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* io.c - I/O API based on vector for dynamic allocation
3+
*
4+
* SPDX-License: GPL-3.0-or-later
5+
*
6+
* Copyright (C) 2026 Dawid Papiewski
7+
* This program is free software: you can redistribute it and/or modify it
8+
* under the terms of the GNU General Public License as published by the
9+
* Free Software Foundation, either version 3 of the License, or (at your option)
10+
* any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14+
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15+
* You should have received a copy of the GNU General Public License along with this
16+
* program. If not, see <https://www.gnu.org/licenses/>.
17+
*
18+
*/
19+
#include <stdio.h>
20+
#include <YAVL/vec.h>
21+
#include <YAVL/io.h>
22+
#include "namespace.h"
23+
24+
NS(vec_res_t) NS(io_fromstream)(NS(vec_t) *const vec, FILE *const stream) {
25+
if(vec->allign != sizeof(char)) return NS_UPPER(VEC_RES_FAIL);
26+
for(
27+
size_t cr=1;
28+
cr!=0;
29+
cr=fread(
30+
/*__ptr=*/ vec->data+(vec->len*vec->allign),
31+
/*__size=*/ vec->allign,
32+
/*__n=*/ vec->reservd-vec->len,
33+
/*__stream=*/ stream
34+
)
35+
) {
36+
vec->len += cr;
37+
// Autoscale
38+
NS(vec_res_t) res;
39+
if(vec->reservd <= vec->len) res = NS(vec_scale)(vec, vec->reservd*2);
40+
if(res != NS_UPPER(VEC_RES_OK)) return res;
41+
}
42+
NS(vec_fit)(vec);
43+
return NS_UPPER(VEC_RES_OK);
44+
}
45+
46+
NS(vec_res_t) NS(io_readline)(NS(vec_t) *const vec, FILE *const stream) {
47+
if(vec->allign != sizeof(char)) return NS_UPPER(VEC_RES_FAIL);
48+
for(
49+
int ch=0;
50+
ch!='\n'||ch==EOF;
51+
ch=getc(stream)
52+
) {
53+
char c = ch; NS(vec_res_t) res;
54+
if((res = NS(vec_push)(vec,&c,1)) != NS_UPPER(VEC_RES_OK))
55+
return res;
56+
}
57+
const char trail[] = "\n";
58+
NS(vec_push)(vec, trail, sizeof(trail)/sizeof(trail[0]));
59+
return NS(vec_fit)(vec);
60+
}

src/vec.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/*
2+
* vec.c - Core vector API
3+
*
24
* SPDX-License: GPL-3.0-or-later
35
*
46
* Copyright (C) 2026 Dawid Papiewski

template/include/YAVL/io.h.in

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/** @file
2+
*
3+
* I/O API extension
4+
*
5+
* SPDX-License: GPL-3.0-or-later
6+
*
7+
* @author Dawid Papiewski
8+
* @copyright (C) 2026 Dawid Papiewski
9+
*
10+
* This program is free software: you can redistribute it and/or modify it
11+
* under the terms of the GNU General Public License as published by the
12+
* Free Software Foundation, either version 3 of the License, or (at your option)
13+
* any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful, but WITHOUT
16+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17+
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18+
* You should have received a copy of the GNU General Public License along with this
19+
* program. If not, see <https://www.gnu.org/licenses/>.
20+
*/
21+
#pragma once
22+
#ifndef @YAVL_C_NAMESPACE_UPPER_@IO_H
23+
#define @YAVL_C_NAMESPACE_UPPER_@IO_H
24+
#pragma clang final(@YAVL_C_NAMESPACE_UPPER_@IO_H)
25+
#include "vec.h"
26+
/** @defgroup IO I/O API
27+
*
28+
* @brief Stream read and buffering API,
29+
* made for parsers in mind.
30+
*
31+
* @{
32+
*/
33+
34+
/**
35+
* Read from stream until finding '\n'
36+
*
37+
* @param vec Vector to push buffer into.
38+
* @param stream Stream to read line from.
39+
*/
40+
@YAVL_C_NAMESPACE_@vec_res_t @YAVL_C_NAMESPACE_@io_readline(@YAVL_C_NAMESPACE_@vec_t *const vec, FILE *const stream);
41+
/**
42+
* Buffer entire stream into vector.
43+
*
44+
* @param vec Vector to buffer stream into.
45+
* @param stream Stream to read data from.
46+
*/
47+
@YAVL_C_NAMESPACE_@vec_res_t @YAVL_C_NAMESPACE_@io_fill(@YAVL_C_NAMESPACE_@vec_t *const vec, FILE *const stream);
48+
49+
/** @} */ // IO
50+
#endif

template/include/YAVL/vec.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* program. If not, see <https://www.gnu.org/licenses/>.
2020
*
2121
*/
22+
#pragma once
2223
#ifndef @YAVL_C_NAMESPACE_UPPER_@VEC_H
2324
#define @YAVL_C_NAMESPACE_UPPER_@VEC_H
2425
#pragma clang final(@YAVL_C_NAMESPACE_UPPER_@VEC_H)

0 commit comments

Comments
 (0)