char *parse_options(char *params, t_options *options, t_list *output) { params = parse_argpos(params, options, output); params = parse_flags(params, options); params = parse_field_width(params, options, output); check_field_width(options); params = parse_precision(params, options, output); params = parse_casters(params, options); return (params); }
int handle_conversion(t_env *e, va_list ap) { if (check_percent_percent(e) || e->str[e->current] == '\0') return (0); parse_flags(e); parse_width(e, ap); parse_precision(e, ap); parse_modificator(e); parse_convertor(e); return (1); }
bool parse_format_string( const exprt &format_arg, format_token_listt &token_list) { token_list.clear(); if(format_arg.id()==ID_string_constant) { const std::string &arg_string = id2string(format_arg.get(ID_value)); std::string::const_iterator it=arg_string.begin(); while(it!=arg_string.end()) { if(*it=='%') { token_list.push_back(format_tokent()); format_tokent &curtok=token_list.back(); it++; parse_flags(it, curtok); parse_field_width(it, curtok); parse_precision(it, curtok); parse_length_modifier(it, curtok); parse_conversion_specifier(arg_string, it, curtok); } else { if(token_list.back().type!=format_tokent::TEXT) token_list.push_back(format_tokent(format_tokent::TEXT)); std::string tmp; for(;it!=arg_string.end() && *it!='%';it++) tmp+=*it; token_list.back().value=tmp; } } return true; } return false; // non-const format string }
format_token_listt parse_format_string(const std::string &arg_string) { format_token_listt token_list; std::string::const_iterator it=arg_string.begin(); while(it!=arg_string.end()) { if(*it=='%') { token_list.push_back(format_tokent()); format_tokent &curtok=token_list.back(); it++; parse_flags(it, curtok); parse_field_width(it, curtok); parse_precision(it, curtok); parse_length_modifier(it, curtok); parse_conversion_specifier(arg_string, it, curtok); } else { if(token_list.empty() || token_list.back().type!=format_tokent::TEXT) token_list.push_back(format_tokent(format_tokent::TEXT)); std::string tmp; for(;it!=arg_string.end() && *it!='%';it++) tmp+=*it; assert(!token_list.empty()); token_list.back().value=tmp; } } return token_list; }
/* -------------------------------------------------------------------------- */ int parse_cmdline_decoder(int argc, char **argv, opj_decompress_parameters *parameters,img_fol_t *img_fol, char *indexfilename) { /* parse the command line */ int totlen, c; opj_option_t long_option[]={ {"ImgDir", REQ_ARG, NULL,'y'}, {"OutFor", REQ_ARG, NULL,'O'}, {"force-rgb", NO_ARG, NULL, 1}, {"upsample", NO_ARG, NULL, 1}, {"split-pnm", NO_ARG, NULL, 1} }; const char optlist[] = "i:o:r:l:x:d:t:p:" /* UniPG>> */ #ifdef USE_JPWL "W:" #endif /* USE_JPWL */ /* <<UniPG */ "h" ; long_option[2].flag = &(parameters->force_rgb); long_option[3].flag = &(parameters->upsample); long_option[4].flag = &(parameters->split_pnm); totlen=sizeof(long_option); opj_reset_options_reading(); img_fol->set_out_format = 0; do { c = opj_getopt_long(argc, argv,optlist,long_option,totlen); if (c == -1) break; switch (c) { case 0: /* long opt with flag */ break; case 'i': /* input file */ { char *infile = opj_optarg; parameters->decod_format = infile_format(infile); switch(parameters->decod_format) { case J2K_CFMT: break; case JP2_CFMT: break; case JPT_CFMT: break; case -2: fprintf(stderr, "!! infile cannot be read: %s !!\n\n", infile); return 1; default: fprintf(stderr, "[ERROR] Unknown input file format: %s \n" " Known file formats are *.j2k, *.jp2, *.jpc or *.jpt\n", infile); return 1; } strncpy(parameters->infile, infile, sizeof(parameters->infile)-1); } break; /* ----------------------------------------------------- */ case 'o': /* output file */ { char *outfile = opj_optarg; parameters->cod_format = get_file_format(outfile); switch(parameters->cod_format) { case PGX_DFMT: break; case PXM_DFMT: break; case BMP_DFMT: break; case TIF_DFMT: break; case RAW_DFMT: break; case RAWL_DFMT: break; case TGA_DFMT: break; case PNG_DFMT: break; default: fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!! \n", outfile); return 1; } strncpy(parameters->outfile, outfile, sizeof(parameters->outfile)-1); } break; /* ----------------------------------------------------- */ case 'O': /* output format */ { char outformat[50]; char *of = opj_optarg; sprintf(outformat,".%s",of); img_fol->set_out_format = 1; parameters->cod_format = get_file_format(outformat); switch(parameters->cod_format) { case PGX_DFMT: img_fol->out_format = "pgx"; break; case PXM_DFMT: img_fol->out_format = "ppm"; break; case BMP_DFMT: img_fol->out_format = "bmp"; break; case TIF_DFMT: img_fol->out_format = "tif"; break; case RAW_DFMT: img_fol->out_format = "raw"; break; case RAWL_DFMT: img_fol->out_format = "rawl"; break; case TGA_DFMT: img_fol->out_format = "raw"; break; case PNG_DFMT: img_fol->out_format = "png"; break; default: fprintf(stderr, "Unknown output format image %s [only *.pnm, *.pgm, *.ppm, *.pgx, *.bmp, *.tif, *.raw or *.tga]!! \n", outformat); return 1; break; } } break; /* ----------------------------------------------------- */ case 'r': /* reduce option */ { sscanf(opj_optarg, "%u", &(parameters->core.cp_reduce)); } break; /* ----------------------------------------------------- */ case 'l': /* layering option */ { sscanf(opj_optarg, "%u", &(parameters->core.cp_layer)); } break; /* ----------------------------------------------------- */ case 'h': /* display an help description */ decode_help_display(); return 1; /* ----------------------------------------------------- */ case 'y': /* Image Directory path */ { img_fol->imgdirpath = (char*)malloc(strlen(opj_optarg) + 1); strcpy(img_fol->imgdirpath,opj_optarg); img_fol->set_imgdir=1; } break; /* ----------------------------------------------------- */ case 'd': /* Input decode ROI */ { int size_optarg = (int)strlen(opj_optarg) + 1; char *ROI_values = (char*) malloc((size_t)size_optarg); ROI_values[0] = '\0'; strncpy(ROI_values, opj_optarg, strlen(opj_optarg)); ROI_values[strlen(opj_optarg)] = '\0'; /*printf("ROI_values = %s [%d / %d]\n", ROI_values, strlen(ROI_values), size_optarg ); */ parse_DA_values( ROI_values, ¶meters->DA_x0, ¶meters->DA_y0, ¶meters->DA_x1, ¶meters->DA_y1); free(ROI_values); } break; /* ----------------------------------------------------- */ case 't': /* Input tile index */ { sscanf(opj_optarg, "%u", ¶meters->tile_index); parameters->nb_tile_to_decode = 1; } break; /* ----------------------------------------------------- */ case 'x': /* Creation of index file */ { char *index = opj_optarg; strncpy(indexfilename, index, OPJ_PATH_LEN); } break; /* ----------------------------------------------------- */ case 'p': /* Force precision */ { if (!parse_precision(opj_optarg, parameters)) { return 1; } } break; /* ----------------------------------------------------- */ /* UniPG>> */ #ifdef USE_JPWL case 'W': /* activate JPWL correction */ { char *token = NULL; token = strtok(opj_optarg, ","); while(token != NULL) { /* search expected number of components */ if (*token == 'c') { static int compno; compno = JPWL_EXPECTED_COMPONENTS; /* predefined no. of components */ if(sscanf(token, "c=%d", &compno) == 1) { /* Specified */ if ((compno < 1) || (compno > 256)) { fprintf(stderr, "ERROR -> invalid number of components c = %d\n", compno); return 1; } parameters->jpwl_exp_comps = compno; } else if (!strcmp(token, "c")) { /* default */ parameters->jpwl_exp_comps = compno; /* auto for default size */ } else { fprintf(stderr, "ERROR -> invalid components specified = %s\n", token); return 1; }; } /* search maximum number of tiles */ if (*token == 't') { static int tileno; tileno = JPWL_MAXIMUM_TILES; /* maximum no. of tiles */ if(sscanf(token, "t=%d", &tileno) == 1) { /* Specified */ if ((tileno < 1) || (tileno > JPWL_MAXIMUM_TILES)) { fprintf(stderr, "ERROR -> invalid number of tiles t = %d\n", tileno); return 1; } parameters->jpwl_max_tiles = tileno; } else if (!strcmp(token, "t")) { /* default */ parameters->jpwl_max_tiles = tileno; /* auto for default size */ } else { fprintf(stderr, "ERROR -> invalid tiles specified = %s\n", token); return 1; }; } /* next token or bust */ token = strtok(NULL, ","); }; parameters->jpwl_correct = OPJ_TRUE; fprintf(stdout, "JPWL correction capability activated\n"); fprintf(stdout, "- expecting %d components\n", parameters->jpwl_exp_comps); } break; #endif /* USE_JPWL */ /* <<UniPG */ /* ----------------------------------------------------- */ default: fprintf(stderr, "[WARNING] An invalid option has been ignored.\n"); break; } }while(c != -1); /* check for possible errors */ if(img_fol->set_imgdir==1){ if(!(parameters->infile[0]==0)){ fprintf(stderr, "[ERROR] options -ImgDir and -i cannot be used together.\n"); return 1; } if(img_fol->set_out_format == 0){ fprintf(stderr, "[ERROR] When -ImgDir is used, -OutFor <FORMAT> must be used.\n"); fprintf(stderr, "Only one format allowed.\n" "Valid format are PGM, PPM, PNM, PGX, BMP, TIF, RAW and TGA.\n"); return 1; } if(!((parameters->outfile[0] == 0))){ fprintf(stderr, "[ERROR] options -ImgDir and -o cannot be used together.\n"); return 1; } }else{ if((parameters->infile[0] == 0) || (parameters->outfile[0] == 0)) { fprintf(stderr, "[ERROR] Required parameters are missing\n" "Example: %s -i image.j2k -o image.pgm\n",argv[0]); fprintf(stderr, " Help: %s -h\n",argv[0]); return 1; } } return 0; }
int parse_expression(char *expr) { if (strlen(strtrim(expr)) == 0) return BREAK; char *token; static const char *operators = "+/*-%^"; static const char *mfuncs = "|ln|sqrt|sin|cos|tan|asin|acos|atan|"; static const char *constants = "|e|pi|"; double operand; char *ptr; while ((token = strsep(&expr, " \n"))) { if (strlen(token) == 0) continue; if (*token == ':') /* precision specified */ parse_precision(++token); else if (strchr(operators, *token) && strlen(token) == 1) { /* operator */ if (stackptr - opstack < 2) { fprintf(stderr, "!! Malformed expression -- too few operands.\n"); return CONTINUE; } if (parse_operator(*token) > 0) { return CONTINUE; } } else if ((ptr = strstr(mfuncs, strlower(token))) != NULL && *(ptr - 1) == '|' && *(ptr + strlen(token)) == '|') { /* validated trig function */ if (stackptr - opstack < 1) { fprintf(stderr, "!! Malformed expression -- too few operands.\n"); return CONTINUE; } parse_mfunc(token); } else if ((ptr = strstr(constants, strlower(token))) != NULL && *(ptr - 1) == '|' && *(ptr + strlen(token)) == '|') { /* validated constant */ parse_constant(token); } else { /* it's an operand, or it's bad input */ if (parse_operand(token, &operand) > 0) /* parsing failed on bad input */ return CONTINUE; if (stackptr == &opstack[STACK_SIZE]) { /* stack overflow */ fprintf(stderr, "!! Stack overflow. Expression too large.\n"); return CONTINUE; } *stackptr++ = operand; } } if (stackptr - opstack > 1) fprintf(stderr, "!! Malformed expression -- too many operands.\n"); else if (stackptr - opstack == 1) { if (verbose >= 1) printf(" = %.*f\n", precision, *--stackptr); else printf("%.*f\n", precision, *--stackptr); } return CONTINUE; }