test: update b_stream usage

This commit is contained in:
2025-10-24 12:51:54 +01:00
parent a956e21fc4
commit e499d8631f
12 changed files with 57 additions and 74 deletions

View File

@@ -82,7 +82,7 @@ int main(int argc, const char **argv)
printf("Done\n"); printf("Done\n");
b_cstream_close(cstream); b_cstream_close(cstream);
b_stream_close(out_stream); b_stream_unref(out_stream);
fclose(out_fp); fclose(out_fp);
return 0; return 0;

View File

@@ -76,7 +76,7 @@ int main(int argc, const char **argv)
printf("Done\n"); printf("Done\n");
b_cstream_close(cstream); b_cstream_close(cstream);
b_stream_close(in_stream); b_stream_unref(in_stream);
fclose(in_fp); fclose(in_fp);
return 0; return 0;

View File

@@ -43,7 +43,7 @@ static int compress(const b_compression_function *func, FILE *in, FILE *out)
b_cstream_end_compressed_section(cstream, NULL, NULL); b_cstream_end_compressed_section(cstream, NULL, NULL);
b_cstream_close(cstream); b_cstream_close(cstream);
b_stream_close(out_stream); b_stream_unref(out_stream);
return 0; return 0;
} }
@@ -86,7 +86,7 @@ static int decompress(const b_compression_function *func, FILE *in, FILE *out)
b_cstream_end_compressed_section(cstream, NULL, NULL); b_cstream_end_compressed_section(cstream, NULL, NULL);
b_cstream_close(cstream); b_cstream_close(cstream);
b_stream_close(in_stream); b_stream_unref(in_stream);
return 0; return 0;
} }

View File

@@ -153,39 +153,33 @@ void test_queue_iterate(CuTest *tc)
void test_stringstream_1(CuTest *tc) void test_stringstream_1(CuTest *tc)
{ {
char buf[1024]; char buf[1024];
b_stringstream s; b_stringstream *s = b_stringstream_create_with_buffer(buf, sizeof buf);
b_stringstream_begin(&s, buf, sizeof buf);
b_stringstream_add(&s, "hello"); b_stream_write_string(s, "hello", NULL);
b_stringstream_addf(&s, "(%d + %.1f)", 32, 2.3); b_stream_write_fmt(s, NULL, "(%d + %.1f)", 32, 2.3);
const char *x[] = {"ABC", "DEF", NULL}; char *end = b_stringstream_steal(s);
b_stringstream_unref(s);
b_stringstream_addv(&s, x); CuAssertStrEquals(tc, "hello(32 + 2.3)", end);
b_stringstream_addvl(&s, x, 2);
b_stringstream_add_many(&s, "more", "more", "more", NULL);
const char *end = b_stringstream_end(&s);
CuAssertStrEquals(tc, "hello(32 + 2.3)ABCDEFABCDEFmoremoremore", end);
} }
void test_stringstream_2(CuTest *tc) void test_stringstream_2(CuTest *tc)
{ {
char buf[1024]; char buf[1024];
b_stringstream s; b_stringstream *s = b_stringstream_create_with_buffer(buf, sizeof buf);
b_stringstream_begin(&s, buf, sizeof buf);
b_stringstream_add(&s, "{\n"); b_stream_write_string(s, "{\n", NULL);
b_stringstream_push_indent(&s, 1); b_stream_push_indent(s, 1);
b_stringstream_add(&s, "a = 32,\n"); b_stream_write_string(s, "a = 32,\n", NULL);
b_stringstream_add(&s, "b = 64\n"); b_stream_write_string(s, "b = 64\n", NULL);
b_stringstream_pop_indent(&s); b_stream_pop_indent(s);
b_stringstream_add(&s, "}"); b_stream_write_string(s, "}", NULL);
char *str = b_stringstream_end(&s); char *str = b_stringstream_steal(s);
b_stringstream_unref(s);
CuAssertStrEquals(tc, "{\n a = 32,\n b = 64\n}", str); CuAssertStrEquals(tc, "{\n a = 32,\n b = 64\n}", str);
} }

View File

