File: filter_range.cpp

package info (click to toggle)
odin 2.0.5-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,196 kB
  • sloc: cpp: 62,638; sh: 4,541; makefile: 779
file content (47 lines) | stat: -rw-r--r-- 1,538 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "filter_range.h"

bool str2range(const STD_string& str, Range& range, int srcsize) {
  Log<Filter> odinlog("","str2range");
  if(str=="") return false;
  int increment=1;
  svector tokscol=tokens(str,':');
  int colsize=tokscol.size();
  if(colsize<1 || colsize>2) return false;
  if(colsize==2) increment=atoi(tokscol[1].c_str());
  STD_string fromto=tokscol[0];
  bool result=false;
  if(tolowerstr(fromto)=="all") {
    range=Range::all();
    result=true;
  } else {
    svector toks=tokens(fromto,'-');
    if(toks.size()==2) {range=Range(atoi(toks[0].c_str()),atoi(toks[1].c_str()),increment); result=true;}
    if(toks.size()==1) {
      int singleval=atoi(toks[0].c_str());
      int low=singleval;
      int upp=singleval;
      if(fromto.length()) {
        if(fromto[0]=='-') low=0;
        if(fromto[fromto.length()-1]=='-') upp=srcsize-1;
      }
      range=Range(low,upp,increment);
      result=true;
    }
  }

  if(!result) {
    ODINLOG(odinlog,errorLog) << "Error parsing range string >" << str << "<" << STD_endl;
  }

  if(result) {
    if(range.first()>range.last()) result=false;
    if(range.first()<0 || range.first()>=srcsize) result=false;
    if(range.last()<0  || range.last()>=srcsize)  result=false;
    if(!result) {
      ODINLOG(odinlog,errorLog) << "selected " << range << " out of valid range (0," << (srcsize-1) << ")" << STD_endl;
    }
  }
  return result;
}

STD_string str2range_usage() {return "single position, or \'all\', or explicit range, optionally with increment (e.g. 1-10:3)";}