frameT *read_csv (char *file_name) { FILE *fp; unsigned char buf[CSV_BUF_SIZE]; unsigned char header_buf[CSV_BUF_SIZE]; unsigned char *row[MAX_COLUMNS]; unsigned char *header[MAX_COLUMNS]; int ncol, cols; int n, i; frameT *frame; csvT *csv; csv = open_csv (file_name); fp = csv->fp; // Parse header n = csv_row_fread (fp, header_buf, CSV_BUF_SIZE, header, MAX_COLUMNS, ',', CSV_TRIM | CSV_QUOTES, &ncol); if (n <= 0) { fprintf (stderr, "Failed to read header from CSV file\n"); exit (-1); } // Parse the first data row to determine data types n = csv_row_fread (fp, buf, CSV_BUF_SIZE, row, ncol, ',', CSV_TRIM | CSV_QUOTES, &cols); if (n <= 0) { fprintf (stderr, "File has no data\n"); exit (-1); } frame = new_frame (file_name, ncol); frame->csv = csv; for (i=0; i<ncol; i++) init_column (frame, i, (char *)header[i], guess_type((char *) row[i])); for (i=0; i<ncol; i++) column_init_data (frame, i, csv->est_rows); frame->allocated_rows = csv->est_rows; frame->region_rows = (unsigned char *) malloc (frame->allocated_rows); if (!frame->region_rows) { fprintf (stderr, "Failed to allocate region_rows\n"); exit (-1); } frame->allocated_region_rows = frame->allocated_rows; if (csv->est_rows < 10000) { load_all_rows (frame); } else { load_random_rows (frame, 1.0); } return (frame); }
FileParserPtr Indexer::detect_parser(const unicode& filename) { /* * Given a filename, this method returns a matching parser, * if one could not be found, it defaults to the text/plain * parser */ unicode mime = guess_type(filename); auto parser = parsers_.find(mime); if(parser == parsers_.end()) { return parsers_.at("text/plain"); } else { return (*parser).second; } }
static void print_cell(buffer *b, int s, int r, int c, char *p) { char *texti; cval value; value.number = 0; switch (guess_type(p)) { case EMPTY: break; case CONSTANT: texti = p; value.number = strtod(texti, NULL); ins_data(b, siod_interpreter, texti, value, CONSTANT, s, r, c); ins_format(b, s, r, c, sf); break; default: /* anything else is a label */ texti = p; ins_data(b, siod_interpreter, texti, value, LABEL, s, r, c); ins_format(b, s, r, c, sf); } }
static cval parse_tcl_expr(buffer *b, char *expr, int s, int row, int col) { cval nothing; char p[80]; int result; nothing.number = 0; if (interp == NULL) { /* not initialized */ siag_type = ERROR; return nothing; } siag_row = row; siag_col = col; siag_buffer = b; siag_sht = s; sprintf(p, "%d", siag_row); Tcl_SetVar(interp, "R", p, 0); sprintf(p, "%d", siag_col); Tcl_SetVar(interp, "C", p, 0); result = Tcl_Eval(interp, expr); if (result != TCL_OK) { errorflag = 1; siag_type = ERROR; return nothing; } errorflag = 0; siag_type = guess_type(interp->result); if (siag_type == STRING) { siag_result.text = interp->result; } else { siag_result.number = value; } return siag_result; }
/* Convert external DOS file representation to internal. Return the count of characters left in the buffer. Build table to map character positions when reporting byte counts. */ static inline int undossify_input (char *buf, size_t buflen) { int chars_left = 0; if (totalcc == 0) { /* New file: forget everything we knew about character position mapping table and file type. */ inp_map_idx = 0; out_map_idx = 1; dos_pos_map_used = 0; dos_stripped_crs = 0; dos_file_type = dos_use_file_type; } /* Guess if this file is binary, unless we already know that. */ if (dos_file_type == UNKNOWN) dos_file_type = guess_type(buf, buflen); /* If this file is to be treated as DOS Text, strip the CR characters and maybe build the table for character position mapping on output. */ if (dos_file_type == DOS_TEXT) { char *destp = buf; while (buflen--) { if (*buf != '\r') { *destp++ = *buf++; chars_left++; } else { buf++; if (out_byte && !dos_report_unix_offset) { dos_stripped_crs++; while (buflen && *buf == '\r') { dos_stripped_crs++; buflen--; buf++; } if (inp_map_idx >= dos_pos_map_size - 1) { dos_pos_map_size = inp_map_idx ? inp_map_idx * 2 : 1000; dos_pos_map = xrealloc(dos_pos_map, dos_pos_map_size * sizeof(struct dos_map)); } if (!inp_map_idx) { /* Add sentinel entry. */ dos_pos_map[inp_map_idx].pos = 0; dos_pos_map[inp_map_idx++].add = 0; /* Initialize first real entry. */ dos_pos_map[inp_map_idx].add = 0; } /* Put the new entry. If the stripped CR characters precede a Newline (the usual case), pretend that they were found *after* the Newline. This makes displayed byte offsets more reasonable in some cases, and fits better the intuitive notion that the line ends *before* the CR, not *after* it. */ inp_map_idx++; dos_pos_map[inp_map_idx-1].pos = (*buf == '\n' ? destp + 1 : destp ) - bufbeg + totalcc; dos_pos_map[inp_map_idx].add = dos_stripped_crs; dos_pos_map_used = inp_map_idx; /* The following will be updated on the next pass. */ dos_pos_map[inp_map_idx].pos = destp - bufbeg + totalcc + 1; } } } return chars_left; } return buflen; }
zathura_document_t* zathura_document_open(zathura_plugin_manager_t* plugin_manager, const char* path, const char* password, zathura_error_t* error) { if (path == NULL) { return NULL; } if (g_file_test(path, G_FILE_TEST_EXISTS) == FALSE) { girara_error("File '%s' does not exist", path); return NULL; } const gchar* content_type = guess_type(path); if (content_type == NULL) { girara_error("Could not determine file type."); return NULL; } /* determine real path */ long path_max; #ifdef PATH_MAX path_max = PATH_MAX; #else path_max = pathconf(path,_PC_PATH_MAX); if (path_max <= 0) path_max = 4096; #endif char* real_path = NULL; zathura_document_t* document = NULL; real_path = malloc(sizeof(char) * path_max); if (real_path == NULL) { g_free((void*)content_type); return NULL; } if (realpath(path, real_path) == NULL) { g_free((void*)content_type); free(real_path); return NULL; } zathura_plugin_t* plugin = zathura_plugin_manager_get_plugin(plugin_manager, content_type); g_free((void*)content_type); if (plugin == NULL) { girara_error("unknown file type\n"); goto error_free; } document = g_malloc0(sizeof(zathura_document_t)); document->file_path = real_path; document->password = password; document->scale = 1.0; document->plugin = plugin; document->adjust_mode = ZATHURA_ADJUST_NONE; /* open document */ zathura_plugin_functions_t* functions = zathura_plugin_get_functions(plugin); if (functions->document_open == NULL) { girara_error("plugin has no open function\n"); goto error_free; } zathura_error_t int_error = functions->document_open(document); if (int_error != ZATHURA_ERROR_OK) { if (error != NULL) { *error = int_error; } girara_error("could not open document\n"); goto error_free; } /* read all pages */ document->pages = calloc(document->number_of_pages, sizeof(zathura_page_t*)); if (document->pages == NULL) { goto error_free; } for (unsigned int page_id = 0; page_id < document->number_of_pages; page_id++) { zathura_page_t* page = zathura_page_new(document, page_id, NULL); if (page == NULL) { goto error_free; } document->pages[page_id] = page; } return document; error_free: free(real_path); if (document != NULL && document->pages != NULL) { for (unsigned int page_id = 0; page_id < document->number_of_pages; page_id++) { zathura_page_free(document->pages[page_id]); } free(document->pages); } g_free(document); return NULL; }
/* * Read FITS file filename to a structure image */ gboolean readfits(gchar *filename, IMAGE *image){ FNAME(); gboolean ret = TRUE; fitsfile *fp; GLfloat nullval = 0., imBits, bZero = 0., bScale = 1.; free(image->data); image->data = NULL; int i, j, hdunum, keynum, morekeys, dtype, stat; int naxis, w, h; double val_f = -1e6; // VAL_F value long naxes[4]; ssize_t sz; //char card[FLEN_CARD]; char keyname[FLEN_KEYWORD], keyval[FLEN_VALUE]; char keycomment[FLEN_COMMENT], cdtype[32]; TRYFITS(fits_open_file, &fp, filename, READWRITE); /* TRYFITS(fits_get_hdrspace, fp, &keynum, &morekeys); if(morekeys == -1){ return FALSE; } g_print("Read header of %s, %d keys\n", filename, keynum);*/ FITSFUN(fits_get_num_hdus, fp, &hdunum); //~ g_print(_("\nFile includes %d HDUs\n"), hdunum); if(hdunum < 1){ g_err(_("Can't read HDU")); TRYFITS(fits_close_file, fp); return FALSE; } FITSFUN(fits_get_hdu_type, fp, &dtype); //~ if(dtype) g_print(_("Current HDU type: %d\n"), dtype); init_keylist(image); FITSFUN(fits_get_hdrpos ,fp, &keynum, &morekeys); for (j = 1; j <= keynum; j++){ /*FITSFUN(fits_read_record, fp, j, card); if(!strip_card(card, keyname, keyval, keycomment)){ g_print("Corrupted card: %s!\n", card); continue; }*/ FITSFUN(fits_read_keyn, fp, j, keyname, keyval, keycomment); if(strcmp(keyname, "SIMPLE") == 0 || strcmp(keyname, "EXTEND") == 0) // key "file does conform ..." continue; else if(strcmp(keyname, "COMMENT") == 0) // comment of obligatory key in FITS head continue; else if(strstr(keyname, "NAXIS") == keyname || strcmp(keyname, "BITPIX") == 0) // NAXIS, NAXISxxx, BITPIX continue; else if(strcmp(keyname, "BZERO") == 0){ bZero = atof(keyval); //continue; } else if(strcmp(keyname, "BSCALE") == 0){ bScale = atof(keyval); if(bScale < SCALE_MIN) bScale = 1.; //continue; } else if(strcmp(keyname, "VAL_F") == 0){ val_f = atof(keyval); } dtype = guess_type(keyval, cdtype); //DBG("key#%d; name=%s, dtype=%d (%s), value=%s, comment=%s\n", // j, keyname, dtype, cdtype, keyval, keycomment); add_key(image, cdtype, keyname, keyval, keycomment, dtype); } if(hdunum > 1){ for(i = 2; !(fits_movabs_hdu(fp, i, &dtype, &stat)); i++){ FITSFUN(fits_get_hdrpos ,fp, &keynum, &morekeys); for (j = 1; j <= keynum; j++){ FITSFUN(fits_read_keyn, fp, j, keyname, keyval, keycomment); dtype = guess_type(keyval, cdtype); add_key(image, cdtype, keyname, keyval, keycomment, dtype); } } if(stat != END_OF_FILE){ fits_report_error(stderr, (stat)); ret = FALSE; } } TRYFITS(fits_get_img_param, fp, 4, &dtype, &naxis, naxes); if(dtype > 0) imBits = (GLfloat)(1LL << dtype); // compute the max amplitude else imBits = -1.; // image has floating point type DBG("Image type: %d (%f bits)", dtype, imBits); image->maxVal = imBits; image->dtype = dtype; if(naxis != 2){ g_err(_("Not an image? (dimensions != 2)")); return FALSE;} image->val_f = val_f; image->bZero = bZero; image->bScale = bScale; image->width = w = naxes[0]; image->height = h = naxes[1]; sz = (ssize_t)(w) * (ssize_t)(h); image->data = malloc(sz * sizeof(GLfloat)); TRYFITS(fits_read_img, fp, TFLOAT, 1, sz, &nullval, image->data, &stat); GLfloat *ptr = image->data; GLfloat min, max; min = 1e6; max = 0.; for(i=0; i<h; i++) for(j=0; j<w; j++, ptr++){ GLfloat tmp = *ptr; if(tmp > max) max = tmp; else if(tmp < min) min = tmp; } DBG("min = %f, wd = %f", min, max-min); image->stat.max = max; image->stat.min = min; // histogram isn't initialized yet image->stat.histogram.data = NULL; image->stat.histogram.size = 0; /* ptr = image->data; for(i=0; i<h; i++) for(j=0; j<w; j++, ptr++){ // *ptr = (*ptr * bScale + bZero) / imBits; *ptr = (*ptr - min) / wd; } */ TRYFITS(fits_close_file, fp); return(ret); }