[gbrainy] Unix only functions



commit b92a6f38ca45a17bc32904494d92be1d770efaf4
Author: Jordi Mas <jmas softcatala org>
Date:   Sat Oct 17 23:11:38 2009 +0200

    Unix only functions

 src/Makefile.am |    1 +
 src/Unix.cs     |  119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/gbrainy.cs  |   92 +-----------------------------------------
 3 files changed, 122 insertions(+), 90 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index fe7a78b..fd08081 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -86,6 +86,7 @@ GBRAINY_CSDISTFILES =				\
 	$(srcdir)/CalculationGames/CalculationRatio.cs \
 	$(srcdir)/PuzzleGames/PuzzlePercentage.cs	\
 	$(srcdir)/SimpleLabel.cs	\
+	$(srcdir)/Unix.cs	\
 	$(srcdir)/gbrainy.cs			
 
 
diff --git a/src/Unix.cs b/src/Unix.cs
new file mode 100644
index 0000000..7aeb0b4
--- /dev/null
+++ b/src/Unix.cs
@@ -0,0 +1,119 @@
+/*
+ * Copyright (C) 2007-2009 Jordi Mas i Hernàndez <jmas softcatala org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU 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.
+ */
+
+using System;
+using System.Runtime.InteropServices;
+using System.Text;
+using System.Globalization;
+using System.Threading;
+
+//
+// Unix system calls
+//
+static public class Unix
+{
+	[DllImport("libc")]
+	static extern IntPtr localeconv ();
+
+	[DllImport ("libc")] // Linux
+	static extern int prctl (int option, byte [] arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5);
+
+	[DllImport ("libc")] // BSD
+	static extern void setproctitle (byte [] fmt, byte [] str_arg);
+ 
+	
+	/* Taken from locale.h  */
+	[StructLayout (LayoutKind.Sequential)]
+	struct lconv
+	{
+		public string decimal_point;
+		public string thousands_sep;		
+		public string grouping;
+		public string int_curr_symbol;
+		public string currency_symbol;
+		public string mon_decimal_point;
+		public string mon_thousands_sep;
+		public string mon_grouping;
+		public string positive_sign;
+		public string negative_sign;
+		char int_frac_digits;
+		char frac_digits;
+		char p_cs_precedes;
+		char p_sep_by_space;
+		char n_cs_precedes;
+		char n_sep_by_space;
+		char p_sign_posn;
+		char n_sign_posn;
+		char int_p_cs_precedes;
+		char int_p_sep_by_space;
+		char int_n_cs_precedes;
+		char int_n_sep_by_space;
+		char int_p_sign_posn;
+		char int_n_sign_posn;
+	}
+
+	// Mono supports less locales that Unix systems
+	// To overcome this limitation we setup the right locale parameters
+	// when the Mono locale is InvariantCulture, that is, when the user's locale
+	// has not been identified and the default Mono locale is used
+	//
+	// See: https://bugzilla.novell.com/show_bug.cgi?id=420468
+	// 
+	static public void FixLocaleInfo ()
+	{
+		IntPtr st = IntPtr.Zero;
+		lconv lv;
+		int platform = (int) Environment.OSVersion.Platform;
+		
+		if (platform != 4 && platform != 128) // Only in Unix based systems
+			return;
+
+		if (CultureInfo.CurrentCulture != CultureInfo.InvariantCulture) // Culture well supported
+			return;
+
+		try {
+			st = localeconv ();
+			if (st == IntPtr.Zero) return;
+
+			lv = (lconv) Marshal.PtrToStructure (st, typeof (lconv));
+			CultureInfo culture =  (CultureInfo) CultureInfo.CurrentCulture.Clone ();
+			culture.NumberFormat.NumberDecimalSeparator = lv.decimal_point;
+			Thread.CurrentThread.CurrentCulture = culture;
+		}
+		catch (Exception) {}
+	}
+
+	public static void SetProcessName (string name)
+	{
+		int platform = (int) Environment.OSVersion.Platform;		
+		if (platform != 4 && platform != 128)
+			return;
+
+		try {
+			if (prctl (15 /* PR_SET_NAME */, Encoding.ASCII.GetBytes (name + "\0"),
+				IntPtr.Zero, IntPtr.Zero, IntPtr.Zero) != 0) {
+				throw new ApplicationException ("Error setting process name: " + 
+					Mono.Unix.Native.Stdlib.GetLastError ());
+			}
+		} catch (EntryPointNotFoundException) {
+			setproctitle (Encoding.ASCII.GetBytes ("%s\0"), 
+				Encoding.ASCII.GetBytes (name + "\0"));
+		}
+	}
+}
diff --git a/src/gbrainy.cs b/src/gbrainy.cs
index df8191c..d7621c3 100644
--- a/src/gbrainy.cs
+++ b/src/gbrainy.cs
@@ -70,7 +70,7 @@ public class gbrainy: Program
 		Gtk.MenuItem item;
 
 		Catalog.Init ("gbrainy", Defines.GNOME_LOCALE_DIR);
