示例#1
0
int gretl_read_foreign_data (const char *fname, GretlFileType file_type,
			     DATASET *dset, PRN *prn)
{
    int err = 0;

    if (fname == NULL || dset == NULL) {
	err = E_INVARG;
    } else if (dset->Z != NULL) {
	fprintf(stderr, "gretl_read_foreign_data: Z must be NULL on entry\n");
	err = E_INVARG;
    } if (file_type == GRETL_CSV) {
	err = import_csv(fname, dset, OPT_NONE, prn);
    } else if (SPREADSHEET_IMPORT(file_type)) {
	err = import_spreadsheet(fname, file_type, NULL, NULL,
				 dset, OPT_NONE, prn);
    } else if (OTHER_IMPORT(file_type)) {
	err = import_spreadsheet(fname, file_type, NULL, NULL,
				 dset, OPT_NONE, prn);
    } else {	
	gretl_errmsg_set("Unknown data import type");
	err = E_INVARG;
    }

    return err;
}
示例#2
0
void MYMENU_FileOpen()
{
    OPENFILENAMEA ofn;
    ZeroMemory( &ofn, sizeof( ofn ) );
    
    ofn.lStructSize = sizeof( ofn ); // SEE NOTE BELOW
    ofn.hwndOwner = hwnd;
    ofn.lpstrFilter = "CSV Files (*.csv)\0*.csv\0Kiss Files (*.kiss)\0*.kiss\0All Files (*.*)\0*.*\0";
    ofn.lpstrFile = szFileName;
    ofn.nMaxFile = MAX_PATH;
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    ofn.lpstrDefExt = "csv";
    
    if ( GetOpenFileName( &ofn ) ) {
        if ( prompt_save_changes() ) {
            FILE* fp = fopen( szFileName, "r" );
            if ( fp == NULL )
                return;
            free_table( table );
            free_history();
            // parse
            table = create_table();
            if ( strncmp( ofn.lpstrFile + ofn.nFileExtension, "csv", 3 ) == 0 )
                import_csv( fp, table );
            else if ( strncmp( ofn.lpstrFile + ofn.nFileExtension, "kiss", 4 ) == 0 )
                import_kiss( fp, table );
            fclose( fp );
            set_caption( szFileName );
            auto_adapt_layout( table );
            update_selection_rect();
            update_table_view( hwnd );
            unsaved = 0;
        }
    }
}
示例#3
0
文件: bask_import.c 项目: marc-q/bask
/*
	Function: import_csv_cmd (bask_core* tcore, struct bask_task** first, char* filename);
	Description: Cmd handle for importing tasks from a csv formated file.
	InitVersion: 0.0.1
*/
void import_csv_cmd (bask_core* tcore, struct bask_task** first, char* filename)
{
	if (import_csv (tcore, first, filename) == 0)
	{
		import_msg_success (filename);
	}
	
	export_baskbin (tcore, first, tcore->path_baskbin);
}
示例#4
0
文件: gretlcli.c 项目: aylusltd/gretl
static int cli_open_append (CMD *cmd, DATASET *dset, 
			    MODEL *model, PRN *prn)
{
    gretlopt opt = cmd->opt;
    PRN *vprn = prn;
    char newfile[MAXLEN] = {0};
    char response[3];
    int http = 0, dbdata = 0;
    int ftype;
    int err = 0;

    if (opt & OPT_K) {
	/* --frompkg=whatever */
	err = get_package_data_path(cmd->param, newfile);
	if (err) {
	    errmsg(err, prn);
	    return err;
	}
    } else {
	err = cli_try_http(cmd->param, newfile, &http);
	if (err) {
	    errmsg(err, prn);
	    return err;
	}

	if (!http && !(opt & OPT_O)) {
	    /* not using http or ODBC */
	    err = get_full_filename(cmd->param, newfile, (opt & OPT_W)?
				    OPT_W : OPT_NONE);
	    if (err) {
		errmsg(err, prn);
		return err;
	    }
	}
    }

    if (opt & OPT_W) {
	ftype = GRETL_NATIVE_DB_WWW;
    } else if (opt & OPT_O) {
	ftype = GRETL_ODBC;
    } else {
	ftype = detect_filetype(newfile, OPT_P);
    }

    dbdata = (ftype == GRETL_NATIVE_DB || ftype == GRETL_NATIVE_DB_WWW ||
	      ftype == GRETL_RATS_DB || ftype == GRETL_PCGIVE_DB ||
	      ftype == GRETL_ODBC);

    if (data_status && !batch && !dbdata && cmd->ci != APPEND &&
	strcmp(newfile, datafile)) {
	fprintf(stderr, _("Opening a new data file closes the "
			  "present one.  Proceed? (y/n) "));
	if (fgets(response, sizeof response, stdin) != NULL && 
	    *response != 'y' && *response != 'Y') {
	    pprintf(prn, _("OK, staying with current data set\n"));
	    return 0;
	}
    }

    if (!dbdata && cmd->ci != APPEND) {
	cli_clear_data(cmd, dset, model);
    }

    if (opt & OPT_Q) {
	/* --quiet, but in case we hit any problems below... */
	vprn = gretl_print_new(GRETL_PRINT_BUFFER, NULL);
    }

    if (ftype == GRETL_XML_DATA || ftype == GRETL_BINARY_DATA) {
	err = gretl_read_gdt(newfile, dset, opt, vprn);
    } else if (ftype == GRETL_CSV) {
	err = import_csv(newfile, dset, opt, vprn);
    } else if (SPREADSHEET_IMPORT(ftype)) {
	err = import_spreadsheet(newfile, ftype, cmd->list, cmd->parm2,
				 dset, opt, vprn);
    } else if (OTHER_IMPORT(ftype)) {
	err = import_other(newfile, ftype, dset, opt, vprn);
    } else if (ftype == GRETL_ODBC) {
	err = set_odbc_dsn(cmd->param, vprn);
    } else if (dbdata) {
	err = set_db_name(newfile, ftype, vprn);
    } else {
	err = gretl_get_data(newfile, dset, opt, vprn);
    }

    if (vprn != prn) {
	if (err) {
	    /* The user asked for --quiet operation, but something
	       went wrong so let's print any info we got on
	       vprn.
	    */
	    const char *buf = gretl_print_get_buffer(vprn);

	    if (buf != NULL && *buf != '\0') {
		pputs(prn, buf);
	    }
	} else {
	    /* print minimal success message */
	    pprintf(prn, _("Read datafile %s\n"), newfile);
	}
	gretl_print_destroy(vprn);
    }

    if (err) {
	errmsg(err, prn);
	return err;
    }

    if (!dbdata && !http && cmd->ci != APPEND) {
	strncpy(datafile, newfile, MAXLEN - 1);
    }

    data_status = 1;

    if (dset->v > 0 && !dbdata && !(opt & OPT_Q)) {
	varlist(dset, prn);
    }

    if (http) {
	remove(newfile);
    }

    return err;
}
示例#5
0
文件: gretlcli.c 项目: aylusltd/gretl
static void handle_datafile (char *filearg, char *runfile,
			     DATASET *dset, PRN *prn,
			     PRN *cmdprn)
{
    char given_file[MAXLEN];
    int load_datafile = 1;
    int ftype, err = 0;

    strcpy(given_file, filearg);
    strcpy(datafile, filearg);

    ftype = detect_filetype(datafile, OPT_P);

    switch (ftype) {
    case GRETL_UNRECOGNIZED:
    case GRETL_NATIVE_DB:
    case GRETL_RATS_DB:
	exit(EXIT_FAILURE);
	break;
    case GRETL_XML_DATA:
    case GRETL_BINARY_DATA:
	err = gretl_read_gdt(datafile, dset, OPT_NONE, prn);
	break;
    case GRETL_CSV:
	err = import_csv(datafile, dset, OPT_NONE, prn);
	break;
    case GRETL_XLS:
    case GRETL_GNUMERIC:
    case GRETL_ODS:
	err = import_spreadsheet(datafile, ftype, NULL, NULL,
				 dset, OPT_NONE, prn);
	break;
    case GRETL_DTA:
    case GRETL_SAV:
    case GRETL_SAS:
    case GRETL_JMULTI:
    case GRETL_OCTAVE:
    case GRETL_WF1:
	err = import_other(datafile, ftype, dset, 
			   OPT_NONE, prn);
	break;
    case GRETL_SCRIPT:
	runit = 1;
	strcpy(runfile, datafile); 
	memset(datafile, 0, sizeof datafile);
	load_datafile = 0;
	break;
    default:
	break;
    }

    if (load_datafile) {
	if (err) {
	    errmsg(err, prn);
	    if (err == E_FOPEN) {
		show_paths();
	    }
	    exit(EXIT_FAILURE);
	}
	data_status = 1;
	if (!batch) { 
	    pprintf(cmdprn, "open %s\n", given_file);
	}
    }
}
示例#6
0
int main(int argc, char** argv)
{
        int arg1 = 1;

        while (1) {
                int option_index = 0;

                static struct option long_options[] = {
                        {"help", 0, 0, 'h'},
                        {"version", 0, 0, 'v'},
                        {"dir", 1, 0, 'd'},
                        {0, 0, 0, 0}
                };

                int c = getopt_long(argc, argv, "d:hv", 
                                long_options, &option_index);
                if (c == -1) {
                        break;
                }

                switch (c) {
                case 0:
                        printf ("option %s", long_options[option_index].name);
                        if (optarg) {
                                printf (" with arg %s", optarg);
                        }
                        printf ("\n");
                        break;
                case 'd':
                        _dir = optarg;
                        break;
                case 'v':
                        print_version(); /* and don't come back */
                        break;
                case 'h':
                        print_help();
                        break;
                case '?':
                        printf ("Unknown option %c\n", optopt);
                        print_usage();
                        exit(0);
                        break;
                default:
                        printf ("?? getopt returned character code 0%o ??\n", c);
                }
        }

        arg1 = optind;

        if (arg1 >= argc)
                print_usage();

        //---------------------------------------

        osdb_t* osdb = osdb_init();
        osdb_set_option(osdb, "dir", _dir);

        //---------------------------------------

        if (strcmp(argv[arg1], "import") == 0) {
                if (arg1 + 1 < argc) {
                        import_csv(argv[arg1 + 1]);
                }

        } else if (strcmp(argv[arg1], "timeseries") == 0) {
                if (arg1 + 1 < argc) {
                        uint32_t id = atol(argv[arg1 + 1]);
                        list_timeseries(id);
                }

        } else if (strcmp(argv[arg1], "select") == 0) {
                if (arg1 + 3 < argc) {
                        uint32_t id = atol(argv[arg1 + 1]);
                        timestamp_t start = atof(argv[arg1 + 2]);
                        timestamp_t end = atof(argv[arg1 + 3]);
                        list_datapoints(osdb, id, start, end);
                }

        } else if (strcmp(argv[arg1], "delete") == 0) {
                if (arg1 + 3 < argc) {
                        uint32_t id = atol(argv[arg1 + 1]);
                        timestamp_t start = atof(argv[arg1 + 2]);
                        timestamp_t end = atof(argv[arg1 + 3]);
                        delete_datapoints(osdb, id, start, end);
                }
        }

        osdb_finalize(osdb);

        return 0;
}