|
6 | 6 |
|
7 | 7 | #pragma once
|
8 | 8 |
|
9 |
| -#include <assert.h> |
10 |
| -#include <binsparse/matrix.h> |
11 |
| -#include <cJSON/cJSON.h> |
12 |
| -#include <unistd.h> |
13 |
| - |
14 |
| -char* bsp_generate_json(bsp_matrix_t matrix, cJSON* user_json) { |
15 |
| - cJSON* j = cJSON_CreateObject(); |
16 |
| - assert(j != NULL); |
17 |
| - |
18 |
| - cJSON* binsparse = cJSON_CreateObject(); |
19 |
| - |
20 |
| - assert(binsparse != NULL); |
21 |
| - |
22 |
| - cJSON_AddItemToObject(j, "binsparse", binsparse); |
23 |
| - |
24 |
| - cJSON* item; |
25 |
| - cJSON_ArrayForEach(item, user_json) { |
26 |
| - cJSON_AddItemToObject(j, item->string, item); |
27 |
| - } |
28 |
| - |
29 |
| - cJSON_AddStringToObject(binsparse, "version", BINSPARSE_VERSION); |
30 |
| - |
31 |
| - cJSON_AddStringToObject(binsparse, "format", |
32 |
| - bsp_get_matrix_format_string(matrix.format)); |
33 |
| - |
34 |
| - cJSON* shape = cJSON_AddArrayToObject(binsparse, "shape"); |
35 |
| - |
36 |
| - cJSON* nrows = cJSON_CreateNumber(matrix.nrows); |
37 |
| - cJSON* ncols = cJSON_CreateNumber(matrix.ncols); |
38 |
| - |
39 |
| - cJSON_AddItemToArray(shape, nrows); |
40 |
| - cJSON_AddItemToArray(shape, ncols); |
41 |
| - |
42 |
| - cJSON_AddNumberToObject(binsparse, "number_of_stored_values", matrix.nnz); |
43 |
| - |
44 |
| - cJSON* data_types = cJSON_AddObjectToObject(binsparse, "data_types"); |
45 |
| - |
46 |
| - if (!matrix.is_iso) { |
47 |
| - cJSON_AddStringToObject(data_types, "values", |
48 |
| - bsp_get_type_string(matrix.values.type)); |
49 |
| - } else { |
50 |
| - char* base_type_string = bsp_get_type_string(matrix.values.type); |
51 |
| - size_t len = strlen(base_type_string) + 6; |
52 |
| - char* type_string = (char*) malloc(sizeof(char) * len); |
53 |
| - |
54 |
| - strncpy(type_string, "iso[", len); |
55 |
| - strncpy(type_string + 4, base_type_string, len - 4); |
56 |
| - strncpy(type_string + len - 2, "]", 2); |
57 |
| - |
58 |
| - cJSON_AddStringToObject(data_types, "values", type_string); |
| 9 | +#ifdef __cplusplus |
| 10 | +extern "C" { |
| 11 | +#endif |
59 | 12 |
|
60 |
| - free(type_string); |
61 |
| - } |
| 13 | +// TODO: make cJSON optional. |
62 | 14 |
|
63 |
| - if (matrix.indices_0.data != NULL) { |
64 |
| - cJSON_AddStringToObject(data_types, "indices_0", |
65 |
| - bsp_get_type_string(matrix.indices_0.type)); |
66 |
| - } |
67 |
| - |
68 |
| - if (matrix.indices_1.data != NULL) { |
69 |
| - cJSON_AddStringToObject(data_types, "indices_1", |
70 |
| - bsp_get_type_string(matrix.indices_1.type)); |
71 |
| - } |
72 |
| - |
73 |
| - if (matrix.pointers_to_1.data != NULL) { |
74 |
| - cJSON_AddStringToObject(data_types, "pointers_to_1", |
75 |
| - bsp_get_type_string(matrix.pointers_to_1.type)); |
76 |
| - } |
77 |
| - |
78 |
| - if (matrix.structure != BSP_GENERAL) { |
79 |
| - cJSON_AddStringToObject(binsparse, "structure", |
80 |
| - bsp_get_structure_string(matrix.structure)); |
81 |
| - } |
82 |
| - |
83 |
| - char* string = cJSON_Print(j); |
84 |
| - |
85 |
| - cJSON_Delete(j); |
| 15 | +#include <binsparse/matrix.h> |
| 16 | +#include <cJSON/cJSON.h> |
86 | 17 |
|
87 |
| - return string; |
88 |
| -} |
| 18 | +#ifdef BSP_USE_HDF5 |
| 19 | +#include <hdf5.h> |
89 | 20 |
|
90 | 21 | int bsp_write_matrix_to_group(hid_t f, bsp_matrix_t matrix, cJSON* user_json,
|
91 |
| - int compression_level) { |
92 |
| - int result = |
93 |
| - bsp_write_array(f, (char*) "values", matrix.values, compression_level); |
94 |
| - |
95 |
| - if (result != 0) |
96 |
| - return result; |
97 |
| - |
98 |
| - if (matrix.indices_0.size > 0) { |
99 |
| - result = bsp_write_array(f, (char*) "indices_0", matrix.indices_0, |
100 |
| - compression_level); |
101 |
| - if (result != 0) { |
102 |
| - return result; |
103 |
| - } |
104 |
| - } |
105 |
| - |
106 |
| - if (matrix.indices_1.size > 0) { |
107 |
| - result = bsp_write_array(f, (char*) "indices_1", matrix.indices_1, |
108 |
| - compression_level); |
109 |
| - if (result != 0) { |
110 |
| - return result; |
111 |
| - } |
112 |
| - } |
113 |
| - |
114 |
| - if (matrix.pointers_to_1.size > 0) { |
115 |
| - result = bsp_write_array(f, (char*) "pointers_to_1", matrix.pointers_to_1, |
116 |
| - compression_level); |
117 |
| - if (result != 0) { |
118 |
| - return result; |
119 |
| - } |
120 |
| - } |
121 |
| - |
122 |
| - char* json_string = bsp_generate_json(matrix, user_json); |
123 |
| - |
124 |
| - bsp_write_attribute(f, (char*) "binsparse", json_string); |
125 |
| - free(json_string); |
126 |
| - |
127 |
| - return 0; |
128 |
| -} |
| 22 | + int compression_level); |
| 23 | +#endif |
129 | 24 |
|
130 | 25 | int bsp_write_matrix(const char* fname, bsp_matrix_t matrix, const char* group,
|
131 |
| - cJSON* user_json, int compression_level) { |
132 |
| - if (group == NULL) { |
133 |
| - hid_t f = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); |
134 |
| - bsp_write_matrix_to_group(f, matrix, user_json, compression_level); |
135 |
| - H5Fclose(f); |
136 |
| - } else { |
137 |
| - hid_t f; |
138 |
| - if (access(fname, F_OK) == 0) { |
139 |
| - f = H5Fopen(fname, H5F_ACC_RDWR, H5P_DEFAULT); |
140 |
| - } else { |
141 |
| - f = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); |
142 |
| - } |
143 |
| - hid_t g = H5Gcreate1(f, group, H5P_DEFAULT); |
144 |
| - bsp_write_matrix_to_group(g, matrix, user_json, compression_level); |
145 |
| - H5Gclose(g); |
146 |
| - H5Fclose(f); |
147 |
| - } |
148 |
| - return 0; |
| 26 | + cJSON* user_json, int compression_level); |
| 27 | + |
| 28 | +#ifdef __cplusplus |
149 | 29 | }
|
| 30 | +#endif |
0 commit comments