diff options
author | Determinant <[email protected]> | 2015-05-24 20:35:40 +0800 |
---|---|---|
committer | Determinant <[email protected]> | 2015-05-24 20:35:40 +0800 |
commit | 3ccca6ba1eb7b6732036f4128c977c0b02ef3836 (patch) | |
tree | 150ea05f65a2cc041433a601a83c4c887a7bcc86 /matrix | |
parent | 5d1cb5619ea720968e01ec9a63422b2cc30295ef (diff) |
add write functionality to ParamFile
Diffstat (limited to 'matrix')
-rw-r--r-- | matrix/generic/mmatrix.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/matrix/generic/mmatrix.c b/matrix/generic/mmatrix.c index d37cd80..c25b9f7 100644 --- a/matrix/generic/mmatrix.c +++ b/matrix/generic/mmatrix.c @@ -68,8 +68,37 @@ int nerv_matrix_(load)(lua_State *L) { return 1; } +int nerv_matrix_(save)(lua_State *L) { + ParamFileHandle *chunk = luaT_checkudata(L, 2, + nerv_param_file_handle_tname); + Matrix *self = luaT_checkudata(L, 1, nerv_matrix_(tname)); + int i, j; + long nrow = self->nrow, ncol = self->ncol; + FILE *fp = chunk->fp; + if (fprintf(fp, "%ld %ld\n", nrow, ncol) < 0) + return 0; + for (i = 0; i < nrow; i++) + { + MATRIX_ELEM *row = MATRIX_ROW_PTR(self, i); + for (j = 0; j < ncol; j++) + if (fprintf(fp, MATRIX_ELEM_FMT " ", row[j]) < 0) + { + free(self); + return 0; + } + if (fprintf(fp, "\n") < 0) + { + free(self); + return 0; + } + } + return 0; +} + + static const luaL_Reg nerv_matrix_(extra_methods)[] = { {"load", nerv_matrix_(load)}, + {"save", nerv_matrix_(save)}, {NULL, NULL} }; |