From d8f96db8dacbda24147aeab0af1c74f5ddbec8bb Mon Sep 17 00:00:00 2001 From: Steven Tamm Date: Fri, 3 Dec 2004 17:00:11 +0000 Subject: [PATCH] * macterm.h (cfstring_create_with_utf8_cstring): Added prototype. * image.c (image_load_quartz2d): Use cfstring_create_with_utf8_cstring * macmenu.c (add_menu_item): Use cfstring_create_with_utf8_cstring * macfns.c (x_set_name, x_set_title): Use cfstring_create_with_utf8_cstring (Fx_file_dialog): Use cfstring_create_with_utf8_cstring and use constant CFRefs instead of creating them each time for labels. * mac.c (cfstring_create_with_utf8_cstring): Added to prevent crashes with invalid characters. --- src/ChangeLog | 14 ++++++++++++++ src/image.c | 3 +-- src/mac.c | 16 ++++++++++++++++ src/macfns.c | 31 +++++++++---------------------- src/macmenu.c | 13 ++----------- src/macterm.h | 4 ++++ 6 files changed, 46 insertions(+), 35 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index f6ebe27ebfb..5d3e621a568 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,17 @@ +2004-12-03 YAMAMOTO Mitsuharu + + * macterm.h (cfstring_create_with_utf8_cstring): Added prototype. + * image.c (image_load_quartz2d): Use + cfstring_create_with_utf8_cstring + * macmenu.c (add_menu_item): Use + cfstring_create_with_utf8_cstring + * macfns.c (x_set_name, x_set_title): Use + cfstring_create_with_utf8_cstring + (Fx_file_dialog): Use cfstring_create_with_utf8_cstring and use + constant CFRefs instead of creating them each time for labels. + * mac.c (cfstring_create_with_utf8_cstring): Added to prevent + crashes with invalid characters. + 2004-12-02 Richard M. Stallman * config.in (RE_TRANSLATE_P): If make_number is not a macro, diff --git a/src/image.c b/src/image.c index 4ab672ca58e..d1925cf36e4 100644 --- a/src/image.c +++ b/src/image.c @@ -2416,8 +2416,7 @@ image_load_quartz2d (f, img, png_p) UNGCPRO; return 0; } - path = CFStringCreateWithCString (NULL, SDATA (file), - kCFStringEncodingUTF8); + path = cfstring_create_with_utf8_cstring (SDATA (file)); url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0); CFRelease (path); diff --git a/src/mac.c b/src/mac.c index 91d07372578..53e56cfb541 100644 --- a/src/mac.c +++ b/src/mac.c @@ -262,6 +262,22 @@ posix_to_mac_pathname (const char *ufn, char *mfn, int mfnbuflen) return 1; } +#if TARGET_API_MAC_CARBON +CFStringRef +cfstring_create_with_utf8_cstring (c_str) + const char *c_str; +{ + CFStringRef str; + + str = CFStringCreateWithCString (NULL, c_str, kCFStringEncodingUTF8); + if (str == NULL) + /* Failed to interpret as UTF 8. Fall back on Mac Roman. */ + str = CFStringCreateWithCString (NULL, c_str, kCFStringEncodingMacRoman); + + return str; +} +#endif + #ifndef MAC_OSX /* The following functions with "sys_" prefix are stubs to Unix diff --git a/src/macfns.c b/src/macfns.c index 9220bdd2798..fdfe7a52416 100644 --- a/src/macfns.c +++ b/src/macfns.c @@ -1932,8 +1932,7 @@ x_set_name (f, name, explicit) { #if TARGET_API_MAC_CARBON CFStringRef windowTitle = - CFStringCreateWithCString (NULL, SDATA (name), - kCFStringEncodingUTF8); + cfstring_create_with_utf8_cstring (SDATA (name)); SetWindowTitleWithCFString (FRAME_MAC_WINDOW (f), windowTitle); CFRelease (windowTitle); @@ -2015,8 +2014,7 @@ x_set_title (f, name, old_name) { #if TARGET_API_MAC_CARBON CFStringRef windowTitle = - CFStringCreateWithCString (NULL, SDATA (name), - kCFStringEncodingUTF8); + cfstring_create_with_utf8_cstring (SDATA (name)); SetWindowTitleWithCFString (FRAME_MAC_WINDOW (f), windowTitle); CFRelease (windowTitle); @@ -4246,8 +4244,7 @@ If ONLY-DIR-P is non-nil, the user can only select directories. */) NavDialogRef dialogRef; NavTypeListHandle fileTypes = NULL; NavUserAction userAction; - CFStringRef message=NULL, client=NULL, saveName = NULL, ok = NULL; - CFStringRef title = NULL; + CFStringRef message=NULL, saveName = NULL; BLOCK_INPUT; /* No need for a callback function because we are modal */ @@ -4259,15 +4256,11 @@ If ONLY-DIR-P is non-nil, the user can only select directories. */) options.optionFlags |= kNavSelectAllReadableItem; if (!NILP(prompt)) { - message = CFStringCreateWithCStringNoCopy(NULL, SDATA(prompt), - kCFStringEncodingUTF8, - kCFAllocatorNull); + message = cfstring_create_with_utf8_cstring (SDATA (prompt)); options.message = message; } /* Don't set the application, let it use default. - client = CFStringCreateWithCStringNoCopy(NULL, "Emacs", - kCFStringEncodingMacRoman, NULL); - options.clientName = client; + options.clientName = CFSTR ("Emacs"); */ if (!NILP (only_dir_p)) @@ -4276,17 +4269,14 @@ If ONLY-DIR-P is non-nil, the user can only select directories. */) else if (NILP (mustmatch)) { /* This is a save dialog */ - ok = CFStringCreateWithCString (NULL, "Ok", kCFStringEncodingUTF8); - title = CFStringCreateWithCString (NULL, "Enter name", - kCFStringEncodingUTF8); options.optionFlags |= kNavDontConfirmReplacement; - options.actionButtonLabel = ok; - options.windowTitle = title; + options.actionButtonLabel = CFSTR ("Ok"); + options.windowTitle = CFSTR ("Enter name"); if (!NILP(default_filename)) { - saveName = CFStringCreateWithCString(NULL, SDATA(default_filename), - kCFStringEncodingUTF8); + saveName = + cfstring_create_with_utf8_cstring (SDATA (default_filename)); options.saveFileName = saveName; options.optionFlags |= kNavSelectDefaultLocation; } @@ -4320,10 +4310,7 @@ If ONLY-DIR-P is non-nil, the user can only select directories. */) } if (saveName) CFRelease(saveName); - if (client) CFRelease(client); if (message) CFRelease(message); - if (ok) CFRelease(ok); - if (title) CFRelease(title); if (status == noErr) { userAction = NavDialogGetUserAction(dialogRef); diff --git a/src/macmenu.c b/src/macmenu.c index af24cfe237c..f0696a49774 100644 --- a/src/macmenu.c +++ b/src/macmenu.c @@ -2264,17 +2264,8 @@ add_menu_item (MenuHandle menu, widget_value *wv, int submenu, item_name[255] = 0; #if TARGET_API_MAC_CARBON { - CFStringRef string = - CFStringCreateWithCString (NULL, item_name, kCFStringEncodingUTF8); - - if (string == NULL) - { - /* Failed to interpret as UTF8. Fall back to Mac Roman. - Maybe wv->name is corrupted? */ - string = CFStringCreateWithCString (NULL, item_name, - kCFStringEncodingMacRoman); - } - + CFStringRef string = cfstring_create_with_utf8_cstring (item_name); + SetMenuItemTextWithCFString (menu, pos, string); CFRelease (string); } diff --git a/src/macterm.h b/src/macterm.h index f9bde772fcd..ff7110828b1 100644 --- a/src/macterm.h +++ b/src/macterm.h @@ -600,5 +600,9 @@ extern void mac_draw_line_to_pixmap P_ ((Display *, Pixmap, GC, int, int, #define FONT_TYPE_FOR_UNIBYTE(font, ch) 0 #define FONT_TYPE_FOR_MULTIBYTE(font, ch) 0 +#if TARGET_API_MAC_CARBON +extern CFStringRef cfstring_create_with_utf8_cstring P_ ((const char *)); +#endif + /* arch-tag: 6b4ca125-5bef-476d-8ee8-31ed808b7e79 (do not change this comment) */ -- 2.39.5