FreeBSD/contrib/ntp/ntpsnmpd/ntpSnmpSubagentObject.c
/*****************************************************************************
 *
 * ntpsnmpd_parse_string
 *
 *  This function will parse a given NULL terminated string and cut it
 *  into a fieldname and a value part (using the '=' as the delimiter. 
 *  The fieldname will be converted to uppercase and all whitespace 
 *  characters are removed from it.
 *  The value part is stripped, e.g. all whitespace characters are removed
 *  from the beginning and end of the string.
 *  If the value is started and ended with quotes ("), they will be removed
 *  and everything between the quotes is left untouched (including 
 *  whitespace)
 *  Example:
 *     server host name =   hello world!
 *  will result in a field string "SERVERHOSTNAME" and a value
 *  of "hello world!".
 *     My first Parameter		=		"  is this!    "
  * results in a field string "MYFIRSTPARAMETER" and a value " is this!    "
 ****************************************************************************
 * Parameters:
 *	string		const char *	The source string to parse.
 *					NOTE: must be NULL terminated!
 *	field		char *		The buffer for the field name.
 *	fieldsize	size_t		The size of the field buffer.
 *	value		char *		The buffer for the value.
 *	valuesize	size_t		The size of the value buffer.
 *
 * Returns:
 *	size_t			length of value string 
 ****************************************************************************/
ntpsnmpd_parse_string
This function will parse a given NULL terminated string and cut it
into a fieldname and a value part (using the '=' as the delimiter.
The fieldname will be converted to uppercase and all whitespace
characters are removed from it.
The value part is stripped, e.g. all whitespace characters are removed
from the beginning and end of the string.
If the value is started and ended with quotes ("), they will be removed
and everything between the quotes is left untouched (including
whitespace)
Example:
server host name =   hello world.
will result in a field string "SERVERHOSTNAME" and a value
of "hello world!".
My first Parameter               =               "  is this.    "
results in a field string "MYFIRSTPARAMETER" and a value " is this.

string          const char *    The source string to parse.
NOTE: must be NULL terminated.
field           char *          The buffer for the field name.
fieldsize       size_t          The size of the field buffer.
value           char *          The buffer for the value.
valuesize       size_t          The size of the value buffer.

size_t                  length of value string
ntpsnmpd_parse_string This function will parse a given NULL terminated string and cut it into a fieldname and a value part ( using the ' = ' as the delimiter . The fieldname will be converted to uppercase and all whitespace characters are removed from it . The value part is stripped e . g . all whitespace characters are removed from the beginning and end of the string . If the value is started and ended with quotes ( " ) they will be removed and everything between the quotes is left untouched ( including whitespace ) Example : server host name = hello world . will result in a field string " SERVERHOSTNAME " and a value of " hello world! " . My first Parameter = " is this . " results in a field string " MYFIRSTPARAMETER " and a value " is this . string const char * The source string to parse . NOTE : must be NULL terminated . field char * The buffer for the field name . fieldsize size_t The size of the field buffer . value char * The buffer for the value . valuesize size_t The size of the value buffer . size_t length of value string
size_t
ntpsnmpd_parse_string(
	const char *	string,
	char *		field,
	size_t		fieldsize,
	char *		value,
	size_t		valuesize
	)
{
	int i;
	int j;
	int loop;
	size_t str_cnt;
	size_t val_cnt;
	if (fieldsize < 1 || valuesize < 1)
		return 0;
	str_cnt = strlen(string);
	j = 0;
	loop = TRUE;
	for (i = 0; loop && i <= str_cnt; i++) {
		switch (string[i]) {
		case '\t': 	
		case '\n':	
		case '\r':	
		case ' ':  	
			break;
		case '=':
			loop = FALSE;
			break;
		default:
			if (j < fieldsize)
				field[j++] = toupper(string[i]);
		}
	}
	j = min(j, fieldsize - 1);
	field[j] = '\0';
	value[0] = '\0';
	j = 0; 
	for (val_cnt = 0; i < str_cnt; i++) {
		if (string[i] > 0x0D && string[i] != ' ')
			val_cnt = min(j + 1, valuesize - 1);
		if (value[0] != '\0' ||
		    (string[i] > 0x0D && string[i] != ' ')) {
			if (j < valuesize)
				value[j++] = string[i];
		}
	}
	value[val_cnt] = '\0';
	if (value[0] == '"') {
		val_cnt--;
		strlcpy(value, &value[1], valuesize);
		if (val_cnt > 0 && value[val_cnt - 1] == '"') {
			val_cnt--;
			value[val_cnt] = '\0';
		}
	}
	return val_cnt;
}
size_t ntpsnmpd_parse_string ( const char * string , char * field , size_t fieldsize , char * value , size_t valuesize ) { int i ; int j ; int loop ; size_t str_cnt ; size_t val_cnt ; if ( fieldsize < 1 || valuesize < 1 ) return 0 ; str_cnt = strlen ( string ) ; j = 0 ; loop = TRUE ; for ( i = 0 ; loop && i <= str_cnt ; i ++ ) { switch ( string [ i ] ) { case ' \t ' : case ' \n ' : case ' \r ' : case ' ' : break ; case ' ' : loop = FALSE ; break ; default : if ( j < fieldsize ) field [ j ++ ] = toupper ( string [ i ] ) ; } } j = min ( j , fieldsize - 1 ) ; field [ j ] = ' \0 ' ; value [ 0 ] = ' \0 ' ; j = 0 ; for ( val_cnt = 0 ; i < str_cnt ; i ++ ) { if ( string [ i ] > 0x0D && string [ i ] != ' ' ) val_cnt = min ( j + 1 , valuesize - 1 ) ; if ( value [ 0 ] != ' \0 ' || ( string [ i ] > 0x0D && string [ i ] != ' ' ) ) { if ( j < valuesize ) value [ j ++ ] = string [ i ] ; } } value [ val_cnt ] = ' \0 ' ; if ( value [ 0 ] == ' ' ) { val_cnt -- ; strlcpy ( value , & value [ 1 ] , valuesize ) ; if ( val_cnt > 0 && value [ val_cnt - 1 ] == ' ' ) { val_cnt -- ; value [ val_cnt ] = ' \0 ' ; } } return val_cnt ; }
ntpsnmpd_parse_string
  This function will parse a given NULL terminated string and cut it
  into a fieldname and a value part (using the '=' as the delimiter.
ntpsnmpd_parse_string This function will parse a given NULL terminated string and cut it into a fieldname and a value part ( using the ' = ' as the delimiter .
/* we need at least one byte to work with to simplify */ /* Parsing the field name */ /* Tab */ /* LF */ /* CR */ /* Space */ /* Now parsing the value */