/*----------------------------------------------------------
Converts a string into a vector of strings based on the delimiter given
------------------------------------------------------------*/
vector<string> ConvertStringToVectorUsingDelimiter(string MainString, string Delimiter)
{
vector<string> ReturnVector;
int iDelLen = Delimiter.length();
string sCur = "";
int iN = 1; // iN = occurrence of the delimiter within the string
int iPrevInd = 0;
int iCurrInd = GetIndexOfNthOccurrenceInString(MainString,Delimiter,iN);
if (iCurrInd == -1)
{
ReturnVector.push_back(MainString);
return ReturnVector;
}
while (iCurrInd != -1)
{
string sCur = MainString.substr( (iPrevInd + iDelLen) , iCurrInd - (iPrevInd + iDelLen) );
ReturnVector.push_back(sCur);
iPrevInd = iCurrInd;
iN++;
iCurrInd = GetIndexOfNthOccurrenceInString(MainString,Delimiter,iN);
}
return ReturnVector;
}
/*----------------------------------------------------------
Gets the nth occurrence of the substring within the string
------------------------------------------------------------*/
int GetIndexOfNthOccurrenceInString(string sMain, string sSubstr, int iN)
{
int iIndex = -1;
size_t stIndex = 0;
switch (iN)
{
case -1: // Uuser wants the very last occurrence of sSubstr
stIndex = sMain.find_last_of(sSubstr);
if (stIndex == string::npos)
{
return -1;
}
return int(stIndex);
case 0: // provide for if the user asks for the 0th occurrence (make this the first occurence)
iN = 1;
break;
}
int iCurOccurrence = 0;
while ( (iCurOccurrence != iN) && (iCurOccurrence < sMain.size()) )
{
stIndex++;
stIndex = sMain.find(sSubstr, stIndex);
if (stIndex == string::npos)
{
return -1;
}
iCurOccurrence++;
iIndex = int(stIndex);
}
int iPos = int(stIndex);
return iIndex;
}