-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbsp-ls.c
More file actions
117 lines (84 loc) · 2.98 KB
/
bsp-ls.c
File metadata and controls
117 lines (84 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* SPDX-FileCopyrightText: 2024 Binsparse Developers
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <binsparse/binsparse_all.h>
herr_t visit_group(hid_t loc_id, const char* name, const H5L_info_t* linfo,
void* opdata);
void print_group_info(hid_t g, const char* name) {
hid_t bsp_json;
H5E_BEGIN_TRY {
bsp_json = H5Aopen(g, "binsparse", H5P_DEFAULT);
}
H5E_END_TRY;
if (bsp_json != H5I_INVALID_HID) {
char* json_string;
bsp_error_t error = bsp_read_attribute(&json_string, g, "binsparse");
if (error != BSP_SUCCESS) {
printf("Error reading binsparse attribute: %s\n",
bsp_get_error_string(error));
return;
}
cJSON* j = cJSON_Parse(json_string);
assert(j != NULL);
assert(cJSON_IsObject(j));
cJSON* binsparse = cJSON_GetObjectItemCaseSensitive(j, "binsparse");
assert(cJSON_IsObject(binsparse));
cJSON* version_ = cJSON_GetObjectItemCaseSensitive(binsparse, "version");
assert(version_ != NULL);
assert(cJSON_IsString(version_));
char* version_string = cJSON_GetStringValue(version_);
// TODO: check version.
cJSON* format_ = cJSON_GetObjectItemCaseSensitive(binsparse, "format");
assert(format_ != NULL);
char* format_string = cJSON_GetStringValue(format_);
cJSON* nnz_ =
cJSON_GetObjectItemCaseSensitive(binsparse, "number_of_stored_values");
assert(nnz_ != NULL);
size_t nnz = cJSON_GetNumberValue(nnz_);
cJSON* shape_ = cJSON_GetObjectItemCaseSensitive(binsparse, "shape");
assert(shape_ != NULL);
assert(cJSON_GetArraySize(shape_) == 2);
cJSON* nrows_ = cJSON_GetArrayItem(shape_, 0);
assert(nrows_ != NULL);
size_t nrows = cJSON_GetNumberValue(nrows_);
cJSON* ncols_ = cJSON_GetArrayItem(shape_, 1);
assert(ncols_ != NULL);
size_t ncols = cJSON_GetNumberValue(ncols_);
char full_group_path[2048];
size_t size = H5Iget_name(g, full_group_path, 2048);
printf("Group \"%s\": Version %s Binsparse matrix. Format %s, %zu x %zu. "
"%zu stored values.\n",
full_group_path, version_string, format_string, nrows, ncols, nnz);
cJSON* data_types =
cJSON_GetObjectItemCaseSensitive(binsparse, "data_types");
assert(data_types != NULL);
cJSON* item;
cJSON_ArrayForEach(item, data_types) {
printf(" %s: %s\n", item->string, cJSON_Print(item));
}
}
H5Literate(g, H5_INDEX_NAME, H5_ITER_INC, NULL, visit_group, NULL);
}
herr_t visit_group(hid_t loc_id, const char* name, const H5L_info_t* linfo,
void* opdata) {
hid_t g = H5Oopen(loc_id, name, H5P_DEFAULT);
H5I_type_t type = H5Iget_type(g);
if (type == H5I_GROUP) {
print_group_info(g, name);
}
H5Oclose(g);
return 0;
}
int main(int argc, char** argv) {
if (argc < 2) {
printf("usage: ./bsp-ls [file_name.mtx]\n");
return 1;
}
char* fname = argv[1];
hid_t f = H5Fopen(fname, H5F_ACC_RDONLY, H5P_DEFAULT);
print_group_info(f, "/");
H5Fclose(f);
return 0;
}