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; }
static int make_graphpage_tex (void) { char *fname; FILE *fp; int err = 0; fname = gpage_fname(".tex", 0); fp = gretl_fopen(fname, "w"); if (fp == NULL) { return 1; } doctop(fp); err = geomline(gpage.ngraphs, fp); if (!err) { common_setup(fp); err = tex_graph_setup(gpage.ngraphs, fp); } if (!err) { common_end(fp); } fclose(fp); return err; }
static int add_unique_output_file (MODEL *pmod, const char *path) { char outname[FILENAME_MAX]; char unique[FILENAME_MAX]; char line[256]; FILE *f0, *f1; sprintf(outname, "%s.out", path); f0 = gretl_fopen(outname, "r"); if (f0 == NULL) { return E_FOPEN; } sprintf(unique, "%s.XXXXXX", outname); f1 = gretl_mktemp(unique, "w"); if (f1 == NULL) { fclose(f0); return E_FOPEN; } while (fgets(line, sizeof line, f0)) { fputs(line, f1); } fclose(f0); fclose(f1); gretl_remove(outname); gretl_model_set_data(pmod, "x12a_output", g_strdup(unique), GRETL_TYPE_STRING, strlen(unique) + 1); return 0; }
int gretl_bundle_write_to_file (gretl_bundle *b, const char *fname, int to_dotdir) { char fullname[FILENAME_MAX]; FILE *fp; int err = 0; if (to_dotdir) { build_path(fullname, gretl_dotdir(), fname, NULL); } else { strcpy(fullname, fname); gretl_maybe_switch_dir(fullname); } fp = gretl_fopen(fullname, "wb"); if (fp == NULL) { err = E_FOPEN; } else { gretl_xml_header(fp); gretl_bundle_serialize(b, "temp", fp); } return err; }
static int get_roots (const char *fname, MODEL *pmod, arma_info *ainfo) { FILE *fp; char line[132]; int i, nr, err = 0; cmplx *roots; nr = ainfo->p + ainfo->q + ainfo->P + ainfo->Q; if (nr == 0) { return 0; } roots = malloc(nr * sizeof *roots); if (roots == NULL) { return E_ALLOC; } fp = gretl_fopen(fname, "r"); if (fp == NULL) { fprintf(stderr, "Couldn't read from '%s'\n", fname); free(roots); return E_FOPEN; } gretl_push_c_numeric_locale(); i = 0; while (fgets(line, sizeof line, fp) && i < nr) { double re, im; if (!strncmp(line, "AR", 2) || !strncmp(line, "MA", 2)) { if (sscanf(line, "%*s %*s %*s %lf %lf", &re, &im) == 2) { roots[i].r = re; roots[i].i = im; i++; } } } gretl_pop_c_numeric_locale(); fclose(fp); if (i != nr) { fprintf(stderr, "Error reading '%s'\n", fname); free(roots); roots = NULL; err = E_DATA; } if (roots != NULL) { gretl_model_set_data(pmod, "roots", roots, GRETL_TYPE_CMPLX_ARRAY, nr * sizeof *roots); } return err; }
static size_t gretl_write_func (void *buf, size_t size, size_t nmemb, void *data) { urlinfo *u = (urlinfo *) data; size_t bgot = size * nmemb; size_t ret = 0; #if WDEBUG > 1 fprintf(stderr, "write_func: size = %d, nmemb = %d\n", (int) size, (int) nmemb); #endif if (u == NULL || u->err) { return 0; } if (u->saveopt == SAVE_TO_FILE) { if (u->fp == NULL) { u->fp = gretl_fopen(u->localfile, "wb"); if (u->fp == NULL) { u->err = E_FOPEN; return 0; } } ret = fwrite(buf, size, nmemb, u->fp); } else if (u->saveopt == SAVE_TO_BUFFER) { if (u->getbuf == NULL) { u->getbuf = calloc(WBUFSIZE, 1); #if WDEBUG fprintf(stderr, "u->getbuf started at %p\n", (void *) u->getbuf); #endif if (u->getbuf == NULL) { u->err = E_ALLOC; return 0; } u->buflen = WBUFSIZE; } if (u->datalen + bgot > u->buflen) { u->err = grow_read_buffer(u, bgot); if (u->err) { return 0; } } memcpy(u->getbuf + u->datalen, buf, bgot); ret = nmemb; } if (ret != 0) { u->datalen += ret * size; } return ret; }
static FILE *cli_gretlnet_open (const char *prog) { FILE *fp = NULL; set_gretlnet_filename(prog); if (*netfile != '\0') { fp = gretl_fopen(netfile, "r"); } return fp; }
static void print_x12a_error_file (const char *fname, PRN *prn) { FILE *fp = gretl_fopen(fname, "r"); if (fp != NULL) { char line[512]; while (fgets(line, sizeof line, fp)) { pputs(prn, line); } fclose(fp); } }
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; }
static int get_x12a_vcv (const char *fname, MODEL *pmod, int nc) { FILE *fp; char line[1024], valstr[24]; double x; int i, j, k, nt = (nc * nc + nc) / 2; int err = 0; fp = gretl_fopen(fname, "r"); if (fp == NULL) return 1; pmod->vcv = malloc(nt * sizeof *pmod->vcv); if (pmod->vcv == NULL) { fclose(fp); return 1; } for (i=0; i<nt; i++) { pmod->vcv[i] = NADBL; } gretl_push_c_numeric_locale(); j = 1; while (fgets(line, sizeof line, fp)) { if (!strncmp(line, "Nonseas", 7)) { char *p = line + strcspn(line, "+-"); for (i=1; i<nc; i++) { sscanf(p, "%22s", valstr); p += 22; if (i >= j) { x = atof(valstr); k = ijton(i, j, nc); pmod->vcv[k] = x; } } j++; } } gretl_pop_c_numeric_locale(); fclose(fp); return err; }
FILE *gretl_print_read_tempfile (PRN *prn) { FILE *fp = NULL; if (prn->fp != NULL) { fflush(prn->fp); } if (prn->fname != NULL) { fp = gretl_fopen(prn->fname, "r"); if (fp != NULL && prn->savepos > 0) { fseek(fp, (long) prn->savepos, SEEK_SET); } } return fp; }
static int get_uhat (const char *fname, MODEL *pmod, const DATASET *dset) { FILE *fp; char line[64], date[9]; double x; int t, start = 0, nobs = 0; int err = 0; fp = gretl_fopen(fname, "r"); if (fp == NULL) { fprintf(stderr, "Couldn't read from '%s'\n", fname); return E_FOPEN; } gretl_push_c_numeric_locale(); while (fgets(line, sizeof line, fp)) { if (*line == '-') { start = 1; continue; } if (start && sscanf(line, "%s %lf", date, &x) == 2) { t = x12_date_to_n(date, dset); if (t >= 0 && t < dset->n) { pmod->uhat[t] = x; nobs++; } } } gretl_pop_c_numeric_locale(); fclose(fp); if (nobs == 0) { fprintf(stderr, "Error reading '%s'\n", fname); err = E_DATA; } return err; }
static int get_ll_stats (const char *fname, MODEL *pmod) { FILE *fp; char line[80], statname[12]; int nefobs = 0; double x; fp = gretl_fopen(fname, "r"); if (fp == NULL) { fprintf(stderr, "Couldn't read from '%s'\n", fname); return E_FOPEN; } pmod->sigma = NADBL; gretl_push_c_numeric_locale(); while (fgets(line, sizeof line, fp)) { if (sscanf(line, "%11s %lf", statname, &x) == 2) { if (!strcmp(statname, "nefobs")) nefobs = (int) x; else if (!strcmp(statname, "var")) pmod->sigma = sqrt(x); else if (!strcmp(statname, "lnlkhd")) pmod->lnL = x; else if (!strcmp(statname, "aic")) pmod->criterion[C_AIC] = x; else if (!strcmp(statname, "bic")) pmod->criterion[C_BIC] = x; else if (!strcmp(statname, "hnquin")) pmod->criterion[C_HQC] = x; } } gretl_pop_c_numeric_locale(); fclose(fp); if (nefobs > 0) { pmod->nobs = nefobs; pmod->t1 = pmod->t2 - nefobs + 1; } return 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; }
int midi_play_graph (const char *fname, const char *userdir, const char *midiplayer) { char outname[FILENAME_MAX]; midi_spec spec; midi_track track; dataset dset; sprintf(outname, "%sgretl.mid", userdir); spec.fp = gretl_fopen(outname, "wb"); if (spec.fp == NULL) { fprintf(stderr, "Couldn't write to '%s'\n", outname); return 1; } if (read_datafile(fname, &dset)) { audio_graph_error("Error reading data file"); fclose(spec.fp); return 1; } spec.ntracks = 1 + dset.series2; spec.nticks = 96; spec.dset = &dset; spec.nsecs = 8; four_four_header(&spec); print_dataset_comments(&dset); play_dataset(&spec, &track, &dset); dataset_free(&dset); fclose(spec.fp); midi_fork(outname, midiplayer); return 0; }
static int print_iterations (const char *path, PRN *prn) { char fname[MAXLEN]; FILE *fp; char line[129]; int print = 0; int err = 0; sprintf(fname, "%s.out", path); fp = gretl_fopen(fname, "r"); if (fp == NULL) { fprintf(stderr, "Couldn't read from '%s'\n", fname); err = 1; } else { while (fgets(line, sizeof line, fp)) { if (!strncmp(line, " MODEL EST", 10)) print = 1; if (print) pputs(prn, line); if (!strncmp(line, " Estimatio", 10)) break; } fclose(fp); } return err; }
static int write_arma_spc_file (const char *fname, const DATASET *dset, arma_info *ainfo, int pdmax, gretlopt opt) { int maxobs = pdmax * 50; int maxfcast = pdmax * 5; int ylist[2]; int *xlist = NULL; FILE *fp; char datestr[12]; int nfcast = 0; int t1 = ainfo->t1; int tmax; int i; if (ainfo->nexo > 0) { xlist = arma_info_get_x_list(ainfo); if (xlist == NULL) { return E_ALLOC; } } fp = gretl_fopen(fname, "w"); if (fp == NULL) { fprintf(stderr, "Couldn't write to '%s'\n", fname); return 1; } gretl_push_c_numeric_locale(); fprintf(fp, "series{\n title=\"%s\"\n period=%d\n", dset->varname[ainfo->yno], dset->pd); t1 -= ainfo->maxlag; make_x12a_date_string(t1, dset, datestr); fprintf(fp, " start=%s\n", datestr); ylist[0] = 1; ylist[1] = ainfo->yno; tmax = ainfo->t2; if ((opt & OPT_F) && ainfo->t2 < dset->n - 1) { int nobs; tmax = dset->n - 1; nobs = tmax - ainfo->t1 + 1; /* t1? */ if (nobs > maxobs) { tmax -= nobs - maxobs; } nfcast = tmax - ainfo->t2; if (nfcast > maxfcast) { tmax -= nfcast - maxfcast; nfcast -= nfcast - maxfcast; } #if 0 fprintf(stderr, "x12a: doing forecast: nfcast = %d\n", nfcast); #endif } output_series_to_spc(ylist, dset, t1, tmax, fp); if (tmax > ainfo->t2) { make_x12a_date_string(ainfo->t2, dset, datestr); fprintf(fp, " span = ( , %s)\n", datestr); } fputs("}\n", fp); /* regression specification */ fputs("Regression {\n", fp); if (ainfo->ifc) { fputs(" variables = (const)\n", fp); } if (ainfo->nexo > 0) { fputs(" user = ( ", fp); for (i=1; i<=xlist[0]; i++) { fprintf(fp, "%s ", dset->varname[xlist[i]]); } fputs(")\n", fp); output_series_to_spc(xlist, dset, t1, tmax, fp); free(xlist); } fputs("}\n", fp); /* arima specification */ fputs("arima {\n model = ", fp); x12_pdq_string(ainfo, fp); if (ainfo->P > 0 || ainfo->Q > 0) { fprintf(fp, "(%d %d %d)\n}\n", ainfo->P, ainfo->D, ainfo->Q); } else { fputs("\n}\n", fp); } fputs("estimate {\n", fp); /* could enable here: "tol = XX, maxiter = NN" the default is tol = 1.0e-5, maxiter = 200 */ if (opt & OPT_V) { fputs(" print = (acm itr lkf lks mdl est rts rcm)\n", fp); } else { fputs(" print = (acm lkf lks mdl est rts rcm)\n", fp); } fputs(" save = (rsd est lks acm rts rcm)\n", fp); if (opt & OPT_C) { fputs(" exact = none\n", fp); } fputs("}\n", fp); if (nfcast > 0) { fputs("forecast {\n save = (ftr)\n", fp); fprintf(fp, " maxlead = %d\n}\n", nfcast); } gretl_pop_c_numeric_locale(); fclose(fp); return 0; }
int gretl_string_table_print (gretl_string_table *gst, DATASET *dset, const char *fname, PRN *prn) { series_table *st; const char *fshort; char stname[MAXLEN]; FILE *fp; int i, j, ncols = 0; int err = 0; if (gst == NULL) { return E_DATA; } strcpy(stname, "string_table.txt"); gretl_path_prepend(stname, gretl_workdir()); fp = gretl_fopen(stname, "w"); if (fp == NULL) { return E_FOPEN; } fshort = strrchr(fname, SLASH); if (fshort != NULL) { fprintf(fp, "%s\n", fshort + 1); } else { fprintf(fp, "%s\n", fname); } ncols = (gst->cols_list != NULL)? gst->cols_list[0] : 0; if (ncols > 0) { fputc('\n', fp); fputs(_("One or more non-numeric variables were found.\n" "These variables have been given numeric codes as follows.\n\n"), fp); if (gst->extra != NULL) { fputs(_("In addition, some mappings from numerical values to string\n" "labels were found, and are printed below.\n\n"), fp); } } for (i=0; i<ncols; i++) { int vi = gst->cols_list[i+1]; st = gst->cols[i]; if (i > 0) { fputc('\n', fp); } fprintf(fp, _("String code table for variable %d (%s):\n"), vi, dset->varname[vi]); for (j=0; j<st->n_strs; j++) { fprintf(fp, "%3d = '%s'\n", j+1, st->strs[j]); } if (dset->varinfo != NULL) { series_attach_string_table(dset, vi, st); gst->cols[i] = NULL; } } if (gst->extra != NULL) { fputs(gst->extra, fp); } pprintf(prn, _("String code table written to\n %s\n"), stname); fclose(fp); set_string_table_written(); return err; }
static PRN *real_gretl_print_new (PrnType ptype, const char *fname, char *buf, FILE *fp, int glib, int *perr) { PRN *prn = malloc(sizeof *prn); int err = 0; if (prn == NULL) { if (perr != NULL) { *perr = E_ALLOC; } return NULL; } prn->fp = NULL; prn->fplist = NULL; prn->buf = NULL; prn->bufsize = 0; prn->blen = 0; prn->savepos = -1; prn->format = GRETL_FORMAT_TXT; prn->fixed = 0; prn->delim = ','; prn->fname = NULL; if (ptype == GRETL_PRINT_STREAM) { prn->fp = fp; } else if (ptype == GRETL_PRINT_FILE) { prn->fp = gretl_fopen(fname, "wb"); if (prn->fp == NULL) { err = E_FOPEN; free(prn); prn = NULL; } } else if (ptype == GRETL_PRINT_TEMPFILE) { err = prn_add_tempfile(prn); if (err) { free(prn); prn = NULL; } else { prn->savepos = 0; } } else if (ptype == GRETL_PRINT_STDOUT) { prn->fp = stdout; } else if (ptype == GRETL_PRINT_STDERR) { prn->fp = stderr; } else if (ptype == GRETL_PRINT_BUFFER) { if (buf != NULL) { prn->buf = buf; prn->fixed = glib ? GBUF_FIXED : BUF_FIXED; #if PRN_DEBUG fprintf(stderr, "prn with fixed buffer\n"); #endif } else { int p = pprintf(prn, "@init"); if (p < 0) { err = E_ALLOC; free(prn); prn = NULL; } } } #if PRN_DEBUG fprintf(stderr, "real_gretl_print_new at %p: type=%d, fname='%s', " "buf=%p, fp=%p\n", (void *) prn, ptype, fname, (void *) buf, (void *) fp); #endif if (perr != NULL) { *perr = err; } return prn; }
static int gp_make_outfile (const char *gfname, int i, double scale) { char *fname; FILE *fp, *fq; int err = 0; fp = gretl_fopen(gfname, "r"); if (fp == NULL) { file_read_errbox(gfname); return E_FOPEN; } fname = gpage_fname(".plt", 0); fq = gretl_fopen(fname, "w"); if (fq == NULL) { file_write_errbox(fname); fclose(fp); return E_FOPEN; } gretl_push_c_numeric_locale(); /* FIXME: is "dashed" wanted when "mono" is given below? */ if (gpage.term == GP_TERM_PDF) { /* PDF output */ int fontsize = gp_cairo_fontsize(); fprintf(fq, "set term pdfcairo font \"sans,%d\"%s", fontsize, (gpage.mono)? " mono dashed" : " "); fname = gpage_fname(".pdf", i); } else { /* EPS output variants */ int fontsize = gp_cairo_fontsize(); fprintf(fq, "set term epscairo font \"sans,%d\"%s", fontsize, (gpage.mono)? " mono dashed" : " "); fname = gpage_fname(".ps", i); } if (scale != 1.0) { fprintf(fq, " size %g,%g\n", scale * 5.0, scale * 3.5); } else { fputc('\n', fq); } gretl_pop_c_numeric_locale(); fprintf(fq, "set output '%s'\n", fname); filter_gnuplot_file(0, gpage.mono, fp, fq); fclose(fp); fclose(fq); fname = gpage_fname(".plt", 0); err = gnuplot_compile(fname); return err; }
int maybe_rewrite_gp_file (const char *fname) { FILE *fin, *fout; gchar *trbuf, *modname = NULL; char line[512]; double gpver; int modified = 0; int recoded = 0; int fix_xrange = 0; int err = 0; fin = gretl_fopen(fname, "r"); if (fin == NULL) { return 1; } modname = g_strdup_printf("%s.tr", fname); fout = gretl_fopen(modname, "w"); if (fout == NULL) { fclose(fin); g_free(modname); return 1; } gpver = gnuplot_version(); while (fgets(line, sizeof line, fin)) { int modline = 0; if (!strncmp(line, "set missing", 11)) { fputs("set datafile missing \"?\"\n", fout); modline = 1; } else if (!strncmp(line, "# set term", 10)) { /* skip it */ modline = 1; } else if (!strncmp(line, "set term png", 12)) { if (maybe_adjust_cairo(line)) { fputs(line, fout); modline = 1; } } else if (!strncmp(line, "set xdata time", 14)) { int zy2000 = strstr(line, "ZERO_YEAR=2000") != NULL; if (gpver >= 4.7 && zy2000) { fputs("set xdata time\n", fout); modline = 1; fix_xrange = 1; } else if (gpver < 4.7 && !zy2000) { fputs("set xdata time # ZERO_YEAR=2000\n", fout); modline = 1; fix_xrange = 2; } } else if (!strncmp(line, "set xrange", 10) && fix_xrange) { do_fix_xrange(line, fix_xrange, fout); modline = 1; } if (!modline && !g_utf8_validate(line, -1, NULL)) { trbuf = gp_locale_to_utf8(line, !recoded); if (trbuf != NULL) { fputs(trbuf, fout); g_free(trbuf); } modline = recoded = 1; } if (modline) { modified = 1; } else { /* pass old line through */ fputs(line, fout); } } fclose(fin); fclose(fout); if (modified) { err = copyfile(modname, fname); } gretl_remove(modname); g_free(modname); return err; }
static int get_estimates (const char *fname, MODEL *pmod, arma_info *ainfo) { double *ar_coeff = pmod->coeff + ainfo->ifc; double *ma_coeff = ar_coeff + ainfo->np + ainfo->P; double *x_coeff = ma_coeff + ainfo->nq + ainfo->Q; double *ar_sderr = pmod->sderr + ainfo->ifc; double *ma_sderr = ar_sderr + ainfo->np + ainfo->P; double *x_sderr = ma_sderr + ainfo->nq + ainfo->Q; FILE *fp; char line[128], word[16]; double b, se; int i, j, k; int err = 0; fp = gretl_fopen(fname, "r"); if (fp == NULL) { fprintf(stderr, "Couldn't read from '%s'\n", fname); return E_FOPEN; } for (i=0; i<ainfo->nc; i++) { pmod->coeff[i] = pmod->sderr[i] = NADBL; } gretl_push_c_numeric_locale(); i = j = k = 0; while (fgets(line, sizeof line, fp) && i < ainfo->nc) { if (sscanf(line, "%15s", word) == 1) { if (!strcmp(word, "Constant")) { if (sscanf(line, "%*s %*s %lf %lf", &b, &se) == 2) { pmod->coeff[0] = b; pmod->sderr[0] = se; } } else if (!strcmp(word, "User-defined")) { if (sscanf(line, "%*s %*s %lf %lf", &b, &se) == 2) { x_coeff[i] = b; x_sderr[i] = se; i++; } } else if (!strcmp(word, "AR")) { if (sscanf(line, "%*s %*s %*s %*s %lf %lf", &b, &se) == 2) { ar_coeff[j] = b; ar_sderr[j] = se; j++; } } else if (!strcmp(word, "MA")) { if (sscanf(line, "%*s %*s %*s %*s %lf %lf", &b, &se) == 2) { ma_coeff[k] = -b; /* MA sign conventions */ ma_sderr[k] = se; k++; } } } } gretl_pop_c_numeric_locale(); fclose(fp); for (i=0; i<ainfo->nc; i++) { if (na(pmod->coeff[i]) || na(pmod->sderr[i])) { fprintf(stderr, "Error reading '%s'\n", fname); err = E_DATA; break; } } return err; }
static int read_datafile (const char *fname, dataset *dset) { #ifdef PLAY_AUTOFIT_LINE int fitline = 0; #endif char line[256]; int i, err = 0; int got_e = 0, y2data = 0; FILE *fdat; dataset_init(dset); fdat = gretl_fopen(fname, "r"); if (fdat == NULL) { fprintf(stderr, "Couldn't open '%s'\n", fname); return 1; } else { fprintf(stderr, "Reading %s...\n", fname); } while (fgets(line, sizeof line, fdat)) { tailstrip(line); if (get_comment(line, dset)) { continue; } else if (!strcmp(line, "e")) { fprintf(stderr, "Got end of data marker\n"); got_e++; if (got_e == 2) { /* can't handle more than two series! */ break; } } else if (strstr(line, "automatic fitted")) { #ifdef PLAY_AUTOFIT_LINE fitline = 1; #endif continue; } else if (isdigit((unsigned char) line[0])) { if (strstr(line, "title")) { #ifdef PLAY_AUTOFIT_LINE if (fitline) { get_fit_params(line, dset); } #endif continue; } if (!got_e) { dset->n += 1; } else if (!y2data) { y2data = 1; } } } if (dset->n == 0) { fprintf(stderr, "No data in '%s'\n", fname); err = 1; goto bailout; } dset->points = malloc(dset->n * sizeof *dset->points); if (dset->points == NULL) { err = 1; fputs("Out of memory\n", stderr); goto bailout; } if (y2data) { dset->y2 = malloc(dset->n * sizeof *dset->y2); if (dset->y2 == NULL) { err = 1; fputs("Out of memory\n", stderr); goto bailout; } dset->series2 = 1; } rewind(fdat); i = got_e = 0; while (!err && fgets(line, 256, fdat)) { tailstrip(line); if (!strcmp(line, "e")) { got_e++; if (got_e == 2) { break; } i = 0; } else if (isdigit((unsigned char) line[0])) { double x, y; if (strstr(line, "title")) { continue; } if (get_data_x_y(line, &x, &y)) { fprintf(stderr, "Couldn't read data on line %d\n", i + 1); err = 1; } else { if (!got_e) { dset->points[i].x = x; dset->points[i].y = y; } else { dset->y2[i] = y; } } i++; } } bailout: fclose(fdat); if (err) { dataset_free(dset); } return err; }
int wf1_get_data (const char *fname, DATASET *dset, gretlopt opt, PRN *prn) { FILE *fp; DATASET *newset = NULL; unsigned offset; int nvread, ftype; int err = 0; fp = gretl_fopen(fname, "rb"); if (fp == NULL) { return E_FOPEN; } ftype = wf1_check_file_type(fp); if (ftype < 0) { fclose(fp); pputs(prn, "This file does not seem to be an EViews workfile\n"); return E_DATA; } if (ftype == 1) { pputs(prn, "EViews 7+ file: expect problems!\n"); } newset = datainfo_new(); if (newset == NULL) { pputs(prn, _("Out of memory\n")); fclose(fp); return E_ALLOC; } err = parse_wf1_header(fp, ftype, newset, &offset); if (err) { pputs(prn, _("Error reading workfile header\n")); free_datainfo(newset); fclose(fp); return err; } err = start_new_Z(newset, 0); if (err) { pputs(prn, _("Out of memory\n")); free_datainfo(newset); fclose(fp); return E_ALLOC; } err = read_wf1_variables(fp, ftype, offset, newset, &nvread, prn); if (err) { destroy_dataset(newset); } else { int merge = (dset->Z != NULL); int nvtarg = newset->v - 1; if (nvread < nvtarg) { dataset_drop_last_variables(newset, nvtarg - nvread); } if (fix_varname_duplicates(newset)) { pputs(prn, _("warning: some variable names were duplicated\n")); } err = merge_or_replace_data(dset, &newset, opt, prn); if (!err && !merge) { dataset_add_import_info(dset, fname, GRETL_WF1); } } fclose(fp); return err; }