Re: Convenience method, function for converting 4CCs to strings
Re: Convenience method, function for converting 4CCs to strings
- Subject: Re: Convenience method, function for converting 4CCs to strings
- From: Doug Wyatt <email@hidden>
- Date: Thu, 28 May 2009 12:56:43 -0700
On May 28, 2009, at 12:41 , Chris Adamson wrote:
The need to do an endian flip kind of surprised me. I guess I would
have expected that a definition like:
kAudioFileUnsupportedDataFormatError = 'fmt?',
would have maintained the same byte order from the header to the
iPhone Simulator or device. Or maybe it does and I'm just not
thinking through where each char ends up when it's stored as a UInt32?
'abcd' is a 32-bit integer constant equal to ('a' << 24) | ('b' << 16)
| ('c' << 8) | 'd'. Because it is a 32-bit integer, in memory in
appears as 'abcd' on a big-endian machine and 'dcba' on a little-
endian machine.
Personally I'm fond of the following, which will format the error as a
4CC if all 4 bytes are printable (as used by some parts of the
CoreAudio frameworks), a decimal integer if it's reasonably small (as
used by a large part of the traditional MacOS), or a hex integer if
it's larger (as used by Mach/IOKit).
class CAX4CCString {
public:
CAX4CCString(OSStatus error) {
// see if it appears to be a 4-char-code
char *str = mStr;
*(UInt32 *)(str + 1) = CFSwapInt32HostToBig(error);
if (isprint(str[1]) && isprint(str[2]) && isprint(str[3]) && isprint
(str[4])) {
str[0] = str[5] = '\'';
str[6] = '\0';
} else if (error > -200000 && error < 200000)
// no, format it as an integer
sprintf(str, "%d", (int)error);
else
sprintf(str, "0x%x", (int)error);
}
const char *get() const { return mStr; }
private:
char mStr[16];
};
The nice thing about a C++ class is that the temporary buffer gets
hidden instead a short-lived stack object and you can write compact
code like this:
OSStatus err = some_function();
if (err)
fprintf(stderr, "some_function() returned %s\n", CAX4CCString
(err).get());
Doug
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden