Skip to content

Commit cd37ef7

Browse files
committed
Implement bsp-ls, which prints out a summary of Binsparse matrices in
a file.
1 parent 3939e16 commit cd37ef7

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ add_example(simple_write)
1010
add_example(mtx2bsp)
1111
add_example(bsp2mtx)
1212
add_example(check_equivalence)
13+
add_example(bsp-ls)

examples/bsp-ls.c

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <binsparse/binsparse.h>
2+
3+
herr_t visit_group(hid_t loc_id, const char* name, const H5L_info_t* linfo,
4+
void* opdata);
5+
6+
void print_group_info(hid_t g, const char* name) {
7+
hid_t bsp_json;
8+
9+
H5E_BEGIN_TRY {
10+
bsp_json = H5Aopen(g, "binsparse", H5P_DEFAULT);
11+
}
12+
H5E_END_TRY;
13+
14+
if (bsp_json != H5I_INVALID_HID) {
15+
char* json_string = bsp_read_attribute(g, "binsparse");
16+
17+
cJSON* j = cJSON_Parse(json_string);
18+
19+
assert(j != NULL);
20+
assert(cJSON_IsObject(j));
21+
22+
cJSON* binsparse = cJSON_GetObjectItemCaseSensitive(j, "binsparse");
23+
assert(cJSON_IsObject(binsparse));
24+
25+
cJSON* version_ = cJSON_GetObjectItemCaseSensitive(binsparse, "version");
26+
27+
assert(version_ != NULL);
28+
29+
assert(cJSON_IsString(version_));
30+
char* version_string = cJSON_GetStringValue(version_);
31+
32+
// TODO: check version.
33+
34+
cJSON* format_ = cJSON_GetObjectItemCaseSensitive(binsparse, "format");
35+
assert(format_ != NULL);
36+
char* format_string = cJSON_GetStringValue(format_);
37+
38+
cJSON* nnz_ = cJSON_GetObjectItemCaseSensitive(binsparse, "nnz");
39+
assert(nnz_ != NULL);
40+
size_t nnz = cJSON_GetNumberValue(nnz_);
41+
42+
cJSON* shape_ = cJSON_GetObjectItemCaseSensitive(binsparse, "shape");
43+
assert(shape_ != NULL);
44+
45+
assert(cJSON_GetArraySize(shape_) == 2);
46+
47+
cJSON* nrows_ = cJSON_GetArrayItem(shape_, 0);
48+
assert(nrows_ != NULL);
49+
50+
size_t nrows = cJSON_GetNumberValue(nrows_);
51+
52+
cJSON* ncols_ = cJSON_GetArrayItem(shape_, 1);
53+
assert(ncols_ != NULL);
54+
55+
size_t ncols = cJSON_GetNumberValue(ncols_);
56+
57+
char full_group_path[2048];
58+
size_t size = H5Iget_name(g, full_group_path, 2048);
59+
60+
printf("Group \"%s\": Version %s Binsparse matrix. Format %s, %zu x %zu.\n",
61+
full_group_path, version_string, format_string, nrows, ncols);
62+
}
63+
64+
H5Literate(g, H5_INDEX_NAME, H5_ITER_INC, NULL, visit_group, NULL);
65+
}
66+
67+
herr_t visit_group(hid_t loc_id, const char* name, const H5L_info_t* linfo,
68+
void* opdata) {
69+
hid_t g = H5Oopen(loc_id, name, H5P_DEFAULT);
70+
H5I_type_t type = H5Iget_type(g);
71+
if (type == H5I_GROUP) {
72+
print_group_info(g, name);
73+
}
74+
H5Oclose(g);
75+
return 0;
76+
}
77+
78+
int main(int argc, char** argv) {
79+
80+
if (argc < 2) {
81+
printf("usage: ./bsp-ls [file_name.mtx]\n");
82+
return 1;
83+
}
84+
85+
char* fname = argv[1];
86+
87+
hid_t f = H5Fopen(fname, H5F_ACC_RDONLY, H5P_DEFAULT);
88+
89+
print_group_info(f, "/");
90+
91+
H5Fclose(f);
92+
93+
return 0;
94+
}

0 commit comments

Comments
 (0)