示例#1
0
/****************************************************************************
Desc:	This code is common to all of the SetParam() functions
****************************************************************************/
RCODE F_IniFile::setParamCommon( 
	INI_LINE **		ppLine,
	const char *	pszParamName)
{
	RCODE				rc = NE_FLM_OK;
	INI_LINE *		pLine;

	if( RC_BAD( rc = m_pool.poolCalloc( 
		sizeof( INI_LINE), (void **)&pLine)))
	{
		goto Exit;
	}
	
	if( m_pLastLine)
	{
		m_pLastLine->pNext = pLine;
	}
	
	pLine->pPrev = m_pLastLine;
	m_pLastLine = pLine;
	
	if( !m_pFirstLine)
	{
		m_pFirstLine = pLine;
	}

	if( RC_BAD( rc = m_pool.poolAlloc( f_strlen(pszParamName)+1,
								(void **)&pLine->pszParamName)))
	{
		goto Exit;
	}

	f_strcpy( pLine->pszParamName, pszParamName);

Exit:

	if( RC_OK( rc))
	{
		*ppLine = pLine;
	}

	return( rc);
}
示例#2
0
/****************************************************************************
Desc:	Parse a single line from the ini file into its name, value and comment
	   parts.
****************************************************************************/
RCODE F_IniFile::parseBuffer(
	char *		pszBuf,
	FLMUINT		uiNumBytes)
{
	RCODE			rc = NE_FLM_OK;
	FLMUINT		uiCurrentChar = 0;
	char *		pszNameStart = NULL;
	char *		pszNameEnd = NULL;
	char *		pszValStart = NULL;
	char *		pszValEnd = NULL;
	char *		pszCommentStart = NULL;
	INI_LINE *	pLine = NULL;
	FLMUINT		uiStrLen = 0;

	f_assert( pszBuf);
	f_assert( uiNumBytes);

	// Start looking for the parameter name...
	
	while (uiCurrentChar < uiNumBytes)
	{
		if( !isWhiteSpace( pszBuf[uiCurrentChar]))
		{
			if (pszBuf[uiCurrentChar] == '#') 
			{
				goto Comment;
			}
			else
			{
				pszNameStart = &pszBuf[uiCurrentChar];
				break;
			}
		}
		uiCurrentChar++;
	}

	// We've found a param name, now mark the end of it
	// We determine the end by looking for whitespace or '='
	// or '#'
	
	while (uiCurrentChar < uiNumBytes)
	{
		if( isWhiteSpace( pszBuf[uiCurrentChar]) ||
			  (pszBuf[uiCurrentChar] == '=') || 
			  (pszBuf[uiCurrentChar] == '#'))
		{
			pszNameEnd = &pszBuf[uiCurrentChar-1];
			break;
		}

		uiCurrentChar++;
	}

	if( (uiCurrentChar == uiNumBytes) && 
		  (pszNameEnd == NULL) )
	{
		pszNameEnd = &pszBuf[uiCurrentChar - 1];
	}

	// Now, there may be a value part or a comment part next.  If there's a
	// value, it had better be preceeded by an '='
	
	while( (uiCurrentChar < uiNumBytes) && 
			  isWhiteSpace( pszBuf[uiCurrentChar]) )
	{
		uiCurrentChar++;
	}
	
	if( uiCurrentChar < uiNumBytes && pszBuf[ uiCurrentChar] == '#')
	{
		goto Comment;
	}

	if( uiCurrentChar < uiNumBytes && pszBuf[uiCurrentChar] != '=' )
	{
		rc = RC_SET( NE_FLM_SYNTAX);
		goto Exit;
	}

	// Ok - at this point pszBuf[uiCurrentChar] contains an =.  Skip over
	// the = and any whitespace that follows.
	
	while( uiCurrentChar < uiNumBytes)
	{
		uiCurrentChar++;
		if( !isWhiteSpace( pszBuf[uiCurrentChar]))
		{
			pszValStart = &pszBuf[uiCurrentChar];
			break;
		}
	}

	// Now mark the end of the value.
	// We determine the end by looking for whitespace or '#'
	
	while( uiCurrentChar < uiNumBytes) 
	{
		if( isWhiteSpace( pszBuf[uiCurrentChar]) || 
			  (pszBuf[uiCurrentChar] == '#'))
		{
			pszValEnd = &pszBuf[uiCurrentChar-1];
			break;
		}		
		uiCurrentChar++;
	}

	if( uiCurrentChar == uiNumBytes && !pszValEnd)
	{
		pszValEnd = &pszBuf[uiCurrentChar-1];
	}

Comment:

	// Check out the rest of the line to see if there's a comment
	
	while( uiCurrentChar < uiNumBytes)
	{
		if( !isWhiteSpace( pszBuf[ uiCurrentChar]) &&
			 pszBuf[ uiCurrentChar] != '#')
		{
			rc = RC_SET( NE_FLM_SYNTAX);
			goto Exit;
		}
		else if( pszBuf[ uiCurrentChar] == '#')
		{
			// Comment found.  Set pszCommentStart to the next char
			
			pszCommentStart = &pszBuf[uiCurrentChar+1];
			break;
		}
		uiCurrentChar++;
	}

	// Done parsing.  Now, assuming the line had any info in it,
	// store all the strings...
	
	if( pszNameStart || pszCommentStart)
	{
		if( RC_BAD( rc = m_pool.poolCalloc( sizeof( INI_LINE),
										(void **)&pLine)))
		{
			goto Exit;
		}
		
		if( pszNameStart)
		{
			uiStrLen = pszNameEnd - pszNameStart + 1;
			if( RC_BAD( rc = m_pool.poolAlloc( uiStrLen + 1,
								(void **)&pLine->pszParamName)))
			{
				goto Exit;
			}
			
			f_memcpy( pLine->pszParamName, pszNameStart, uiStrLen);
			pLine->pszParamName[uiStrLen] = '\0';
		}

		if( pszValStart)
		{
			uiStrLen = pszValEnd - pszValStart + 1;
			if( RC_BAD( rc = m_pool.poolAlloc( uiStrLen + 1,
						(void **)&pLine->pszParamValue)))
			{
				goto Exit;
			}
			
			f_memcpy(pLine->pszParamValue, pszValStart, uiStrLen);
			pLine->pszParamValue[uiStrLen] = '\0';
		}
		
		if (pszCommentStart)
		{
			uiStrLen = uiNumBytes-(pszCommentStart-pszBuf);
			if (RC_BAD( rc = m_pool.poolAlloc( uiStrLen + 1,
										(void **)&pLine->pszComment)))
			{
				goto Exit;
			}
			
			f_memcpy(pLine->pszComment, pszCommentStart, uiStrLen);
			pLine->pszComment[uiStrLen] = '\0';
		}
		
		// Insert this struct into the linked list
		
		if( m_pLastLine)
		{
			m_pLastLine->pNext = pLine;
		}
		
		pLine->pPrev = m_pLastLine;
		pLine->pNext = NULL;
		m_pLastLine = pLine;
		
		if( !m_pFirstLine)
		{
			m_pFirstLine = pLine;
		}
	}
	
Exit:

	return( rc);
}