From ec123de01ae3d72b56434353c66ef965b25c083d Mon Sep 17 00:00:00 2001 From: skywind3000 Date: Thu, 12 Mar 2020 18:35:03 +0800 Subject: [PATCH] update --- czmod.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/czmod.c b/czmod.c index 2cdfde9..c100496 100644 --- a/czmod.c +++ b/czmod.c @@ -1,9 +1,20 @@ +//===================================================================== +// +// czmod.c - c module to boost z.lua +// +// Created by skywind on 2020/03/11 +// Last Modified: 2020/03/11 16:37:01 +// +//===================================================================== #include #include #include #include +#include +#ifdef __linux #include +#endif #include "imembase.c" @@ -32,6 +43,12 @@ #endif +//--------------------------------------------------------------------- +// internal functions +//--------------------------------------------------------------------- +static const char *get_data_file(void); + + //--------------------------------------------------------------------- // get environ //--------------------------------------------------------------------- @@ -50,7 +67,7 @@ static ib_string* os_getenv(const char *name) //--------------------------------------------------------------------- // get data file //--------------------------------------------------------------------- -static inline const char *get_data_file(void) +static const char *get_data_file(void) { static ib_string *text = NULL; if (text != NULL) { @@ -71,6 +88,38 @@ static inline const char *get_data_file(void) return text->ptr; } + +//--------------------------------------------------------------------- +// load file content +//--------------------------------------------------------------------- +ib_string *load_content(const char *filename) +{ + FILE *fp = fopen(filename, "r"); + if (fp == NULL) { + return NULL; + } + ib_string *text = ib_string_new(); + size_t block = 65536; + ib_string_resize(text, block); + size_t pos = 0; + while (feof(fp) == 0) { + size_t avail = text->size - pos; + if (avail < block) { + ib_string_resize(text, text->size + block); + avail = text->size - pos; + } + size_t hr = fread(&(text->ptr[pos]), 1, avail, fp); + pos += hr; + } + fclose(fp); + ib_string_resize(text, pos); + return text; +} + + +//--------------------------------------------------------------------- +// main entry +//--------------------------------------------------------------------- int main(int argc, char *argv[]) { if (argc <= 1) {