int gu_compress(const char *filename_in, const char *filename_out) { gzFile gzf = NULL; FILE *f = NULL; char *buf = NULL; long size = 0; if (NULL == (f = fopen(filename_in, "rb"))) { return 1; } fseek(f, 0, SEEK_END); size = ftell(f); rewind(f); buf = malloc(size); fread(buf, 1, size, f); fclose(f); if (NULL == (gzf = gzopen(filename_out, "wb"))) { return 1; } gzwrite(gzf, buf, size); gzclose_w(gzf); free(buf); return 0; }
/* gzclose() is in a separate file so that it is linked in only if it is used. That way the other gzclose functions can be used instead to avoid linking in unneeded compression or decompression routines. */ int ZEXPORT gzclose( gzFile file ) { #ifndef NO_GZCOMPRESS gz_statep state; if (file == NULL) return Z_STREAM_ERROR; state = (gz_statep)file; return state->mode == GZ_READ ? gzclose_r(file) : gzclose_w(file); #else return gzclose_r(file); #endif }
// reads an uncompressed or gzipped file Tag * Tag::loadFromFile(std::string path) { DEBUG printf("Reading file '%s' ...\n", path.c_str()); // read buffer from file gzFile file = gzopen(path.c_str(), "rb"); if (file == NULL) { DEBUG printf("ERROR: Could not open file\n"); return this; } const unsigned short stepSize = 4096; unsigned long int bufSize = stepSize; char * buffer = new char[bufSize]; for (;;) { // breaks on success int bytesRead = gzread(file, buffer+(bufSize-stepSize), stepSize); // error while ungzipping? // TODO better error handling if (bytesRead < 0) { printf("Error in gzread (readBytes < 0)\n"); return this; } if (bytesRead < stepSize) break; // EOF, success // buffer too small, increase buffer size unsigned long int oldSize = bufSize; bufSize += stepSize; char * oldBuffer = buffer; buffer = new char[bufSize]; memcpy(buffer, oldBuffer, oldSize); delete[] oldBuffer; } gzclose_w(file); // parse data loadFromBytestream(new Bytestream(buffer, bufSize)); delete[] buffer; DEBUG printf("File reading successful.\n"); return this; }
/* Dump database into output_file_name if it's non-NULL, stdout * otherwise. */ int notmuch_database_dump (notmuch_database_t *notmuch, const char *output_file_name, const char *query_str, dump_format_t output_format, notmuch_bool_t gzip_output) { gzFile output = NULL; const char *mode = gzip_output ? "w9" : "wT"; const char *name_for_error = output_file_name ? output_file_name : "stdout"; char *tempname = NULL; int outfd = -1; int ret = -1; if (output_file_name) { tempname = talloc_asprintf (notmuch, "%s.XXXXXX", output_file_name); outfd = mkstemp (tempname); } else { outfd = dup (STDOUT_FILENO); } if (outfd < 0) { fprintf (stderr, "Bad output file %s\n", name_for_error); goto DONE; } output = gzdopen (outfd, mode); if (output == NULL) { fprintf (stderr, "Error opening %s for (gzip) writing: %s\n", name_for_error, strerror (errno)); if (close (outfd)) fprintf (stderr, "Error closing %s during shutdown: %s\n", name_for_error, strerror (errno)); goto DONE; } ret = database_dump_file (notmuch, output, query_str, output_format); if (ret) goto DONE; ret = gzflush (output, Z_FINISH); if (ret) { fprintf (stderr, "Error flushing output: %s\n", gzerror (output, NULL)); goto DONE; } if (output_file_name) { ret = fdatasync (outfd); if (ret) { fprintf (stderr, "Error syncing %s to disk: %s\n", name_for_error, strerror (errno)); goto DONE; } } if (gzclose_w (output) != Z_OK) { fprintf (stderr, "Error closing %s: %s\n", name_for_error, gzerror (output, NULL)); ret = EXIT_FAILURE; output = NULL; goto DONE; } if (output_file_name) { ret = rename (tempname, output_file_name); if (ret) { fprintf (stderr, "Error renaming %s to %s: %s\n", tempname, output_file_name, strerror (errno)); goto DONE; } } DONE: if (ret != EXIT_SUCCESS && output) (void) gzclose_w (output); if (ret != EXIT_SUCCESS && output_file_name) (void) unlink (tempname); return ret; }
int main(int argc, char **argv) { MPI_Init(nullptr, nullptr); int world_size; MPI_Comm_size(MPI_COMM_WORLD, &world_size); int world_rank; MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); options_t options; if(!parse_options(options, world_rank==0, argc, argv)) return 1; if(world_rank == 0) { printf("Calculating buddhabrot %"PRIu64"x%"PRIu64" image with a " "run length of %"PRIu64" and a block size of %"PRIu64", " "considering a total of %"PRIu64" orbits per block\n", options.width, options.height, options.run_size, options.block_size, options.run_size*options.block_size); printf("The calculated will continue until "); if(options.use_max) { printf("%"PRIu64" blocks have been processed", options.max_blocks); if(options.use_err) printf(", or "); else printf("\n"); } if(options.use_err) printf("the average error falls under %f\n", options.error); printf("An orbit will be included in the image if it escapes the " "circle of raduis %f between %"PRIu64" and %"PRIu64" iterations\n", options.radius, options.iter_low, options.iter_high); printf("Random points are drawn from [%f,%f]+i[%f,%f]\n", options.rand_rlow, options.rand_rhigh, options.rand_ilow, options.rand_ihigh); printf("The image is mapped from the region [%f,%f]+i[%f,%f]\n", options.map_rlow, options.map_rhigh, options.map_ilow, options.map_ihigh); } const size_t image_size = options.width*options.height; std::vector<uint32_t> img; if(options.double_precision) { iterate<double, uint64_t>(options, img); } else { iterate<float, uint32_t>(options, img); } if(world_rank == 0) { if(options.compress) { gzFile f = gzopen(options.filename.c_str(), "wb9"); if(!f) { fprintf(stderr, "Error: Coulnd't open file \"%s\" for compressed writing\n", options.filename.c_str()); } else { printf("Writing compressed output...\n"); gzwrite(f, img.data(), image_size*sizeof(uint32_t)); gzclose_w(f); } } else { FILE *f = fopen(options.filename.c_str(), "wb"); if(!f) { fprintf(stderr, "Error: Couldn't open file \"%s\" for writing\n", options.filename.c_str()); } else { printf("Writing output...\n"); fwrite(img.data(), sizeof(uint32_t), image_size, f); fclose(f); } } } MPI_Finalize(); return 0; }
static bool gzip_close(void *fp) { return gzclose_w(fp) == Z_OK; }
unsigned int post_file(const char *filetag) { char jsonfile[BUFLEN] = NULLSTR; char jsonfilegz[BUFLEN] = NULLSTR; char jsonpfile[BUFLEN] = NULLSTR; char jsonpfilegz[BUFLEN] = NULLSTR; unsigned char filebuf[32768]; SNPRINTF(jsonfile, BUFLEN, "%s%s.json\0", JSON_DIR, filetag); SNPRINTF(jsonfilegz, BUFLEN, "%s%s.json.gz\0", JSON_DIR, filetag); SNPRINTF(jsonpfile, BUFLEN, "%s%s.jsonp\0", JSON_DIR, filetag); SNPRINTF(jsonpfilegz, BUFLEN, "%s%s.jsonp.gz\0", JSON_DIR, filetag); // printf("post process %s - ", filetag); printf("post process - "); if (ACCESS(jsonfile, 0) != 0) { printf("err\n"); fprintf(stderr, "could not access file\n"); return 1; } FILE *j = fopen(jsonfile, "r"); if (j == NULL) { printf("err\n"); fprintf(stderr, "error opening file\n"); return 1; } FILE *f = NULL; if (JSONP_FLAG) { f = fopen(jsonpfile, "w"); if (f == NULL) { fclose(j); printf("err\n"); fprintf(stderr, "error opening file\n"); return 1; } } gzFile jz = NULL; gzFile fz = NULL; if (GZIP_FLAG) { jz = gzopen(jsonfilegz, "wb9"); if (jz == NULL) { printf("err\n"); fprintf(stderr, "error opening file\n"); return 1; } gzbuffer(jz, 65536); } if (GZIP_FLAG && JSONP_FLAG) { fz = gzopen(jsonpfilegz, "wb9"); if (fz == NULL) { printf("err\n"); fprintf(stderr, "error opening file\n"); return 1; } gzbuffer(fz, 65536); } if (JSONP_FLAG) fprintf(f, "EVEoj_"); if (GZIP_FLAG && JSONP_FLAG) gzprintf(fz, "EVEoj_"); if (JSONP_FLAG) fprintf(f, filetag); if (GZIP_FLAG && JSONP_FLAG) gzprintf(fz, filetag); if (JSONP_FLAG) fprintf(f, "_callback("); if (GZIP_FLAG && JSONP_FLAG) gzprintf(fz, "_callback("); size_t readsize = 32768; while (readsize == 32768) { readsize = fread(filebuf, sizeof(unsigned char), 32768, j); if (JSONP_FLAG) fwrite(filebuf, sizeof(unsigned char), readsize, f); if (GZIP_FLAG) gzwrite(jz, filebuf, readsize); if (GZIP_FLAG && JSONP_FLAG) gzwrite(fz, filebuf, readsize); } if (GZIP_FLAG) gzclose_w(jz); if (GZIP_FLAG && JSONP_FLAG) { gzprintf(fz, ");"); gzclose_w(fz); } if (JSONP_FLAG) { fprintf(f, ");"); fclose(f); } fclose(j); printf("OK\n"); return 0; }