/* * returns the content of a json file wrapped in parenthesis * so that they can be directly evaluated as js. Currently * we don't have a C JSON parse API. */ static char *read_json_file(const char *path) { c_file_t fp; char *p; long file_size; if ((fp = c_fopen(path, "r")) == INVALID_FILE) { return NULL; } else if ((file_size = v7_get_file_size(fp)) <= 0) { c_fclose(fp); return NULL; } else if ((p = (char *) calloc(1, (size_t) file_size + 3)) == NULL) { c_fclose(fp); return NULL; } else { c_rewind(fp); if ((c_fread(p + 1, 1, (size_t) file_size, fp) < (size_t) file_size) && c_ferror(fp)) { c_fclose(fp); return NULL; } c_fclose(fp); p[0] = '('; p[file_size + 1] = ')'; return p; } }
/* * returns the content of a json file wrapped in parenthesis * so that they can be directly evaluated as js. Currently * we don't have a C JSON parse API. */ static char *read_json_file(const char *path) { c_file_t fp; char *p; long file_size; if ((fp = c_fopen(path, "r")) == INVALID_FILE) { return NULL; /* * TODO(alashkin): add function sj_get_file_size to HAL interface * and remove v7_get_file_size from everywhere */ } else if ((file_size = v7_get_file_size(fp)) <= 0) { c_fclose(fp); return NULL; } else if ((p = (char *) calloc(1, (size_t) file_size + 3)) == NULL) { c_fclose(fp); return NULL; } else { c_rewind(fp); if ((c_fread(p + 1, 1, (size_t) file_size, fp) < (size_t) file_size) && c_ferror(fp)) { c_fclose(fp); return NULL; } c_fclose(fp); p[0] = '('; p[file_size + 1] = ')'; return p; } }
int xferror(XFILE *fp) { int ret; switch (fp->type) { case FH_FILE: ret = c_ferror(fp->fh); break; case FH_ZLIB: ret = z_ferror(fp->fh); break; default: xassert(fp != fp); } return ret; }
LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) { LoadF lf; int status, readstatus; int c; int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ lf.extraline = 0; if (filename == NULL) { lua_pushliteral(L, "=stdin"); lf.f = c_stdin; } else { lua_pushfstring(L, "@%s", filename); lf.f = c_fopen(filename, "r"); if (lf.f == NULL) return errfile(L, "open", fnameindex); } c = c_getc(lf.f); if (c == '#') { /* Unix exec. file? */ lf.extraline = 1; while ((c = c_getc(lf.f)) != EOF && c != '\n') ; /* skip first line */ if (c == '\n') c = c_getc(lf.f); } if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ lf.f = c_freopen(filename, "rb", lf.f); /* reopen in binary mode */ if (lf.f == NULL) return errfile(L, "reopen", fnameindex); /* skip eventual `#!...' */ while ((c = c_getc(lf.f)) != EOF && c != LUA_SIGNATURE[0]) ; lf.extraline = 0; } c_ungetc(c, lf.f); status = lua_load(L, getF, &lf, lua_tostring(L, -1)); readstatus = c_ferror(lf.f); if (filename) c_fclose(lf.f); /* close file (even in case of errors) */ if (readstatus) { lua_settop(L, fnameindex); /* ignore results from `lua_load' */ return errfile(L, "read", fnameindex); } lua_remove(L, fnameindex); return status; }