File: write-csharp.c

package info (click to toggle)
gettext 0.19.8.1-9
  • links: PTS
  • area: main
  • in suites: buster
  • size: 108,976 kB
  • sloc: ansic: 388,375; sh: 51,026; makefile: 8,530; perl: 4,181; lisp: 3,413; yacc: 1,069; cpp: 673; java: 613; cs: 578; sed: 369; objc: 337; awk: 80; tcl: 63; xml: 27; pascal: 11; php: 8
file content (786 lines) | stat: -rw-r--r-- 25,369 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/* Writing C# satellite assemblies.
   Copyright (C) 2003-2010, 2015-2016 Free Software Foundation, Inc.
   Written by Bruno Haible <bruno@clisp.org>, 2003.

   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 3 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, see <http://www.gnu.org/licenses/>.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <alloca.h>

/* Specification.  */
#include "write-csharp.h"

#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <sys/stat.h>
#if !defined S_ISDIR && defined S_IFDIR
# define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
#endif
#if !S_IRUSR && S_IREAD
# define S_IRUSR S_IREAD
#endif
#if !S_IRUSR
# define S_IRUSR 00400
#endif
#if !S_IWUSR && S_IWRITE
# define S_IWUSR S_IWRITE
#endif
#if !S_IWUSR
# define S_IWUSR 00200
#endif
#if !S_IXUSR && S_IEXEC
# define S_IXUSR S_IEXEC
#endif
#if !S_IXUSR
# define S_IXUSR 00100
#endif
#if !S_IRGRP
# define S_IRGRP (S_IRUSR >> 3)
#endif
#if !S_IWGRP
# define S_IWGRP (S_IWUSR >> 3)
#endif
#if !S_IXGRP
# define S_IXGRP (S_IXUSR >> 3)
#endif
#if !S_IROTH
# define S_IROTH (S_IRUSR >> 6)
#endif
#if !S_IWOTH
# define S_IWOTH (S_IWUSR >> 6)
#endif
#if !S_IXOTH
# define S_IXOTH (S_IXUSR >> 6)
#endif

#include "c-ctype.h"
#include "relocatable.h"
#include "error.h"
#include "xerror.h"
#include "csharpcomp.h"
#include "message.h"
#include "msgfmt.h"
#include "msgl-iconv.h"
#include "msgl-header.h"
#include "plural-exp.h"
#include "po-charset.h"
#include "xalloc.h"
#include "xmalloca.h"
#include "concat-filename.h"
#include "fwriteerror.h"
#include "clean-temp.h"
#include "unistr.h"
#include "gettext.h"

#define _(str) gettext (str)


/* Convert a resource name to a class name.
   Return a nonempty string consisting of alphanumerics and underscores
   and starting with a letter or underscore.  */
static char *
construct_class_name (const char *resource_name)
{
  /* This code must be kept consistent with intl.cs, function
     GettextResourceManager.ConstructClassName.  */
  /* We could just return an arbitrary fixed class name, like "Messages",
     assuming that every assembly will only ever contain one
     GettextResourceSet subclass, but this assumption would break the day
     we want to support multi-domain PO files in the same format...  */
  bool valid;
  const char *p;

  /* Test for a valid ASCII identifier:
     - nonempty,
     - first character is A..Za..z_ - see x-csharp.c:is_identifier_start.
     - next characters are A..Za..z_0..9 - see x-csharp.c:is_identifier_part.
   */
  valid = (resource_name[0] != '\0');
  for (p = resource_name; valid && *p != '\0'; p++)
    {
      char c = *p;
      if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c == '_')
            || (p > resource_name && c >= '0' && c <= '9')))
        valid = false;
    }
  if (valid)
    return xstrdup (resource_name);
  else
    {
      static const char hexdigit[] = "0123456789abcdef";
      const char *str = resource_name;
      const char *str_limit = str + strlen (str);
      char *class_name = XNMALLOC (12 + 6 * (str_limit - str) + 1, char);
      char *b;

      b = class_name;
      memcpy (b, "__UESCAPED__", 12); b += 12;
      while (str < str_limit)
        {
          ucs4_t uc;
          str += u8_mbtouc (&uc, (const unsigned char *) str, str_limit - str);
          if (uc >= 0x10000)
            {
              *b++ = '_';
              *b++ = 'U';
              *b++ = hexdigit[(uc >> 28) & 0x0f];
              *b++ = hexdigit[(uc >> 24) & 0x0f];
              *b++ = hexdigit[(uc >> 20) & 0x0f];
              *b++ = hexdigit[(uc >> 16) & 0x0f];
              *b++ = hexdigit[(uc >> 12) & 0x0f];
              *b++ = hexdigit[(uc >> 8) & 0x0f];
              *b++ = hexdigit[(uc >> 4) & 0x0f];
              *b++ = hexdigit[uc & 0x0f];
            }
          else if (!((uc >= 'A' && uc <= 'Z') || (uc >= 'a' && uc <= 'z')
                     || (uc >= '0' && uc <= '9')))
            {
              *b++ = '_';
              *b++ = 'u';
              *b++ = hexdigit[(uc >> 12) & 0x0f];
              *b++ = hexdigit[(uc >> 8) & 0x0f];
              *b++ = hexdigit[(uc >> 4) & 0x0f];
              *b++ = hexdigit[uc & 0x0f];
            }
          else
            *b++ = uc;
        }
      *b++ = '\0';
      return (char *) xrealloc (class_name, b - class_name);
    }
}


