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