Ejemplo n.º 1
0
/*
 * 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) {
    FILE *fp;
    char *p;
    long file_size;

    if ((fp = fopen(path, "r")) == NULL) {
        return NULL;
    } else if ((file_size = v7_get_file_size(fp)) <= 0) {
        fclose(fp);
        return NULL;
    } else if ((p = (char *) calloc(1, (size_t) file_size + 3)) == NULL) {
        fclose(fp);
        return NULL;
    } else {
        rewind(fp);
        if ((fread(p + 1, 1, (size_t) file_size, fp) < (size_t) file_size) &&
                ferror(fp)) {
            fclose(fp);
            return NULL;
        }
        fclose(fp);
        p[0] = '(';
        p[file_size + 1] = ')';
        return p;
    }
}
Ejemplo n.º 2
0
/*
 * 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;
  }
}