/* Write a string in C# Unicode notation to the given stream.  */
static void
write_csharp_string (FILE *stream, const char *str)
{
  static const char hexdigit[] = "0123456789abcdef";
  const char *str_limit = str + strlen (str);

  fprintf (stream, "\"");
  while (str < str_limit)
    {
      ucs4_t uc;
      str += u8_mbtouc (&uc, (const unsigned char *) str, str_limit - str);
      if (uc == 0x0000)
        fprintf (stream, "\\0");
      else if (uc == 0x0007)
        fprintf (stream, "\\a");
      else if (uc == 0x0008)
        fprintf (stream, "\\b");
      else if (uc == 0x0009)
        fprintf (stream, "\\t");
      else if (uc == 0x000a)
        fprintf (stream, "\\n");
      else if (uc == 0x000b)
        fprintf (stream, "\\v");
      else if (uc == 0x000c)
        fprintf (stream, "\\f");
      else if (uc == 0x000d)
        fprintf (stream, "\\r");
      else if (uc == 0x0022)
        fprintf (stream, "\\\"");
      else if (uc == 0x005c)
        fprintf (stream, "\\\\");
      else if (uc >= 0x0020 && uc < 0x007f)
        fprintf (stream, "%c", (int) uc);
      else if (uc < 0x10000)
        fprintf (stream, "\\u%c%c%c%c",
                 hexdigit[(uc >> 12) & 0x0f], hexdigit[(uc >> 8) & 0x0f],
                 hexdigit[(uc >> 4) & 0x0f], hexdigit[uc & 0x0f]);
      else
        fprintf (stream, "\\U%c%c%c%c%c%c%c%c",
                 hexdigit[(uc >> 28) & 0x0f], hexdigit[(uc >> 24) & 0x0f],
                 hexdigit[(uc >> 20) & 0x0f], hexdigit[(uc >> 16) & 0x0f],
                 hexdigit[(uc >> 12) & 0x0f], hexdigit[(uc >> 8) & 0x0f],
                 hexdigit[(uc >> 4) & 0x0f], hexdigit[uc & 0x0f]);
    }
  fprintf (stream, "\"");
}


/* Write a (msgctxt, msgid) pair as a string in C# Unicode notation to the
   given stream.  */
static void
write_csharp_msgid (FILE *stream, message_ty *mp)
{
  const char *msgctxt = mp->msgctxt;
  const char *msgid = mp->msgid;

  if (msgctxt == NULL)
    write_csharp_string (stream, msgid);
  else
    {
      size_t msgctxt_len = strlen (msgctxt);
      size_t msgid_len = strlen (msgid);
      size_t combined_len = msgctxt_len + 1 + msgid_len;
      char *combined;

      combined = (char *) xmalloca (combined_len);
      memcpy (combined, msgctxt, msgctxt_len);
      combined[msgctxt_len] = MSGCTXT_SEPARATOR;
      memcpy (combined + msgctxt_len + 1, msgid, msgid_len + 1);

      write_csharp_string (stream, combined);

      freea (combined);
    }
}


/* Write C# code that returns the value for a message.  If the message
   has plural forms, it is an expression of type System.String[], otherwise it
   is an expression of type System.String.  */
