X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=lib%2Fargs.h;h=f8cda88b6d51aa79b861f9a0e9a20e5f8126eac8;hb=34de503961eb25613a5b3f2a8e5bdb5d1f5de0e9;hp=2d5c6615175ee0663968aa17fd3b5e067ebbd2dd;hpb=b4c1a60dc1d38ee55f2fc58cbfc3a9f5c613101c;p=swftools.git diff --git a/lib/args.h b/lib/args.h index 2d5c661..f8cda88 100644 --- a/lib/args.h +++ b/lib/args.h @@ -115,4 +115,79 @@ int args_long2shortoption(struct options_t*options, char*name, char*val) exit(1); } +/* check whether the value t is in a given range. + examples: 3 is in range 1-10: true + 7 is in range 2-4,6,8-10: false + 9 is in range 1,2,3-12: true +*/ +char is_in_range(int t, char*irange) +{ + char*pos = irange; + char*digits; + int num; + char range = 0; + int last=0; + char tmp; + + if(!irange) // no range resembles (-OO,OO) + return 1; + + while(*pos) + { + while(*pos == ' ' || *pos == '\r' || *pos == '\n' || *pos == '\t') + pos++; + + digits = pos; + while(*digits>='0' && *digits<='9') + digits++; + if(digits == pos) { + fprintf(stderr, "Error: \"%s\" is not a valid format (digit expected)\n",irange); + exit(1); + } + + tmp=*digits;*digits=0; + num = atoi(pos); + *digits=tmp; + pos = digits; + + while(*pos == ' ' || *pos == '\r' || *pos == '\n' || *pos == '\t') + pos++; + + if(range && last<=t && num>=t) + return 1; + if(range) { + range = 0; + if(*pos) + pos ++; + continue; + } + + if(*pos=='-') + { + if(range) { + fprintf(stderr, "Error: \"%s\" is not a valid format (too many '-'s)\n",irange); + exit(1); + } + last = num; + range = 1; + if(*pos) + pos ++; + continue; + } + else + { + /* if it isn't a '-', we assume it is a seperator like + ',', ';', ':', whatever. */ + if(t == num) + return 1; + if(*pos) + pos ++; + continue; + } + } + if(range && last<=t) + return 1; + return 0; +} + #endif //__args_h__