static inline wbExport_t wbExport_open(const char *file, wbExportKind_t kind) { wbExport_t exprt; if (file == NULL) { wbLog(ERROR, "Go NULL for file value."); wbExit(); } wbExport_setFile(exprt, NULL); wbExport_setKind(exprt, kind); if (kind == wbExportKind_raw) { wbExportRaw_t raw = wbExportRaw_new(); wbExportRaw_setFile(raw, file); wbExport_setRaw(exprt, raw); } else if (kind == wbExportKind_tsv || kind == wbExportKind_csv) { wbExportCSV_t csv = wbExportCSV_new(); if (kind == wbExportKind_csv) { wbExportCSV_setSeperator(csv, ','); } else { wbExportCSV_setSeperator(csv, '\t'); } wbExportCSV_setFile(csv, file); wbExport_setCSV(exprt, csv); } else if (kind == wbExportKind_ppm) { wbExport_setFile(exprt, wbString_duplicate(file)); } else { wbLog(ERROR, "Invalid export type."); wbExit(); } return exprt; }
static inline wbImport_t wbImport_open(const char * file, const char * type0) { wbImport_t imp; wbImportKind_t kind; char * type; type = wbString_toLower(type0); if (wbString_sameQ(type, "cvs")) { kind = wbImportKind_csv; } else if (wbString_sameQ(type, "tsv")) { kind = wbImportKind_tsv; } else if (wbString_sameQ(type, "raw") || wbString_sameQ(type, "dat")) { kind = wbImportKind_raw; } else if (wbString_sameQ(type, "ppm")) { kind = wbImportKind_ppm; } else { wbLog(ERROR, "Invalid import type ", type0); wbExit(); } imp = wbImport_open(file, kind); wbDelete(type); return imp; }
static inline void wbExport_write(wbExport_t exprt, void *data, int rows, int columns, char sep, wbType_t type) { wbExportKind_t kind; kind = wbExport_getKind(exprt); if (kind == wbExportKind_tsv || kind == wbExportKind_csv) { wbExportCSV_t csv = wbExport_getCSV(exprt); wbExportCSV_write(csv, data, rows, columns, sep, type); } else if (kind == wbExportKind_raw) { wbExportRaw_t raw = wbExport_getRaw(exprt); wbExportRaw_write(raw, data, rows, columns, type); } else if (kind == wbExportKind_text) { wbExportText_t text = wbExport_getText(exprt); if (columns == 0) { columns = 1; } if (rows == 0) { rows = 1; } wbExportText_write(text, (const char *)data, rows * columns); } else { wbLog(ERROR, "Invalid export type."); wbExit(); } return; }
static inline void wbExport_close(wbExport_t exprt) { wbExportKind_t kind; kind = wbExport_getKind(exprt); if (wbExport_getFile(exprt)) { wbDelete(wbExport_getFile(exprt)); } if (kind == wbExportKind_tsv || kind == wbExportKind_csv) { wbExportCSV_t csv = wbExport_getCSV(exprt); wbExportCSV_delete(csv); wbExport_setCSV(exprt, NULL); } else if (kind == wbExportKind_raw) { wbExportRaw_t raw = wbExport_getRaw(exprt); wbExportRaw_delete(raw); wbExport_setRaw(exprt, NULL); } else if (kind == wbExportKind_text) { wbExportText_t text = wbExport_getText(exprt); wbExportText_delete(text); wbExport_setText(exprt, NULL); } else if (kind == wbExportKind_ppm) { } else { wbLog(ERROR, "Invalid export type."); wbExit(); } return; }
static int wbFile_nextIndex(void) { int ii; for (ii = 0; ii < wbFile_maxCount; ii++) { if (wbFile_handles[ii] == NULL) { return ii; } } wbLog(ERROR, "Ran out of file handles."); wbExit(); return -1; }
static inline wbImport_t wbImport_open(const char * file, wbImportKind_t kind) { wbImport_t imp; if (file == NULL) { wbLog(ERROR, "Go NULL for file value."); wbExit(); } if (!wbFile_existsQ(file)) { wbLog(ERROR, "File ", file, " does not exist."); wbExit(); } wbImport_setKind(imp, kind); if (kind == wbImportKind_raw) { wbImportRaw_t raw = wbImportRaw_new(); wbImportRaw_setFile(raw, file); wbImport_setRaw(imp, raw); } else if (kind == wbImportKind_tsv || kind == wbImportKind_csv) { wbImportCSV_t csv = wbImportCSV_new(); if (kind == wbImportKind_csv) { wbImportCSV_setSeperator(csv, ','); } else { wbImportCSV_setSeperator(csv, '\t'); } wbImportCSV_setFile(csv, file); wbImport_setCSV(imp, csv); } else if (kind == wbImportKind_ppm) { wbImage_t img = wbPPM_import(file); wbImport_setImage(imp, img); } else { wbLog(ERROR, "Invalid import type."); wbExit(); } return imp; }
static void * wbImport(const char * file, int * resRows, int * resColumns, const char * type) { void * data, * res; wbImport_t imp; size_t sz; int columns = 0, rows = 0; wbImportKind_t kind; if (file == NULL) { fprintf(stderr, "Failed to import file.\n"); wbExit(); } kind = _parseImportExtension(file); wbAssert(kind != wbImportKind_unknown); imp = wbImport_open(file, kind); if (wbString_sameQ(type, "Real")) { data = wbImport_readAsReal(imp); sz = sizeof(wbReal_t); } else { data = wbImport_readAsInteger(imp); sz = sizeof(int); } if (kind == wbImportKind_csv || kind == wbImportKind_tsv) { rows = wbImportCSV_getRowCount(wbImport_getCSV(imp)); columns = wbImportCSV_getColumnCount(wbImport_getCSV(imp)); } else if (kind == wbImportKind_raw) { rows = wbImportRaw_getRowCount(wbImport_getRaw(imp)); columns = wbImportRaw_getColumnCount(wbImport_getRaw(imp)); } if (resRows != NULL) { *resRows = rows; } if (resColumns != NULL) { *resColumns = columns; } res = wbMalloc(sz * rows * columns); memcpy(res, data, sz * rows * columns); wbImport_close(imp); return res; }
static inline void * wbImport_read(wbImport_t imp, wbBool asIntegerQ) { void * data = NULL; wbImportKind_t kind; kind = wbImport_getKind(imp); if (kind == wbImportKind_tsv || kind == wbImportKind_csv) { wbImportCSV_t csv = wbImport_getCSV(imp); wbImportCSV_read(csv, asIntegerQ); data = wbImportCSV_getData(csv); } else if (kind == wbImportKind_raw) { wbImportRaw_t raw = wbImport_getRaw(imp); wbImportRaw_read(raw, asIntegerQ); data = wbImportRaw_getData(raw); } else { wbLog(ERROR, "Invalid import type."); wbExit(); } return data; }
static inline void wbImport_close(wbImport_t imp) { wbImportKind_t kind; kind = wbImport_getKind(imp); if (kind == wbImportKind_tsv || kind == wbImportKind_csv) { wbImportCSV_t csv = wbImport_getCSV(imp); wbImportCSV_delete(csv); wbImport_setCSV(imp, NULL); } else if (kind == wbImportKind_raw) { wbImportRaw_t raw = wbImport_getRaw(imp); wbImportRaw_delete(raw); wbImport_setRaw(imp, NULL); } else if (kind == wbImportKind_ppm) { } else { wbLog(ERROR, "Invalid import type."); wbExit(); } return ; }
wbImage_t wbImport(const char * file) { wbImage_t img; wbImport_t imp; wbImportKind_t kind; if (file == NULL) { fprintf(stderr, "Failed to import file.\n"); wbExit(); } kind = _parseImportExtension(file); wbAssert(kind == wbImportKind_ppm); imp = wbImport_open(file, kind); img = wbImport_getImage(imp); wbImport_close(imp); return img; }