/**
* Copyright (c) 2008, Corey's Consulting LLC. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/******************************************************************************
getField()
This gets a field from a delimited string. It will not affect the original
string in any way. It does NOT handle quoted fields.
Parameters:
cpLine - i:line containing fields, assumed to be null terminated
cDelim - i:field delimiter to use
iDesiredFld - i:field number to get, starting at 1
cpOutBuf - o:output buffer, where field will go, will be null terminated
iSizBuf - i:size of output buffer, to prevent overruns
Returns:
GF_xxx
****/
int
getField(char *cpLine,char cDelim, int iDesiredFld, char *cpOutBuf,int iSizBuf)
{
int iRv=GF_SUCCESS;
int iCurField=1;
int iBytesCopied=0;
char *cpField;
for(;;) /* enables single exit point */
{
/**********
check parms
**********/
if (iSizBuf < 1)
{
iRv = GF_SIZE_TOO_SMALL;
break;
}
if (iDesiredFld < 1)
{
iRv = GF_FIELD_NOT_FOUND;
break;
}
/**************************
find start of desired field
**************************/
cpField = cpLine;
if (iDesiredFld == 1)
iRv = GF_SUCCESS; /* first field starts at offset 0 */
else
{
/* to be here, iDesiredFld must be greater than 1 */
while (iCurField < iDesiredFld)
{
iCurField++;
/* find beginning of next field */
cpField = strchr(cpField, cDelim);
if (cpField == NULL)
{ /* the previous field was the last field */
iRv = GF_FIELD_NOT_FOUND;
break;
}
cpField++; /* point to beginning of field */
/* loop exits when current and desired field match */
}
}
/* make sure the proper field was found previously */
if (iRv != GF_SUCCESS)
break;
/* cpField is pointing to the first byte of the desired field */
while ( (*cpField != cDelim) &&
(*cpField != (char)NULL) &&
(iBytesCopied < iSizBuf)
)
{
cpOutBuf[iBytesCopied] = *cpField;
cpField++;
iBytesCopied++;
}
/* if the copy stopped because we ran out of room... */
if (iBytesCopied >= iSizBuf)
{
iRv = GF_SIZE_TOO_SMALL;
}
/* if copy stopped because we encountered a null (last field)... */
else if (cpOutBuf[iBytesCopied] == (char)NULL)
{
/* nothing more to do, outbuf is null terminated */
}
/* if copy stopped because we encountered a delimiter... */
else if (cpOutBuf[iBytesCopied] == cDelim)
{
cpOutBuf[iBytesCopied] = (char)NULL; /* null terminate */
}
/* should never occur */
else
{
iRv = GF_INTERNAL_ERROR;
}
if (1==1)
break;
} /* end single exit point "for" */