Skip to content

Commit 61f288f

Browse files
committed
Separate functions that depend on cJSON or hdf5 into separate headers.
`binsparse_all.h` includes everything.
1 parent 355caa3 commit 61f288f

File tree

9 files changed

+138
-39
lines changed

9 files changed

+138
-39
lines changed

examples/bsp-ls.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* SPDX-License-Identifier: BSD-3-Clause
55
*/
66

7-
#include <binsparse/binsparse.h>
7+
#include <binsparse/binsparse_all.h>
88

99
herr_t visit_group(hid_t loc_id, const char* name, const H5L_info_t* linfo,
1010
void* opdata);

examples/mtx2bsp.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* SPDX-License-Identifier: BSD-3-Clause
55
*/
66

7-
#include <binsparse/binsparse.h>
7+
#include <binsparse/binsparse_all.h>
88
#include <stdio.h>
99

1010
#include <time.h>
@@ -163,8 +163,8 @@ int main(int argc, char** argv) {
163163

164164
printf(" === Writing to %s... ===\n", output_fname);
165165
begin = gettime();
166-
BSP_CHECK(bsp_write_matrix(output_fname, matrix, group_name, user_json,
167-
compression_level));
166+
BSP_CHECK(bsp_write_matrix_cjson(output_fname, matrix, group_name, user_json,
167+
compression_level));
168168
end = gettime();
169169
duration = end - begin;
170170
printf("%lf seconds writing Binsparse file...\n", duration);

include/binsparse/binsparse_all.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Binsparse Developers
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <binsparse/binsparse.h>
10+
#include <binsparse/binsparse_cJSON.h>
11+
#include <binsparse/binsparse_hdf5.h>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Binsparse Developers
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <binsparse/detail/allocator.h>
10+
#include <binsparse/matrix.h>
11+
12+
#include <cJSON/cJSON.h>
13+
14+
#ifndef BSP_BINSPARSE_CJSON_H
15+
#define BSP_BINSPARSE_CJSON_H
16+
#endif
17+
18+
#ifdef __cplusplus
19+
extern "C" {
20+
#endif
21+
22+
bsp_error_t bsp_write_matrix_cjson(const char* fname, bsp_matrix_t matrix,
23+
const char* group, cJSON* user_json,
24+
int compression_level);
25+
26+
#ifdef BSP_BINSPARSE_HDF5_H
27+
bsp_error_t bsp_write_matrix_to_group_cjson(hid_t f, bsp_matrix_t matrix,
28+
cJSON* user_json,
29+
int compression_level);
30+
#endif
31+
32+
#ifdef __cplusplus
33+
}
34+
#endif

include/binsparse/binsparse_hdf5.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024 Binsparse Developers
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <binsparse/detail/allocator.h>
10+
#include <binsparse/matrix.h>
11+
12+
#include <hdf5.h>
13+
14+
#ifndef BSP_BINSPARSE_HDF5_H
15+
#define BSP_BINSPARSE_HDF5_H
16+
#endif
17+
18+
#ifdef __cplusplus
19+
extern "C" {
20+
#endif
21+
22+
#if __STDC_VERSION__ >= 201112L
23+
bsp_error_t bsp_read_matrix_from_group_parallel(bsp_matrix_t* matrix, hid_t f,
24+
int num_threads);
25+
#endif
26+
27+
bsp_error_t bsp_read_matrix_from_group(bsp_matrix_t* matrix, hid_t f);
28+
bsp_error_t bsp_read_matrix_from_group_allocator(bsp_matrix_t* matrix, hid_t f,
29+
bsp_allocator_t allocator);
30+
31+
bsp_error_t bsp_write_matrix_to_group(hid_t f, bsp_matrix_t matrix,
32+
const char* user_json,
33+
int compression_level);
34+
35+
#ifdef BSP_BINSPARSE_CJSON_H
36+
bsp_error_t bsp_write_matrix_to_group_cjson(hid_t f, bsp_matrix_t matrix,
37+
cJSON* user_json,
38+
int compression_level);
39+
#endif
40+
41+
#ifdef __cplusplus
42+
}
43+
#endif

include/binsparse/read_matrix.h

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,11 @@
66

77
#pragma once
88

9-
#ifdef __cplusplus
10-
extern "C" {
11-
#endif
12-
13-
#ifdef BSP_USE_HDF5
149
#include <binsparse/detail/allocator.h>
15-
#include <hdf5.h>
10+
#include <binsparse/matrix.h>
1611

17-
#if __STDC_VERSION__ >= 201112L
18-
bsp_error_t bsp_read_matrix_from_group_parallel(bsp_matrix_t* matrix, hid_t f,
19-
int num_threads);
20-
#endif
21-
22-
bsp_error_t bsp_read_matrix_from_group(bsp_matrix_t* matrix, hid_t f);
23-
bsp_error_t bsp_read_matrix_from_group_allocator(bsp_matrix_t* matrix, hid_t f,
24-
bsp_allocator_t allocator);
12+
#ifdef __cplusplus
13+
extern "C" {
2514
#endif
2615

2716
#if __STDC_VERSION__ >= 201112L

include/binsparse/write_matrix.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,9 @@ extern "C" {
1313
// TODO: make cJSON optional.
1414

1515
#include <binsparse/matrix.h>
16-
#include <cJSON/cJSON.h>
17-
18-
#ifdef BSP_USE_HDF5
19-
#include <hdf5.h>
20-
21-
bsp_error_t bsp_write_matrix_to_group(hid_t f, bsp_matrix_t matrix,
22-
cJSON* user_json, int compression_level);
23-
#endif
2416

2517
bsp_error_t bsp_write_matrix(const char* fname, bsp_matrix_t matrix,
26-
const char* group, cJSON* user_json,
18+
const char* group, const char* user_json,
2719
int compression_level);
2820

2921
#ifdef __cplusplus

src/read_matrix.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66

77
#include <assert.h>
8+
#include <binsparse/binsparse_cJSON.h>
9+
#include <binsparse/binsparse_hdf5.h>
810
#include <binsparse/detail/allocator.h>
911
#include <binsparse/hdf5_wrapper.h>
1012
#include <binsparse/matrix.h>
@@ -24,8 +26,9 @@ bsp_error_t bsp_read_matrix_from_group_parallel(bsp_matrix_t* matrix, hid_t f,
2426

2527
cJSON* j = cJSON_Parse(json_string);
2628

27-
assert(j != NULL);
28-
assert(cJSON_IsObject(j));
29+
if (j == NULL || !cJSON_IsObject(j)) {
30+
return BSP_ERROR_FORMAT;
31+
}
2932

3033
cJSON* binsparse = cJSON_GetObjectItemCaseSensitive(j, "binsparse");
3134
assert(cJSON_IsObject(binsparse));

src/write_matrix.c

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
#include <assert.h>
8-
#include <binsparse/binsparse.h>
8+
#include <binsparse/binsparse_all.h>
99
#include <binsparse/matrix.h>
1010
#include <cJSON/cJSON.h>
1111
#include <unistd.h>
@@ -87,8 +87,9 @@ char* bsp_generate_json(bsp_matrix_t matrix, cJSON* user_json) {
8787
return string;
8888
}
8989

90-
bsp_error_t bsp_write_matrix_to_group(hid_t f, bsp_matrix_t matrix,
91-
cJSON* user_json, int compression_level) {
90+
bsp_error_t bsp_write_matrix_to_group_cjson(hid_t f, bsp_matrix_t matrix,
91+
cJSON* user_json,
92+
int compression_level) {
9293
bsp_error_t error =
9394
bsp_write_array(f, (char*) "values", matrix.values, compression_level);
9495
if (error != BSP_SUCCESS) {
@@ -131,13 +132,26 @@ bsp_error_t bsp_write_matrix_to_group(hid_t f, bsp_matrix_t matrix,
131132
return BSP_SUCCESS;
132133
}
133134

134-
bsp_error_t bsp_write_matrix(const char* fname, bsp_matrix_t matrix,
135-
const char* group, cJSON* user_json,
136-
int compression_level) {
135+
bsp_error_t bsp_write_matrix_to_group(hid_t f, bsp_matrix_t matrix,
136+
const char* user_json,
137+
int compression_level) {
138+
cJSON* user_json_cjson = cJSON_Parse(user_json);
139+
if (user_json_cjson == NULL) {
140+
return BSP_ERROR_FORMAT;
141+
}
142+
bsp_error_t error = bsp_write_matrix_to_group_cjson(
143+
f, matrix, user_json_cjson, compression_level);
144+
cJSON_Delete(user_json_cjson);
145+
return error;
146+
}
147+
148+
bsp_error_t bsp_write_matrix_cjson(const char* fname, bsp_matrix_t matrix,
149+
const char* group, cJSON* user_json,
150+
int compression_level) {
137151
if (group == NULL) {
138152
hid_t f = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
139-
bsp_error_t error =
140-
bsp_write_matrix_to_group(f, matrix, user_json, compression_level);
153+
bsp_error_t error = bsp_write_matrix_to_group_cjson(f, matrix, user_json,
154+
compression_level);
141155
if (error != BSP_SUCCESS) {
142156
H5Fclose(f);
143157
return error;
@@ -151,8 +165,8 @@ bsp_error_t bsp_write_matrix(const char* fname, bsp_matrix_t matrix,
151165
f = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
152166
}
153167
hid_t g = H5Gcreate1(f, group, H5P_DEFAULT);
154-
bsp_error_t error =
155-
bsp_write_matrix_to_group(g, matrix, user_json, compression_level);
168+
bsp_error_t error = bsp_write_matrix_to_group_cjson(g, matrix, user_json,
169+
compression_level);
156170
if (error != BSP_SUCCESS) {
157171
H5Gclose(g);
158172
H5Fclose(f);
@@ -163,3 +177,16 @@ bsp_error_t bsp_write_matrix(const char* fname, bsp_matrix_t matrix,
163177
}
164178
return BSP_SUCCESS;
165179
}
180+
181+
bsp_error_t bsp_write_matrix(const char* fname, bsp_matrix_t matrix,
182+
const char* group, const char* user_json,
183+
int compression_level) {
184+
cJSON* user_json_cjson = cJSON_Parse(user_json);
185+
if (user_json_cjson == NULL) {
186+
return BSP_ERROR_FORMAT;
187+
}
188+
bsp_error_t error = bsp_write_matrix_cjson(
189+
fname, matrix, group, user_json_cjson, compression_level);
190+
cJSON_Delete(user_json_cjson);
191+
return error;
192+
}

0 commit comments

Comments
 (0)