diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2016-02-17 22:17:26 +0100 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2016-02-18 16:50:40 +0100 |
| commit | 89486c17e6436fe2cbe10cf4e9f7d385aa35c3fe (patch) | |
| tree | 5defb0736e21af2e949195d1b03174517e1936ad | |
| parent | a04408c5918376be63a1902fd71726dfff9edb3e (diff) | |
| download | vis-89486c17e6436fe2cbe10cf4e9f7d385aa35c3fe.tar.gz vis-89486c17e6436fe2cbe10cf4e9f7d385aa35c3fe.tar.xz | |
vis-lua: add file:content(pos, len) function
| -rw-r--r-- | vis-lua.c | 19 |
1 files changed, 19 insertions, 0 deletions
@@ -706,12 +706,31 @@ static int file_lines_iterator_it(lua_State *L) { return 1; } +static int file_content(lua_State *L) { + File *file = obj_ref_check(L, 1, "vis.file"); + if (!file) + goto err; + size_t pos = luaL_checkunsigned(L, 2); + size_t len = luaL_checkunsigned(L, 3); + char *data = malloc(len); + if (!data) + goto err; + len = text_bytes_get(file->text, pos, len, data); + lua_pushlstring(L, data, len); + free(data); + return 1; +err: + lua_pushnil(L); + return 1; +} + static const struct luaL_Reg file_funcs[] = { { "__index", file_index }, { "__newindex", file_newindex }, { "insert", file_insert }, { "delete", file_delete }, { "lines_iterator", file_lines_iterator }, + { "content", file_content }, { NULL, NULL }, }; |
