Пример #1
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;
}
Пример #2
0
int gretl_print_rename_file (PRN *prn, const char *oldpath,
			     const char *newpath)
{
    int err = 0;

    if (prn == NULL) {
	fprintf(stderr, "gretl_print_rename_file: prn is NULL\n");
	return E_DATA;
    }

    if (prn->fp == NULL || prn_fp_list_active(prn)) {
	return E_DATA;
    }

#if PRN_DEBUG
    fprintf(stderr, "gretl_print_rename_file, prn at %p:\n oldpath='%s'\n"
	    " newpath='%s'\n prn->fp=%p (closing)\n", (void *) prn,
	    oldpath, newpath, (void *) prn->fp);
    fprintf(stderr, " (old prn->fname = '%s')\n", prn->fname);
#endif

    fclose(prn->fp);
    prn->fp = NULL;

    if (oldpath == NULL && prn->fname != NULL) {
	/* renaming from tempfile */
	err = gretl_rename(prn->fname, newpath);
    } else {
	err = gretl_rename(oldpath, newpath);
    }

    if (err) {
	fprintf(stderr, "%s\n", gretl_errmsg_get());
    } else {
	/* re-open the stream under its new name */
	prn->fp = gretl_fopen(newpath, "a");
#if PRN_DEBUG
	fprintf(stderr, "gretl_print_rename_file: new fp=%p\n", prn->fp);
#endif
	if (prn->fname != NULL) {
	    /* @prn originally used a tempfile: the record of
	       the temporary filename should be deleted 
	    */
	    free(prn->fname);
	    prn->fname = NULL;
	}
    }

    return err;
}
Пример #3
0
static int check_imported_varname (char *vname, int vnum,
				   int row, int col,
				   PRN *prn)
{
    int err = 0;

    if (*vname == '\0') {
	if (vnum > 0) {
	    fprintf(stderr, "variable name %d is missing\n", vnum);
	    sprintf(vname, "v%d", vnum);
	} else {
	    err = missing_varname();
	}
    } else if (numeric_string(vname)) {
	err = check_varname(vname);
    } else {
	char *s, tmp[VNAMELEN];

	strcpy(tmp, vname);
	s = tmp;
	*vname = '\0';
	
	while (*s && !isalpha(*s)) s++;
	if (*s == '\0') {
	    if (vnum > 0) {
		fprintf(stderr, "variable name %d is garbage\n", vnum);
		sprintf(vname, "v%d", vnum);
	    } else {
		err = missing_varname();
	    }
	} else {
	    strncat(vname, s, VNAMELEN - 1);
	}
	iso_to_ascii(vname);
	strip_vname_illegals(vname);
	err = check_varname(vname);
    }

    if (err) {
	err = E_DATA;
	if (row >= 0 && col >= 0) {
	    pprintf(prn, _("At row %d, column %d:\n"), row, col);
	}
	pputs(prn, gretl_errmsg_get());
    }

    return err;
}
Пример #4
0
int gretl_spawn (char *cmdline)
{
    GError *error = NULL;
    gchar *errout = NULL;
    gchar *sout = NULL;
    int ok, status;
    int ret = 0;

    gretl_error_clear();

    ok = g_spawn_command_line_sync(cmdline,
				   &sout,   /* standard output */
				   &errout, /* standard error */
				   &status, /* exit status */
				   &error);

    if (!ok) {
	gretl_errmsg_set(error->message);
	fprintf(stderr, "gretl_spawn: '%s'\n", error->message);
	g_error_free(error);
	ret = 1;
    } else if (errout && *errout) {
	fprintf(stderr, "stderr: '%s'\n", errout);
	if (!non_fatal(errout)) {
	    gretl_errmsg_set(errout);
	    fprintf(stderr, "gretl_errmsg: '%s'\n", gretl_errmsg_get());
	    ret = 1;
	}
    } else if (status != 0) {
	if (sout != NULL && *sout) {
	    gretl_errmsg_set(sout);
	    fprintf(stderr, "gretl_spawn: status = %d: '%s'\n", status, sout);
	} else {
	    gretl_errmsg_set(_("Command failed"));
	    fprintf(stderr, "gretl_spawn: status = %d\n", status);
	}
	ret = 1;
    }

    if (errout != NULL) g_free(errout);
    if (sout != NULL) g_free(sout);

    if (ret) {
	fprintf(stderr, "Failed command: '%s'\n", cmdline);
    } 

    return ret;
}
Пример #5
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;
}
Пример #6
0
static int xlsx_read_shared_strings (xlsx_info *xinfo, PRN *prn)
{
    xmlDocPtr doc = NULL;
    xmlNodePtr cur = NULL;
    xmlNodePtr val;
    char *tmp;
    int i, n = 0;
    int err = 0;

    err = gretl_xml_open_doc_root(xinfo->stringsfile, "sst", 
				  &doc, &cur);

    if (err) {
	pprintf(prn, "Couldn't find shared strings table\n");
	pprintf(prn, "%s", gretl_errmsg_get());
	return err;
    }

    tmp = (char *) xmlGetProp(cur, (XUC) "uniqueCount");
    if (tmp == NULL) {
	tmp = (char *) xmlGetProp(cur, (XUC) "count");
    }

    if (tmp == NULL) {
	pprintf(prn, "didn't get sst count\n");
	err = E_DATA;
    } else {
	n = atoi(tmp);
	if (n <= 0) {
	    pprintf(prn, "didn't get valid sst count\n");
	    err = E_DATA;
	}
	free(tmp);
    }

    if (!err) {
	xinfo->strings = strings_array_new(n);
	if (xinfo->strings == NULL) {
	    err = E_ALLOC;
	}
    }

    cur = cur->xmlChildrenNode;

    /* The strings in an <sst> are mostly set up as 

       <si><t>XXX</t></si>
       <si><t>YYY</t></si> ...

       But there are also weird cases where junk is interposed
       and the structure becomes

       <si><r>...<t>XXX</t></r><r>...<t>YYY</t></r></si> ...

       That is, an <si> element may contain more than one <r>
       element, which embeds a <t> along with formatting crap.
    */

    i = 0;
    while (cur != NULL && !err) {
	if (!xmlStrcmp(cur->name, (XUC) "si")) {
	    int gotstr = 0;

	    val = cur->xmlChildrenNode;
	    while (val != NULL && !err && !gotstr) {
		if (!xmlStrcmp(val->name, (XUC) "t")) {
		    /* got a regular <t> element */
		    tmp = (char *) xmlNodeGetContent(val);
		    if (tmp == NULL) {
			pprintf(prn, "failed reading string %d\n", i);
			err = E_DATA;
		    } else {
			xinfo->strings[i++] = tmp;
			gotstr = 1;
		    }
		} else if (!xmlStrcmp(val->name, (XUC) "r")) {
		    /* hunt for <t> inside an <r> element */
		    xmlNodePtr sub = val->xmlChildrenNode;

		    while (sub != NULL && !err && i < n) {
			if (!xmlStrcmp(sub->name, (XUC) "t")) {
			    tmp = (char *) xmlNodeGetContent(sub);
			    if (tmp == NULL) {
				pprintf(prn, "failed reading string %d\n", i);
				err = E_DATA;
			    } else {
				xinfo->strings[i++] = tmp;
				gotstr = 1;
			    }
			}
			sub = sub->next;
		    }
		}
		val = val->next;
	    }
	}
	if (i == n) {
	    break;
	}
	cur = cur->next;
    }

    if (!err && i < n) {
	pprintf(prn, "expected %d shared strings but only found %d\n",
		n, i);
	err = E_DATA;
    }

    if (!err) {
	xinfo->n_strings = i;
    } else if (xinfo->strings != NULL) {
	strings_array_free(xinfo->strings, n);
	xinfo->strings = NULL;
    }

    xmlFreeDoc(doc);

    return err;
}
Пример #7
0
static int xlsx_read_worksheet (xlsx_info *xinfo, PRN *prn) 
{
    xmlDocPtr doc = NULL;
    xmlNodePtr data_node = NULL;
    xmlNodePtr cur = NULL;
    xmlNodePtr c1;
    int gotdata = 0;
    int err = 0;

    sprintf(xinfo->sheetfile, "xl%c%s", SLASH, 
	    xinfo->filenames[xinfo->selsheet]);

#if XDEBUG
    fprintf(stderr, "xlsx_read_worksheet: sheetnum=%d, name='%s'\n",
	    xinfo->selsheet, xinfo->filenames[xinfo->selsheet]);
#endif

    sprintf(xinfo->stringsfile, "xl%csharedStrings.xml", SLASH);

    err = gretl_xml_open_doc_root(xinfo->sheetfile, "worksheet", 
				  &doc, &cur);

    if (err) {
	pprintf(prn, "didn't get worksheet\n");
	pprintf(prn, "%s", gretl_errmsg_get());
	return err;
    }

    /* walk the tree, first pass */
    cur = cur->xmlChildrenNode;
    while (cur != NULL && !err && !gotdata) {
        if (!xmlStrcmp(cur->name, (XUC) "sheetData")) {
	    data_node = c1 = cur->xmlChildrenNode;
	    while (c1 != NULL && !err) {
		if (!xmlStrcmp(c1->name, (XUC) "row")) {
		    err = xlsx_read_row(c1, xinfo, prn);
		}
		c1 = c1->next;
	    }
	    gotdata = 1;
	}
	cur = cur->next;
    }

#if XDEBUG
    if (!err) {
	pprintf(prn, "Max row = %d, max col = %d\n", xinfo->maxrow,
		xinfo->maxcol);
	pprintf(prn, "Accessed %d shared strings\n", xinfo->n_strings);
    }
#endif

    if (!err && xinfo->dset == NULL) {
	err = xlsx_check_dimensions(xinfo, prn);
	if (!err) {
	    gretl_push_c_numeric_locale();
	    c1 = data_node;
	    while (c1 != NULL && !err) {
		if (!xmlStrcmp(c1->name, (XUC) "row")) {
		    err = xlsx_read_row(c1, xinfo, prn);
		}
		c1 = c1->next;
	    }
	    gretl_pop_c_numeric_locale();
	}
    }

    xmlFreeDoc(doc);

    return err;
}
Пример #8
0
static int open_import_zipfile (const char *fname, char *dname,
				PRN *prn)
{
    const char *udir = gretl_dotdir();
    const char *real_fname = fname;
    char *recoded_fname = NULL;
    char *abspath = NULL;
    FILE *fp;
    int err = 0;

    errno = 0;
    *dname = '\0';

    /* In case @fname is in UTF-8 but we're on MS Windows (or
       conceivably on an old non-UTF-8 *nix system), grab the
       appropriately recoded filename to pass into the
       zipfile apparatus, since in that context a filename
       that works with plain system stdio is expected.
       FIXME: is this right if we're using libgsf to do the
       unzipping? Maybe this doesn't matter if we're using
       libgsf only on UTF-8 systems (e.g. modern Linux), in
       which case no recoding will be required.
    */

    fp = gretl_fopen_with_recode(fname, "r", &recoded_fname);
    if (fp == NULL) {
	return E_FOPEN;
    }

    fclose(fp);

    if (recoded_fname != NULL) {
	real_fname = recoded_fname;
    }

    /* by doing chdir, we may lose track of the file if 
       its path is relative */
    if (!g_path_is_absolute(real_fname)) {
	abspath = get_absolute_path(real_fname);
	if (abspath != NULL) {
	    real_fname = abspath;
	}
    }

    /* cd to user dir and make temporary dir */
    if (gretl_chdir(udir)) {
	gretl_errmsg_set_from_errno(udir);
	err = E_FOPEN;
    } else {
	err = gretl_make_tempdir(dname);
	if (!err) {
	    err = gretl_chdir(dname);
	    if (err) {
		gretl_remove(dname);
	    }
	}
    }
    
    if (!err) {
	/* if all has gone OK, we're now "in" the temporary
	   directory under dotdir, and @real_fname is the
	   absolute path to the file to be unzipped.
	*/
	err = gretl_unzip(real_fname);
	if (err) {
	    pprintf(prn, "gretl_unzip: %s\n", gretl_errmsg_get());
	}
    }

    free(abspath);
    free(recoded_fname);

    return err;
}
Пример #9
0
int main (void)
{

    DATASET *dataset;       /* pointer to dataset struct */
    int *list;              /* list of regressors etc. */
    MODEL *model;           /* pointer to model struct */
    PRN *prn;               /* pointer to struct for printing */
    int model_count = 0;    /* keep a tally of models estimated */
    int i;                  /* index variable */

    /* the first data series to load */
    double z1[] = { 
	199.9, 228, 235, 285, 239, 293, 285, 
	365, 295, 290, 385, 505, 425, 415 
    };
    /* and the second series */
    double z2[] = { 
	1065, 1254, 1300, 1577, 1600, 1750, 1800,
	1870, 1935, 1948, 2254, 2600, 2800, 3000
    };

    /* basic initialization of library */
    libgretl_init();

    prn = gretl_print_new(GRETL_PRINT_STDOUT, NULL); /* simple printing */

    /* allocate the dataset struct: the parameters are the number of
       variables (here 3, allowing for the constant in position 0),
       the number of observations on each variable (here 14), and
       a 0/1 flag indicating whether we want to supply "case marker" 
       strings for the observations (here we don't).
    */
    dataset = create_new_dataset(3, 14, 0);
    if (dataset == NULL) noalloc();

    /* copy in the names of the variables (starting at [1]
       because [0] refers to the constant) */
    strcpy(dataset->varname[1], "price");
    strcpy(dataset->varname[2], "sqft");

    /* Fill in the data array, starting at variable 1. Note that 
       this array may be a superset of the data actually used in 
       the regression equation. Note that dataset->n records the
       number of observations.
    */

    for (i=0; i<dataset->n; i++) {
	dset_set_data(dataset, 1, i, z1[i]);
	dset_set_data(dataset, 2, i, z2[i]);
    }

    /* Set up the "list", which is fed to the regression function.
       The first element of list represents the length of the list
       vector itself, counting from zero.  The second entry is the ID
       number of the dependent variable (i.e. its place in the data
       set Z) counting from one (zero being reserved for the
       constant).  The third entry (and there can be more) is the ID
       number of the first independent variable.
    */

    list = gretl_list_new(3); /* number of terms will be 3 */
    list[1] = 1;   /* the dependent variable is the one with ID# 1 */
    list[2] = 0;   /* we include a constant (ID# 0) */
    list[3] = 2;   /* the independent variable has ID# 2 */

    /* Now we call the lsq function from libgretl to get least squares 
       estimates and associated statistics. */
    model = gretl_model_new();
    if (model == NULL) noalloc();
    *model = lsq(list,     /* regressand and regressors */
		 dataset,  /* the dataset */
		 OLS,      /* use Ordinary Least Squares */
		 OPT_NONE  /* no special options */
		 );

    /* Handle case where lsq bombed */
    if (model->errcode) {
        printf("model->errcode: %d\n", model->errcode);
        printf("error message: %s\n", gretl_errmsg_get());
        return 1;
    }

    /* Otherwise give this model an ID number for reference */
    model->ID = ++model_count;

    /* and print the regression results */
    printmodel(model, dataset, OPT_NONE, prn);

    /* memory management check -- try explicitly freeing all allocated
       memory */
    gretl_model_free(model);
    free(list);
    destroy_dataset(dataset); 
    gretl_print_destroy(prn);

    libgretl_cleanup();

    return 0;
}