From 2c8a2dfe95385204e07928a2beeb515275de3f07 Mon Sep 17 00:00:00 2001 From: firemac Date: Sun, 3 Jun 2007 19:22:06 +0200 Subject: [PATCH] Now i'm able to get the a c# string from a CFString :) --- Fireball.Carbon/Fireball.Carbon/CFStringRef.cs | 96 ++++++++++++++++++---- .../Fireball.Carbon/Fireball.Carbon.csproj | 3 +- Fireball.Carbon/build.sh | 9 ++ Fireball.Carbon/test/Program.cs | 8 +- Fireball.Carbon/test/test.csproj | 4 +- 5 files changed, 98 insertions(+), 22 deletions(-) create mode 100644 Fireball.Carbon/build.sh diff --git a/Fireball.Carbon/Fireball.Carbon/CFStringRef.cs b/Fireball.Carbon/Fireball.Carbon/CFStringRef.cs index 56389e9..01f90f9 100644 --- a/Fireball.Carbon/Fireball.Carbon/CFStringRef.cs +++ b/Fireball.Carbon/Fireball.Carbon/CFStringRef.cs @@ -5,7 +5,7 @@ using System.Runtime.InteropServices; namespace Fireball.Carbon { - public class CFStringRef:ICloneable + public unsafe class CFStringRef:ICloneable { #region Constants private const string LIB = "/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon"; @@ -28,10 +28,10 @@ namespace Fireball.Carbon /// /// /// CFStringRef - [DllImport(LIB)] + [DllImport(LIB,CharSet = CharSet.Auto)] public static extern IntPtr /*CFStringRef*/CFStringCreateWithCString ( IntPtr alloc/*CFAllocatorRef*/, - [MarshalAs(UnmanagedType.LPStr)]string cStr, + string cStr, CFStringEncoding encoding ); @@ -55,11 +55,11 @@ namespace Fireball.Carbon /// /// /// - [DllImport(LIB)] + [DllImport(LIB,CharSet=CharSet.Auto)] public static extern bool CFStringGetCString( IntPtr theString, - [MarshalAs(UnmanagedType.LPStr)]string buffer, - Int32 bufferSize, + StringBuilder buffer, + int bufferSize, CFStringEncoding encoding ); @@ -74,7 +74,8 @@ namespace Fireball.Carbon ); [DllImport(LIB,CharSet=CharSet.Auto)] - public static extern IntPtr CFStringGetCStringPtr ( + /*[return:MarshalAs(UnmanagedType.LPStr)]*/ + public static extern char* CFStringGetCStringPtr ( IntPtr theString, CFStringEncoding encoding ); @@ -82,8 +83,16 @@ namespace Fireball.Carbon [DllImport(LIB)] public static extern CFStringEncoding CFStringGetFastestEncoding(IntPtr theString); + [DllImport(LIB)] + public static extern IntPtr CFStringCreateWithCStringNoCopy(IntPtr alloc,[MarshalAs(UnmanagedType.LPStr)]string s,CFStringEncoding encoding,IntPtr deAlloc); #endregion + [DllImport(LIB)] + public static extern CFStringEncoding CFStringGetSystemEncoding (); + + [DllImport(LIB)] + public static extern void CFShowStr(IntPtr cfString); + /// /// Return the value of CFStringRef as a standard .net String /// @@ -91,24 +100,77 @@ namespace Fireball.Carbon public override string ToString() { int size = CFStringGetLength(this.Pointer); - - //StringBuilder sb = new StringBuilder(size); - string sout = new string(new char(),size); +//CFShowStr(this.Pointer); + /*StringBuilder sb = new StringBuilder(); + sb.EnsureCapacity(size); + //string sout = new string(new char(),size); Console.WriteLine("Size:" + size.ToString()); + */ + CFShowStr(this.Pointer); + /*char[] tmp = new char[size+1]; + + string finalStr = null; + + fixed(char* str = tmp) + {*/ + + StringBuilder sb = new StringBuilder(); + sb.EnsureCapacity(size+1); + bool b = CFStringGetCString(this.Pointer,sb, size+1, CFStringEncoding.kCFStringEncodingMacRoman); + string finalStr = sb.ToString(); - /*if (CFStringGetCString(this.Pointer, sout, 256, CFStringEncoding.kCFStringEncodingUTF32)) - return sout;//sb.ToString();*/ - sout = CFStringGetCStringPtr(this.Pointer,CFStringGetFastestEncoding(this.Pointer)).ToString(); + Console.WriteLine("CFStringGetCString: " + b.ToString()); + //} - return sout; + Mono.Unix.UnixEncoding encoder = new Mono.Unix.UnixEncoding(); + + byte[] bts = encoder.GetBytes(finalStr); + + bts = System.Text.Encoding.Convert(encoder,System.Text.Encoding.UTF8,bts); + + finalStr = System.Text.Encoding.UTF8.GetString(bts); + + return finalStr; + +//Console.WriteLine("Size:" +size.ToString()); + /*string str = ""; + for(int i = 0; i < size+1;i++) + str += " "; + + bool b = CFStringGetCString(this.Pointer, str, size+1, CFStringEncoding.kCFStringEncodingUTF8); + //str = new String(str); + + Console.WriteLine("CFStringGetCString: " + b.ToString()); + return str; +*/ + + + // return new String(CFStringGetCStringPtr(this.Pointer,CFStringEncoding.kCFStringEncodingUTF8)); } public CFStringRef(string s) { - _Pointer = CFStringCreateWithCString(IntPtr.Zero, s, - CFStringEncoding.kCFStringEncodingUTF32); - } + //s+= "aaa"; + + byte[] bts = System.Text.Encoding.UTF8.GetBytes(s); + + Mono.Unix.UnixEncoding encoder = new Mono.Unix.UnixEncoding(); + + bts = System.Text.Encoding.Convert(System.Text.Encoding.UTF7,encoder,bts); + string enc = encoder.GetString(bts); +/* + //char[] tmp = enc.ToCharArray(); + //Array.Resize(ref tmp,tmp.Length+1); + //tmp[tmp.Length-1] = '\n'; +*/ + //fixed(char* str = s) + //{ + //IntPtr ptS = Marshal.StringToBSTR(s); + _Pointer = CFStringCreateWithCString(IntPtr.Zero, enc, + CFStringEncoding.kCFStringEncodingMacRoman); + //} + } internal CFStringRef(IntPtr cfstring) { diff --git a/Fireball.Carbon/Fireball.Carbon/Fireball.Carbon.csproj b/Fireball.Carbon/Fireball.Carbon/Fireball.Carbon.csproj index 8ac73b7..51c5326 100644 --- a/Fireball.Carbon/Fireball.Carbon/Fireball.Carbon.csproj +++ b/Fireball.Carbon/Fireball.Carbon/Fireball.Carbon.csproj @@ -33,6 +33,7 @@ + @@ -51,4 +52,4 @@ --> - \ No newline at end of file + diff --git a/Fireball.Carbon/build.sh b/Fireball.Carbon/build.sh new file mode 100644 index 0000000..0192d28 --- /dev/null +++ b/Fireball.Carbon/build.sh @@ -0,0 +1,9 @@ +cd Fireball.Carbon/ + +xbuild Fireball.Carbon.csproj + +cd ../ +cd test/ + +xbuild test.csproj + diff --git a/Fireball.Carbon/test/Program.cs b/Fireball.Carbon/test/Program.cs index 9c5028b..942316a 100644 --- a/Fireball.Carbon/test/Program.cs +++ b/Fireball.Carbon/test/Program.cs @@ -23,12 +23,16 @@ namespace test pt = CGContextRef.CGPDFContextCreateWithURL("/Users/dotnetfireball/py.pdf", rect, null);*/ CFStringRef rs = new CFStringRef("hello"); - rs = (CFStringRef)rs.Clone(); + // rs = (CFStringRef)rs.Clone(); pt = rs.Pointer; Console.WriteLine(pt.ToString()); - Console.WriteLine("S:" +rs.ToString()); + string s = rs.ToString(); + if(!string.IsNullOrEmpty(s)) + Console.WriteLine("S:" + s); + else + Console.WriteLine("La stringa e' nulla!"); } catch(Exception ex) { diff --git a/Fireball.Carbon/test/test.csproj b/Fireball.Carbon/test/test.csproj index 3105d6f..c17419a 100644 --- a/Fireball.Carbon/test/test.csproj +++ b/Fireball.Carbon/test/test.csproj @@ -37,7 +37,7 @@ - + {2EC28E57-A6C2-469D-B61E-F9FE2707E2E8} Fireball.Carbon @@ -50,4 +50,4 @@ --> - \ No newline at end of file + -- 2.11.4.GIT