2004-12-21 19:47:55 +00:00
|
|
|
// GLib.Argv.cs : Argv marshaling class
|
|
|
|
//
|
|
|
|
// Author: Mike Kestner <mkestner@novell.com>
|
|
|
|
//
|
|
|
|
// Copyright (c) 2004 Novell, Inc.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
|
|
|
|
namespace GLib {
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
public class Argv {
|
|
|
|
|
|
|
|
IntPtr[] arg_ptrs;
|
|
|
|
IntPtr handle;
|
2005-01-11 20:49:50 +00:00
|
|
|
bool add_progname = false;
|
2004-12-21 19:47:55 +00:00
|
|
|
|
|
|
|
~Argv ()
|
|
|
|
{
|
2013-11-17 15:27:39 +00:00
|
|
|
Marshaller.Free (arg_ptrs);
|
|
|
|
Marshaller.Free (handle);
|
2004-12-21 19:47:55 +00:00
|
|
|
}
|
|
|
|
|
2005-01-11 20:49:50 +00:00
|
|
|
public Argv (string[] args) : this (args, false) {}
|
|
|
|
|
|
|
|
public Argv (string[] args, bool add_program_name)
|
2004-12-21 19:47:55 +00:00
|
|
|
{
|
2005-01-11 20:49:50 +00:00
|
|
|
add_progname = add_program_name;
|
|
|
|
if (add_progname) {
|
|
|
|
string[] full = new string [args.Length + 1];
|
|
|
|
full [0] = System.Environment.GetCommandLineArgs ()[0];
|
|
|
|
args.CopyTo (full, 1);
|
|
|
|
args = full;
|
|
|
|
}
|
|
|
|
|
2004-12-21 19:47:55 +00:00
|
|
|
arg_ptrs = new IntPtr [args.Length];
|
|
|
|
|
|
|
|
for (int i = 0; i < args.Length; i++)
|
2005-03-08 21:28:08 +00:00
|
|
|
arg_ptrs [i] = Marshaller.StringToPtrGStrdup (args[i]);
|
2004-12-21 19:47:55 +00:00
|
|
|
|
2013-11-17 15:27:39 +00:00
|
|
|
handle = Marshaller.Malloc ((ulong)(IntPtr.Size * args.Length));
|
2004-12-21 19:47:55 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < args.Length; i++)
|
|
|
|
Marshal.WriteIntPtr (handle, i * IntPtr.Size, arg_ptrs [i]);
|
|
|
|
}
|
2005-01-11 20:49:50 +00:00
|
|
|
|
2004-12-21 19:47:55 +00:00
|
|
|
public IntPtr Handle {
|
|
|
|
get {
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string[] GetArgs (int argc)
|
|
|
|
{
|
2005-01-11 20:49:50 +00:00
|
|
|
int count = add_progname ? argc - 1 : argc;
|
|
|
|
int idx = add_progname ? 1 : 0;
|
|
|
|
string[] result = new string [count];
|
2004-12-21 19:47:55 +00:00
|
|
|
|
2005-01-11 20:49:50 +00:00
|
|
|
for (int i = 0; i < count; i++, idx++)
|
2005-03-08 21:28:08 +00:00
|
|
|
result [i] = Marshaller.Utf8PtrToString (Marshal.ReadIntPtr (handle, idx * IntPtr.Size));
|
2004-12-21 19:47:55 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|