void file_read( FILE * f, void * buffer, size_t size ) { if( size!=fread(buffer,1,size,f) ) throw boost::enable_current_exception(file_read_error()) << errno_info(errno); }
size_t read_file( boost::shared_ptr<FILE> const & f, void * buf, size_t size ) { size_t nr=fread(buf,1,size,f.get()); if( ferror(f.get()) ) BOOST_THROW_EXCEPTION( file_read_error() << boost::errinfo_api_function("fread") << boost::errinfo_errno(errno) << boost::errinfo_file_handle(f) ); return nr; }
static int copy_segment(VSTREAM *qfile, VSTREAM *cleanup, PICKUP_INFO *info, VSTRING *buf, char *expected) { int type; int check_first = (*expected == REC_TYPE_CONTENT[0]); int time_seen = 0; char *attr_name; char *attr_value; char *saved_attr; int skip_attr; /* * Limit the input record size. All front-end programs should protect the * mail system against unreasonable inputs. This also requires that we * limit the size of envelope records written by the local posting agent. * * Records with named attributes are filtered by postdrop(1). * * We must allow PTR records here because of "postsuper -r". */ for (;;) { if ((type = rec_get(qfile, buf, var_line_limit)) < 0 || strchr(expected, type) == 0) return (file_read_error(info, type)); if (msg_verbose) msg_info("%s: read %c %s", info->id, type, vstring_str(buf)); if (type == *expected) break; if (type == REC_TYPE_FROM) { if (info->sender == 0) info->sender = mystrdup(vstring_str(buf)); /* Compatibility with Postfix < 2.3. */ if (time_seen == 0) rec_fprintf(cleanup, REC_TYPE_TIME, "%ld", (long) info->st.st_mtime); } if (type == REC_TYPE_TIME) time_seen = 1; /* * XXX Workaround: REC_TYPE_FILT (used in envelopes) == REC_TYPE_CONT * (used in message content). * * As documented in postsuper(1), ignore content filter record. */ if (*expected != REC_TYPE_CONTENT[0]) { if (type == REC_TYPE_FILT) /* Discard FILTER record after "postsuper -r". */ continue; if (type == REC_TYPE_RDR) /* Discard REDIRECT record after "postsuper -r". */ continue; } if (*expected == REC_TYPE_EXTRACT[0]) { if (type == REC_TYPE_RRTO) /* Discard return-receipt record after "postsuper -r". */ continue; if (type == REC_TYPE_ERTO) /* Discard errors-to record after "postsuper -r". */ continue; if (type == REC_TYPE_ATTR) { saved_attr = mystrdup(vstring_str(buf)); skip_attr = (split_nameval(saved_attr, &attr_name, &attr_value) == 0 && rec_attr_map(attr_name) == 0); myfree(saved_attr); /* Discard other/header/body action after "postsuper -r". */ if (skip_attr) continue; } } /* * XXX Force an empty record when the queue file content begins with * whitespace, so that it won't be considered as being part of our * own Received: header. What an ugly Kluge. */ if (check_first && (type == REC_TYPE_NORM || type == REC_TYPE_CONT)) { check_first = 0; if (VSTRING_LEN(buf) > 0 && IS_SPACE_TAB(vstring_str(buf)[0])) rec_put(cleanup, REC_TYPE_NORM, "", 0); } if ((REC_PUT_BUF(cleanup, type, buf)) < 0) return (cleanup_service_error(info, CLEANUP_STAT_WRITE)); } return (0); }
int main(int argc, char *argv[]) { char opt, *framename, *listname, *outname, *e_outname, buffer[BUFSIZ]; int n, row, col, cX, cY, npatch, npxi, npxt, nprof; int flag_autoname, flag_verbose, status, bitmask[YAR][XAR]; long xsize, ysize, lowleft[DIM], upright[DIM], increment[DIM] = {1L, 1L}, framesize[DIM]; float mask_val, cutoff, framefragment[YAR][XAR]; struct star* patchstars; FILE *listfile; fitsfile *frame, *maskedframe; extern char *optarg; extern int opterr; framename = NULL; listname = NULL; outname = NULL; mask_val = DEFAULT_MASK; cutoff = DEFAULT_CUTOFF; flag_autoname = 1; flag_verbose = 0; opterr = 0; while ((opt=getopt(argc, argv, "f:l:o:m:c:v")) != -1) { switch (opt) { case 'f': framename = optarg; break; case 'l': listname = optarg; break; case 'o': flag_autoname = 0; outname = optarg; break; case 'm': mask_val = atof(optarg); break; case 'c': cutoff = atof(optarg); break; case 'v': flag_verbose = 1; break; default: usage(argv); } } /* check mandatory parameters: */ if (framename == NULL || listname == NULL) usage(argv); if (cutoff < 0.0) { fprintf(stderr, " WARNING: invalid cut-off value (%.1f ADU), using default (%.1f ADU)\n", cutoff, DEFAULT_CUTOFF); cutoff = DEFAULT_CUTOFF; } /* read ID and coordinates from any single-lined Daophot output, detect and skip the header if necessary: */ if ((listfile=fopen(listname, "r")) == NULL) { file_read_error(listname); } else { npatch = line_count(listfile); if (has_daophot_header(listfile) == 1) { npatch -= DAO_HEADER_SIZE; line_skip(listfile, DAO_HEADER_SIZE); } if (npatch <= 0) { fclose(listfile); fprintf(stderr, " couldn't read contents of '%s', exiting.\n\n", listname); exit(EXIT_FAILURE); } patchstars = calloc(npatch, sizeof(struct star)); if (patchstars == NULL) { fclose(listfile); memory_error(); } for (n=0; n < npatch; n++) { fgets(buffer, BUFSIZ, listfile); sscanf(buffer, "%d %lf %lf", &patchstars[n].num, &patchstars[n].X, &patchstars[n].Y); } fclose(listfile); } status = 0; fits_open_file(&frame, framename, READONLY, &status); if (status != 0) { free(patchstars); file_read_error(framename); } fits_get_img_size(frame, DIM, framesize, &status); fits_print_error(status); xsize = framesize[0]; ysize = framesize[1]; printf(" %s: %ld x %ld pixels\n", framename, xsize, ysize); printf(" %s: %d stars in the list\n", listname, npatch); printf(" mask value: %.1f ADU\n", mask_val); printf(" cut-off level: %.1f ADU\n", cutoff); if (flag_autoname == 1) { outname = expand_filename(framename, "-msk", 0, 0); if (outname == NULL) { free(patchstars); fits_close_file(frame, &status); memory_error(); } } printf(" output file: %s\n", outname); /* force cfitsio to overwrite already existing file (prepend ! to the filename) */ e_outname = prepend_bang(outname); if (e_outname == NULL) { free(patchstars); fits_close_file(frame, &status); memory_error(); } /* create output (masked) frame: */ fits_create_file(&maskedframe, e_outname, &status); fits_print_error(status); free(e_outname); if (flag_autoname == 1) /* only necessary if outname was created automatically */ free(outname); fits_copy_file(frame, maskedframe, 1, 1, 1, &status); fits_print_error(status); npxt = 0; nprof = 0; /* process the stars in the list: */ for (n=0; n < npatch; n++) { if (flag_verbose == 1) printf("\n star #%d:\n", patchstars[n].num); /* cfitsio doesn't understand fractional pixels, round the coordinates to nearest integer: */ cX = rint(patchstars[n].X); cY = rint(patchstars[n].Y); /* make sure center is in the picture, skip this star if it is not: */ if (cX < 1 || cY < 1 || cX > xsize || cY > ysize) { printf(" star #%d is outside the frame (%d,%d), skipping.\n", patchstars[n].num, cX, cY); continue; } /* * in fitsio pixel index runs from 1 to N, not from 0 to N-1. * consider XAR=21 (reading 10 pixels right and left from center), XSIZE=2044. * example 1: star with x=10. fitsio will reach and try to read pixel at x=0, ILLEGAL. * example 2: star with x=2034. fitsio will reach and try to read pixel at x=2044, LEGAL. * we have to use LESS OR EQUAL when checking lower and left boundary, * for upper and right boundary only using GREATER is fine */ else if (cX <= (XAR-1)/2 || cY <= (YAR-1)/2 || cX > xsize-(XAR-1)/2 || cY > ysize-(YAR-1)/2) { printf(" star #%d is too close to the edge or partially outside the frame (%d,%d), skipping.\n", patchstars[n].num, cX, cY); continue; } if (flag_verbose == 1) printf(" coordinates of star #%d center in current frame: (%d,%d)\n", patchstars[n].num, cX, cY); /* compute the coordinates of lower left and upper right pixel: */ lowleft[0] = cX - (XAR-1)/2; lowleft[1] = cY - (YAR-1)/2; upright[0] = cX + (XAR-1)/2; upright[1] = cY + (YAR-1)/2; /* load frame fragment with the star into table: */ fits_read_subset(frame, TFLOAT, lowleft, upright, increment, 0, framefragment, 0, &status); fits_print_error(status); /* initialise the bitmask with zeros: */ for (row=0; row < YAR; row++) for (col=0; col < XAR; col++) bitmask[row][col] = 0; /* detect holes and write to bitmask: */ for (row=YAR-1; row >= 0; row--) examine_row(framefragment, bitmask, row, cutoff); for (col=0; col < XAR; col++) examine_col(framefragment, bitmask, col, cutoff); npxi = apply_bitmask(framefragment, bitmask, mask_val); npxt += npxi; if (npxi > 0) ++nprof; if (flag_verbose == 1) { printf("\n"); print_bitmask(bitmask); printf("\n star %d: %d pixels masked\n", patchstars[n].num, npxi); } /* write the modified frame subsection to output frame: */ fits_write_subset(maskedframe, TFLOAT, lowleft, upright, framefragment, &status); fits_print_error(status); } printf(" %d pixels were masked in %d stars.\n", npxt, nprof); fits_close_file(frame, &status); fits_print_error(status); fits_close_file(maskedframe, &status); fits_print_error(status); free(patchstars); return 0; }
void file::check_for_input() const { if (!this->file_stream.good()) { throw file_read_error("Could not read file " + this->file_path); } }