@@ -1,18 +1,19 @@
#include <blue/core/stream.h> #include <blue/core/stream.h>
#include <blue/core/stringstream.h>
#include <blue/ds/string.h> #include <blue/ds/string.h>
#include <stdio.h> #include <stdio.h>
int main(int argc, const char **argv) int main(int argc, const char **argv)
{ {
size_t nr_read = 0; size_t nr_read = 0;
b_string *str = b_string_create(); b_stringstream *dest_stream = b_stringstream_create();
b_stream *dest_stream; b_stream_buffer *buf = b_stream_buffer_create_dynamic(1024);
b_stream_pipeline *pipeline; b_stream_read_all_bytes_s(b_stdin, dest_stream, buf, &nr_read);
b_status status = b_string_open_stream(str, &dest_stream);
b_stream_pipeline_create(1024, &pipeline);
b_stream_read_all_bytes_s(b_stdin, dest_stream, pipeline, &nr_read);
printf("done. read %zu bytes total.\n", nr_read); printf("done. read %zu bytes total.\n", nr_read);
printf("%s\n", b_string_ptr(str)); printf("%s\n", b_stringstream_ptr(dest_stream));
b_stringstream_unref(dest_stream);
b_stream_buffer_unref(buf);
return 0; return 0;
} }

View File

@@ -28,10 +28,13 @@ int main(void)
b_string_unref(str); b_string_unref(str);
b_stringstream strv; b_stringstream *strv = b_stringstream_create();
b_stringstream_begin_dynamic(&strv); b_stream_write_string(strv, "Hello", NULL);
b_stringstream_add_many(&strv, "Hello", ", world", "!", NULL); b_stream_write_string(strv, ", world", NULL);
char *s = b_stringstream_end(&strv); b_stream_write_string(strv, "!", NULL);
char *s = b_stringstream_steal(strv);
b_stringstream_unref(strv);
printf("%s\n", s); printf("%s\n", s);
free(s); free(s);

View File

@@ -7,10 +7,9 @@ void test_path_1(CuTest *tc)
{ {
b_path *path = b_path_create_from_cstr("C:\\hello\\world\\"); b_path *path = b_path_create_from_cstr("C:\\hello\\world\\");
char buf[512]; char buf[512];
b_stringstream str; b_stringstream *str = b_stringstream_create_with_buffer(buf, sizeof buf);
b_stringstream_begin(&str, buf, sizeof buf);
b_object_to_string(path, (b_stream *)&str); b_object_to_string(path, str);
printf("%s\n", buf); printf("%s\n", buf);
@@ -21,8 +20,8 @@ void test_path_1(CuTest *tc)
b_path *path4 = b_path_join(paths, sizeof paths / sizeof paths[0]); b_path *path4 = b_path_join(paths, sizeof paths / sizeof paths[0]);
b_stringstream_begin(&str, buf, sizeof buf); b_stringstream_reset_with_buffer(str, buf, sizeof buf);
b_object_to_string(path4, (b_stream *)&str); b_object_to_string(path4, str);
printf("%s\n", buf); printf("%s\n", buf);
} }

View File

@@ -15,11 +15,8 @@ int main(int argc, const char **argv)
} }
size_t nr_read = 0; size_t nr_read = 0;
b_stream *dest_stream; b_stream_buffer *buf = b_stream_buffer_create_dynamic(1024);
b_stream_pipeline *pipeline; b_stream_read_all_bytes_s(b_stdin, dest, buf, &nr_read);
b_status status = b_file_open_stream(dest, &dest_stream);
b_stream_pipeline_create(1024, &pipeline);
b_stream_read_all_bytes_s(b_stdin, dest_stream, pipeline, &nr_read);
printf("done. read %zu bytes total.\n", nr_read); printf("done. read %zu bytes total.\n", nr_read);
return 0; return 0;

View File

