2 * ustring.cs - Managed LibVLC string
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 * Managed class for UTF-8 nul-terminated character arrays
32 [StructLayout (LayoutKind.Sequential)]
33 public sealed struct U8String
37 public U8String (string value)
42 byte[] bytes = System.Text.Encoding.UTF8.GetBytes (value);
43 mb_str = new byte[bytes.Length + 1];
44 Array.Copy (bytes, mb_str, bytes.Length);
45 mb_str[bytes.Length] = 0;
48 public U8String (IntPtr ptr)
50 if (ptr == IntPtr.Zero)
54 while (Marshal.ReadByte (ptr, i) != 0)
59 Marshal.Copy (ptr, mb_str, 0, i);
62 public override string ToString ()
67 byte[] bytes = new byte[mb_str.Length - 1];
68 Array.Copy (mb_str, bytes, bytes.Length);
70 return System.Text.Encoding.UTF8.GetString (bytes);
73 public static string FromNative (IntPtr ptr)
75 return new U8String (ptr).ToString ();