]> git.sesse.net Git - vlc/blob - bindings/cil/src/ustring.cs
DBus: ifdef buggy input code out
[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     internal 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
95     /**
96      * @brief MemoryHandle: heap allocation by the C run-time
97      * @ingroup Internals
98      */
99     internal class MemoryHandle : NonNullHandle
100     {
101         /**
102          * NonNullHandle.Destroy
103          */
104         protected override void Destroy ()
105         {
106             LibVLC.Free (handle);
107         }
108     };
109
110     /**
111      * @brief StringHandle: heap-allocated characters array
112      * @ingroup Internals
113      */
114     internal sealed class StringHandle : MemoryHandle
115     {
116         /**
117          * Converts an heap-allocated nul-terminated UTF-8 characters array
118          * into a managed string.
119          * @return the resulting managed string.
120          */
121         public override string ToString ()
122         {
123             return U8String.FromNative (handle);
124         }
125
126         /**
127          * Converts the buffer (as in ToString()) and release it.
128          * @return managed string representation of the buffer
129          */
130         public string Transform ()
131         {
132             string value = ToString ();
133             Close ();
134             return value;
135         }
136     };
137 };