-		FixLocaleInfo ();
+		Unix.FixLocaleInfo ();
 
 		IconFactory icon_factory = new IconFactory ();
                 AddIcon (icon_factory, "logic-games", "logic-games-32.png");
@@ -180,70 +180,6 @@ public class gbrainy: Program
 		ActiveInputControls (false);
 	}
 
-	/* Taken from locale.h  */
-	[StructLayout (LayoutKind.Sequential)]
-	public struct lconv
-	{
-		public string decimal_point;
-		public string thousands_sep;		
-		public string grouping;
-		public string int_curr_symbol;
-		public string currency_symbol;
-		public string mon_decimal_point;
-		public string mon_thousands_sep;
-		public string mon_grouping;
-		public string positive_sign;
-		public string negative_sign;
-		char int_frac_digits;
-		char frac_digits;
-		char p_cs_precedes;
-		char p_sep_by_space;
-		char n_cs_precedes;
-		char n_sep_by_space;
-		char p_sign_posn;
-		char n_sign_posn;
-		char int_p_cs_precedes;
-		char int_p_sep_by_space;
-		char int_n_cs_precedes;
-		char int_n_sep_by_space;
-		char int_p_sign_posn;
-		char int_n_sign_posn;
-	}
-
-	[DllImport("libc")]
-	static extern IntPtr localeconv ();
-
-	// Mono supports less locales that Unix systems
-	// To overcome this limitation we setup the right locale parameters
-	// when the Mono locale is InvariantCulture, that is, when the user's locale
-	// has not been identified and the default Mono locale is used
-	//
-	// See: https://bugzilla.novell.com/show_bug.cgi?id=420468
-	// 
-	static void FixLocaleInfo ()
-	{
-		IntPtr st = IntPtr.Zero;
-		lconv lv;
-		int platform = (int) Environment.OSVersion.Platform;
-		
-		if (platform != 4 && platform != 128) // Only in Unix based systems
-			return;
-
-		if (CultureInfo.CurrentCulture != CultureInfo.InvariantCulture) // Culture well supported
-			return;
-
-		try {
-			st = localeconv ();
-			if (st == IntPtr.Zero) return;
-
-			lv = (lconv) Marshal.PtrToStructure (st, typeof (lconv));
-			CultureInfo culture =  (CultureInfo) CultureInfo.CurrentCulture.Clone ();
-			culture.NumberFormat.NumberDecimalSeparator = lv.decimal_point;
-			Thread.CurrentThread.CurrentCulture = culture;
-		}
-		catch (Exception) {}
-	}
-
 	public void UpdateStatusBar ()
 	{
 		statusbar.Push (0, session.StatusText);
@@ -599,34 +535,10 @@ public class gbrainy: Program
 		full_screen = !full_screen;
 	}
 
-	[DllImport ("libc")] // Linux
-	private static extern int prctl (int option, byte [] arg2, IntPtr arg3, IntPtr arg4, IntPtr arg5);
-
-	[DllImport ("libc")] // BSD
-	private static extern void setproctitle (byte [] fmt, byte [] str_arg);
- 
-	public static void SetProcessName (string name)
-	{
-		int platform = (int) Environment.OSVersion.Platform;		
-		if (platform != 4 && platform != 128)
-			return;
-
-		try {
-			if (prctl (15 /* PR_SET_NAME */, Encoding.ASCII.GetBytes (name + "\0"),
-				IntPtr.Zero, IntPtr.Zero, IntPtr.Zero) != 0) {
-				throw new ApplicationException ("Error setting process name: " + 
-					Mono.Unix.Native.Stdlib.GetLastError ());
-			}
-		} catch (EntryPointNotFoundException) {
-			setproctitle (Encoding.ASCII.GetBytes ("%s\0"), 
-				Encoding.ASCII.GetBytes (name + "\0"));
-		}
-	}
-	
 	public static void Main (string [] args) 
 	{
 		try {
-			SetProcessName ("gbrainy");
+			Unix.SetProcessName ("gbrainy");
 		} catch {}
 
 		gbrainy gui = new gbrainy (args);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]