Skip to content

Commit 781f8ea

Browse files
authored
Merge pull request #599 from jeromekelleher/dumpload-fileobj
Dump/load from file objects
2 parents e5b38e4 + 0199fbb commit 781f8ea

File tree

21 files changed

+620
-59
lines changed

21 files changed

+620
-59
lines changed

c/CHANGELOG.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ In development.
5555
- Tables loaded from a file can now be edited in the same way as any other
5656
table collection (:user:`jeromekelleher`, :issue:`536`, :pr:`530`.
5757

58+
- Support for reading/writing to arbitrary file streams with the loadf/dumpf
59+
variants for tree sequence and table collection load/dump
60+
(:user:`jeromekelleher`, :user:`grahamgower`, :issue:`565`, :pr:`599`).
5861

5962
**Deprecated**
6063

c/examples/Makefile

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Simple Makefile for building examples.
2-
# This will build the examples in the current directory by compiling in the
2+
# This will build the examples in the current directory by compiling in the
33
# full tskit source into each of the examples. This is *not* recommended for
44
# real projects!
55
#
@@ -8,28 +8,31 @@
88
#
99
# **Note**: This repo uses git submodules, and these must be checked out
1010
# correctly for this makefile to work, e.g.:
11-
#
11+
#
1212
# $ git clone [email protected]:tskit-dev/tskit.git --recurse-submodules
13-
#
13+
#
1414
# See the documentation (https://tskit.readthedocs.io/en/stable/c-api.html)
15-
# for more details on how to use the C API, and the tskit build examples
16-
# repo (https://github.com/tskit-dev/tskit-build-examples) for examples
15+
# for more details on how to use the C API, and the tskit build examples
16+
# repo (https://github.com/tskit-dev/tskit-build-examples) for examples
1717
# of how to set up a production-ready build with tskit.
1818
#
1919

2020
CFLAGS=-I../ -I../subprojects/kastore
2121
TSKIT_SOURCE=../tskit/*.c ../subprojects/kastore/kastore.c
2222

23-
all: tree_iteration haploid_wright_fisher tree_traversal
23+
all: tree_iteration haploid_wright_fisher tree_traversal streaming
24+
25+
tree_iteration: tree_iteration.c
26+
${CC} ${CFLAGS} -o $@ $< ${TSKIT_SOURCE} -lm
2427

25-
tree_iteration: tree_iteration.c
28+
tree_traversal: tree_traversal.c
2629
${CC} ${CFLAGS} -o $@ $< ${TSKIT_SOURCE} -lm
2730

28-
tree_traversal: tree_traversal.c
31+
streaming: streaming.c
2932
${CC} ${CFLAGS} -o $@ $< ${TSKIT_SOURCE} -lm
3033

3134
# This needs GSL
32-
haploid_wright_fisher: haploid_wright_fisher.c
35+
haploid_wright_fisher: haploid_wright_fisher.c
3336
${CC} ${CFLAGS} -o $@ $< ${TSKIT_SOURCE} -lgsl -lgslcblas -lm
3437

3538
clean:

c/examples/streaming.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <tskit/tables.h>
4+
5+
#define check_tsk_error(val) \
6+
if (val < 0) { \
7+
fprintf(stderr, "Error: line %d: %s\n", __LINE__, tsk_strerror(val)); \
8+
exit(EXIT_FAILURE); \
9+
}
10+
11+
int
12+
main(int argc, char **argv)
13+
{
14+
int ret;
15+
int j = 0;
16+
tsk_table_collection_t tables;
17+
18+
ret = tsk_table_collection_init(&tables, 0);
19+
check_tsk_error(ret);
20+
21+
while (true) {
22+
ret = tsk_table_collection_loadf(&tables, stdin, TSK_NO_INIT);
23+
if (ret == TSK_ERR_EOF) {
24+
break;
25+
}
26+
check_tsk_error(ret);
27+
fprintf(stderr, "Tree sequence %d had %d mutations\n", j,
28+
(int) tables.mutations.num_rows);
29+
ret = tsk_mutation_table_truncate(&tables.mutations, 0);
30+
check_tsk_error(ret);
31+
ret = tsk_table_collection_dumpf(&tables, stdout, 0);
32+
check_tsk_error(ret);
33+
j++;
34+
}
35+
tsk_table_collection_free(&tables);
36+
return EXIT_SUCCESS;
37+
}

c/meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ if not meson.is_subproject()
9999
sources: ['examples/tree_iteration.c'], link_with: [tskit_lib], dependencies: lib_deps)
100100
executable('tree_traversal',
101101
sources: ['examples/tree_traversal.c'], link_with: [tskit_lib], dependencies: lib_deps)
102+
executable('streaming',
103+
sources: ['examples/streaming.c'], link_with: [tskit_lib], dependencies: lib_deps)
102104

103105
gsl_dep = dependency('gsl', required: false)
104106
if gsl_dep.found()

c/tests/meson-subproject/example.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ test_load_error()
5252
printf("test_open_error\n");
5353
tsk_treeseq_t ts;
5454
int ret = tsk_treeseq_load(&ts, "no such file", 0);
55-
assert(tsk_is_kas_error(ret));
55+
assert(ret == TSK_ERR_IO);
5656
tsk_treeseq_free(&ts);
5757
}
5858

c/tests/test_core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ test_strerror(void)
4747
static void
4848
test_strerror_kastore(void)
4949
{
50-
int kastore_errors[] = { KAS_ERR_NO_MEMORY, KAS_ERR_IO, KAS_ERR_KEY_NOT_FOUND };
50+
int kastore_errors[] = { KAS_ERR_NO_MEMORY, KAS_ERR_KEY_NOT_FOUND };
5151
size_t j;
5252
int err;
5353

0 commit comments

Comments
 (0)