static void
write_csharp_msgstr (FILE *stream, message_ty *mp)
{
  if (mp->msgid_plural != NULL)
    {
      bool first;
      const char *p;

      fprintf (stream, "new System.String[] { ");
      for (p = mp->msgstr, first = true;
           p < mp->msgstr + mp->msgstr_len;
           p += strlen (p) + 1, first = false)
        {
          if (!first)
            fprintf (stream, ", ");
          write_csharp_string (stream, p);
        }
      fprintf (stream, " }");
    }
  else
    {
      if (mp->msgstr_len != strlen (mp->msgstr) + 1)
        abort ();

      write_csharp_string (stream, mp->msgstr);
    }
}


/* Tests whether a plural expression, evaluated according to the C rules,
   can only produce the values 0 and 1.  */
static bool
is_expression_boolean (struct expression *exp)
{
  switch (exp->operation)
    {
    case var:
    case mult:
    case divide:
    case module:
    case plus:
    case minus:
      return false;
    case lnot:
    case less_than:
    case greater_than:
    case less_or_equal:
    case greater_or_equal:
    case equal:
    case not_equal:
    case land:
    case lor:
      return true;
    case num:
      return (exp->val.num == 0 || exp->val.num == 1);
    case qmop:
      return is_expression_boolean (exp->val.args[1])
             && is_expression_boolean (exp->val.args[2]);
    default:
      abort ();
    }
}


/* Write C# code that evaluates a plural expression according to the C rules.
   The variable is called 'n'.  */
static void
write_csharp_expression (FILE *stream, const struct expression *exp, bool as_boolean)
{
  /* We use parentheses everywhere.  This frees us from tracking the priority
     of arithmetic operators.  */
  if (as_boolean)
    {
      /* Emit a C# expression of type 'bool'.  */
      switch (exp->operation)
        {
        case num:
          fprintf (stream, "%s", exp->val.num ? "true" : "false");
          return;
        case lnot:
          fprintf (stream, "(!");
          write_csharp_expression (stream, exp->val.args[0], true);
          fprintf (stream, ")");
          return;
        case less_than:
          fprintf (stream, "(");
          write_csharp_expression (stream, exp->val.args[0], false);
          fprintf (stream, " < ");
          write_csharp_expression (stream, exp->val.args[1], false);
          fprintf (stream, ")");
          return;
        case greater_than:
          fprintf (stream, "(");
          write_csharp_expression (stream, exp->val.args[0], false);
          fprintf (stream, " > ");
          write_csharp_expression (stream, exp->val.args[1], false);
          fprintf (stream, ")");
          return;
        case less_or_equal:
          fprintf (stream, "(");
          write_csharp_expression (stream, exp->val.args[0], false);
          fprintf (stream, " <= ");
          write_csharp_expression (stream, exp->val.args[1], false);
          fprintf (stream, ")");
          return;
        case greater_or_equal:
          fprintf (stream, "(");
          write_csharp_expression (stream, exp->val.args[0], false);
          fprintf (stream, " >= ");
          write_csharp_expression (stream, exp->val.args[1], false);
          fprintf (stream, ")");
          return;
        case equal:
          fprintf (stream, "(");
          write_csharp_expression (stream, exp->val.args[0], false);
          fprintf (stream, " == ");
          write_csharp_expression (stream, exp->val.args[1], false);
          fprintf (stream, ")");
          return;
        case not_equal:
          fprintf (stream, "(");
          write_csharp_expression (stream, exp->val.args[0], false);
          fprintf (stream, " != ");
          write_csharp_expression (stream, exp->val.args[1], false);
          fprintf (stream, ")");
          return;
        case land:
          fprintf (stream, "(");
          write_csharp_expression (stream, exp->val.args[0], true);
          fprintf (stream, " && ");
          write_csharp_expression (stream, exp->val.args[1], true);
          fprintf (stream, ")");
          return;
        case lor:
          fprintf (stream, "(");
          write_csharp_expression (stream, exp->val.args[0], true);
          fprintf (stream, " || ");
          write_csharp_expression (stream, exp->val.args[1], true);
          fprintf (stream, ")");
          return;
        case qmop:
          if (is_expression_boolean (exp->val.args[1])
              && is_expression_boolean (exp->val.args[2]))
            {
              fprintf (stream, "(");
              write_csharp_expression (stream, exp->val.args[0], true);
              fprintf (stream, " ? ");
              write_csharp_expression (stream, exp->val.args[1], true);
              fprintf (stream, " : ");
              write_csharp_expression (stream, exp->val.args[2], true);
              fprintf (stream, ")");
              return;
            }
          /*FALLTHROUGH*/
        case var:
        case mult:
        case divide:
        case module:
        case plus:
        case minus:
          fprintf (stream, "(");
          write_csharp_expression (stream, exp, false);
          fprintf (stream, " != 0)");
          return;
        default:
          abort ();
        }
    }
  else
    {
      /* Emit a C# expression of type 'long'.  */
      switch (exp->operation)
        {
        case var:
          fprintf (stream, "n");
          return;
        case num:
          fprintf (stream, "%lu", exp->val.num);
          return;
        case mult:
          fprintf (stream, "(");
          write_csharp_expression (stream, exp->val.args[0], false);
          fprintf (stream, " * ");
          write_csharp_expression (stream, exp->val.args[1], false);
          fprintf (stream, ")");
          return;
        case divide:
          fprintf (stream, "(");
          write_csharp_expression (stream, exp->val.args[0], false);
          fprintf (stream, " / ");
          write_csharp_expression (stream, exp->val.args[1], false);
          fprintf (stream, ")");
          return;
        case module:
          fprintf (stream, "(");
          write_csharp_expression (stream, exp->val.args[0], false);
          fprintf (stream, " %% ");
          write_csharp_expression (stream, exp->val.args[1], false);
          fprintf (stream, ")");
          return;
        case plus:
          fprintf (stream, "(");
          write_csharp_expression (stream, exp->val.args[0], false);
          fprintf (stream, " + ");
          write_csharp_expression (stream, exp->val.args[1], false);
          fprintf (stream, ")");
          return;
        case minus:
          fprintf (stream, "(");
          write_csharp_expression (stream, exp->val.args[0], false);
          fprintf (stream, " - ");
          write_csharp_expression (stream, exp->val.args[1], false);
          fprintf (stream, ")");
          return;
        case qmop:
          fprintf (stream, "(");
          write_csharp_expression (stream, exp->val.args[0], true);
          fprintf (stream, " ? ");
          write_csharp_expression (stream, exp->val.args[1], false);
          fprintf (stream, " : ");
          write_csharp_expression (stream, exp->val.args[2], false);
          fprintf (stream, ")");
          return;
        case lnot:
        case less_than:
        case greater_than:
        case less_or_equal:
        case greater_or_equal:
        case equal:
        case not_equal:
        case land:
        case lor:
          fprintf (stream, "(");
          write_csharp_expression (stream, exp, true);
          fprintf (stream, " ? 1 : 0)");
          return;
        default:
          abort ();
        }
    }
}


