Exemplo n.º 1
0
int gretl_copy_file (const char *src, const char *dest) 
{
    FILE *srcfd, *destfd;
    char buf[8192];
    size_t n;

    if (!strcmp(src, dest)) {
	return 1;
    }
   
    if ((srcfd = gretl_fopen(src, "rb")) == NULL) {
	gretl_errmsg_sprintf(_("Couldn't open %s"), src);
	return 1; 
    }

    if ((destfd = gretl_fopen(dest, "wb")) == NULL) {
	gretl_errmsg_sprintf(_("Couldn't write to %s"), dest);
	fclose(srcfd);
	return 1;
    }

    while ((n = fread(buf, 1, sizeof buf, srcfd)) > 0) {
	fwrite(buf, 1, n, destfd);
    }

    fclose(srcfd);
    fclose(destfd);

    return 0;
}  
Exemplo n.º 2
0
void undefined_symbol_error (const char *s, parser *p)
{
    parser_print_input(p);

    if (p->ch == '.') {
	gretl_errmsg_sprintf(_("%s: no such object"), s);
    } else {
	gretl_errmsg_sprintf(_("The symbol '%s' is undefined"), s);
    }

    p->err = E_UNKVAR;
}
Exemplo n.º 3
0
void *gretl_bundle_get_data (gretl_bundle *bundle, const char *key,
			     GretlType *type, int *size, int *err)
{
    void *ret = NULL;

    if (bundle == NULL) {
	if (err != NULL) {
	    *err = E_DATA;
	}
    } else {
	gpointer p = g_hash_table_lookup(bundle->ht, key);

	if (p != NULL) {
	    bundled_item *item = p;

	    ret = item->data;
	    if (type != NULL) {
		*type = item->type;
	    }
	    if (size != NULL) {
		*size = item->size;
	    }
	} else if (err != NULL) {
	    gretl_errmsg_sprintf("\"%s\": %s", key, _("no such item"));
	    *err = E_DATA;
	}
    }

    return ret;
}
Exemplo n.º 4
0
int test_locale (const char *langstr)
{
#ifndef ENABLE_NLS
    return 1;
#else
    const char *lcode;
    char *orig, ocpy[64];
    int langid, err = 0;

    langid = lang_id_from_name(langstr);
    lcode = get_setlocale_string(langid);
    orig = setlocale(LC_ALL, NULL);

    gretl_error_clear();

    *ocpy = '\0';
    strncat(ocpy, orig, 63);

    err = set_locale_with_workaround(langid, lcode);

    if (err) {
	gretl_errmsg_sprintf(_("%s: locale is not supported "
			       "on this system"), lcode);
    } else {
	setlocale(LC_ALL, ocpy); /* restore the original locale */
    } 

    return err;
#endif
}
Exemplo n.º 5
0
static int wbook_check_params (wbook *book)
{
    if (book->targname != NULL) {
	int i;

	book->selected = -1;
	for (i=0; i<book->nsheets; i++) {
	    if (!strcmp(book->targname, book->sheetnames[i])) {
		book->selected = i;
	    }
	}
	if (book->selected < 0) {
	    gretl_errmsg_sprintf("\"%s\": no such sheet", book->targname);
	    return E_DATA;
	}
    }

    if (book->selected < 0 || book->selected >= book->nsheets) {
	return E_DATA;
    } else if (book->col_offset < 0 || book->row_offset < 0) {
	return E_DATA;
    } else {
	return 0;
    }
}
Exemplo n.º 6
0
static int 
catch_setobs_errors (const char *stobs, int pd, int min, int structure)
{
    int panel = structure == STACKED_TIME_SERIES || 
	structure == STACKED_CROSS_SECTION;
    int err = 0;

    if (pd == 1) {
	if (min > 0) {
	    gretl_errmsg_set(_("no ':' allowed in starting obs with "
			       "frequency 1"));
	    err = 1;
	} else if (panel) {
	    gretl_errmsg_set(_("panel data must have frequency > 1"));
	    err = 1;
	}
    } else {
	if (min == 0) {
	    gretl_errmsg_set(_("starting obs must contain a ':' with "
			       "frequency > 1"));
	    err = 1;
	} else if (min > pd) {
	    gretl_errmsg_sprintf(_("starting obs '%s' is incompatible with frequency"), 
				 stobs);
	    err = 1;
	} else if (structure == CROSS_SECTION) {
	    gretl_errmsg_set(_("cross-sectional data: frequency must be 1"));
	    err = 1;
	}
    }

    return err;
}
Exemplo n.º 7
0
char *retrieve_public_file_as_buffer (const char *uri, size_t *len,
				      int *err)
{
    char *buf = NULL;

    if (proto_length(uri) == 0) {
	*err = E_DATA;
	return NULL;
    } else {
	urlinfo u;

	urlinfo_init(&u, NULL, SAVE_TO_BUFFER, NULL);
	urlinfo_set_url(&u, uri);
	*err = curl_get(&u);
	urlinfo_finalize(&u, &buf, err);
	*len = (*err)? 0 : u.datalen;
    }

    if (*err) {
	const char *s = gretl_errmsg_get();

	if (*s == '\0') {
	    /* no error message in place */
	    gretl_errmsg_sprintf("%s\ndownload failed", uri);
	}
    }

    return buf;
}
Exemplo n.º 8
0
static int trailing_junk_error (const char *s)
{
    char junk[16] = {0};
 
    s += strspn(s, " \t");
    sscanf(s, "%15[^ ]", junk);
    gretl_errmsg_sprintf(_("field '%s' in command is invalid"), junk);
    return E_PARSE;
}
Exemplo n.º 9
0
static void function_noargs_error (const char *s, parser *p)
{
    parser_print_input(p);

    pprintf(p->prn, _("'%s': no argument was given"), s);
    pputc(p->prn, '\n');
    gretl_errmsg_sprintf(_("'%s': no argument was given"), s);

    p->err = E_ARGS;
}
Exemplo n.º 10
0
char *json_get (const char *data, const char *path, int *n_objects,
		int *err)
{
    GError *gerr = NULL;
    JsonParser *parser;
    char *ret = NULL;
    int n = 0;

    if (data == NULL || path == NULL) {
	if (n_objects != NULL) {
	    *n_objects = 0;
	}
	return NULL;
    }

    parser = json_parser_new();
    if (parser == NULL) {
	gretl_errmsg_set("json_parser_new returned NULL!\n");
	*err = 1;
	return NULL;
    }

    json_parser_load_from_data(parser, data, -1, &gerr);

    if (gerr != NULL) {
	gretl_errmsg_sprintf("Couldn't parse JSON input: %s",
			     gerr->message);
	g_error_free(gerr);
	*err = E_DATA;
    } else {
	PRN *prn = gretl_print_new(GRETL_PRINT_BUFFER, err);

	if (!*err) {
	    *err = real_json_get(parser, path, &n, prn);
	    if (!*err) {
		ret = gretl_print_steal_buffer(prn);
	    }
	    gretl_print_destroy(prn);
	}
    }

    if (*err) {
	fprintf(stderr, "json_get: err = %d\n", *err);
    }

    if (n_objects != NULL) {
	*n_objects = n;
    }    

    g_object_unref(parser);

    return ret;
}
Exemplo n.º 11
0
static int real_win_run_sync (char *cmdline, const char *currdir,
			      int console_app) 
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi; 
    DWORD exitcode;
    DWORD flags;
    int ok, err = 0;

    ZeroMemory(&si, sizeof si);
    ZeroMemory(&pi, sizeof pi);  

    si.cb = sizeof si;

    if (console_app) {
	flags = CREATE_NO_WINDOW | HIGH_PRIORITY_CLASS;
    } else {
	si.dwFlags = STARTF_USESHOWWINDOW;
	si.wShowWindow = SW_SHOWMINIMIZED;
	flags = HIGH_PRIORITY_CLASS;
    }

    /* zero return means failure */
    ok = CreateProcess(NULL, cmdline, 
		       NULL, NULL, FALSE,
		       flags,
		       NULL, currdir,
		       &si, &pi);

    if (!ok) {
	fprintf(stderr, "win_run_sync: failed command:\n%s\n", cmdline);
	win_copy_last_error();
	err = 1;
    } else {
	WaitForSingleObject(pi.hProcess, INFINITE); 
	if (GetExitCodeProcess(pi.hProcess, &exitcode)) {
	    if (exitcode != 0) {
		gretl_errmsg_sprintf("%s: exit code %d\n", cmdline, 
				     exitcode);
		err = 1;
	    }
	} else {
	    fprintf(stderr, "win_run_sync: no exit code:\n%s\n", cmdline);
	    win_copy_last_error();
	    err = 1;
	}
    }
   
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);

    return err;
}
Exemplo n.º 12
0
int gretl_if_state_check (int indent0)
{
    int indent = ifstate(GETINDENT, 0, NULL);
    int err = 0;

    if (indent != indent0) {
	gretl_errmsg_sprintf(_("Unmatched \"%s\""), "if");
	ifstate(RELAX, 0, NULL);
	err = E_PARSE;
    }

    return err;
}
Exemplo n.º 13
0
static int graph_page_set_font_scale (const char *s)
{
    double x;

    s += strspn(s, " ");
    x = dot_atof(s);

    if (x > 0.0 && x < 4.0) {
	gp_fontscale = x;
	return 0;
    } else {
	gretl_errmsg_sprintf("'%s': invalid fontscale", s);
	return E_DATA;
    }
}
Exemplo n.º 14
0
void *gretl_bundle_steal_data (gretl_bundle *bundle, const char *key,
			       GretlType *type, int *size, int *err)
{
    void *ret = NULL;

    if (bundle == NULL) {
	if (err != NULL) {
	    *err = E_DATA;
	}
    } else {
	gpointer p = g_hash_table_lookup(bundle->ht, key);

	if (p != NULL) {
	    GList *keys = g_hash_table_get_keys(bundle->ht);
	    bundled_item *item = p;
	    gchar *keycpy = NULL;

	    ret = item->data;
	    if (type != NULL) {
		*type = item->type;
	    }
	    if (size != NULL) {
		*size = item->size;
	    }
	    while (keys) {
		if (!strcmp(keys->data, key)) {
		    keycpy = keys->data;
		    break;
		} else if (keys->next) {
		    keys = keys->next;
		} else {
		    break;
		}
	    }
	    g_hash_table_steal(bundle->ht, key);
	    g_free(keycpy);
	    g_list_free(keys);
	    free(item);
	} else if (err != NULL) {
	    gretl_errmsg_sprintf("\"%s\": %s", key, _("no such item"));
	    *err = E_DATA;
	}	    
    }

    return ret;
}
Exemplo n.º 15
0
int retrieve_public_file (const char *uri, char *localname)
{
    int pl = proto_length(uri);
    int err = 0;

    if (pl == 0) {
	return E_DATA;
    } else if (*localname == '\0') {
	/* extract the filename from the uri */
	const char *s = strrchr(uri + pl, '/');

	if (s == NULL || *(s+1) == '\0') {
	    err = E_DATA;
	} else {
	    /* save to user's dotdir by default */
	    strcat(localname, gretl_dotdir());
	    strcat(localname, s + 1);
	}
    }

    if (!err) {
	urlinfo u;

	urlinfo_init(&u, NULL, SAVE_TO_FILE, localname);
	urlinfo_set_url(&u, uri);
	if (gretl_in_gui_mode()) {
	    urlinfo_set_show_progress(&u);
	}
	err = curl_get(&u);
	urlinfo_finalize(&u, NULL, &err);
    }

    if (err) {
	const char *s = gretl_errmsg_get();

	if (*s == '\0') {
	    /* no error message in place */
	    gretl_errmsg_sprintf("%s\ndownload failed", uri);
	}
    } else {
	err = check_downloaded_file(localname, uri);
    }

    return err;
}
Exemplo n.º 16
0
int gretl_array_set_list (gretl_array *A, int i, 
			  int *L, int copy)
{
    int err = 0;

    if (A == NULL) {
	err = E_DATA;
    } else if (A->type != GRETL_TYPE_LISTS) {
	err = E_TYPES;
    } else if (i < 0 || i >= A->n) {
	gretl_errmsg_sprintf(_("Index value %d is out of bounds"), i+1);
	err = E_DATA;
    } else {
	free(A->data[i]);
	err = set_list(A, i, L, copy);
    }

    return err;
}
Exemplo n.º 17
0
int gretl_array_set_string (gretl_array *A, int i, 
			    char *s, int copy)
{
    int err = 0;

    if (A == NULL) {
	err = E_DATA;
    } else if (A->type != GRETL_TYPE_STRINGS) {
	err = E_TYPES;
    } else if (i < 0 || i >= A->n) {
	gretl_errmsg_sprintf(_("Index value %d is out of bounds"), i+1);
	err = E_DATA;
    } else {
	free(A->data[i]);
	err = set_string(A, i, s, copy);
    }

    return err;
}
Exemplo n.º 18
0
static int output_json_node_value (JsonNode *node,
				   PRN *prn)
{
    GType type = 0;
    int err = 0;

    if (null_node(node)) {
	gretl_errmsg_set("jsonget: got a null node");
	return E_DATA;
    }

    type = json_node_get_value_type(node);

#if 0
    fprintf(stderr, "jsonget: node type %s\n", g_type_name(type));
#endif    
    
    if (!handled_type(type)) {
	gretl_errmsg_sprintf("jsonget: unhandled object type '%s'", 
			     g_type_name(type));
	err = E_DATA;
    } else if (type == G_TYPE_STRING) {
	const gchar *s = json_node_get_string(node);

	if (s != NULL) {
	    pputs(prn, s);
	} else {
	    err = E_DATA;
	}	
    } else if (type == G_TYPE_DOUBLE) {
	double x = json_node_get_double(node);

	pprintf(prn, "%.15g", x);
    } else {
	gint64 k = json_node_get_int(node);
	double x = (double) k;

	pprintf(prn, "%.15g", x);
    }

    return err;
}
Exemplo n.º 19
0
int gretl_array_set_bundle (gretl_array *A, int i, 
			    gretl_bundle *b,
			    int copy)
{
    int err = 0;

    if (A == NULL) {
	err = E_DATA;
    } else if (A->type != GRETL_TYPE_BUNDLES) {
	err = E_TYPES;
    } else if (i < 0 || i >= A->n) {
	gretl_errmsg_sprintf(_("Index value %d is out of bounds"), i+1);
	err = E_DATA;
    } else {
	gretl_bundle_destroy(A->data[i]);
	err = set_bundle(A, i, b, copy);
    }

    return err;
}
Exemplo n.º 20
0
int gretl_array_set_matrix (gretl_array *A, int i, 
			    gretl_matrix *m,
			    int copy)
{
    int err = 0;

    if (A == NULL) {
	err = E_DATA;
    } else if (A->type != GRETL_TYPE_MATRICES) {
	err = E_TYPES;
    } else if (i < 0 || i >= A->n) {
	gretl_errmsg_sprintf(_("Index value %d is out of bounds"), i+1);
	err = E_DATA;
    } else {
	gretl_matrix_free(A->data[i]);
	err = set_matrix(A, i, m, copy);
    }

    return err;
}
Exemplo n.º 21
0
static int check_downloaded_file (const char *fname,
				  const char *dl)
{
    int err = 0;
    
    if (has_suffix(fname, ".zip") &&
	!gretl_is_pkzip_file(fname)) {
	err = E_DATA;
    } else if (has_suffix(fname, ".gfn") &&
	       !gretl_is_xml_file(fname)) {
	err = E_DATA;
    }
	
    if (err) {
	/* let's see what we got */
	FILE *fp = gretl_fopen(fname, "rb");
	int msg_done = 0;

	if (fp != NULL) {
	    char buf[128] = {0};
	    size_t n;

	    n = fread(buf, 1, 127, fp);
	    if (n > 8 && g_utf8_validate(buf, -1, NULL)) {
		gretl_errmsg_set(g_strchomp(buf));
		msg_done = 1;
	    }
	    fclose(fp);
	    gretl_remove(fname);
	}

	if (!msg_done) {
	    gretl_errmsg_sprintf("%s\ndownload failed", dl);
	}
    }

    return err;
}
Exemplo n.º 22
0
int gretl_delete_var_by_name (const char *s, PRN *prn)
{
    int err = 0;

    if (s == NULL || *s == '\0') {
	return E_PARSE;
    }

    if (object_is_function_arg(s)) {
	gretl_errmsg_sprintf(_("The variable %s is read-only"), s);
	return E_DATA;
    }

    if (!strcmp(s, "kalman")) {
	err = delete_kalman(prn);
    } else if (gretl_is_user_var(s)) {
	err = user_var_delete_by_name(s, prn);
    } else {
	err = maybe_delete_bundle_value(s, prn);
    } 

    return err;
}
Exemplo n.º 23
0
GretlType gretl_bundle_get_type (gretl_bundle *bundle, const char *key,
				 int *err)
{
    GretlType ret = GRETL_TYPE_NONE;

    if (bundle == NULL) {
	*err = E_DATA;
    } else {
	gpointer p = g_hash_table_lookup(bundle->ht, key);

	if (p != NULL) {
	    bundled_item *item = p;
	    
	    ret = item->type;
	} else {
	    gretl_errmsg_sprintf("\"%s\": %s", key, _("no such item"));
	    *err = E_DATA;
	}
				 
    }

    return ret;
}
Exemplo n.º 24
0
static int real_bundle_set_data (gretl_bundle *b, const char *key,
				 void *ptr, GretlType type,
				 int size, int copy, 
				 const char *note)
{
    bundled_item *item = NULL;
    int err, replace = 0;

    err = strlen(key) >= VNAMELEN ? E_DATA : 0;

    if (err) {
	gretl_errmsg_sprintf("'%s': invalid key string", key);
    } else {
	item = g_hash_table_lookup(b->ht, key);
	if (item != NULL) {
	    replace = 1;
	    if (item->type == type) {
		return bundled_item_replace_data(item, type, ptr, 
						 size, copy);
	    }
	}
	item = bundled_item_new(type, ptr, size, copy, note, &err);
    }

    if (!err) {
	gchar *k = g_strdup(key);

	if (replace) {
	    g_hash_table_replace(b->ht, k, item);
	} else {
	    g_hash_table_insert(b->ht, k, item);
	}
    }

    return err;
}
Exemplo n.º 25
0
static int real_json_get (JsonParser *parser, const char *pathstr,
                          int *n_objects, PRN *prn)
{
    GError *gerr = NULL;
    JsonNode *match, *node;
    JsonPath *path;
    GType ntype;
    double x;
    int err = 0;

    *n_objects = 0;

    node = json_parser_get_root(parser);
    path = json_path_new();

    if (!json_path_compile(path, pathstr, &gerr)) {
        if (gerr != NULL) {
            gretl_errmsg_sprintf("Failed to compile JsonPath: %s",
                                 gerr->message);
            g_error_free(gerr);
        } else {
            gretl_errmsg_set("Failed to compile JsonPath");
        }
        g_object_unref(path);
        return E_DATA;
    }

    match = json_path_match(path, node);
    if (match == NULL) {
        /* FIXME : maybe return empty string? */
        g_object_unref(path);
        return E_DATA;
    }

    /* in case we get floating-point output */
    gretl_push_c_numeric_locale();

    if (JSON_NODE_HOLDS_ARRAY(match)) {
        JsonArray *array;

        array = json_node_get_array(match);
        node = json_array_get_element(array, 0);

repeat:

        if (node == NULL) {
            gretl_errmsg_set("Failed to match JsonPath");
            ntype = 0;
        } else {
            ntype = json_node_get_value_type(node);
        }

        if (!handled_type(ntype)) {
            if (JSON_NODE_HOLDS_ARRAY(node)) {
                array = json_node_get_array(node);
                node = json_array_get_element(array, 0);
                goto repeat;
            } else {
                gretl_errmsg_sprintf("Unhandled array type '%s'",
                                     g_type_name(ntype));
                err = E_DATA;
            }
        } else {
            int i, n = json_array_get_length(array);

            for (i=0; i<n; i++) {
                node = json_array_get_element(array, i);
                if (ntype == G_TYPE_STRING) {
                    pputs(prn, json_node_get_string(node));
                } else {
                    x = json_node_get_double(node);
                    pprintf(prn, "%.15g", x);
                }
                if (n > 1) {
                    pputc(prn, '\n');
                }
            }
            *n_objects = n;
        }
    } else {
        ntype = json_node_get_value_type(match);
        if (!handled_type(ntype)) {
            gretl_errmsg_sprintf("Unhandled object type '%s'",
                                 g_type_name(ntype));
            err = E_DATA;
        } else {
            if (ntype == G_TYPE_STRING) {
                pputs(prn, json_node_get_string(match));
            } else {
                x = json_node_get_double(match);
                pprintf(prn, "%.15g", x);
            }
            *n_objects = 1;
        }
    }

    gretl_pop_c_numeric_locale();

    json_node_free(match);
    g_object_unref(path);

    return err;
}
Exemplo n.º 26
0
static int xlsx_gather_sheet_names (xlsx_info *xinfo, 
				    const char *insheet,
				    int *list, 
				    PRN *prn)
{
    gchar *wb_name = g_strdup_printf("xl%cworkbook.xml", SLASH); 
    int i, err;

    err = xlsx_workbook_get_sheetnames(xinfo, wb_name);
    g_free(wb_name);

    if (!err && xinfo->n_sheets == 0) {
	pputs(prn, _("Found no sheets\n"));
	err = E_DATA;
    }

    if (!err) {
	const char *sname;
	int have_insheet_name = 0;
	int target_sheet_number = 0;
	int insheet_idx = 0;

	if (insheet != NULL && *insheet != '\0') {
	    /* try looking for a worksheet specified by name */
	    have_insheet_name = 1;
	    insheet_idx = -1; /* invalidate index */
	} else if (list != NULL && list[0] >= 1 && list[1] > 0) {
	    /* or sheet specified by (1-based) sequence number */
	    target_sheet_number = list[1];
	    insheet_idx = -1; /* invalidate index */
	}

	fprintf(stderr, "Found these worksheets:\n");
	for (i=0; i<xinfo->n_sheets; i++) {
	    sname = xinfo->sheetnames[i];
	    fprintf(stderr, "%d: '%s' (%s)\n", i+1, sname, xinfo->filenames[i]);
	    if (have_insheet_name) {
		if (insheet_idx < 0 && !strcmp(insheet, sname)) {
		    /* found the name we were looking for */
		    insheet_idx = i;
		}
	    } else if (target_sheet_number == i + 1) {
		/* found the number we were looking for */
		insheet_idx = i;
	    }
	}

	if (insheet_idx < 0) {
	    /* a sheet was pre-specified but was not found */
	    if (have_insheet_name && integer_string(insheet)) {
		/* try interpreting as plain integer index? */
		insheet_idx = atoi(insheet) - 1;
		if (insheet_idx < 0 || insheet_idx >= xinfo->n_sheets) {
		    /* no, it doesn't work */
		    insheet_idx = -1;
		} 
	    }
	    if (insheet_idx < 0) {
		if (have_insheet_name) {
		    gretl_errmsg_sprintf(_("'%s': no such worksheet"), insheet);
		} else {
		    gretl_errmsg_set(_("Invalid argument for worksheet import"));
		}
		err = E_DATA;
	    }
	}

	if (!err) {
	    if (have_insheet_name || target_sheet_number > 0) {
		/* avoid the expense of checking all sheets */
		err = xlsx_verify_specific_sheet(xinfo, insheet_idx, prn);
	    } else {
		err = xlsx_verify_sheets(xinfo, prn);
		if (xinfo->n_sheets == 0) {
		    pputs(prn, "\nFound no valid sheets\n");
		    err = E_DATA;
		} else {
		    pprintf(prn, _("\nFound %d valid sheet(s)\n"), xinfo->n_sheets);
		}
	    }
	}
    }

    return err;
}
Exemplo n.º 27
0
static void too_long (void)
{
    gretl_errmsg_sprintf(_("Maximum length of command line "
			   "(%d bytes) exceeded\n"), MAXLINE);
}
Exemplo n.º 28
0
static int real_json_get (JsonParser *parser, const char *pathstr,
			  int *n_objects, PRN *prn)
{
    GError *gerr = NULL;
    JsonNode *match, *node;
    JsonPath *path;
    GType ntype;
    int err = 0;

    *n_objects = 0;

    node = json_parser_get_root(parser);

    if (node == NULL || json_node_is_null(node)) {
	gretl_errmsg_set("jsonget: got null root node");
	return E_DATA;
    }
    
    path = json_path_new();

    if (!json_path_compile(path, pathstr, &gerr)) {
	if (gerr != NULL) {
	    gretl_errmsg_sprintf("jsonget: failed to compile JsonPath: %s",
				 gerr->message);
	    g_error_free(gerr);
	} else {
	    gretl_errmsg_set("jsonget: failed to compile JsonPath");
	}	    
	g_object_unref(path);
	return E_DATA;
    }

    match = json_path_match(path, node);

    if (null_node(match)) {
	/* FIXME : maybe return empty string? */
	g_object_unref(path);
	return E_DATA;
    }

    /* in case we get floating-point output */
    gretl_push_c_numeric_locale();

    if (JSON_NODE_HOLDS_ARRAY(match)) {
	JsonArray *array = json_node_get_array(match);
	int len = 0, index = 0;

	if (non_empty_array(array)) {
	    len = json_array_get_length(array);
	    node = json_array_get_element(array, index);
	} else {
	    node = NULL;
	}

    repeat:

	if (null_node(node)) {
	    gretl_errmsg_set("jsonget: failed to match JsonPath");
	    ntype = 0;
	    err = E_DATA;
	    goto bailout;
	} else {
	    ntype = json_node_get_value_type(node);
	}

	if (node != NULL && !handled_type(ntype)) {
	    if (JSON_NODE_HOLDS_ARRAY(node)) {
		/* recurse on array type */
		array = json_node_get_array(node);
		if (non_empty_array(array)) {
		    node = json_array_get_element(array, 0);
		    goto repeat;
		}
	    } else if (json_node_get_node_type(node) == JSON_NODE_OBJECT) {
		err = excavate_json_object(node, n_objects, prn);
		if (!err) {
		    if (index < len - 1) {
			node = json_array_get_element(array, ++index);
			goto repeat;
		    }
		}
	    } else {
		gretl_errmsg_sprintf("jsonget: unhandled array type '%s'", 
				     g_type_name(ntype));
		err = E_DATA;
	    }
	} else if (array != NULL) {
	    int i, n = json_array_get_length(array);

	    for (i=0; i<n && !err; i++) {
		node = json_array_get_element(array, i);
		err = output_json_node_value(node, prn);
		if (!err) {
		    *n_objects += 1;
		    if (n > 1) {
			pputc(prn, '\n');
		    }
		}
	    }
	}
    } else {
	/* not an array-holding node */
	err = output_json_node_value(match, prn);
	if (!err) {
	    *n_objects += 1;
	}
    }

 bailout:

    gretl_pop_c_numeric_locale();

    json_node_free(match);
    g_object_unref(path);

    return err;
}
Exemplo n.º 29
0
static int ifstate (int code, int val, int *err)
{
    static unsigned short T[IF_DEPTH];
    static unsigned short got_if[IF_DEPTH];
    static unsigned short got_else[IF_DEPTH];
    static unsigned short got_T[IF_DEPTH];
    static unsigned short indent;
    int i, ret = 0;

#if IFDEBUG
    fprintf(stderr, "ifstate: code = %s\n", ifstr(code));
#endif

    if (code == RELAX) {
	indent = 0;
    } else if (code == IFRESET) {
	indent = val;
    } else if (code == GETINDENT) {
	ret = indent;
    } else if (code == UNINDENT) {
	ret = --indent;
    } else if (code == SET_FALSE || code == SET_TRUE) {
	indent++;
	if (indent >= IF_DEPTH) {
	    gretl_errmsg_sprintf("IF depth (%d) exceeded", IF_DEPTH);
	    *err = E_DATA;
	} else {
	    T[indent] = got_T[indent] = (code == SET_TRUE);
	    got_if[indent] = 1;
	    got_else[indent] = 0;
	}
    } else if (code == SET_ELSE || code == SET_ELIF) {
	if (got_else[indent] || !got_if[indent]) {
	    unmatched_message(code);
	    *err = E_PARSE;
	} else {
	    got_else[indent] = (code == SET_ELSE);
	    if (T[indent]) {
		T[indent] = 0;
	    } else if (!got_T[indent]) {
		T[indent] = 1;
	    }
	}
    } else if (code == SET_ENDIF) {
	if (!got_if[indent] || indent == 0) {
	    unmatched_message(code);
	    *err = E_PARSE;
	} else {
	    got_if[indent] = 0;
	    got_else[indent] = 0;
	    got_T[indent] = 0;
	    indent--;
	}
    } else if (code == IS_FALSE || code == IS_TRUE) {
	for (i=1; i<=indent; i++) {
	    if (T[i] == 0) {
		ret = 1;
		break;
	    }
	}
	if (code == IS_TRUE) {
	    ret = !ret;
	}
    } 

#if IFDEBUG
    fprintf(stderr, "ifstate: returning %d (indent %d, err %d)\n", 
	    ret, indent, (err == NULL)? 0 : *err);
#endif

    return ret;
}
Exemplo n.º 30
0
static void unmatched_message (int code)
{
    gretl_errmsg_sprintf(_("Unmatched \"%s\""),
			 (code == SET_ELSE)? "else" : 
			 (code == SET_ELIF)? "elif": "endif");
}