@@ -1237,33 +1237,19 @@ ChannelSelectorBox::~ChannelSelectorBox()
12371237int ChannelSelectorBox::convertToInteger (std::string s)
12381238{
12391239 char ar[20 ];
1240- int i,j,k=0 ;
1241- for (i = 0 ; i < s.size (); i++) // trim whitespace from front
1242- {
1243- if ((int )s[i] != 32 )
1244- {
1245- break ;
1246- }
1247- }
1248- for (j = s.size () - 1 ; j >= 0 ; j--) // trim whitespaces from end
1249- {
1250- if ((int )s[j] != 32 )
1251- {
1252- break ;
1253- }
1254- }
1255- if (j - i > 8 )
1256- {
1257- return 0 ;
1258- }
1259- for (; i <= j; i++)
1240+ int i, j, k = 0 ;
1241+ for (i = 0 ; i < s.size (); i++)
12601242 {
12611243 if (s[i] >= 48 && s[i] <= 57 )
12621244 {
12631245 ar[k] = s[i];
12641246 k++;
12651247 }
12661248 }
1249+ if (k>7 )
1250+ {
1251+ return 1000000 ;
1252+ }
12671253 ar[k] = ' \0 ' ;
12681254 k = atoi (ar);
12691255 return k;
@@ -1278,87 +1264,88 @@ int ChannelSelectorBox::convertToInteger(std::string s)
12781264*/
12791265std::vector<int > ChannelSelectorBox::getBoxInfo (int len)
12801266{
1281- std::string s = getText ().toStdString ();
1282- std::vector<std::string> parsed;
1283- std::vector<int > finalList,colonNum,boxList;
1284- finalList.clear ();
1285- int i, j, k, a, otherChar = 0 , b, x;
1286-
1287- for (i = 0 ; i < s.size (); i++) // fetch all valid ranges from text box
1267+ std::string s = " ," ;
1268+ s += getText ().toStdString ();
1269+ std::vector<int > finalList,separator,rangeseparator;
1270+ int i, j, a, b, k, openb, closeb, otherchar,x,y;
1271+ s += " ," ;
1272+ for (i = 0 ; i < s.size (); i++) // split string by ' , ' or ' ; '
12881273 {
1289- if (s[i] == ' [ ' )
1274+ if (s[i] == ' ; ' || s[i] == ' , ' )
12901275 {
1291- boxList.push_back (i);
1292- j = i + 1 ;
1293- for (; j < s.size (); j++)
1294- {
1295- if (s[j] == ' ]' )
1296- {
1297- boxList.push_back (j);
1298- break ;
1299- }
1300- }
1301- i = j;
1276+ separator.push_back (i);
13021277 }
1303- else if ((int )s[i] >= 48 && (int )s[i] <= 57 ) // when input is given for a single channel without square brackets.
1278+ }
1279+ for (i = 0 ; i < separator.size ()-1 ; i++) // split ranges by ' : ' or ' - '
1280+ {
1281+ j = k = separator[i] + 1 ;
1282+ openb = closeb = otherchar = 0 ;
1283+ rangeseparator.clear ();
1284+ for (; j < separator[i + 1 ]; j++)
13041285 {
1305- j = i;
1306- for (; j < s.size (); j++)
1286+ if (s[j] == ' -' || s[j] == ' :' )
13071287 {
1308- if ((int )s[j] > 57 || (int )s[j] < 48 )
1309- {
1310- break ;
1311- }
1288+ rangeseparator.push_back (j);
13121289 }
1313- a = convertToInteger (s.substr (i, j - i + 1 ));
1314- if (a == 0 || a > len)
1290+ else if (((int )s[j] == 32 ))
13151291 {
1316- i = j;
13171292 continue ;
13181293 }
1319- finalList.push_back (a - 1 );
1320- finalList.push_back (a - 1 );
1321- finalList.push_back (1 );
1322- i = j;
1294+ else if (s[j] == ' [' || s[j] == ' {' || s[j] == ' (' )
1295+ {
1296+ openb++;
1297+ }
1298+ else if (s[j] == ' ]' || s[j] == ' }' || s[j] == ' )' )
1299+ {
1300+ closeb++;
1301+ }
1302+ else if ( (int )s[j] > 57 || (int )s[j] < 48 )
1303+ {
1304+ otherchar++;
1305+ }
13231306 }
1324- }
1325-
1326- if (boxList.size () % 2 != 0 )
1327- {
1328- boxList.pop_back ();
1329- }
13301307
1331- /*
1332- for each valid ranges fetch the start value, end value, common difference.
1333- */
1334- for (i = 0 ; i < boxList.size (); i+=2 )
1335- {
1336- colonNum.clear ();
1337- otherChar = 0 ;
1338- for (x = boxList[i] + 1 ; x < boxList[i + 1 ]; x++)
1308+ if (openb != closeb || openb > 1 || closeb > 1 || otherchar > 0 ) // Invalid input
13391309 {
1340- if (s[x] == ' :' )
1341- {
1342- colonNum.push_back (x);
1343- }
1344- else if ((int )s[x] == 32 )
1310+ continue ;
1311+ }
1312+
1313+
1314+ for (x = separator[i] + 1 ; x < separator[i + 1 ]; x++) // trim whitespace and brackets from front
1315+ {
1316+ if (((int )s[x] >= 48 && (int )s[x] <= 57 ) || s[x] == ' :' || s[x] == ' -' )
13451317 {
1346- continue ;
1318+ break ;
13471319 }
1348- else if (((int )s[x] < 48 || (int )s[x]>57 ))
1320+ }
1321+ for (y = separator[i + 1 ] - 1 ; y > separator[i]; y--) // trim whitespace and brackets from end
1322+ {
1323+ if (((int )s[y] >= 48 && (int )s[y] <= 57 ) || s[y] == ' :' || s[y] == ' -' )
13491324 {
1350- otherChar++ ;
1325+ break ;
13511326 }
13521327 }
1353- if (colonNum. size ()> 2 || otherChar > 0 )
1328+ if (x > y )
13541329 {
13551330 continue ;
13561331 }
13571332
1358- if (colonNum.size () == 1 ) // when range is of form [x:y]
1333+
1334+ if (rangeseparator.size () == 0 ) // syntax of form - x or [x]
13591335 {
1360- a = convertToInteger (s.substr (boxList[i], colonNum[0 ] - boxList[i] + 1 ));
1361- b = convertToInteger (s.substr (colonNum[0 ], boxList[i+1 ] - colonNum[0 ] + 1 ));
1336+ a = convertToInteger (s.substr (x, y - x + 1 ));
1337+ if (a == 0 ||a>len)
1338+ {
1339+ continue ;
1340+ }
1341+ finalList.push_back (a - 1 );
1342+ finalList.push_back (a - 1 );
1343+ finalList.push_back (1 );
1344+ }
1345+ else if (rangeseparator.size () == 1 ) // syntax of type - x-y or [x-y]
1346+ {
1347+ a = convertToInteger (s.substr (x, rangeseparator[0 ] - x + 1 ));
1348+ b = convertToInteger (s.substr (rangeseparator[0 ], y - rangeseparator[0 ] + 1 ));
13621349 if (a == 0 )
13631350 {
13641351 a = 1 ;
@@ -1367,23 +1354,19 @@ std::vector<int> ChannelSelectorBox::getBoxInfo(int len)
13671354 {
13681355 b = len;
13691356 }
1370- if (a > len || b > len || a > b )
1357+ if (a > b || a > len || b > len )
13711358 {
13721359 continue ;
13731360 }
13741361 finalList.push_back (a - 1 );
13751362 finalList.push_back (b - 1 );
13761363 finalList.push_back (1 );
13771364 }
1378- else if (colonNum .size () == 2 ) // when range is of form [x:k:y]
1365+ else if (rangeseparator .size () == 2 ) // syntax of type [x:y:z] or x-y-z
13791366 {
1380- a = convertToInteger (s.substr (boxList[i], colonNum[0 ] - boxList[i] + 1 ));
1381- k = convertToInteger (s.substr (colonNum[0 ], colonNum[1 ] - colonNum[0 ] + 1 ));
1382- b = convertToInteger (s.substr (colonNum[1 ], boxList[i+1 ] - colonNum[1 ] + 1 ));
1383- if (k == 0 )
1384- {
1385- k = 1 ;
1386- }
1367+ a = convertToInteger (s.substr (x, rangeseparator[0 ] - x + 1 ));
1368+ k = convertToInteger (s.substr (rangeseparator[0 ], rangeseparator[1 ] - rangeseparator[0 ] + 1 ));
1369+ b = convertToInteger (s.substr (rangeseparator[1 ], y - rangeseparator[1 ] + 1 ));
13871370 if (a == 0 )
13881371 {
13891372 a = 1 ;
@@ -1392,25 +1375,17 @@ std::vector<int> ChannelSelectorBox::getBoxInfo(int len)
13921375 {
13931376 b = len;
13941377 }
1395-
1396- if (a > len || b > len || a > b)
1378+ if (k == 0 )
13971379 {
1398- continue ;
1380+ k = 1 ;
13991381 }
1400- finalList.push_back (a - 1 );
1401- finalList.push_back (b - 1 );
1402- finalList.push_back (k);
1403- }
1404- else if (colonNum.size () == 0 ) // when range is of form [x]
1405- {
1406- a = convertToInteger (s.substr (boxList[i], boxList[i + 1 ] - boxList[i] + 1 ));
1407- if (a == 0 )
1382+ if (a > b || a > len || b > len)
14081383 {
14091384 continue ;
14101385 }
14111386 finalList.push_back (a - 1 );
1412- finalList.push_back (a - 1 );
1413- finalList.push_back (1 );
1387+ finalList.push_back (b - 1 );
1388+ finalList.push_back (k );
14141389 }
14151390 }
14161391 return finalList;
0 commit comments