/* * 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; } }
/* * 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; } }
static const char *getF (lua_State *L, void *ud, size_t *size) { LoadF *lf = (LoadF *)ud; (void)L; if (lf->extraline) { lf->extraline = 0; *size = 1; return "\n"; } if (c_feof(lf->f)) return NULL; *size = c_fread(lf->buff, 1, sizeof(lf->buff), lf->f); return (*size > 0) ? lf->buff : NULL; }