2004-11-18 Lluis Sanchez Gual <lluis@novell.com>
[mono.git] / mcs / class / Mono.Posix / Mono.Unix / Catalog.cs
1 //
2 // Mono.Unix.Catalog.cs: Wrappers for the libintl library.
3 //
4 // Author:
5 //   Edd Dumbill (edd@usefulinc.com)
6 //
7 // (C) 2004 Edd Dumbill
8 //
9 // This file implements the low-level syscall interface to the POSIX
10 // subsystem.
11 //
12 // This file tries to stay close to the low-level API as much as possible
13 // using enumerations, structures and in a few cases, using existing .NET
14 // data types.
15 //
16 // Implementation notes:
17 //
18 //    Since the values for the various constants on the API changes
19 //    from system to system (even Linux on different architectures will
20 //    have different values), we define our own set of values, and we
21 //    use a set of C helper routines to map from the constants we define
22 //    to the values of the native OS.
23 //
24 //    Bitfields are flagged with the [Map] attribute, and a helper program
25 //    generates a set of map_XXXX routines that we can call to convert
26 //    from our value definitions to the value definitions expected by the
27 //    OS.
28 //
29 //    Methods that require tuning are bound as `internal syscal_NAME' methods
30 //    and then a `NAME' method is exposed.
31 //
32
33 using System;
34 using System.Runtime.InteropServices;
35
36 namespace Mono.Unix {
37
38         public class Catalog {
39                 [DllImport("libintl")]
40                 static extern IntPtr bindtextdomain (IntPtr domainname, IntPtr dirname);
41                 [DllImport("libintl")]
42                 static extern IntPtr bind_textdomain_codeset (IntPtr domainname,
43                         IntPtr codeset);
44                 [DllImport("libintl")]
45                 static extern IntPtr textdomain (IntPtr domainname);
46                 
47                 public static void Init (String package, String localedir)
48                 {
49                         IntPtr ipackage = Marshal.StringToHGlobalAuto (package);
50                         IntPtr ilocaledir = Marshal.StringToHGlobalAuto (localedir);
51                         IntPtr iutf8 = Marshal.StringToHGlobalAuto ("UTF-8");
52                         bindtextdomain (ipackage, ilocaledir);
53                         bind_textdomain_codeset (ipackage, iutf8);
54                         textdomain (ipackage);
55                         Marshal.FreeHGlobal (ipackage);
56                         Marshal.FreeHGlobal (ilocaledir);
57                         Marshal.FreeHGlobal (iutf8);
58                 }
59         
60                 [DllImport("libintl")]
61                 static extern IntPtr gettext (IntPtr instring);
62                 
63                 public static String GetString (String s)
64                 {
65                         IntPtr ints = Marshal.StringToHGlobalAuto (s);
66                         String t = Marshal.PtrToStringAuto (gettext (ints));
67                         Marshal.FreeHGlobal (ints);
68                         return t;
69                 }
70         
71                 [DllImport("libintl")]
72                 static extern IntPtr ngettext (IntPtr singular, IntPtr plural, Int32 n);
73                 
74                 public static String GetPluralString (String s, String p, Int32 n)
75                 {
76                         IntPtr ints = Marshal.StringToHGlobalAuto (s);
77                         IntPtr intp = Marshal.StringToHGlobalAuto (p);
78                         String t = Marshal.PtrToStringAnsi (ngettext (ints, intp, n));
79                         Marshal.FreeHGlobal (ints);
80                         Marshal.FreeHGlobal (intp);
81                         return t;
82                 }
83         
84         }
85 }