CFLite/CFTest/main.cpp: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 35: | Line 35: | ||
void CFLog::operator()(CFTypeRef valRef) { | void CFLog::operator()(CFTypeRef valRef) { | ||
#if | #if 0 | ||
SuperString valStr; | SuperString valStr; | ||
Revision as of 16:58, 29 June 2008
#include "SuperString.h"
#include <CoreFoundation/CFLocale.h>
#include <CoreFoundation/CFNumberFormatter.h>
#include <CoreFoundation/CFDateFormatter.h>
#include <CoreFoundation/CFCalendar.h>
#include <CoreFoundation/CFBundle.h>
static void ShowDiacriticSensitiveCompare(bool insensitiveB)
{
g_pref_diacritic_insensitive_searchB = insensitiveB;
printf("diacritic %ssensitive\n", insensitiveB ? "in" : "");
SuperString str1("NeUi");
#if defined(__WIN32__)
CF_ASSERT(kSourceFileEncoding == kCFStringEncodingWindowsLatin1);
SuperString str2("ÒȸÓ"); // kCFStringEncodingWindowsLatin1
#else
SuperString str2("ñéüî"); // utf8
#endif
bool diacritic_insensitive_compareB = str1 == str2;
printf("%s %s %s\n",
str1.consoleZ(),
diacritic_insensitive_compareB ? "==" : "!=",
str2.consoleZ());
if (insensitiveB) {
CF_ASSERT(diacritic_insensitive_compareB);
}
}
void CFLog::operator()(CFTypeRef valRef) {
#if 0
SuperString valStr;
valStr.Set_CFType(valRef);
printf("%s\n", valStr.consoleZ());
#else
fflush(stdout);
CFShow(valRef);
#endif
}
void CFTest(const char *pathZ);
void CFTest(const char *pathZ)
{
#if defined(__WIN32__)
#define kCodePage_WindowsLatin1 1252
SetConsoleOutputCP(kCodePage_WindowsLatin1);
#endif
// test console logging and CFSTR macro
{
CFLog()(CFSTR("------------------Strings---------------"));
CFLog()(CFSTR("Hello, World!"));
ScCFReleaser<CFDataRef> dataRef(SuperString("yeah baby").CopyDataRef());
SuperString str;
str.Set(dataRef);
printf(SuperString("The next line should read " kLeftQuote "yeah baby" kRightQuote "\n").consoleZ());
CFLog()(str.ref());
SuperString str1("foscoobyar");
SuperString str2("scooby");
printf("\n%s %s %s\n",
str1.consoleZ(),
str1.Contains(str2) ? "contains" : "$$$ Error: does not contain(!?)",
str2.consoleZ());
str1.Replace(str2, "o b");
printf(SuperString("The next line should read " kLeftQuote "foo bar" kRightQuote "\n").consoleZ());
CFLog()(str1.ref());
CFLog()(CFSTR("------------------Case Insensitive Compare---------------"));
ShowDiacriticSensitiveCompare(false);
printf("\n");
ShowDiacriticSensitiveCompare(true);
{
CFLog()(CFSTR("------------------Conversion---------------"));
SuperString jStr;
SuperString escd;
#if 0
jStr.Set("ゑをぼまあズジヅポ");
escd.Set(CFURLCreateStringByAddingPercentEscapes(
kCFAllocatorDefault, jStr, NULL, NULL, kCFStringEncodingUTF8), false);
printf("%s\n", escd.consoleZ());
#else
escd.Set("%E3%82%91%E3%82%92%E3%81%BC%E3%81%BE%E3%81%82%E3%82%BA%E3%82%B8%E3%83%85%E3%83%9D");
jStr.Set(CFURLCreateStringByReplacingPercentEscapes(
kCFAllocatorDefault, escd, CFSTR("")), false);
#endif
#if defined(__WIN32__)
#define kConvertEncode kCFStringEncodingDOSJapanese
#else
#define kConvertEncode kCFStringEncodingMacJapanese
#endif
ustring j;
CopyCFStringToUString(jStr.ref(), j, kConvertEncode);
SuperString convertedJ; convertedJ.Set(j, kConvertEncode);
if (kConsoleEncoding == kCFStringEncodingUTF8) {
printf(SuperString("The next line should read " kLeftQuote "%s" kRightQuote "\n%s\n").consoleZ(),
jStr.consoleZ(), convertedJ.consoleZ());
}
printf("conversion: %s\n", convertedJ == jStr ? "Success!" : "$$ FAILED!");
}
}
{
CFLog()(CFSTR("------------------Encoding---------------"));
CFStringEncoding encoding = CFStringGetSystemEncoding();
printf("Encoding: %s\n", SuperString(CFStringGetNameOfEncoding(encoding)).consoleZ());
printf("IANA charset: %s\n", SuperString(CFStringConvertEncodingToIANACharSetName(encoding)).consoleZ());
#if defined(__WIN32__)
UInt32 codePage = CFStringConvertEncodingToWindowsCodepage(encoding);
if (encoding == kCFStringEncodingWindowsLatin1) {
CF_ASSERT(codePage == kCodePage_WindowsLatin1);
}
printf("codepage: %ld\n", codePage);
CF_ASSERT(CFStringConvertWindowsCodepageToEncoding(codePage) == encoding);
#endif
}
CFLog()(CFSTR("------------------Locale---------------"));
ScCFReleaser<CFLocaleRef> locale(CFLocaleCopyCurrent());
{
SuperString localIdStr(CFLocaleGetIdentifier(locale));
printf("Locale ID: %s\n", localIdStr.consoleZ());
ScCFReleaser<CFDictionaryRef> dictRef(CFLocaleCreateComponentsFromLocaleIdentifier(kCFAllocatorDefault, localIdStr.ref()));
dict_for_each(dictRef, CFLog());
}
{
CFLog()(CFSTR("------------------Preferred Languages---------------"));
ScCFReleaser<CFArrayRef> arrayRef(CFLocaleCopyPreferredLanguages());
array_for_each(arrayRef, CFLog());
}
{
CFLog()(CFSTR("------------------Calendar---------------"));
ScCFReleaser<CFCalendarRef> calendarRef(CFCalendarCopyCurrent());
printf("Calendar ID: %s\n", SuperString(CFCalendarGetIdentifier(calendarRef)).consoleZ());
ScCFReleaser<CFTimeZoneRef> timeZoneRef(CFCalendarCopyTimeZone(calendarRef));
CFLog()(timeZoneRef.Get());
CFAbsoluteTime absTime = CFAbsoluteTimeGetCurrent();
ScCFReleaser<CFDateRef> dateRef(CFDateCreate(kCFAllocatorDefault, absTime));
CFLog()(dateRef.Get());
CFGregorianDate gregDate(CFAbsoluteTimeGetGregorianDate(absTime, timeZoneRef));
printf("year: %d\nmonth: %d\nday: %d\nhour: %d\nminute: %d\nsecond: %f\n",
(int)gregDate.year,
(int)gregDate.month,
(int)gregDate.day,
(int)gregDate.hour,
(int)gregDate.minute,
(float)gregDate.second);
ScCFReleaser<CFDateFormatterRef> dateFormatterRef(CFDateFormatterCreate(
kCFAllocatorDefault, locale, kCFDateFormatterFullStyle, kCFDateFormatterFullStyle));
ScCFReleaser<CFStringRef> dateStr(CFDateFormatterCreateStringWithDate(
kCFAllocatorDefault, dateFormatterRef, dateRef));
CFLog()(CFSTR("Make sure time zone is correct in the next line:"));
CFLog()(dateStr.Get());
}
{
CFLog()(CFSTR("------------------Numbers---------------"));
ScCFReleaser<CFNumberFormatterRef> numFormatRef;
ScCFReleaser<CFStringRef> numStr;
float numF = 123456.789;
ScCFReleaser<CFNumberRef> numberRef(CFNumberCreate(
kCFAllocatorDefault, kCFNumberFloat32Type, &numF));
numFormatRef.adopt(CFNumberFormatterCreate(
kCFAllocatorDefault, locale, kCFNumberFormatterDecimalStyle));
numStr.adopt(CFNumberFormatterCreateStringWithNumber(
kCFAllocatorDefault, numFormatRef, numberRef));
CFLog()(numStr.Get());
numFormatRef.adopt(CFNumberFormatterCreate(
kCFAllocatorDefault, locale, kCFNumberFormatterCurrencyStyle));
numStr.adopt(CFNumberFormatterCreateStringWithNumber(
kCFAllocatorDefault, numFormatRef, numberRef));
CFLog()(numStr.Get());
}
{
CFLog()(CFSTR("------------------Bundle---------------"));
ScCFReleaser<CFBundleRef> bundleRef(CFBundleGetMainBundle());
ScCFReleaser<CFURLRef> bundleUrlRef;
if (bundleRef.Get() == NULL) {
if (pathZ != NULL) {
bundleUrlRef.adopt(CFURLCreateWithFileSystemPath(
kCFAllocatorDefault, SuperString(pathZ), kCFURLWindowsPathStyle, false));
}
} else {
bundleUrlRef.adopt(CFBundleCopyBundleURL(bundleRef));
// this is a "get" not a "copy" so extra retain will cancel the extra release
bundleRef.Retain();
}
CF_ASSERT(bundleUrlRef.Get());
if (bundleUrlRef.Get() != NULL) {
CFLog()(bundleUrlRef.Get());
if (bundleRef.Get() != NULL) {
ScCFReleaser<CFDictionaryRef> dictRef(CFBundleGetInfoDictionary(bundleRef));
dict_for_each(dictRef, CFLog());
}
{
CFLog()(CFSTR("------------------plist - xml---------------"));
#if defined(__WIN32__)
// you may need to fix this relative URL here
SuperString relPathStr("");
#else
SuperString relPathStr("../../");
#endif
SuperString testRelPath("test.xml"); testRelPath.prepend(relPathStr);
ScCFReleaser<CFURLRef> xmlUrlRef(CFURLCreateWithFileSystemPathRelativeToBase(
kCFAllocatorDefault, testRelPath.ref(), kCFURLPOSIXPathStyle, false, bundleUrlRef));
if (xmlUrlRef.Get()) {
ScCFReleaser<CFDictionaryRef> dictRef;
ScCFReleaser<CFURLRef> absUrlRef(CFURLCopyAbsoluteURL(xmlUrlRef));
printf("URL: %s\n", SuperString(CFURLGetString(absUrlRef)).consoleZ());
if (Read_PList(xmlUrlRef, dictRef.AddressOf())) {
dict_for_each(dictRef, CFLog());
SuperString outRelpath("out.xml"); outRelpath.prepend(relPathStr);
ScCFReleaser<CFURLRef> outXmlUrlRef(CFURLCreateWithFileSystemPathRelativeToBase(
kCFAllocatorDefault, outRelpath.ref(), kCFURLPOSIXPathStyle, false, bundleUrlRef));
Write_PList(dictRef.Get(), outXmlUrlRef);
}
} else {
CFLog()(CFSTR("error illegal file path?"));
}
}
}
}
CFLog()(CFSTR("Note: not exercied here, but I want full CFXML support: input stream, node, parser, tree"));
/*
bonus points: CFCharacterSet
*/
CFLog()(CFSTR("--------------------------------------"));
}
int main (int argc, const char * argv[]) {
CFTest(argv[0]);
}