range.cpp 2.14 KB
#include "range.h"

range parseRange(char* input, int total)
{
        bool includeRange = false;
        range r;
        if(input[0] == '~')
        {
                if(strlen(input) > 2)
                {
                        r.min = 0;
                        if(input[1] == ':')
                        {
                                if(input[strlen(input)-1] == '%')
                                {
                                        input[strlen(input)-1] = '\0';
                                        r.max = total*((double)str2int(string(input+2))/100);
                                }
                                r.max = str2int(string(input+2));
                        }
                        return r;
                }
                throw rangeException("Wrong range format or maximum and minimum are unsatisfiable");
        }
        else if(input[0] == '@')
 	{
                includeRange = true;
                input++;
        }
        string str = string(input);
        size_t pos = str.find_first_of(":");
        if(pos != string::npos)
        {
                if(str.find_first_of(":",pos+1) != string::npos)
                {
                        throw rangeException("Wrong range format or maximum and minimum are unsatisfiable");
                }
                if(str[pos-1] == '%')
                {
                        r.min = total*((double)str2int(str.substr(0,pos-1))/100);
                }
                else
                {
                        r.min = str2int(str.substr(0,pos));
                }
        }
        else
        {
                r.min = 0;
                if(str[str.length()-1] == '%')
                {
                        r.max = total*((double)str2int(str.substr(0,str.length()-1))/100);
                }
                else
                {
                        r.max = str2int(str);
                }
        }
        if(includeRange)
        {
                r.min--;
                r.max++;
        }
        if(r.min > r.max)
        {
                throw rangeException("Wrong range format or maximum and minimum are unsatisfiable");
        }
        return r;
}