2003-05-29 Rachel Hestilow <rachel@nullenvoid.com>
* gconf/Value.cs: Update to use new string marshalling.
* generator/StringGen.cs, ConstStringGen.cs: Added.
* generator/IGeneratable.cs: Add new method ToNativeReturn.
* generator/CallbackGen.cs: Implement ToNativeReturn. Call
ToNativeReturn for the return statement. Fix a couple of
places where s_ret was being used incorrectly for m_ret.
* generator/ClassGen.cs, EnumGen.cs, ManualGen.cs,
SimpleGen.cs, StructBase.cs: Implement ToNativeReturn.
* generator/SignalHandler.cs: Call ToNativeReturn for the
return statement, instead of CallByName.
* generator/SymbolTable.cs: Use StringGen for gchar, char,
and gunichar, and ConstStringGen for their const variants.
Add a new method wrapper for ToNativeReturn.
(Trim): Add a special-case for const strings so that the
const is not stripped. Otherwise there is no way of
resolving the const case.
* glade/XML.custom: Update to use new string marshalling.
* glib/Marshaller.cs: Added.
* glib/GException.cs, Markup.cs, ObjectManager.cs,
Value.cs: Update to use new string marshalling.
* glib/Object.cs: Remove old g_type_name DllImport
as it is no longer used.
* glue/fileselection.c (gtksharp_file_selection_get_fileop_entry):
Mark this as const return.
* gtk/ColorSelection.custom, FileSelection.custom,
SelectionData.custom: Update to use new string marshalling.
svn path=/trunk/gtk-sharp/; revision=15286
2003-06-10 18:09:47 +00:00
|
|
|
// GLibSharp.Marshaller.cs : Marshalling utils
|
|
|
|
//
|
|
|
|
// Author: Rachel Hestilow <rachel@nullenvoid.com>
|
2004-05-19 18:57:28 +00:00
|
|
|
// Mike Kestner <mkestner@ximian.com>
|
2003-05-29 Rachel Hestilow <rachel@nullenvoid.com>
* gconf/Value.cs: Update to use new string marshalling.
* generator/StringGen.cs, ConstStringGen.cs: Added.
* generator/IGeneratable.cs: Add new method ToNativeReturn.
* generator/CallbackGen.cs: Implement ToNativeReturn. Call
ToNativeReturn for the return statement. Fix a couple of
places where s_ret was being used incorrectly for m_ret.
* generator/ClassGen.cs, EnumGen.cs, ManualGen.cs,
SimpleGen.cs, StructBase.cs: Implement ToNativeReturn.
* generator/SignalHandler.cs: Call ToNativeReturn for the
return statement, instead of CallByName.
* generator/SymbolTable.cs: Use StringGen for gchar, char,
and gunichar, and ConstStringGen for their const variants.
Add a new method wrapper for ToNativeReturn.
(Trim): Add a special-case for const strings so that the
const is not stripped. Otherwise there is no way of
resolving the const case.
* glade/XML.custom: Update to use new string marshalling.
* glib/Marshaller.cs: Added.
* glib/GException.cs, Markup.cs, ObjectManager.cs,
Value.cs: Update to use new string marshalling.
* glib/Object.cs: Remove old g_type_name DllImport
as it is no longer used.
* glue/fileselection.c (gtksharp_file_selection_get_fileop_entry):
Mark this as const return.
* gtk/ColorSelection.custom, FileSelection.custom,
SelectionData.custom: Update to use new string marshalling.
svn path=/trunk/gtk-sharp/; revision=15286
2003-06-10 18:09:47 +00:00
|
|
|
//
|
2004-05-19 18:57:28 +00:00
|
|
|
// Copyright (c) 2002, 2003 Rachel Hestilow
|
|
|
|
// Copyright (c) 2004 Novell, Inc.
|
2004-06-25 18:42:19 +00:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of version 2 of the Lesser GNU General
|
|
|
|
// Public License as published by the Free Software Foundation.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this program; if not, write to the
|
|
|
|
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
// Boston, MA 02111-1307, USA.
|
|
|
|
|
2003-05-29 Rachel Hestilow <rachel@nullenvoid.com>
* gconf/Value.cs: Update to use new string marshalling.
* generator/StringGen.cs, ConstStringGen.cs: Added.
* generator/IGeneratable.cs: Add new method ToNativeReturn.
* generator/CallbackGen.cs: Implement ToNativeReturn. Call
ToNativeReturn for the return statement. Fix a couple of
places where s_ret was being used incorrectly for m_ret.
* generator/ClassGen.cs, EnumGen.cs, ManualGen.cs,
SimpleGen.cs, StructBase.cs: Implement ToNativeReturn.
* generator/SignalHandler.cs: Call ToNativeReturn for the
return statement, instead of CallByName.
* generator/SymbolTable.cs: Use StringGen for gchar, char,
and gunichar, and ConstStringGen for their const variants.
Add a new method wrapper for ToNativeReturn.
(Trim): Add a special-case for const strings so that the
const is not stripped. Otherwise there is no way of
resolving the const case.
* glade/XML.custom: Update to use new string marshalling.
* glib/Marshaller.cs: Added.
* glib/GException.cs, Markup.cs, ObjectManager.cs,
Value.cs: Update to use new string marshalling.
* glib/Object.cs: Remove old g_type_name DllImport
as it is no longer used.
* glue/fileselection.c (gtksharp_file_selection_get_fileop_entry):
Mark this as const return.
* gtk/ColorSelection.custom, FileSelection.custom,
SelectionData.custom: Update to use new string marshalling.
svn path=/trunk/gtk-sharp/; revision=15286
2003-06-10 18:09:47 +00:00
|
|
|
|
2004-05-19 18:57:28 +00:00
|
|
|
namespace GLib {
|
2003-05-29 Rachel Hestilow <rachel@nullenvoid.com>
* gconf/Value.cs: Update to use new string marshalling.
* generator/StringGen.cs, ConstStringGen.cs: Added.
* generator/IGeneratable.cs: Add new method ToNativeReturn.
* generator/CallbackGen.cs: Implement ToNativeReturn. Call
ToNativeReturn for the return statement. Fix a couple of
places where s_ret was being used incorrectly for m_ret.
* generator/ClassGen.cs, EnumGen.cs, ManualGen.cs,
SimpleGen.cs, StructBase.cs: Implement ToNativeReturn.
* generator/SignalHandler.cs: Call ToNativeReturn for the
return statement, instead of CallByName.
* generator/SymbolTable.cs: Use StringGen for gchar, char,
and gunichar, and ConstStringGen for their const variants.
Add a new method wrapper for ToNativeReturn.
(Trim): Add a special-case for const strings so that the
const is not stripped. Otherwise there is no way of
resolving the const case.
* glade/XML.custom: Update to use new string marshalling.
* glib/Marshaller.cs: Added.
* glib/GException.cs, Markup.cs, ObjectManager.cs,
Value.cs: Update to use new string marshalling.
* glib/Object.cs: Remove old g_type_name DllImport
as it is no longer used.
* glue/fileselection.c (gtksharp_file_selection_get_fileop_entry):
Mark this as const return.
* gtk/ColorSelection.custom, FileSelection.custom,
SelectionData.custom: Update to use new string marshalling.
svn path=/trunk/gtk-sharp/; revision=15286
2003-06-10 18:09:47 +00:00
|
|
|
using System;
|
2012-11-04 15:43:35 +00:00
|
|
|
using System.Collections.Generic;
|
2003-05-29 Rachel Hestilow <rachel@nullenvoid.com>
* gconf/Value.cs: Update to use new string marshalling.
* generator/StringGen.cs, ConstStringGen.cs: Added.
* generator/IGeneratable.cs: Add new method ToNativeReturn.
* generator/CallbackGen.cs: Implement ToNativeReturn. Call
ToNativeReturn for the return statement. Fix a couple of
places where s_ret was being used incorrectly for m_ret.
* generator/ClassGen.cs, EnumGen.cs, ManualGen.cs,
SimpleGen.cs, StructBase.cs: Implement ToNativeReturn.
* generator/SignalHandler.cs: Call ToNativeReturn for the
return statement, instead of CallByName.
* generator/SymbolTable.cs: Use StringGen for gchar, char,
and gunichar, and ConstStringGen for their const variants.
Add a new method wrapper for ToNativeReturn.
(Trim): Add a special-case for const strings so that the
const is not stripped. Otherwise there is no way of
resolving the const case.
* glade/XML.custom: Update to use new string marshalling.
* glib/Marshaller.cs: Added.
* glib/GException.cs, Markup.cs, ObjectManager.cs,
Value.cs: Update to use new string marshalling.
* glib/Object.cs: Remove old g_type_name DllImport
as it is no longer used.
* glue/fileselection.c (gtksharp_file_selection_get_fileop_entry):
Mark this as const return.
* gtk/ColorSelection.custom, FileSelection.custom,
SelectionData.custom: Update to use new string marshalling.
svn path=/trunk/gtk-sharp/; revision=15286
2003-06-10 18:09:47 +00:00
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
public class Marshaller {
|
2003-11-28 05:29:34 +00:00
|
|
|
|
|
|
|
private Marshaller () {}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2003-05-29 Rachel Hestilow <rachel@nullenvoid.com>
* gconf/Value.cs: Update to use new string marshalling.
* generator/StringGen.cs, ConstStringGen.cs: Added.
* generator/IGeneratable.cs: Add new method ToNativeReturn.
* generator/CallbackGen.cs: Implement ToNativeReturn. Call
ToNativeReturn for the return statement. Fix a couple of
places where s_ret was being used incorrectly for m_ret.
* generator/ClassGen.cs, EnumGen.cs, ManualGen.cs,
SimpleGen.cs, StructBase.cs: Implement ToNativeReturn.
* generator/SignalHandler.cs: Call ToNativeReturn for the
return statement, instead of CallByName.
* generator/SymbolTable.cs: Use StringGen for gchar, char,
and gunichar, and ConstStringGen for their const variants.
Add a new method wrapper for ToNativeReturn.
(Trim): Add a special-case for const strings so that the
const is not stripped. Otherwise there is no way of
resolving the const case.
* glade/XML.custom: Update to use new string marshalling.
* glib/Marshaller.cs: Added.
* glib/GException.cs, Markup.cs, ObjectManager.cs,
Value.cs: Update to use new string marshalling.
* glib/Object.cs: Remove old g_type_name DllImport
as it is no longer used.
* glue/fileselection.c (gtksharp_file_selection_get_fileop_entry):
Mark this as const return.
* gtk/ColorSelection.custom, FileSelection.custom,
SelectionData.custom: Update to use new string marshalling.
svn path=/trunk/gtk-sharp/; revision=15286
2003-06-10 18:09:47 +00:00
|
|
|
static extern void g_free (IntPtr mem);
|
|
|
|
|
2005-03-08 21:28:08 +00:00
|
|
|
public static void Free (IntPtr ptr)
|
|
|
|
{
|
|
|
|
g_free (ptr);
|
|
|
|
}
|
|
|
|
|
2007-11-12 17:27:35 +00:00
|
|
|
public static void Free (IntPtr[] ptrs)
|
|
|
|
{
|
2008-04-15 17:43:55 +00:00
|
|
|
if (ptrs == null)
|
|
|
|
return;
|
|
|
|
|
2007-11-12 17:27:35 +00:00
|
|
|
for (int i = 0; i < ptrs.Length; i++)
|
|
|
|
g_free (ptrs [i]);
|
|
|
|
}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2005-06-02 14:16:52 +00:00
|
|
|
static extern IntPtr g_filename_to_utf8 (IntPtr mem, int len, IntPtr read, out IntPtr written, out IntPtr error);
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll)]
|
2009-11-30 17:39:11 +00:00
|
|
|
static extern IntPtr g_filename_to_utf8_utf8 (IntPtr mem, int len, IntPtr read, out IntPtr written, out IntPtr error);
|
|
|
|
|
2005-06-02 14:16:52 +00:00
|
|
|
public static string FilenamePtrToString (IntPtr ptr)
|
|
|
|
{
|
2005-06-09 14:52:24 +00:00
|
|
|
if (ptr == IntPtr.Zero) return null;
|
|
|
|
|
2005-06-02 14:16:52 +00:00
|
|
|
IntPtr dummy, error;
|
2009-11-30 17:39:11 +00:00
|
|
|
IntPtr utf8;
|
|
|
|
|
|
|
|
if (Global.IsWindowsPlatform)
|
|
|
|
utf8 = g_filename_to_utf8_utf8 (ptr, -1, IntPtr.Zero, out dummy, out error);
|
|
|
|
else
|
|
|
|
utf8 = g_filename_to_utf8 (ptr, -1, IntPtr.Zero, out dummy, out error);
|
|
|
|
|
2005-06-02 14:16:52 +00:00
|
|
|
if (error != IntPtr.Zero)
|
|
|
|
throw new GLib.GException (error);
|
|
|
|
return Utf8PtrToString (utf8);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string FilenamePtrToStringGFree (IntPtr ptr)
|
|
|
|
{
|
|
|
|
string ret = FilenamePtrToString (ptr);
|
|
|
|
g_free (ptr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-12-19 18:57:42 +00:00
|
|
|
static unsafe ulong strlen (IntPtr s)
|
|
|
|
{
|
|
|
|
ulong cnt = 0;
|
|
|
|
byte *b = (byte *)s;
|
|
|
|
while (*b != 0) {
|
|
|
|
b++;
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
2005-03-04 18:59:09 +00:00
|
|
|
|
|
|
|
public static string Utf8PtrToString (IntPtr ptr)
|
|
|
|
{
|
2005-03-09 17:01:35 +00:00
|
|
|
if (ptr == IntPtr.Zero)
|
|
|
|
return null;
|
|
|
|
|
2008-12-19 18:57:42 +00:00
|
|
|
int len = (int) (uint) strlen (ptr);
|
2005-03-04 18:59:09 +00:00
|
|
|
byte[] bytes = new byte [len];
|
|
|
|
Marshal.Copy (ptr, bytes, 0, len);
|
|
|
|
return System.Text.Encoding.UTF8.GetString (bytes);
|
|
|
|
}
|
|
|
|
|
2005-04-22 19:43:46 +00:00
|
|
|
public static string[] Utf8PtrToString (IntPtr[] ptrs) {
|
|
|
|
// The last pointer is a null terminator.
|
|
|
|
string[] ret = new string[ptrs.Length - 1];
|
|
|
|
for (int i = 0; i < ret.Length; i++)
|
|
|
|
ret[i] = Utf8PtrToString (ptrs[i]);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-03-04 18:59:09 +00:00
|
|
|
public static string PtrToStringGFree (IntPtr ptr)
|
|
|
|
{
|
|
|
|
string ret = Utf8PtrToString (ptr);
|
2003-05-29 Rachel Hestilow <rachel@nullenvoid.com>
* gconf/Value.cs: Update to use new string marshalling.
* generator/StringGen.cs, ConstStringGen.cs: Added.
* generator/IGeneratable.cs: Add new method ToNativeReturn.
* generator/CallbackGen.cs: Implement ToNativeReturn. Call
ToNativeReturn for the return statement. Fix a couple of
places where s_ret was being used incorrectly for m_ret.
* generator/ClassGen.cs, EnumGen.cs, ManualGen.cs,
SimpleGen.cs, StructBase.cs: Implement ToNativeReturn.
* generator/SignalHandler.cs: Call ToNativeReturn for the
return statement, instead of CallByName.
* generator/SymbolTable.cs: Use StringGen for gchar, char,
and gunichar, and ConstStringGen for their const variants.
Add a new method wrapper for ToNativeReturn.
(Trim): Add a special-case for const strings so that the
const is not stripped. Otherwise there is no way of
resolving the const case.
* glade/XML.custom: Update to use new string marshalling.
* glib/Marshaller.cs: Added.
* glib/GException.cs, Markup.cs, ObjectManager.cs,
Value.cs: Update to use new string marshalling.
* glib/Object.cs: Remove old g_type_name DllImport
as it is no longer used.
* glue/fileselection.c (gtksharp_file_selection_get_fileop_entry):
Mark this as const return.
* gtk/ColorSelection.custom, FileSelection.custom,
SelectionData.custom: Update to use new string marshalling.
svn path=/trunk/gtk-sharp/; revision=15286
2003-06-10 18:09:47 +00:00
|
|
|
g_free (ptr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string[] PtrToStringGFree (IntPtr[] ptrs) {
|
|
|
|
// The last pointer is a null terminator.
|
|
|
|
string[] ret = new string[ptrs.Length - 1];
|
|
|
|
for (int i = 0; i < ret.Length; i++) {
|
2005-03-04 18:59:09 +00:00
|
|
|
ret[i] = Utf8PtrToString (ptrs[i]);
|
2003-05-29 Rachel Hestilow <rachel@nullenvoid.com>
* gconf/Value.cs: Update to use new string marshalling.
* generator/StringGen.cs, ConstStringGen.cs: Added.
* generator/IGeneratable.cs: Add new method ToNativeReturn.
* generator/CallbackGen.cs: Implement ToNativeReturn. Call
ToNativeReturn for the return statement. Fix a couple of
places where s_ret was being used incorrectly for m_ret.
* generator/ClassGen.cs, EnumGen.cs, ManualGen.cs,
SimpleGen.cs, StructBase.cs: Implement ToNativeReturn.
* generator/SignalHandler.cs: Call ToNativeReturn for the
return statement, instead of CallByName.
* generator/SymbolTable.cs: Use StringGen for gchar, char,
and gunichar, and ConstStringGen for their const variants.
Add a new method wrapper for ToNativeReturn.
(Trim): Add a special-case for const strings so that the
const is not stripped. Otherwise there is no way of
resolving the const case.
* glade/XML.custom: Update to use new string marshalling.
* glib/Marshaller.cs: Added.
* glib/GException.cs, Markup.cs, ObjectManager.cs,
Value.cs: Update to use new string marshalling.
* glib/Object.cs: Remove old g_type_name DllImport
as it is no longer used.
* glue/fileselection.c (gtksharp_file_selection_get_fileop_entry):
Mark this as const return.
* gtk/ColorSelection.custom, FileSelection.custom,
SelectionData.custom: Update to use new string marshalling.
svn path=/trunk/gtk-sharp/; revision=15286
2003-06-10 18:09:47 +00:00
|
|
|
g_free (ptrs[i]);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2005-06-02 14:16:52 +00:00
|
|
|
static extern IntPtr g_filename_from_utf8 (IntPtr mem, int len, IntPtr read, out IntPtr written, out IntPtr error);
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll)]
|
2009-11-30 17:39:11 +00:00
|
|
|
static extern IntPtr g_filename_from_utf8_utf8 (IntPtr mem, int len, IntPtr read, out IntPtr written, out IntPtr error);
|
|
|
|
|
2005-06-02 14:16:52 +00:00
|
|
|
public static IntPtr StringToFilenamePtr (string str)
|
|
|
|
{
|
|
|
|
if (str == null)
|
|
|
|
return IntPtr.Zero;
|
|
|
|
|
|
|
|
IntPtr dummy, error;
|
|
|
|
IntPtr utf8 = StringToPtrGStrdup (str);
|
2009-11-30 17:39:11 +00:00
|
|
|
|
|
|
|
IntPtr result;
|
|
|
|
|
|
|
|
if (Global.IsWindowsPlatform)
|
|
|
|
result = g_filename_from_utf8_utf8 (utf8, -1, IntPtr.Zero, out dummy, out error);
|
|
|
|
else
|
|
|
|
result = g_filename_from_utf8 (utf8, -1, IntPtr.Zero, out dummy, out error);
|
|
|
|
|
2005-06-02 14:16:52 +00:00
|
|
|
g_free (utf8);
|
|
|
|
if (error != IntPtr.Zero)
|
|
|
|
throw new GException (error);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2003-05-29 Rachel Hestilow <rachel@nullenvoid.com>
* gconf/Value.cs: Update to use new string marshalling.
* generator/StringGen.cs, ConstStringGen.cs: Added.
* generator/IGeneratable.cs: Add new method ToNativeReturn.
* generator/CallbackGen.cs: Implement ToNativeReturn. Call
ToNativeReturn for the return statement. Fix a couple of
places where s_ret was being used incorrectly for m_ret.
* generator/ClassGen.cs, EnumGen.cs, ManualGen.cs,
SimpleGen.cs, StructBase.cs: Implement ToNativeReturn.
* generator/SignalHandler.cs: Call ToNativeReturn for the
return statement, instead of CallByName.
* generator/SymbolTable.cs: Use StringGen for gchar, char,
and gunichar, and ConstStringGen for their const variants.
Add a new method wrapper for ToNativeReturn.
(Trim): Add a special-case for const strings so that the
const is not stripped. Otherwise there is no way of
resolving the const case.
* glade/XML.custom: Update to use new string marshalling.
* glib/Marshaller.cs: Added.
* glib/GException.cs, Markup.cs, ObjectManager.cs,
Value.cs: Update to use new string marshalling.
* glib/Object.cs: Remove old g_type_name DllImport
as it is no longer used.
* glue/fileselection.c (gtksharp_file_selection_get_fileop_entry):
Mark this as const return.
* gtk/ColorSelection.custom, FileSelection.custom,
SelectionData.custom: Update to use new string marshalling.
svn path=/trunk/gtk-sharp/; revision=15286
2003-06-10 18:09:47 +00:00
|
|
|
public static IntPtr StringToPtrGStrdup (string str) {
|
2005-03-08 21:28:08 +00:00
|
|
|
if (str == null)
|
|
|
|
return IntPtr.Zero;
|
2005-03-04 18:59:09 +00:00
|
|
|
byte[] bytes = System.Text.Encoding.UTF8.GetBytes (str);
|
2005-03-08 21:28:08 +00:00
|
|
|
IntPtr result = g_malloc (new UIntPtr ((ulong)bytes.Length + 1));
|
|
|
|
Marshal.Copy (bytes, 0, result, bytes.Length);
|
|
|
|
Marshal.WriteByte (result, bytes.Length, 0);
|
|
|
|
return result;
|
2003-05-29 Rachel Hestilow <rachel@nullenvoid.com>
* gconf/Value.cs: Update to use new string marshalling.
* generator/StringGen.cs, ConstStringGen.cs: Added.
* generator/IGeneratable.cs: Add new method ToNativeReturn.
* generator/CallbackGen.cs: Implement ToNativeReturn. Call
ToNativeReturn for the return statement. Fix a couple of
places where s_ret was being used incorrectly for m_ret.
* generator/ClassGen.cs, EnumGen.cs, ManualGen.cs,
SimpleGen.cs, StructBase.cs: Implement ToNativeReturn.
* generator/SignalHandler.cs: Call ToNativeReturn for the
return statement, instead of CallByName.
* generator/SymbolTable.cs: Use StringGen for gchar, char,
and gunichar, and ConstStringGen for their const variants.
Add a new method wrapper for ToNativeReturn.
(Trim): Add a special-case for const strings so that the
const is not stripped. Otherwise there is no way of
resolving the const case.
* glade/XML.custom: Update to use new string marshalling.
* glib/Marshaller.cs: Added.
* glib/GException.cs, Markup.cs, ObjectManager.cs,
Value.cs: Update to use new string marshalling.
* glib/Object.cs: Remove old g_type_name DllImport
as it is no longer used.
* glue/fileselection.c (gtksharp_file_selection_get_fileop_entry):
Mark this as const return.
* gtk/ColorSelection.custom, FileSelection.custom,
SelectionData.custom: Update to use new string marshalling.
svn path=/trunk/gtk-sharp/; revision=15286
2003-06-10 18:09:47 +00:00
|
|
|
}
|
|
|
|
|
2005-01-20 15:45:13 +00:00
|
|
|
public static string StringFormat (string format, params object[] args) {
|
|
|
|
string ret = String.Format (format, args);
|
|
|
|
if (ret.IndexOf ('%') == -1)
|
|
|
|
return ret;
|
|
|
|
else
|
|
|
|
return ret.Replace ("%", "%%");
|
|
|
|
}
|
|
|
|
|
2013-10-08 15:54:29 +00:00
|
|
|
public static IntPtr StringArrayToStrvPtr (string[] strs)
|
2011-04-14 19:31:33 +00:00
|
|
|
{
|
|
|
|
IntPtr[] ptrs = StringArrayToNullTermPointer (strs);
|
|
|
|
IntPtr ret = g_malloc (new UIntPtr ((ulong) (ptrs.Length * IntPtr.Size)));
|
|
|
|
Marshal.Copy (ptrs, 0, ret, ptrs.Length);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-08 15:54:29 +00:00
|
|
|
public static IntPtr StringArrayToNullTermStrvPointer (string[] strs)
|
|
|
|
{
|
|
|
|
return StringArrayToStrvPtr (strs);
|
|
|
|
}
|
|
|
|
|
2007-11-12 17:27:35 +00:00
|
|
|
public static IntPtr[] StringArrayToNullTermPointer (string[] strs)
|
|
|
|
{
|
|
|
|
if (strs == null)
|
2008-04-15 17:43:55 +00:00
|
|
|
return null;
|
2007-11-12 17:27:35 +00:00
|
|
|
IntPtr[] result = new IntPtr [strs.Length + 1];
|
|
|
|
for (int i = 0; i < strs.Length; i++)
|
|
|
|
result [i] = StringToPtrGStrdup (strs [i]);
|
|
|
|
result [strs.Length] = IntPtr.Zero;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2008-02-29 23:42:27 +00:00
|
|
|
static extern void g_strfreev (IntPtr mem);
|
|
|
|
|
|
|
|
public static void StrFreeV (IntPtr null_term_array)
|
|
|
|
{
|
|
|
|
g_strfreev (null_term_array);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string[] NullTermPtrToStringArray (IntPtr null_term_array, bool owned)
|
|
|
|
{
|
|
|
|
if (null_term_array == IntPtr.Zero)
|
|
|
|
return new string [0];
|
|
|
|
|
|
|
|
int count = 0;
|
2012-11-04 15:43:35 +00:00
|
|
|
var result = new List<string> ();
|
2008-02-29 23:42:27 +00:00
|
|
|
IntPtr s = Marshal.ReadIntPtr (null_term_array, count++ * IntPtr.Size);
|
|
|
|
while (s != IntPtr.Zero) {
|
|
|
|
result.Add (Utf8PtrToString (s));
|
|
|
|
s = Marshal.ReadIntPtr (null_term_array, count++ * IntPtr.Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (owned)
|
|
|
|
g_strfreev (null_term_array);
|
|
|
|
|
2012-11-04 15:43:35 +00:00
|
|
|
return result.ToArray ();
|
2008-02-29 23:42:27 +00:00
|
|
|
}
|
|
|
|
|
2007-11-08 17:27:13 +00:00
|
|
|
public static string[] PtrToStringArrayGFree (IntPtr string_array)
|
|
|
|
{
|
|
|
|
if (string_array == IntPtr.Zero)
|
|
|
|
return new string [0];
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
while (Marshal.ReadIntPtr (string_array, count*IntPtr.Size) != IntPtr.Zero)
|
|
|
|
++count;
|
|
|
|
|
|
|
|
string[] members = new string[count];
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
IntPtr s = Marshal.ReadIntPtr (string_array, i * IntPtr.Size);
|
2013-11-17 15:26:02 +00:00
|
|
|
members[i] = PtrToStringGFree (s);
|
2007-11-08 17:27:13 +00:00
|
|
|
}
|
2013-11-17 15:26:02 +00:00
|
|
|
Free (string_array);
|
2007-11-08 17:27:13 +00:00
|
|
|
return members;
|
|
|
|
}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2005-03-08 21:28:08 +00:00
|
|
|
static extern IntPtr g_malloc(UIntPtr size);
|
2004-01-19 03:24:25 +00:00
|
|
|
|
2008-02-29 23:42:27 +00:00
|
|
|
public static IntPtr Malloc (ulong size)
|
|
|
|
{
|
|
|
|
return g_malloc (new UIntPtr (size));
|
|
|
|
}
|
|
|
|
|
2013-10-09 16:30:10 +00:00
|
|
|
static System.DateTime local_epoch = new System.DateTime (1970, 1, 1, 0, 0, 0);
|
|
|
|
static int utc_offset = (int) (System.TimeZone.CurrentTimeZone.GetUtcOffset (System.DateTime.Now)).TotalSeconds;
|
2004-05-19 18:57:28 +00:00
|
|
|
|
2013-10-09 16:30:10 +00:00
|
|
|
public static IntPtr DateTimeTotime_t (System.DateTime time)
|
2004-05-19 18:57:28 +00:00
|
|
|
{
|
2008-06-28 17:15:31 +00:00
|
|
|
return new IntPtr (((long)time.Subtract (local_epoch).TotalSeconds) - utc_offset);
|
2004-05-19 18:57:28 +00:00
|
|
|
}
|
|
|
|
|
2013-10-09 16:30:10 +00:00
|
|
|
public static System.DateTime time_tToDateTime (IntPtr time_t)
|
2004-05-19 18:57:28 +00:00
|
|
|
{
|
2008-06-28 17:15:31 +00:00
|
|
|
return local_epoch.AddSeconds (time_t.ToInt64 () + utc_offset);
|
2004-05-19 18:57:28 +00:00
|
|
|
}
|
2004-08-17 20:43:49 +00:00
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2008-12-19 18:57:42 +00:00
|
|
|
static extern IntPtr g_malloc0 (UIntPtr size);
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2008-12-19 18:57:42 +00:00
|
|
|
static extern int g_unichar_to_utf8 (uint c, IntPtr buf);
|
2004-08-17 20:43:49 +00:00
|
|
|
|
|
|
|
public static char GUnicharToChar (uint ucs4_char)
|
|
|
|
{
|
2005-03-29 15:28:02 +00:00
|
|
|
if (ucs4_char == 0)
|
|
|
|
return (char) 0;
|
|
|
|
|
2008-12-19 18:57:42 +00:00
|
|
|
string ret = GUnicharToString (ucs4_char);
|
|
|
|
if (ret.Length != 1)
|
2004-08-17 20:43:49 +00:00
|
|
|
throw new ArgumentOutOfRangeException ("ucs4char is not representable by a char.");
|
|
|
|
|
|
|
|
return ret [0];
|
|
|
|
}
|
|
|
|
|
2008-12-19 18:57:42 +00:00
|
|
|
public static string GUnicharToString (uint ucs4_char)
|
|
|
|
{
|
|
|
|
if (ucs4_char == 0)
|
|
|
|
return String.Empty;
|
|
|
|
|
|
|
|
IntPtr buf = g_malloc0 (new UIntPtr (7));
|
|
|
|
g_unichar_to_utf8 (ucs4_char, buf);
|
|
|
|
return PtrToStringGFree (buf);
|
|
|
|
}
|
|
|
|
|
2013-11-05 10:12:27 +00:00
|
|
|
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
|
2008-12-19 18:57:42 +00:00
|
|
|
static extern IntPtr g_utf16_to_ucs4 (ref ushort c, IntPtr len, IntPtr d1, IntPtr d2, IntPtr d3);
|
2004-08-17 20:43:49 +00:00
|
|
|
|
|
|
|
public static uint CharToGUnichar (char c)
|
|
|
|
{
|
2008-12-19 18:57:42 +00:00
|
|
|
ushort val = (ushort) c;
|
|
|
|
IntPtr ucs4_str = g_utf16_to_ucs4 (ref val, new IntPtr (1), IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
|
|
|
|
uint result = (uint) Marshal.ReadInt32 (ucs4_str);
|
|
|
|
g_free (ucs4_str);
|
|
|
|
return result;
|
2004-08-17 20:43:49 +00:00
|
|
|
}
|
|
|
|
|
2005-06-30 17:09:39 +00:00
|
|
|
public static IntPtr StructureToPtrAlloc (object o)
|
2005-03-04 18:59:09 +00:00
|
|
|
{
|
|
|
|
IntPtr result = Marshal.AllocHGlobal (Marshal.SizeOf (o));
|
|
|
|
Marshal.StructureToPtr (o, result, false);
|
|
|
|
return result;
|
|
|
|
}
|
2005-06-08 16:53:12 +00:00
|
|
|
|
2011-04-28 23:38:07 +00:00
|
|
|
public static IntPtr ArrayToArrayPtr (byte[] array)
|
|
|
|
{
|
|
|
|
IntPtr ret = Malloc ((ulong) array.Length);
|
|
|
|
Marshal.Copy (array, 0, ret, array.Length);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Array ArrayPtrToArray (IntPtr array_ptr, Type element_type, int length, bool owned)
|
|
|
|
{
|
|
|
|
Array result = null;
|
|
|
|
if (element_type == typeof (byte)) {
|
|
|
|
byte[] ret = new byte [length];
|
|
|
|
Marshal.Copy (array_ptr, ret, 0, length);
|
|
|
|
result = ret;
|
|
|
|
} else {
|
|
|
|
throw new InvalidOperationException ("Marshaling of " + element_type + " arrays is not supported");
|
|
|
|
}
|
|
|
|
if (owned)
|
|
|
|
Free (array_ptr);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2008-08-20 19:48:26 +00:00
|
|
|
public static Array ListPtrToArray (IntPtr list_ptr, Type list_type, bool owned, bool elements_owned, Type elem_type)
|
|
|
|
{
|
2009-01-08 18:43:00 +00:00
|
|
|
Type array_type = elem_type == typeof (ListBase.FilenameString) ? typeof (string) : elem_type;
|
2008-08-20 19:48:26 +00:00
|
|
|
ListBase list;
|
|
|
|
if (list_type == typeof(GLib.List))
|
|
|
|
list = new GLib.List (list_ptr, elem_type, owned, elements_owned);
|
|
|
|
else
|
|
|
|
list = new GLib.SList (list_ptr, elem_type, owned, elements_owned);
|
|
|
|
|
|
|
|
using (list)
|
2009-01-08 18:43:00 +00:00
|
|
|
return ListToArray (list, array_type);
|
2008-08-20 19:48:26 +00:00
|
|
|
}
|
|
|
|
|
2008-11-04 20:17:00 +00:00
|
|
|
public static Array PtrArrayToArray (IntPtr list_ptr, bool owned, bool elements_owned, Type elem_type)
|
|
|
|
{
|
|
|
|
GLib.PtrArray array = new GLib.PtrArray (list_ptr, elem_type, owned, elements_owned);
|
|
|
|
Array ret = Array.CreateInstance (elem_type, array.Count);
|
|
|
|
array.CopyTo (ret, 0);
|
|
|
|
array.Dispose ();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-06-08 16:53:12 +00:00
|
|
|
public static Array ListToArray (ListBase list, System.Type type)
|
|
|
|
{
|
|
|
|
Array result = Array.CreateInstance (type, list.Count);
|
2005-06-08 17:29:48 +00:00
|
|
|
if (list.Count > 0)
|
|
|
|
list.CopyTo (result, 0);
|
2006-03-27 18:35:08 +00:00
|
|
|
|
|
|
|
if (type.IsSubclassOf (typeof (GLib.Opaque)))
|
|
|
|
list.elements_owned = false;
|
|
|
|
|
2005-06-08 16:53:12 +00:00
|
|
|
return result;
|
|
|
|
}
|
2013-08-14 11:16:40 +00:00
|
|
|
|
|
|
|
public static T[] StructArrayFromNullTerminatedIntPtr<T> (IntPtr array)
|
|
|
|
{
|
|
|
|
var res = new List<T> ();
|
|
|
|
IntPtr current = array;
|
|
|
|
T currentStruct = default(T);
|
|
|
|
|
|
|
|
while (current != IntPtr.Zero) {
|
|
|
|
Marshal.PtrToStructure (current, currentStruct);
|
|
|
|
res.Add (currentStruct);
|
|
|
|
current = (IntPtr) ((long)current + Marshal.SizeOf (typeof (T)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.ToArray ();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IntPtr StructArrayToNullTerminatedStructArrayIntPtr<T> (T[] InputArray)
|
|
|
|
{
|
|
|
|
int intPtrSize = Marshal.SizeOf (typeof (IntPtr));
|
|
|
|
IntPtr mem = Marshal.AllocHGlobal ((InputArray.Length + 1) * intPtrSize);
|
|
|
|
|
|
|
|
for (int i = 0; i < InputArray.Length; i++) {
|
|
|
|
IntPtr structPtr = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (T)));
|
|
|
|
Marshal.StructureToPtr (InputArray[i], structPtr, false);
|
|
|
|
// jump to next pointer
|
|
|
|
Marshal.WriteIntPtr (mem, structPtr);
|
|
|
|
mem = (IntPtr) ((long)mem + intPtrSize);
|
|
|
|
}
|
|
|
|
// null terminate
|
|
|
|
Marshal.WriteIntPtr (mem, IntPtr.Zero);
|
|
|
|
|
|
|
|
return mem;
|
|
|
|
}
|
2003-05-29 Rachel Hestilow <rachel@nullenvoid.com>
* gconf/Value.cs: Update to use new string marshalling.
* generator/StringGen.cs, ConstStringGen.cs: Added.
* generator/IGeneratable.cs: Add new method ToNativeReturn.
* generator/CallbackGen.cs: Implement ToNativeReturn. Call
ToNativeReturn for the return statement. Fix a couple of
places where s_ret was being used incorrectly for m_ret.
* generator/ClassGen.cs, EnumGen.cs, ManualGen.cs,
SimpleGen.cs, StructBase.cs: Implement ToNativeReturn.
* generator/SignalHandler.cs: Call ToNativeReturn for the
return statement, instead of CallByName.
* generator/SymbolTable.cs: Use StringGen for gchar, char,
and gunichar, and ConstStringGen for their const variants.
Add a new method wrapper for ToNativeReturn.
(Trim): Add a special-case for const strings so that the
const is not stripped. Otherwise there is no way of
resolving the const case.
* glade/XML.custom: Update to use new string marshalling.
* glib/Marshaller.cs: Added.
* glib/GException.cs, Markup.cs, ObjectManager.cs,
Value.cs: Update to use new string marshalling.
* glib/Object.cs: Remove old g_type_name DllImport
as it is no longer used.
* glue/fileselection.c (gtksharp_file_selection_get_fileop_entry):
Mark this as const return.
* gtk/ColorSelection.custom, FileSelection.custom,
SelectionData.custom: Update to use new string marshalling.
svn path=/trunk/gtk-sharp/; revision=15286
2003-06-10 18:09:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|