int main(int argc, char **argv) { int c; bool list_mode = false; bool extract_mode = false; bool create_mode = false; FILE *in; char *inname; int raw_filec = 0; char** raw_filev = 0; set_program_name(argv[0]); #ifdef ENABLE_NLS if (setlocale(LC_ALL, "") == NULL) warn(_("%s: cannot set locale: %s"), program_name, errstr); if (bindtextdomain(PACKAGE, LOCALEDIR) == NULL) warn(_("%s: bindtextdomain failed: %s"), program_name, errstr); if (textdomain(PACKAGE) == NULL) warn(_("%s: cannot set message domain: %s"), program_name, errstr); #endif while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { switch (c) { case 'x': extract_mode = true; break; case 'l': list_mode = true; break; case 'c': create_mode = true; break; case VERSION_OPT: version_etc(stdout, PROGRAM, PACKAGE, VERSION, "Oskar Liljeblad", NULL); exit(0); case HELP_OPT: display_help(); exit(0); case 'o': output = optarg; break; case 'i': if (!parse_int32(optarg, &image_index) || image_index < 0) die(_("invalid index value: %s"), optarg); break; case 'w': if (!parse_int32(optarg, &width) || width < 0) die(_("invalid width value: %s"), optarg); break; case 'h': if (!parse_int32(optarg, &height) || height < 0) die(_("invalid height value: %s"), optarg); break; case 'p': if (!parse_int32(optarg, &palettesize) || palettesize < 0) die(_("invalid palette-size value: %s"), optarg); break; case 'b': if (!parse_int32(optarg, &bitdepth) || bitdepth < 0) die(_("invalid bit-depth value: %s"), optarg); break; /*case 'm': if (!parse_uint32(optarg, &minbitdepth)) die(_("invalid minimum bit-depth value: %s"), optarg); break;*/ case 'X': if (!parse_int32(optarg, &hotspot_x) || hotspot_x < 0) die(_("invalid hotspot-x value: %s"), optarg); hotspot_x_set = true; break; case 'Y': if (!parse_int32(optarg, &hotspot_y) || hotspot_y < 0) die(_("invalid hotspot-y value: %s"), optarg); hotspot_y_set = true; break; case 't': if (!parse_int32(optarg, &alpha_threshold) || alpha_threshold < 0) die(_("invalid alpha-threshold value: %s"), optarg); break; case 'r': raw_filev = realloc (raw_filev, (raw_filec+1)*sizeof (char*)); raw_filev[raw_filec] = optarg; raw_filec++; break; case ICON_OPT: icon_only = true; break; case CURSOR_OPT: cursor_only = true; break; case '?': exit(1); } } if (extract_mode + create_mode + list_mode > 1) die(_("multiple commands specified")); if (extract_mode + create_mode + list_mode == 0) { warn(_("missing argument")); display_help(); exit (1); } if (icon_only && cursor_only) die(_("only one of --icon and --cursor may be specified")); if (list_mode) { if (argc-optind <= 0) die(_("missing file argument")); for (c = optind ; c < argc ; c++) { if (open_file_or_stdin(argv[c], &in, &inname)) { if (!extract_icons(in, inname, true, NULL, filter)) exit(1); if (in != stdin) fclose(in); } } } if (extract_mode) { if (argc-optind <= 0) die(_("missing arguments")); for (c = optind ; c < argc ; c++) { int matched; if (open_file_or_stdin(argv[c], &in, &inname)) { matched = extract_icons(in, inname, false, extract_outfile_gen, filter); if (matched == -1) exit(1); if (matched == 0) fprintf(stderr, _("%s: no images matched\n"), inname); if (in != stdin) fclose(in); } } } if (create_mode) { if (argc-optind+raw_filec <= 0) die(_("missing arguments")); if (!create_icon(argc-optind, argv+optind, raw_filec, raw_filev, create_outfile_gen, (icon_only ? true : !cursor_only), hotspot_x, hotspot_y, alpha_threshold, bitdepth)) exit(1); } exit(0); }
static int key_value_parse_line_with_key(struct key_value_specification *k, char *line, void *base_address[]) { char *valuestr; int rc, len, klen; klen = strlen(k->key); len = strlen(line); if (len < klen + 1) return 1; if (strncmp(line, k->key, klen) != 0) return 1; if (line[klen] != ':') return 1; /* Skip leading spaces */ klen = klen + 1; while (line[klen] == ' ') klen++; /* At this point, we have a match on the key */ valuestr = &line[klen]; switch (k->type) { case KVS_STRING: rc = parse_string(valuestr, k, base_address); break; case KVS_INT64: rc = parse_int64(valuestr, k, base_address); break; case KVS_INT32: rc = parse_int32(valuestr, k, base_address); break; case KVS_INT16: rc = parse_int16(valuestr, k, base_address); break; case KVS_INT8: rc = parse_int8(valuestr, k, base_address); break; case KVS_UINT64: rc = parse_uint64(valuestr, k, base_address); break; case KVS_UINT32: rc = parse_uint32(valuestr, k, base_address); break; case KVS_UINT16: rc = parse_uint16(valuestr, k, base_address); break; case KVS_UINT8: rc = parse_uint8(valuestr, k, base_address); break; case KVS_DOUBLE: rc = parse_double(valuestr, k, base_address); break; case KVS_FLOAT: rc = parse_float(valuestr, k, base_address); break; default: fprintf(stderr, "%s:%d: unknown key type '%c' for key '%s'\n", __func__, __LINE__, k->type, k->key); rc = -1; break; } return rc; }
/** write the data received from curl to the rapid pool. the filename is read from the sdp-list (created at request start) filesize is read from the http-data received (could overlap!) */ static size_t write_streamed_data(const void* tmp, size_t size, size_t nmemb,CSdp *sdp) { char buf[CURL_MAX_WRITE_SIZE]; memcpy(&buf,tmp,CURL_MAX_WRITE_SIZE); if (!sdp->downloadInitialized) { sdp->list_it=sdp->globalFiles->begin(); sdp->downloadInitialized=true; sdp->file_handle=NULL; sdp->file_name=""; sdp->skipped=0; } char* buf_start=(char*)&buf; const char* buf_end=buf_start + size*nmemb; char* buf_pos=buf_start; while (buf_pos<buf_end) { //all bytes written? if (sdp->file_handle==NULL) { //no open file, create one while ( (!(*sdp->list_it)->download==true) && (sdp->list_it!=sdp->globalFiles->end())) { //get file sdp->list_it++; } HashMD5 md5; md5.Set((*sdp->list_it)->md5, sizeof((*sdp->list_it)->md5)); fileSystem->getPoolFilename(md5.toString(), sdp->file_name); sdp->file_handle=new AtomicFile(sdp->file_name); // LOG_DEBUG("opened %s, size: %d", sdp->file_name.c_str(), (*sdp->list_it)->size); //FIXME sdp->setStatsPos(sdp->getStatsPos()+1); if (sdp->file_handle==NULL) { LOG_ERROR("couldn't open %s",(*sdp->list_it)->name.c_str()); return -1; } //here comes the init new file stuff sdp->file_pos=0; } if (sdp->file_handle!=NULL) { if (sdp->skipped<LENGTH_SIZE) { // check if we skipped all 4 bytes, if not so, skip them int toskip=intmin(buf_end-buf_pos,LENGTH_SIZE-sdp->skipped); //calculate bytes we can skip, could overlap received bufs for (int i=0; i<toskip; i++) { //copy bufs avaiable sdp->cursize_buf[sdp->skipped+i]=buf_pos[i]; // if (sdp->skipped>0) { // LOG_DEBUG("copy %d to %d ", i, sdp->skipped+i); // } } // LOG_DEBUG("toskip: %d skipped: %d",toskip,sdp->skipped); sdp->skipped=toskip+sdp->skipped; buf_pos=buf_pos+toskip; if (sdp->skipped==LENGTH_SIZE) { (*sdp->list_it)->compsize=parse_int32(sdp->cursize_buf); // LOG_DEBUG("%s %hhu %hhu %hhu %hhu", sdp->file_name.c_str(), sdp->cursize_buf[0], sdp->cursize_buf[1], sdp->cursize_buf[2], sdp->cursize_buf[3]); // LOG_DEBUG("(data read from sdp)uncompressed size: %d (data read from net)compressed size: %d", (*sdp->list_it)->size, (*sdp->list_it)->compsize); assert((*sdp->list_it)->size+2000 >= (*sdp->list_it)->compsize); } } if (sdp->skipped==LENGTH_SIZE) { int towrite=intmin ((*sdp->list_it)->compsize-sdp->file_pos , //minimum of bytes to write left in file and bytes to write left in buf buf_end-buf_pos); // LOG_DEBUG("%s %d %ld %ld %ld %d %d %d %d %d",sdp->file_name.c_str(), (*sdp->list_it).compsize, buf_pos,buf_end, buf_start, towrite, size, nmemb , sdp->skipped, sdp->file_pos); int res=0; if (towrite>0) { res=sdp->file_handle->Write(buf_pos,towrite); if (res!=towrite) { LOG_ERROR("fwrite error"); return -1; } if (res<=0) { LOG_ERROR("wrote error: %d", res); return -1; } } else if (towrite<0) { LOG_DEBUG("Fatal, something went wrong here! %d", towrite); return -1; } buf_pos=buf_pos+res; sdp->file_pos+=res; if (sdp->file_pos>=(*sdp->list_it)->compsize) { //file finished -> next file sdp->file_handle->Close(); delete sdp->file_handle; sdp->file_handle = NULL; if (!fileSystem->fileIsValid(*sdp->list_it,sdp->file_name.c_str())) { LOG_ERROR("File is broken?!: %s",sdp->file_name.c_str()); remove(sdp->file_name.c_str()); return -1; } sdp->file_handle=NULL; sdp->list_it++; sdp->file_pos=0; sdp->skipped=0; } } } } return buf_pos-buf_start; }
void pgut_setopt(pgut_option *opt, const char *optarg, pgut_optsrc src) { const char *message; if (opt == NULL) { fprintf(stderr, "Try \"%s --help\" for more information.\n", PROGRAM_NAME); exit(EINVAL); } if (opt->source > src) { /* high prior value has been set already. */ return; } else if (src >= SOURCE_CMDLINE && opt->source >= src) { /* duplicated option in command line */ message = "specified only once"; } else { /* can be overwritten if non-command line source */ opt->source = src; switch (opt->type) { case 'b': case 'B': if (optarg == NULL) { *((bool *) opt->var) = (opt->type == 'b'); return; } else if (parse_bool(optarg, (bool *) opt->var)) { return; } message = "a boolean"; break; case 'f': ((pgut_optfn) opt->var)(opt, optarg); return; case 'i': if (parse_int32(optarg, opt->var)) return; message = "a 32bit signed integer"; break; case 'u': if (parse_uint32(optarg, opt->var)) return; message = "a 32bit unsigned integer"; break; case 'I': if (parse_int64(optarg, opt->var)) return; message = "a 64bit signed integer"; break; case 'U': if (parse_uint64(optarg, opt->var)) return; message = "a 64bit unsigned integer"; break; case 's': if (opt->source != SOURCE_DEFAULT) free(*(char **) opt->var); *(char **) opt->var = pgut_strdup(optarg); return; case 't': if (parse_time(optarg, opt->var)) return; message = "a time"; break; case 'y': case 'Y': if (optarg == NULL) { *(YesNo *) opt->var = (opt->type == 'y' ? YES : NO); return; } else { bool value; if (parse_bool(optarg, &value)) { *(YesNo *) opt->var = (value ? YES : NO); return; } } message = "a boolean"; break; default: ereport(ERROR, (errcode(EINVAL), errmsg("invalid option type: %c", opt->type))); return; /* keep compiler quiet */ } } if (isprint(opt->sname)) ereport(ERROR, (errcode(EINVAL), errmsg("option -%c, --%s should be %s: '%s'", opt->sname, opt->lname, message, optarg))); else ereport(ERROR, (errcode(EINVAL), errmsg("option --%s should be %s: '%s'", opt->lname, message, optarg))); }