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 struct U8String
37 public U8String (string value)
43 byte[] bytes = System.Text.Encoding.UTF8.GetBytes (value);
44 mb_str = new byte[bytes.Length + 1];
45 Array.Copy (bytes, mb_str, bytes.Length);
46 mb_str[bytes.Length] = 0;
49 public U8String (IntPtr ptr)
52 if (ptr == IntPtr.Zero)
56 while (Marshal.ReadByte (ptr, i) != 0)
61 Marshal.Copy (ptr, mb_str, 0, i);
64 public override string ToString ()
69 byte[] bytes = new byte[mb_str.Length - 1];
70 Array.Copy (mb_str, bytes, bytes.Length);
72 return System.Text.Encoding.UTF8.GetString (bytes);
75 public static string FromNative (IntPtr ptr)
77 return new U8String (ptr).ToString ();