/* Write the C# code for the GettextResourceSet subclass to the given stream.
   Note that we use fully qualified class names and no "using" statements,
   because applications can have their own classes called X.Y.Hashtable or
   X.Y.String.  */
static void
write_csharp_code (FILE *stream, const char *culture_name, const char *class_name, message_list_ty *mlp)
{
  const char *last_dot;
  const char *class_name_last_part;
  unsigned int plurals;
  size_t j;

  fprintf (stream,
           "/* Automatically generated by GNU msgfmt.  Do not modify!  */\n");

  /* We have to use a "using" statement here, to avoid a bug in the pnet-0.6.0
     compiler.  */
  fprintf (stream, "using GNU.Gettext;\n");

  /* Assign a strong name to the assembly, so that two different localizations
     of the same domain can be loaded one after the other.  This strong name
     tells the Global Assembly Cache that they are meant to be different.  */
  fprintf (stream, "[assembly: System.Reflection.AssemblyCulture(");
  write_csharp_string (stream, culture_name);
  fprintf (stream, ")]\n");

  last_dot = strrchr (class_name, '.');
  if (last_dot != NULL)
    {
      fprintf (stream, "namespace ");
      fwrite (class_name, 1, last_dot - class_name, stream);
      fprintf (stream, " {\n");
      class_name_last_part = last_dot + 1;
    }
  else
    class_name_last_part = class_name;
  fprintf (stream, "public class %s : GettextResourceSet {\n",
           class_name_last_part);

  /* Determine whether there are plural messages.  */
  plurals = 0;
  for (j = 0; j < mlp->nitems; j++)
    if (mlp->item[j]->msgid_plural != NULL)
      plurals++;

  /* Emit the constructor.  */
  fprintf (stream, "  public %s ()\n", class_name_last_part);
  fprintf (stream, "    : base () {\n");
  fprintf (stream, "  }\n");

  /* Emit the TableInitialized field.  */
  fprintf (stream, "  private bool TableInitialized;\n");

  /* Emit the ReadResources method.  */
  fprintf (stream, "  protected override void ReadResources () {\n");
  /* In some implementations, such as mono < 2009-02-27, the ReadResources
     method is called just once, when Table == null.  In other implementations,
     such as mono >= 2009-02-27, it is called at every GetObject call, and it
     is responsible for doing the initialization only once, even when called
     simultaneously from multiple threads.  */
  fprintf (stream, "    if (!TableInitialized) {\n");
  fprintf (stream, "      lock (this) {\n");
  fprintf (stream, "        if (!TableInitialized) {\n");
  /* In some implementations, the ResourceSet constructor initializes Table
     before calling ReadResources().  In other implementations, the
     ReadResources() method is expected to initialize the Table.  */
  fprintf (stream, "          if (Table == null)\n");
  fprintf (stream, "            Table = new System.Collections.Hashtable();\n");
  fprintf (stream, "          System.Collections.Hashtable t = Table;\n");
  for (j = 0; j < mlp->nitems; j++)
    {
      fprintf (stream, "          t.Add(");
      write_csharp_msgid (stream, mlp->item[j]);
      fprintf (stream, ",");
      write_csharp_msgstr (stream, mlp->item[j]);
      fprintf (stream, ");\n");
    }
  fprintf (stream, "          TableInitialized = true;\n");
  fprintf (stream, "        }\n");
  fprintf (stream, "      }\n");
  fprintf (stream, "    }\n");
  fprintf (stream, "  }\n");

  /* Emit the msgid_plural strings.  Only used by msgunfmt.  */
  if (plurals)
    {
      fprintf (stream, "  public static System.Collections.Hashtable GetMsgidPluralTable () {\n");
      fprintf (stream, "    System.Collections.Hashtable t = new System.Collections.Hashtable();\n");
      for (j = 0; j < mlp->nitems; j++)
        if (mlp->item[j]->msgid_plural != NULL)
          {
            fprintf (stream, "    t.Add(");
            write_csharp_msgid (stream, mlp->item[j]);
            fprintf (stream, ",");
            write_csharp_string (stream, mlp->item[j]->msgid_plural);
            fprintf (stream, ");\n");
          }
      fprintf (stream, "    return t;\n");
      fprintf (stream, "  }\n");
    }

  /* Emit the PluralEval function.  It is a subroutine for GetPluralString.  */
  if (plurals)
    {
      message_ty *header_entry;
      const struct expression *plural;
      unsigned long int nplurals;

      header_entry = message_list_search (mlp, NULL, "");
      extract_plural_expression (header_entry ? header_entry->msgstr : NULL,
                                 &plural, &nplurals);

      fprintf (stream, "  protected override long PluralEval (long n) {\n");
      fprintf (stream, "    return ");
      write_csharp_expression (stream, plural, false);
      fprintf (stream, ";\n");
      fprintf (stream, "  }\n");
    }

  /* Terminate the class.  */
  fprintf (stream, "}\n");

  if (last_dot != NULL)
    /* Terminate the namespace.  */
    fprintf (stream, "}\n");
}


