3 * @brief Managed LibVLC strings
7 /**********************************************************************
8 * Copyright (C) 2007 RĂ©mi Denis-Courmont. *
9 * This program is free software; you can redistribute and/or modify *
10 * it under the terms of the GNU General Public License as published *
11 * by the Free Software Foundation; version 2 of the license, or (at *
12 * your option) any later version. *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
17 * See the GNU General Public License for more details. *
19 * You should have received a copy of the GNU General Public License *
20 * along with this program; if not, you can get it from: *
21 * http://www.gnu.org/copyleft/gpl.html *
22 **********************************************************************/
25 using System.Runtime.InteropServices;
27 namespace VideoLAN.LibVLC
30 * @brief U8String: Native UTF-8 characters array
32 * This supports conversion between native UTF-8 nul-terminated characters
33 * arrays (as used by the native LibVLC) and managed strings.
35 [StructLayout (LayoutKind.Sequential)]
36 internal struct U8String
38 public byte[] mb_str; /**< nul-terminated UTF-8 bytes array */
41 * Creates an UTF-8 characters array from a .NET string.
42 * @param value string to convert
44 public U8String (string value)
50 byte[] bytes = System.Text.Encoding.UTF8.GetBytes (value);
51 mb_str = new byte[bytes.Length + 1];
52 Array.Copy (bytes, mb_str, bytes.Length);
53 mb_str[bytes.Length] = 0;
56 private U8String (IntPtr ptr)
59 if (ptr == IntPtr.Zero)
63 while (Marshal.ReadByte (ptr, i) != 0)
68 Marshal.Copy (ptr, mb_str, 0, i);
74 public override string ToString ()
79 byte[] bytes = new byte[mb_str.Length - 1];
80 Array.Copy (mb_str, bytes, bytes.Length);
82 return System.Text.Encoding.UTF8.GetString (bytes);
86 * Converts a pointer to a nul-terminated UTF-8 characters array into
89 public static string FromNative (IntPtr ptr)
91 return new U8String (ptr).ToString ();
96 * @brief MemoryHandle: heap allocation by the C run-time
99 internal sealed class MemoryHandle : NonNullHandle
101 [DllImport ("libvlc.dll", EntryPoint="libvlc_free")]
102 private static extern void Free (IntPtr ptr);
105 * NonNullHandle.Destroy
107 protected override void Destroy ()
112 public override string ToString ()
114 return U8String.FromNative (handle);
118 * Converts the buffer (as in ToString()) and release it.
119 * @return managed string representation of the buffer
121 public string Transform ()
123 string value = ToString ();