void CUtlBufferEditor::PutDelimitedString( CUtlCharConversion *pConv, const char *pString )
{
	if ( !IsText() || !pConv )
	{
		PutString( pString );
		return;
	}

	if ( WasLastCharacterCR() )
	{
		PutTabs();
	}
	Put( pConv->GetDelimiter(), pConv->GetDelimiterLength() );

	int nLen = pString ? Q_strlen( pString ) : 0;
	for ( int i = 0; i < nLen; ++i )
	{
		PutDelimitedCharInternal( pConv, pString[i] );
	}

	if ( WasLastCharacterCR() )
	{
		PutTabs();
	}
	Put( pConv->GetDelimiter(), pConv->GetDelimiterLength() );
}
//-----------------------------------------------------------------------------
// Writes a null-terminated string
//-----------------------------------------------------------------------------
void CUtlBufferEditor::PutString( const char* pString )
{
	if (!IsText())
	{
		if ( pString )
		{
			// Not text? append a null at the end.
			size_t nLen = Q_strlen( pString ) + 1;
			Put( pString, nLen * sizeof(char) );
			return;
		}
		else
		{
			PutTypeBin<char>( 0 );
		}
	}
	else if (pString)
	{
		int nTabCount = ( m_Flags & AUTO_TABS_DISABLED ) ? 0 : m_nTab;
		if ( nTabCount > 0 )
		{
			if ( WasLastCharacterCR() )
			{
				if ( !IsDirectiveLine( pString ) )
					PutTabs();
			}

			const char* pEndl = strchr( pString, '\n' );
			while ( pEndl )
			{
				size_t nSize = (size_t)pEndl - (size_t)pString + sizeof(char);
				Put( pString, nSize );
				pString = pEndl + 1;
				if ( *pString )
				{
					if ( !IsDirectiveLine( pString ) )
						PutTabs();
					pEndl = strchr( pString, '\n' );
				}
				else
				{
					pEndl = NULL;
				}
			}
		}
		size_t nLen = Q_strlen( pString );
		if ( nLen )
		{
			Put( pString, nLen * sizeof(char) );
		}
	}
}
Пример #3
0
void WriteJSON(element *el, long lev)
{
	long tabs = (lev - 1) * 2;
	long i = 0;
	pair *attr = NULL;
	cont *citem = NULL;
	
	PutTabs(tabs);
	fputs("[ \"", ofile);
	Put32StrJSON(el->name, el->namelen);
	fputs("\",", ofile);

	if (!el->attrcnt)
		fputs(" {}, ", ofile);
	else if (el->attrcnt == 1) {
		fputs(" { \"", ofile);
		attr = el->attrs[0];
		Put32StrJSON(attr->name, attr->namelen);
		fputs("\": \"", ofile);
		Put32StrJSON(attr->val, attr->vallen);
		fputs("\" }, ", ofile);
	}
	else {
		fputs("\n", ofile);
		PutTabs(tabs + 1);
		fputc('{', ofile);
		for (i = 0; i < el->attrcnt; i++) {
			if (!i) 
				fputc('\t', ofile);
			else
				PutTabs(tabs + 2);
			fputc('"', ofile);
			attr = el->attrs[i];
			Put32StrJSON(attr->name, attr->namelen);
			fputs("\": \"", ofile);
			Put32StrJSON(attr->val, attr->vallen);
			fputc('"', ofile);
			if (i < (el->attrcnt - 1))
				fputc(',', ofile);
			fputs("\n", ofile);
		}
		PutTabs(tabs + 1);
		fputs("}, ", ofile);
	}

	if (!el->contcnt)
		fputs(" []", ofile);
	else if ((el->contcnt == 1)
	 && (citem = el->content[0])->cnt) {
		fputs("[ \"", ofile);
		Put32StrJSON((unl *) citem->it, citem->cnt);
		fputs("\" ]", ofile);
	}
	else {
		fputs("\n", ofile);
		PutTabs(tabs + 1);
		fputs("[", ofile);
		for (i = 0; i < el->contcnt; i++) {
			citem = el->content[i];
			if (citem->cnt) {  // text
				if (!i) 
					fputc('\t', ofile);
				else
					PutTabs(tabs + 2);
				fputc('"', ofile);
				Put32StrJSON((unl *) citem->it, citem->cnt);
				fputc('"', ofile);
			}
			else { // element
				if (!i)
					fputs("\n", ofile);
				WriteJSON((element *) citem->it, lev + 1);
			}
			if (i < (el->contcnt - 1))
				fputc(',', ofile);
			fputs("\n", ofile);
		}
		PutTabs(tabs + 1);
		fputs("]", ofile);
	}
	fputs("\n", ofile);
	PutTabs(tabs);
	fputs("]", ofile);
}