コード例 #1
0
void GProfile::ProfileParse(const char *szBuffer, __int64 dwSize)
{
	GProfileSection *pSection = 0;

	// parse the file
	__int64 nIdx = 0;

	GString strLine;
	while (nIdx < dwSize)
	{
		GetLine(strLine, szBuffer, &nIdx, dwSize);

		strLine.TrimRightWS();
		strLine.TrimLeftWS();

		if ((strLine.IsEmpty()) || (strLine.GetAt(0) == ';'))
			continue;


		strLine.Replace("\\n", '\n');

		if (strLine.GetAt(0) == '[')
		{
			__int64 nIdx = strLine.Find(']');
			if (nIdx == -1)
				nIdx = strLine.Length();

			// new section
			pSection = new GProfileSection;
			pSection->m_strName = strLine.Mid(1, nIdx - 1);
			pSection->m_strName.TrimLeftWS();
			pSection->m_strName.TrimRightWS();

			m_lstSections.AddLast(pSection);
		}
		else if (pSection)
		{
			__int64 nIdx = strLine.Find('=');
			if (nIdx == -1)
				continue;
			GProfileEntry *pNVP = new GProfileEntry;
			pSection->m_lstNVP.AddLast(pNVP);
			pNVP->m_strName = strLine.Left(nIdx);
			pNVP->m_strName.TrimLeftWS();
			pNVP->m_strName.TrimRightWS();
			
			pNVP->m_strValue = strLine.Mid(nIdx + 1);
			pNVP->m_strValue.TrimLeftWS();
			pNVP->m_strValue.TrimRightWS();
		}
	}
	m_bCached = true;
}
コード例 #2
0
ファイル: GString.cpp プロジェクト: gbaumgart/vt
void GString::FormatNumber(const char *szFormat, 
						   char decimal_separator,
						   char grouping_separator,
						   char minus_sign,
						   const char *NaN,
						   char zero_digit,
						   char digit,
						   char pattern_separator,
						   char percent,
						   char permille)
{
	if (szFormat && *szFormat)
	{
		// make sure that the string is a number {0..9, '.', '-'}
		// if the string contains a character not in the number
		// subset then set the value of the string to NaN.
		const char *szValue = _str;
		if (IsNaN(szValue, '.', ',', '-'))
			*this = NaN;
		else
		{
			// it's a number, get the whole part and the fraction part
			int nIdx = Find('.');
			GString strWhole;
			strWhole = (nIdx == -1) ? _str : (const char *)Left(nIdx);
			GString strFraction('0', (short)1);
			nIdx = Find('.') + 1;
			if (nIdx > 0)
				strFraction = Mid(nIdx);
			bool bIsNeg = (Find(minus_sign) != -1);

			long nWhole = abs(atol((const char *)strWhole));
			long nFract = abs(atol((const char *)strFraction));

			// check for % and ?
			if (percent == szFormat[0])
			{
				double d = atof(_str);
				d *= 100;
				GString strTemp;
				strTemp.Format("%f", d);
				nIdx = strTemp.Find('.');
				strFraction = (nIdx == -1) ? strTemp._str : (const char *)strTemp.Left(nIdx);
				nWhole = atol((const char *)strFraction);
				nFract = 0;
			}
			if (permille == szFormat[0])
			{
				double d = atof(_str);
				d *= 1000;
				GString strTemp;
				strTemp.Format("%f", d);
				nIdx = strTemp.Find('.');
				strFraction = (nIdx == -1) ? strTemp._str : (const char *)strTemp.Left(nIdx);
				nWhole = atol((const char *)strFraction);
				nFract = 0;
			}

			// if the number is negative, get the negative pattern out of the format.
			// if a negative pattern doesn't exist, the minus_sign will be prepended
			// to the positive pattern.
			GString strFormat(szFormat);
			nIdx = strFormat.Find(pattern_separator);
			if (bIsNeg)
			{
				if (nIdx != -1)
					strFormat = strFormat.Mid(nIdx + 1);
				else
					strFormat.Format("%c%s", minus_sign, (const char *)strFormat);
			}
			else
			{
				if (nIdx != -1)
					strFormat = strFormat.Left(nIdx);
			}

			GString strFormatWhole(strFormat);
			GString strFormatDecimal('#', (short)1);

			// determine the number of digits per group
			int nGroup = 0;
			nIdx = strFormat.Find(',');
			if (nIdx != -1)
			{
				nIdx++;
				int nNext = strFormat.Find(',', nIdx);
				if (nNext == -1)
					nNext = strFormat.Find('.', nIdx);
				if (nNext == -1)
					nNext = strFormat.Length();
				nGroup = (nNext - nIdx);
			}

			// determine the number of decimals to display
			int nDecimals = 0;
			nIdx = strFormat.Find('.');
			if ((nIdx != -1) && 
				(percent != szFormat[0]) &&
				(permille != szFormat[0]))
			{
				if (nGroup)
					strFormatWhole = strFormat.Mid(nIdx - nGroup, nGroup);
				else
					strFormatWhole = strFormat.Left(nIdx);
				nIdx++;
				strFormatDecimal = strFormat.Mid(nIdx);
				nDecimals = (strFormat.Length() - nIdx);
			}

			// Format the whole part of the number
			int nCount = CountOf((const char *)strFormatWhole, zero_digit);
			strWhole.Format("%0ld", nWhole);
			if (strWhole.Length() < nCount)
			{
				GString temp(zero_digit, (short)(nCount - (short)strWhole.Length()));
				strWhole.Format("%s%s", (const char *)temp, (const char *)strWhole);
			}

			Empty();

			// add all prefix characters
			nIdx = 0;
			const char *szP = (const char *)strFormat;
			while (*szP)
			{
				if (*szP == digit ||
					*szP == zero_digit ||
					*szP == decimal_separator ||
					*szP == grouping_separator ||
					*szP == percent ||
					*szP == permille)
					break;

				szP++;
				nIdx++;
			}
			strFormat = strFormat.Left(nIdx);

			strFormat.MakeReverse();

			int i, j;
			for (i = 0, j = strWhole.Length() - 1; j >= 0; j--, i++)
			{
				if ((nGroup) && (i == nGroup))
				{
					*this += grouping_separator;
					i = 0;
				}

				*this += strWhole[j];
			}
			*this += strFormat;

			MakeReverse();

			if (nDecimals)
			{
				*this += decimal_separator;

				strFraction.Format("%ld", nFract);
				const char *szF1 = (const char *)strFormatDecimal;
				const char *szF2 = (const char *)strFraction;
				i = 0;
				while (*szF1)
				{
					if (*szF2)
						*this += *szF2;
					else if (*szF1 == zero_digit)
						*this += zero_digit;
					else if (*szF1 != digit) // add all sufix characters
						*this += *szF1;
					if (*szF2)
						szF2++;
					if (*szF1)
						szF1++;
				}
			}

			if (percent == szFormat[0])
				*this += percent;
			if (permille == szFormat[0])
				*this += permille;
		}
	}
}