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 48 49
|
// get a column from a string for a given delimiter
void getColumn(char * inputStr, int columnNo, char delimiter, char * outputStr)
{
// define variables
char inputStr_aux[_COLUMN_SIZE];
strcpy(inputStr_aux, inputStr);
char * columnBegin = inputStr_aux;
char * columnEnd = inputStr_aux;
int columnPos = 1;
// get column
// only once column that is not terminated by a delimiter
if (columnNo == 1 && strchr(columnBegin, delimiter) != NULL)
columnEnd = strchr(columnBegin, delimiter) + 1;
// columnNo smaller than 1
else if (columnNo <= 0)
{
writeTo(_LOG_FILE, "getColumn(char * inputStr, ...) - number of column has to be bigger than 0!");
columnEnd = strchr(columnBegin, delimiter) + 1;
myPause();
}
// other cases
else
{
while (columnPos < columnNo)
{
if (strchr(columnBegin, delimiter) != NULL)
{
columnBegin = strchr(columnBegin, delimiter) + 1;
columnEnd = strchr(columnBegin, delimiter) + 1;
columnPos++;
}
// number of delimiters is smaller than columnNo
else
{
writeTo(_LOG_FILE, "getColumn(char * inputStr, ...) - number of columns does to correspond to number of delimiters!");
columnEnd = columnBegin; //return the last column
myPause();
break;
}
}
}
// return the corresponding column
if (columnBegin != columnEnd)
columnBegin[columnEnd - columnBegin - 1] = '\0';
strcpy(outputStr, columnBegin);
}
|