]> git.sesse.net Git - vlc/blob - bindings/cil/src/ustring.cs
Start rewriting the CIL bindings
[vlc] / bindings / cil / src / ustring.cs
1 /**
2  * @file ustring.cs
3  * @brief Managed LibVLC strings
4  * @ingroup Internals
5  */
6
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.                                   *
13  *                                                                    *
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.              *
18  *                                                                    *
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  **********************************************************************/
23
24 using System;
25 using System.Runtime.InteropServices;
26
27 namespace VideoLAN.LibVLC
28 {
29     /**
30      * @brief U8String: Native UTF-8 characters array
31      * @ingroup Internals
32      * This supports conversion between native UTF-8 nul-terminated characters
33      * arrays (as used by the native LibVLC) and managed strings.
34      */
35     [StructLayout (LayoutKind.Sequential)]
36     public struct U8String
37     {
38         public byte[] mb_str; /**< nul-terminated UTF-8 bytes array */
39
40         /**
41          * Creates an UTF-8 characters array from a .NET string.
42          * @param value string to convert
43          */
44         public U8String (string value)
45         {
46             mb_str = null;
47             if (value == null)
48                 return;
49
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;
54         }
55
56         private U8String (IntPtr ptr)
57         {
58             mb_str = null;
59             if (ptr == IntPtr.Zero)
60                 return;
61
62             int i = 0;
63             while (Marshal.ReadByte (ptr, i) != 0)
64                 i++;
65             i++;
66
67             mb_str = new byte[i];
68             Marshal.Copy (ptr, mb_str, 0, i);
69         }
70
71         /**
72          * Object::ToString.
73          */
74         public override string ToString ()
75         {
76             if (mb_str == null)
77                 return null;
78
79             byte[] bytes = new byte[mb_str.Length - 1];
80             Array.Copy (mb_str, bytes, bytes.Length);
81
82             return System.Text.Encoding.UTF8.GetString (bytes);
83         }
84
85         /**
86          * Converts a pointer to a nul-terminated UTF-8 characters array into
87          * a managed string.
88          */
89         public static string FromNative (IntPtr ptr)
90         {
91             return new U8String (ptr).ToString ();
92         }
93     };
94 };