FreeBSD/contrib/ntp/ntpsnmpd/ntpSnmpSubagentObject.c
/*****************************************************************************
 *
 * ntpsnmpd_cut_string
 *
 *  This function will parse a given NULL terminated string and cut it
 *  into fields using the specified delimiter character. 
 *  It will then copy the requested field into a destination buffer
 *  Example:
 *     ntpsnmpd_cut_string(read:my:lips:fool, RESULT, ':', 2, sizeof(RESULT))
 *  will copy "lips" to RESULT.
 ****************************************************************************
 * Parameters:
 *	src		const char *	The name of the source string variable
 *					NOTE: must be NULL terminated!
 *	dest		char *		The name of the string which takes the
 *					requested field content
 * 	delim		char		The delimiter character
 *	fieldnumber	int		The number of the required field
 *					(start counting with 0)
 *	maxsize		size_t		The maximum size of dest
 *
 * Returns:
 *	size_t		length of resulting dest string 
 ****************************************************************************/
ntpsnmpd_cut_string
This function will parse a given NULL terminated string and cut it
into fields using the specified delimiter character.

src             const char *    The name of the source string variable
NOTE: must be NULL terminated.
dest            char *          The name of the string which takes the
requested field content
delim           char            The delimiter character
fieldnumber     int             The number of the required field
(start counting with 0)
maxsize         size_t          The maximum size of dest

size_t          length of resulting dest string
ntpsnmpd_cut_string This function will parse a given NULL terminated string and cut it into fields using the specified delimiter character . src const char * The name of the source string variable NOTE : must be NULL terminated . dest char * The name of the string which takes the requested field content delim char The delimiter character fieldnumber int The number of the required field ( start counting with 0 ) maxsize size_t The maximum size of dest size_t length of resulting dest string
size_t
ntpsnmpd_cut_string(
	const char *	string,
	char *		dest,
	char		delim,
	int		fieldnumber,
	size_t		maxsize
	)
{
	size_t i;
	size_t j;
	int l;
	size_t str_cnt;
	if (maxsize < 1)
		return 0;
	str_cnt = strlen(string);
	j = 0;
	memset(dest, 0, maxsize);
	for (i = 0, l = 0; i < str_cnt && l <= fieldnumber; i++) {
		if (string[i] == delim)
			l++;	
		else if (l == fieldnumber && j < maxsize)
			dest[j++] = string[i]; 
	}
	j = min(j, maxsize - 1);
	dest[j] = '\0';
	return j;
}
size_t ntpsnmpd_cut_string ( const char * string , char * dest , char delim , int fieldnumber , size_t maxsize ) { size_t i ; size_t j ; int l ; size_t str_cnt ; if ( maxsize < 1 ) return 0 ; str_cnt = strlen ( string ) ; j = 0 ; memset ( dest , 0 , maxsize ) ; for ( i = 0 , l = 0 ; i < str_cnt && l <= fieldnumber ; i ++ ) { if ( string [ i ] == delim ) l ++ ; else if ( l == fieldnumber && j < maxsize ) dest [ j ++ ] = string [ i ] ; } j = min ( j , maxsize - 1 ) ; dest [ j ] = ' \0 ' ; return j ; }
ntpsnmpd_cut_string
  This function will parse a given NULL terminated string and cut it
  into fields using the specified delimiter character.
ntpsnmpd_cut_string This function will parse a given NULL terminated string and cut it into fields using the specified delimiter character .
/* Parsing the field name */ /* next field */