inline int getByteOrder() {
#ifdef __LINUX__
    return __BYTE_ORDER == __LITTLE_ENDIAN ? 0 : 1;
#elif __APPLE__
    return CFByteOrderGetCurrent() == CFByteOrderLittleEndian ? 0 : 1;
#else
    // Assume little endian.
    // TODO(XXX) BSD.
    return 0;
#endif

}
int
checkForHeader(int afd, int *nPoles, float sr)
{
	int magic[2], headersize=0;
	/* see if second 32 bytes are the lpc header magic number */
	if(read(afd, (char *) magic, sizeof(magic)) != sizeof(magic)) {
		die("dataset", "Can't read analysis file.");
		return -1;
	}
	else lseek(afd, 0, 0);	/* back to beginning */

#ifdef MAXMSP
	if (CFByteOrderGetCurrent() == CFByteOrderLittleEndian) {
		magic[0] = CFSwapInt32BigToHost(magic[0]);
		magic[1] = CFSwapInt32BigToHost(magic[1]);
	}
#endif

	if(magic[1] == LP_MAGIC) {	/* has header */
		if(read(afd, (char *) &analheader, sizeof(analheader))
				!= sizeof(analheader)) {
			die("dataset", "Can't read analysis file header.");
			return -1;
		}

#ifdef MAXMSP
		if (CFByteOrderGetCurrent() == CFByteOrderLittleEndian) {
			analheader.headersize = CFSwapInt32BigToHost(analheader.headersize);
			analheader.lpmagic = CFSwapInt32BigToHost(analheader.lpmagic);
			analheader.npoles = CFSwapInt32BigToHost(analheader.npoles);
			analheader.nvals = CFSwapInt32BigToHost(analheader.nvals);
		}
#endif

		rtcmix_advise("dataset", "This is a csound-type data file with header.");
		if(lseek(afd, analheader.headersize, 0) < 0) {
			die("dataset", "Bad lseek past header.");
			return -1;
		}
		if(*nPoles != 0 && *nPoles != analheader.npoles) {
			die("dataset", "LPC header indicates %d poles--check your numbers.", analheader.npoles);
			return -1;
		}
		else if (analheader.npoles > MAXPOLES) {
			die("dataset", "LPC header %d poles > MAXPOLES (%d)!",
					analheader.npoles, MAXPOLES);
			return -1;
		}
		else if(!*nPoles) /* if none previously specified */
			rtcmix_advise("dataset", "npoles set to %d", *nPoles=analheader.npoles);
		if(sr != 0.0 && sr != analheader.srate) {
			rtcmix_warn("dataset",
				 "Warning: LPC header SR (%.1f) != soundfile SR (%.1f)!", 
				 analheader.srate, sr);
		}
		/* for all future lseeks */
		headersize = analheader.headersize;	
	}
	else if(!*nPoles) {	/* no header and no poles specified */
		die("dataset", "You must specify the number of poles for this file!");
		return -1;
	}
	return headersize;
}
static void ShowErrorVa(const wchar_t *de, const wchar_t *en, va_list args)
{
	// Get current language
	const wchar_t *stringToUse = en;
#ifdef _WIN32
	if ((GetUserDefaultLangID() & 0xFF) == LANG_GERMAN) stringToUse = de;		
#elif defined(__APPLE_CC__)
	CFStringRef localisations[2] = { CFSTR("en"), CFSTR("de") };
	CFArrayRef allLocalisations = CFArrayCreate(kCFAllocatorDefault, (const void **) localisations, 2, &kCFTypeArrayCallBacks);
	CFArrayRef preferredLocalisations = CFBundleCopyPreferredLocalizationsFromArray(allLocalisations);
	CFStringRef oldStyleLang = (CFStringRef) CFArrayGetValueAtIndex(preferredLocalisations, 0);
	CFStringRef newStyleLang = CFLocaleCreateCanonicalLanguageIdentifierFromString(kCFAllocatorDefault, oldStyleLang);
	
	if (CFStringHasPrefix(newStyleLang, CFSTR("de"))) stringToUse = de;
	
	CFRelease(allLocalisations);
	CFRelease(preferredLocalisations);
	CFRelease(newStyleLang);
#else
	// TODO: Implement something here for android.
#endif
	// Create output string
	wchar_t text[1024];
	vswprintf(text, 1024, stringToUse, args);
	
	// Show message
#ifdef _WIN32
	MessageBoxW(NULL, text, L"Mindstorms Simulator", MB_TASKMODAL & MB_ICONWARNING);
#elif defined(IPHONE)
	// Ignore it
#elif defined(__APPLE_CC__)
	CFStringRef messageString = CFStringCreateWithBytes(kCFAllocatorDefault, (const UInt8 *) text, sizeof(wchar_t) * wcslen(text), (CFByteOrderGetCurrent() == CFByteOrderBigEndian) ? kCFStringEncodingUTF32BE : kCFStringEncodingUTF32LE, false);
	CFUserNotificationDisplayAlert(0.0, kCFUserNotificationStopAlertLevel, NULL, NULL, NULL, messageString, NULL, NULL, NULL, NULL, NULL);
	CFRelease(messageString);
#else
	// TODO: Implement something here for android.
	size_t length = wcslen(text)+1;
	char *englishMangledASCII = new char[length];
	for (unsigned i = 0; i < length; i++)
		englishMangledASCII[i] = (char) en[i];
	
	__android_log_print(ANDROID_LOG_FATAL, "librobosim.so", "Error: %s", englishMangledASCII);
	delete englishMangledASCII;
#endif	
}