int
msgdomain_write_csharp (message_list_ty *mlp, const char *canon_encoding,
                        const char *resource_name, const char *locale_name,
                        const char *directory)
{
  int retval;
  struct temp_dir *tmpdir;
  char *culture_name;
  char *output_file;
  char *class_name;
  char *csharp_file_name;
  FILE *csharp_file;
  const char *gettextlibdir;
  const char *csharp_sources[1];
  const char *libdirs[1];
  const char *libraries[1];

  /* If no entry for this resource/domain, don't even create the file.  */
  if (mlp->nitems == 0)
    return 0;

  retval = 1;

  /* Convert the messages to Unicode.  */
  iconv_message_list (mlp, canon_encoding, po_charset_utf8, NULL);

  message_list_delete_header_field (mlp, "POT-Creation-Date:");
  
  /* Create a temporary directory where we can put the C# file.
     A simple temporary file would also be possible but would require us to
     define our own variant of mkstemp(): On one hand the functions mktemp(),
     tmpnam(), tempnam() present a security risk, and on the other hand the
     function mkstemp() doesn't allow to specify a fixed suffix of the file.
     It is simpler to create a temporary directory.  */
  tmpdir = create_temp_dir ("msg", NULL, false);
  if (tmpdir == NULL)
    goto quit1;

  /* Assign a default value to the resource name.  */
  if (resource_name == NULL)
    resource_name = "Messages";

  /* Convert the locale name to a .NET specific culture name.  */
  culture_name = xstrdup (locale_name);
  {
    char *p;
    for (p = culture_name; *p != '\0'; p++)
      if (*p == '_')
        *p = '-';
    if (strncmp (culture_name, "sr-CS", 5) == 0)
      memcpy (culture_name, "sr-SP", 5);
    p = strchr (culture_name, '@');
    if (p != NULL)
      {
        if (strcmp (p, "@latin") == 0)
          strcpy (p, "-Latn");
        else if (strcmp (p, "@cyrillic") == 0)
          strcpy (p, "-Cyrl");
      }
    if (strcmp (culture_name, "sr-SP") == 0)
      {
        free (culture_name);
        culture_name = xstrdup ("sr-SP-Latn");
      }
    else if (strcmp (culture_name, "uz-UZ") == 0)
      {
        free (culture_name);
        culture_name = xstrdup ("uz-UZ-Latn");
      }
  }

  /* Compute the output file name.  This code must be kept consistent with
     intl.cs, function GetSatelliteAssembly().  */
  {
    char *output_dir = xconcatenated_filename (directory, culture_name, NULL);
    struct stat statbuf;

    /* Try to create the output directory if it does not yet exist.  */
    if (stat (output_dir, &statbuf) < 0 && errno == ENOENT)
      if (mkdir (output_dir, S_IRUSR | S_IWUSR | S_IXUSR
                             | S_IRGRP | S_IWGRP | S_IXGRP
                             | S_IROTH | S_IWOTH | S_IXOTH) < 0)
        {
          error (0, errno, _("failed to create directory \"%s\""), output_dir);
          free (output_dir);
          goto quit2;
        }

    output_file =
      xconcatenated_filename (output_dir, resource_name, ".resources.dll");

    free (output_dir);
  }

  /* Compute the class name.  This code must be kept consistent with intl.cs,
     function InstantiateResourceSet().  */
  {
    char *class_name_part1 = construct_class_name (resource_name);
    char *p;

    class_name =
      XNMALLOC (strlen (class_name_part1) + 1 + strlen (culture_name) + 1, char);
    sprintf (class_name, "%s_%s", class_name_part1, culture_name);
    for (p = class_name + strlen (class_name_part1) + 1; *p != '\0'; p++)
      if (*p == '-')
        *p = '_';
    free (class_name_part1);
  }

  /* Compute the temporary C# file name.  It must end in ".cs", so that
     the C# compiler recognizes that it is C# source code.  */
  csharp_file_name =
    xconcatenated_filename (tmpdir->dir_name, "resset.cs", NULL);

  /* Create the C# file.  */
  register_temp_file (tmpdir, csharp_file_name);
  csharp_file = fopen_temp (csharp_file_name, "w");
  if (csharp_file == NULL)
    {
      error (0, errno, _("failed to create \"%s\""), csharp_file_name);
      unregister_temp_file (tmpdir, csharp_file_name);
      goto quit3;
    }

  write_csharp_code (csharp_file, culture_name, class_name, mlp);

  if (fwriteerror_temp (csharp_file))
    {
      error (0, errno, _("error while writing \"%s\" file"), csharp_file_name);
      goto quit3;
    }

  /* Make it possible to override the .dll location.  This is
     necessary for running the testsuite before "make install".  */
  gettextlibdir = getenv ("GETTEXTCSHARPLIBDIR");
  if (gettextlibdir == NULL || gettextlibdir[0] == '\0')
    gettextlibdir = relocate (LIBDIR);

  /* Compile the C# file to a .dll file.  */
  csharp_sources[0] = csharp_file_name;
  libdirs[0] = gettextlibdir;
  libraries[0] = "GNU.Gettext";
  if (compile_csharp_class (csharp_sources, 1, libdirs, 1, libraries, 1,
                            output_file, true, false, verbose > 0))
    {
      if (!verbose)
        error (0, 0, _("compilation of C# class failed, please try --verbose"));
      else
        error (0, 0, _("compilation of C# class failed"));
      goto quit3;
    }

  retval = 0;

 quit3:
  free (csharp_file_name);
  free (class_name);
  free (output_file);
 quit2:
  free (culture_name);
  cleanup_temp_dir (tmpdir);
 quit1:
  return retval;
}