@@ -1,7 +1,8 @@
#include <blue/core/stream.h> #include <blue/core/stream.h>
#include <blue/core/stringstream.h>
#include <blue/ds/string.h>
#include <blue/io/file.h> #include <blue/io/file.h>
#include <blue/io/path.h> #include <blue/io/path.h>
#include <blue/ds/string.h>
#include <stdio.h> #include <stdio.h>
int main(int argc, const char **argv) int main(int argc, const char **argv)
@@ -13,25 +14,19 @@ int main(int argc, const char **argv)
const char *path_cstr = argv[1]; const char *path_cstr = argv[1];
b_path *path = b_path_create_from_cstr(path_cstr); b_path *path = b_path_create_from_cstr(path_cstr);
b_file *file = NULL; b_file *src = NULL;
b_result result = b_file_open(NULL, path, B_FILE_READ_ONLY, &file); b_result result = b_file_open(NULL, path, B_FILE_READ_ONLY, &src);
if (b_result_is_error(result)) { if (b_result_is_error(result)) {
b_throw(result); b_throw(result);
return -1; return -1;
} }
b_stream *src_stream, *dest_stream;
b_file_open_stream(file, &src_stream); b_stream_buffer *buf = b_stream_buffer_create_dynamic(1024);
b_string *str = b_string_create();
b_stream_pipeline *pipeline;
b_status status = b_string_open_stream(str, &dest_stream);
b_stream_pipeline_create(1024, &pipeline);
size_t nr_read; size_t nr_read;
b_stream_read_all_bytes_s(src_stream, dest_stream, pipeline, &nr_read); b_stream_read_all_bytes_s(src, b_stdout, buf, &nr_read);
b_stream_buffer_unref(buf);
printf("%s\n", b_string_ptr(str));
return 0; return 0;
} }

View File

@@ -12,23 +12,19 @@ int main(int argc, const char **argv)
const char *path_cstr = argv[1]; const char *path_cstr = argv[1];
b_path *path = b_path_create_from_cstr(path_cstr); b_path *path = b_path_create_from_cstr(path_cstr);
b_file *file = NULL; b_file *src = NULL;
b_result result = b_file_open(NULL, path, B_FILE_READ_ONLY, &file); b_result result = b_file_open(NULL, path, B_FILE_READ_ONLY, &src);
if (b_result_is_error(result)) { if (b_result_is_error(result)) {
b_throw(result); b_throw(result);
return -1; return -1;
} }
b_stream *src_stream;
b_file_open_stream(file, &src_stream);
b_serial_ctx *ctx; b_serial_ctx *ctx;
b_serial_ctx_create(&ctx); b_serial_ctx_create(&ctx);
b_object *data; b_object *data;
b_status status = b_serial_ctx_deserialise( b_status status = b_serial_ctx_deserialise(
ctx, B_SERIAL_FORMAT_JSON, src_stream, &data, 0); ctx, B_SERIAL_FORMAT_JSON, src, &data, 0);
if (!B_OK(status)) { if (!B_OK(status)) {
fprintf(stderr, "cannot read data\n"); fprintf(stderr, "cannot read data\n");
return -1; return -1;
@@ -38,6 +34,7 @@ int main(int argc, const char **argv)
b_stream_write_char(b_stdout, '\n'); b_stream_write_char(b_stdout, '\n');
b_object_unref(data); b_object_unref(data);
b_object_unref(src);
b_serial_ctx_destroy(ctx); b_serial_ctx_destroy(ctx);
return 0; return 0;

View File

@@ -1 +1 @@
tab = 32 k = "v"

View File

@@ -12,23 +12,19 @@ int main(int argc, const char **argv)
const char *path_cstr = argv[1]; const char *path_cstr = argv[1];
b_path *path = b_path_create_from_cstr(path_cstr); b_path *path = b_path_create_from_cstr(path_cstr);
b_file *file = NULL; b_file *src = NULL;
b_result result = b_file_open(NULL, path, B_FILE_READ_ONLY, &file); b_result result = b_file_open(NULL, path, B_FILE_READ_ONLY, &src);
if (b_result_is_error(result)) { if (b_result_is_error(result)) {
b_throw(result); b_throw(result);
return -1; return -1;
} }
b_stream *src_stream;
b_file_open_stream(file, &src_stream);
b_serial_ctx *ctx; b_serial_ctx *ctx;
b_serial_ctx_create(&ctx); b_serial_ctx_create(&ctx);
b_object *data = NULL; b_object *data = NULL;
b_status status = b_serial_ctx_deserialise( b_status status = b_serial_ctx_deserialise(
ctx, B_SERIAL_FORMAT_TOML, src_stream, &data, 0); ctx, B_SERIAL_FORMAT_TOML, src, &data, 0);
if (!B_OK(status)) { if (!B_OK(status)) {
fprintf(stderr, "cannot read data\n"); fprintf(stderr, "cannot read data\n");
return -1; return -1;
@@ -42,6 +38,7 @@ int main(int argc, const char **argv)
b_stream_write_char(b_stdout, '\n'); b_stream_write_char(b_stdout, '\n');
b_object_unref(data); b_object_unref(data);
b_object_unref(src);
b_serial_ctx_destroy(ctx); b_serial_ctx_destroy(ctx);
return 0; return 0;