Пример #1
0
char* Str_SkipSpace( const char* pszNon )
{
	// NOTE: \n is a space.
	ASSERT(pszNon);
	for(;;)
	{
		char ch = *pszNon;
		if ( ch <= 0 )
			break;
		if ( ! Str_IsSpace(ch))
			break;
		pszNon++;
	}
	return (char*) pszNon;
}
Пример #2
0
bool CTaksiConfig::WriteIniFile()
{
	// RETURN: true = success
	//  false = cant save!
	//
	char* pFileOld = NULL;
	DWORD nSizeOld = 0;

	TCHAR szIniFileName[_MAX_PATH];
	Str_MakeFilePath( szIniFileName, COUNTOF(szIniFileName), 
		sg_Shared.m_szIniDir, _T(TAKSI_INI_FILE) );

	// first read all lines
	CNTHandle FileOld( ::CreateFile( szIniFileName, 
		GENERIC_READ, 0, NULL,
		OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ));
	if ( FileOld.IsValidHandle())
	{
		nSizeOld = ::GetFileSize(FileOld, NULL);
		pFileOld = (char*) ::HeapAlloc( g_Proc.m_hHeap, HEAP_ZERO_MEMORY, nSizeOld);
		if (pFileOld == NULL) 
			return false;

		DWORD dwBytesRead = 0;
		::ReadFile(FileOld, pFileOld, nSizeOld, &dwBytesRead, NULL);
		if (dwBytesRead != nSizeOld) 
		{
			::HeapFree( g_Proc.m_hHeap, 0, pFileOld );
			return false;
		}
		FileOld.CloseHandle();
	}

	// create new file
	FILE* pFile = fopen( TAKSI_INI_FILE, "wt");
	if ( pFile == NULL )
		return false;

	// loop over every line from the old file, and overwrite it in the new file
	// if necessary. Otherwise - copy the old line.
	
	CIniObject* pObj = NULL;
	char* pszLine = pFileOld; 

	if (pFileOld)
	while (true)
	{
		if ( pszLine >= pFileOld + nSizeOld )
			break;

		if ( *pszLine == '[' )
		{
			if ( pObj ) // finish previous section.
			{
				pObj->PropsWrite(pFile);
			}
			if ( ! strnicmp( pszLine, "[" TAKSI_SECTION "]", sizeof(TAKSI_SECTION)+1 ))
			{
				pObj = this;
			}
			else if ( ! strnicmp( pszLine, "[" TAKSI_CUSTOM_SECTION " ", sizeof(TAKSI_CUSTOM_SECTION)+1 ))
			{
				TCHAR szSection[ _MAX_PATH ];
#ifdef _UNICODE
				ASSERT(0);
#else
				strncpy( szSection, pszLine+14, sizeof(szSection));
#endif
				TCHAR* pszEnd = _tcschr(szSection, ']');
				if (pszEnd)
					*pszEnd = '\0';
				pObj = CustomConfig_FindAppId(szSection);
			}
			else
			{
				pObj = NULL;
			}
		}

		char* pszEndLine = strchr(pszLine, '\n' ); // INI_CR
		if (pszEndLine) 
		{
			// covers \n or \r\n
			char* pszTmp = pszEndLine;
			for ( ; pszTmp >= pszLine && Str_IsSpace(*pszTmp); pszTmp-- )
				pszTmp[0] = '\0'; 
			pszEndLine++;
		}

		// it's a custom setting.
		bool bReplaced;
		if (pObj)
		{
			bReplaced = pObj->PropWriteName( pFile, pszLine );
		}
		else
		{
			bReplaced = false;
		}
		if (!bReplaced)
		{
			// take the old line as it was, might be blank or comment.
			fprintf(pFile, "%s" INI_CR, pszLine);
		}
		if (pszEndLine == NULL)
			break;
		pszLine = pszEndLine;
	}

	// release buffer
	if(pFileOld)
	{
		::HeapFree( g_Proc.m_hHeap, 0, pFileOld );
	}

	if ( pObj ) // finish previous section.
	{
		pObj->PropsWrite(pFile);
	}

	// if wasn't written, make sure we write it.
	if  (!m_dwWrittenMask)
	{
		fprintf( pFile, "[" TAKSI_SECTION "]" INI_CR );
		PropsWrite(pFile);
	}
	PropsInit();

	// same goes for NEW custom configs
	CTaksiConfigCustom* p = m_pCustomList;
	while (p != NULL)
	{
		if ( ! p->m_dwWrittenMask )
		{
			fprintf( pFile, "[" TAKSI_CUSTOM_SECTION " %s]" INI_CR, p->m_szAppId );
			p->PropsWrite(pFile);
		}
		p->PropsInit();
		p = p->m_pNext;
	}

	fclose(pFile);
	return true;
}