CFStringRef CreateLocalizedStringForParameterValue ( double					inParameterValue,
													 const CAAUParameter *	inParameter,
													 UInt32					inDigits,
													 UInt32					minDigits) {	
	if (!inParameter) return nil;

	AudioUnitParameterInfo info = inParameter->ParamInfo();
	int pow10;

	switch (info.unit) {
		case kAudioUnitParameterUnit_Hertz:
			// number of significant digits based on value
			pow10 = int(log10(fmax(inParameterValue, .000001)));
			break;
		default:
			// number of significant digits based on parameter range
			pow10 = int(log10(fmax(double(info.maxValue - info.minValue), .000001)));
			break;
	}

	// pow10	range			nDigitsAfterDecimal
	//	-2		.0100-.0999		4
	//	-1		.100-.999		3
	//	0		1.00-9.99		2
	//	1		10.0-99.9		1
	//	2		100-999			0
	//	3		1000-9990		-1
	//	4		10000-99900		-2
	
	int nDigitsAfterDecimal = inDigits - (pow10 + 1);
	if (nDigitsAfterDecimal < 0)
		nDigitsAfterDecimal = 0;	// the least number of digits possible is zero

	if (info.flags & kAudioUnitParameterFlag_IsHighResolution)
		nDigitsAfterDecimal = 4;
	
	CFLocaleRef currentLocale = CFLocaleCopyCurrent(); 
	CFNumberFormatterRef numberFormatter = CFNumberFormatterCreate (NULL, currentLocale, kCFNumberFormatterDecimalStyle);
	
	CFNumberRef maxFractionDigits = CFNumberCreate (NULL, kCFNumberIntType, &nDigitsAfterDecimal);
	
	if (nDigitsAfterDecimal > 0)
		nDigitsAfterDecimal = minDigits;
		
	CFNumberRef minFractionDigits = CFNumberCreate (NULL, kCFNumberIntType, &nDigitsAfterDecimal); 

	CFNumberFormatterSetProperty (numberFormatter, kCFNumberFormatterMinFractionDigits, minFractionDigits); 
	CFNumberFormatterSetProperty (numberFormatter, kCFNumberFormatterMaxFractionDigits, maxFractionDigits); 
	CFStringRef formattedNumberString = CFNumberFormatterCreateStringWithValue (NULL, numberFormatter, kCFNumberDoubleType, &inParameterValue); 

	CFRelease(currentLocale); 
	CFRelease(numberFormatter); 
	CFRelease(maxFractionDigits);
	CFRelease(minFractionDigits);

	return formattedNumberString;
}
Пример #2
0
CFStringRef getBuffer()
{
	kern_return_t kernResult;
	bufferStruct myBufStruct;
	IOByteCount structSize = sizeof(myBufStruct);

    kernResult = IOConnectMethodScalarIStructureO(userClient,
													 klogKextBuffer,	
													 0,					// input count
													 &structSize,
													 &myBufStruct);

	CFDataRef result = CFDataCreate(kCFAllocatorDefault,myBufStruct.buffer,myBufStruct.bufLen);
	CFMutableStringRef decodedData = CFStringCreateMutable(kCFAllocatorDefault,0);
	
	if (!keymap)
		return decodedData;
	
	CFDictionaryRef flagsDict = (CFDictionaryRef)CFDictionaryGetValue(keymap,CFSTR("Flags"));
	if (!flagsDict)
		return decodedData;
	CFDictionaryRef ucDict = (CFDictionaryRef)CFDictionaryGetValue(keymap,CFSTR("Uppercase"));
	if (!ucDict)
		return decodedData;
	CFDictionaryRef lcDict = (CFDictionaryRef)CFDictionaryGetValue(keymap,CFSTR("Lowercase"));
	if (!lcDict)
		return decodedData;

	CFNumberFormatterRef myNF = CFNumberFormatterCreate(kCFAllocatorDefault,CFLocaleCopyCurrent(),kCFNumberFormatterNoStyle);
	
	for (int i=0; i<CFDataGetLength(result);i+=2)
	{
		u_int16_t curChar;
		CFDataGetBytes(result,CFRangeMake(i,2),(UInt8*)&curChar);
		bool isUpper = false;
		
		if (CFBooleanGetValue(showMods))
		{
			char flagTmp = (curChar >> 11);
			
			if (flagTmp & 0x01)
				CFStringAppend(decodedData,(CFStringRef)CFDictionaryGetValue(flagsDict,CFSTR("0x01")));

			if (flagTmp & 0x02)
				CFStringAppend(decodedData,(CFStringRef)CFDictionaryGetValue(flagsDict,CFSTR("0x02")));

			if (flagTmp & 0x04)
				CFStringAppend(decodedData,(CFStringRef)CFDictionaryGetValue(flagsDict,CFSTR("0x04")));

			if (flagTmp & 0x08)
				CFStringAppend(decodedData,(CFStringRef)CFDictionaryGetValue(flagsDict,CFSTR("0x08")));
				
			if (flagTmp & 0x10)
				isUpper = true;
		}

		curChar &= 0x07ff;		
		CFStringRef keyChar = CFNumberFormatterCreateStringWithValue(kCFAllocatorDefault,myNF,kCFNumberShortType,&curChar);
		CFStringRef text;

		if (isUpper)
			text = (CFStringRef)CFDictionaryGetValue(ucDict,keyChar);
		else
			text = (CFStringRef)CFDictionaryGetValue(lcDict,keyChar);		
		
		if (text)
		{
			if (CFStringCompare(text,CFSTR("\\n"),0)==kCFCompareEqualTo)
				text = CFSTR("\n");

			CFStringAppend(decodedData,text);
		}
		else
			syslog(LOG_ERR,"Unmapped key %d",curChar);		
	}

	return decodedData;
}