Permalink
Browse files

Hide encoding from 1.8

  • Loading branch information...
1 parent e55c022 commit f3c6780f9c01b69d7342b640b02b322f09b9085e @drbrain committed Mar 25, 2011
Showing with 35 additions and 21 deletions.
  1. +19 −9 ext/pasteboard/pasteboard.c
  2. +16 −12 lib/pasteboard/type.rb
@@ -1,8 +1,11 @@
#include <ruby.h>
-#include <ruby/encoding.h>
#include <Pasteboard.h>
#include "extconf.h"
+#if HAVE_RB_STR_ENCODE
+#include <ruby/encoding.h>
+#endif
+
#define BUFSIZE 128
static VALUE cPB;
@@ -29,20 +32,20 @@ string_ref_to_value(CFStringRef ref) {
static char *
value_to_usascii_cstr(VALUE string) {
- VALUE ascii;
+#if HAVE_RB_STR_ENCODE
+ string = rb_str_encode(string, usascii_encoding, 0, Qnil);
+#endif
- ascii = rb_str_encode(string, usascii_encoding, 0, Qnil);
-
- return StringValueCStr(ascii);
+ return StringValueCStr(string);
}
static char *
value_to_utf8_cstr(VALUE string) {
- VALUE ascii;
-
- ascii = rb_str_encode(string, utf8_encoding, 0, Qnil);
+#if HAVE_RB_STR_ENCODE
+ string = rb_str_encode(string, utf8_encoding, 0, Qnil);
+#endif
- return StringValueCStr(ascii);
+ return StringValueCStr(string);
}
static void pb_free(void *ptr) {
@@ -250,12 +253,14 @@ pb_copy_item_flavor_data(VALUE self, VALUE identifier, VALUE flavor) {
free(buffer);
+#if HAVE_RB_STR_ENCODE
encodings = rb_const_get_at(
rb_const_get_at(cPB, rb_intern("Type")), rb_intern("Encodings"));
encoding = rb_hash_aref(encodings, flavor);
rb_enc_associate(data, rb_to_encoding(encoding));
+#endif
return data;
}
@@ -402,8 +407,13 @@ Init_pasteboard(void) {
cPB = rb_define_class("Pasteboard", rb_cObject);
ePBError = rb_define_class_under(cPB, "Error", rb_eRuntimeError);
+#if HAVE_RB_STR_ENCODE
utf8_encoding = rb_enc_from_encoding(rb_utf8_encoding());
usascii_encoding = rb_enc_from_encoding(rb_usascii_encoding());
+#else
+ utf8_encoding = Qnil;
+ usascii_encoding = Qnil;
+#endif
rb_define_const(cPB, "MODIFIED", ULONG2NUM(kPasteboardModified));
rb_define_const(cPB, "CLIENT_IS_OWNER", ULONG2NUM(kPasteboardClientIsOwner));
View
@@ -6,12 +6,16 @@
module Pasteboard::Type
- ##
- # Maps a type to its encoding. The default encoding is Encoding::BINARY.
+ encoding = defined?(Encoding)
- Encodings = {}
+ if encoding then
+ ##
+ # Maps a type to its encoding. The default encoding is Encoding::BINARY.
- Encodings.default = Encoding::BINARY
+ Encodings = {}
+
+ Encodings.default = Encoding::BINARY
+ end
# :section: System-defined uniform type identifiers
@@ -70,29 +74,29 @@ module Pasteboard::Type
# text/plain
PLAIN_TEXT = 'public.plain-text'
- Encodings[TEXT] = Encoding::US_ASCII
+ Encodings[TEXT] = Encoding::US_ASCII if encoding
# Unicode-8
PLAIN_TEXT_UTF8 = 'public.utf8-plain-text'
UTF8 = PLAIN_TEXT_UTF8
- Encodings[PLAIN_TEXT_UTF8] = Encoding::UTF_8
+ Encodings[PLAIN_TEXT_UTF8] = Encoding::UTF_8 if encoding
# Unicode-16 with byte-order mark (BOM), or if BOM is not present, an
# external representation byte order (big-endian).
PLAIN_TEXT_UTF16_EXTERNAL = 'public.utf16-external-plain-text'
- Encodings[PLAIN_TEXT_UTF16_EXTERNAL] = Encoding::UTF_16BE
+ Encodings[PLAIN_TEXT_UTF16_EXTERNAL] = Encoding::UTF_16BE if encoding
# Unicode-16, native byte order, with an optional byte-order mark (BOM).
PLAIN_TEXT_UTF16 = 'public.utf16-plain-text'
- Encodings[PLAIN_TEXT_UTF16] = Encoding::UTF_16LE
+ Encodings[PLAIN_TEXT_UTF16] = Encoding::UTF_16LE if encoding
# Classic Mac OS text.
PLAIN_TEXT_TRADITIONAL = 'com.apple.traditional-mac-plain-text'
- Encodings[PLAIN_TEXT_TRADITIONAL] = Encoding::MacRoman
+ Encodings[PLAIN_TEXT_TRADITIONAL] = Encoding::MacRoman if encoding
# Rich Text.
@@ -264,17 +268,17 @@ module Pasteboard::Type
# Uniform Resource Locator.
URL = 'public.url'
- Encodings[URL] = Encoding::US_ASCII
+ Encodings[URL] = Encoding::US_ASCII if encoding
# File URL.
FILE_URL = 'public.file-url'
- Encodings[FILE_URL] = Encoding::US_ASCII
+ Encodings[FILE_URL] = Encoding::US_ASCII if encoding
# URL name.
URL_NAME = 'public.url-name'
- Encodings[URL_NAME] = Encoding::UTF_8
+ Encodings[URL_NAME] = Encoding::UTF_8 if encoding
# vCard (electronic business card).

0 comments on commit f3c6780

Please sign in to comment.