コード例 #1
0
ファイル: sj_conf.c プロジェクト: jmptrader/smart.js
/*
 * 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;
  }
}
コード例 #2
0
ファイル: sj_conf.c プロジェクト: victorino80/smart.js
/*
 * 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;
  }
}