From 67d128c386042f36ead2f98c47f1a28a447b57ef Mon Sep 17 00:00:00 2001 From: Al Date: Fri, 9 Oct 2015 15:36:14 -0400 Subject: [PATCH] [graph] graph_load and graph_save --- src/graph.c | 26 ++++++++++++++++++++++++++ src/graph.h | 2 ++ 2 files changed, 28 insertions(+) diff --git a/src/graph.c b/src/graph.c index c0f5fb9d..2b85dfc6 100644 --- a/src/graph.c +++ b/src/graph.c @@ -147,6 +147,19 @@ exit_graph_allocated: return NULL; } + +graph_t *graph_load(char *path) { + FILE *f; + + if ((f = fopen(path, "rb")) == NULL) { + return NULL; + } + + graph_t *graph = graph_read(f); + fclose(f); + return graph; +} + bool graph_write(graph_t *self, FILE *f) { if (self == NULL || self->indptr == NULL || self->indices == NULL) { return false; @@ -184,3 +197,16 @@ bool graph_write(graph_t *self, FILE *f) { return true; } + + +bool graph_save(graph_t *self, char *path) { + FILE *f; + if ((f = fopen(path, "wb")) == NULL) { + return false; + } + bool status = graph_write(self, f); + fclose(f); + return status; +} + + diff --git a/src/graph.h b/src/graph.h index 0bec6a0d..934aaa00 100644 --- a/src/graph.h +++ b/src/graph.h @@ -58,7 +58,9 @@ void graph_append_edges(graph_t *self, uint32_t *col, size_t n); void graph_finalize_vertex(graph_t *self); bool graph_write(graph_t *self, FILE *f); +bool graph_save(graph_t *self, char *path); graph_t *graph_read(FILE *f); +graph_t *graph_load(char *path); #define graph_foreach_row(g, row_var, index_var, length_var, code) { \ uint32_t _row_start = 0, _row_end = 0; \