File: gen_mods

package info (click to toggle)
liblocale-codes-perl 3.82-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,756 kB
  • sloc: perl: 137,696; sh: 22; makefile: 2
file content (557 lines) | stat: -rwxr-xr-x 15,033 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
#!/usr/bin/perl -w

use strict;
use warnings;

use Cwd;
use IO::File;

################################################################################

our($YEAR,$VERSION);
get_vars();

# $type{TYPE} => TYPE_DESC
#
# TYPE_DESC = { label    => [TYPE_LABEL, ...]
#               modlabel => MOD_LABEL
#               default  => CODESET
#               codesets => { CODESET => CODESET_DESC }
#             }
#
# CODESET_DESC = { label => [CODESET_LABEL, ...]
#                  const => [CONST, ...]
#                  desc  => CODE_DESC
#                }
#
# TYPE          : the type of codes                      country
# TYPE_LABEL    : the label(s) for this type of          COUNTRY
#                 code (defaults to TYPE in all
#                 uppercase and with '-' replaced
#                 with '_')
# MOD_LABEL     : the label for the module name          Country
#                 (defaults to ucfirst(TYPE))
# CODESET       : the name of each codeset               alpha-2
# CODESET_LABEL : the label for this codeset             ALPHA_2
#                 (defaults to CODESET in all
#                 uppercase and with '-' replaced
#                 with '_')
# CONST         : the name of 1 or more constants        LOCALE_CODE_ALPHA_2
#                 that will be associated with this
#                 codeset (defaults to a single
#                 constant for each TYPE_LABEL and
#                 CODESET_LABEL combination of the
#                 form:
#                 LOCALE_<TYPE_LABEL>_<CODESET_LABEL>)
# CODE_DESC     : a listref describing the format        ['numeric',3]
#                 of the codes in this codeset;
#                 possible values are:
#                   uc        : uppercase code
#                   lc        : lowercase code
#                   ucfirst   : code with 1st character
#                               uppercase
#                   numeric,N : an N-digit numeric
#                               code

our %type =
  ('country' => {
                 'label'    => ['CODE','COUNTRY'],
                 'default'  => 'alpha-2',
                 'codesets' => { 'alpha-2'      => { 'desc'  => ['lc'], },
                                 'alpha-3'      => { 'desc'  => ['lc'], },
                                 'numeric'      => { 'desc'  => ['numeric',3], },
                                 'dom'          => { 'desc'  => ['lc'], },
                                 'un-alpha-3'   => { 'desc'  => ['uc'], },
                                 'un-numeric'   => { 'desc'  => ['numeric',3], },
                                 'genc-alpha-2' => { 'desc'  => ['uc'], },
                                 'genc-alpha-3' => { 'desc'  => ['uc'], },
                                 'genc-numeric' => { 'desc'  => ['numeric',3], },
                               },
                },
   'language' => {
                  'label'    => ['LANG','LANGUAGE'],
                  'default'  => 'alpha-2',
                  'codesets' => { 'alpha-2'  => { 'desc'   => ['lc'], },
                                  'alpha-3'  => { 'desc'   => ['lc'], },
                                  'term'     => { 'desc'   => ['lc'], },
                                },
                 },
   'currency' => {
                  'label'    => ['CURR','CURRENCY'],
                  'default'  => 'alpha',
                  'codesets' => { 'alpha'   => { 'desc'   => ['uc'], },
                                  'num'     => { 'label'  => ['NUMERIC'],
                                                 'desc'   => ['numeric',3], },
                                },
                 },
   'script' => {
                'default'  => 'alpha',
                'codesets' => { 'alpha'    => { 'desc'   => ['ucfirst'], },
                                'num'      => { 'label'  => ['NUMERIC'],
                                                'desc'   => ['numeric',3], },
                              },
               },
   'langext' => {
                 'default'  => 'alpha',
                 'modlabel' => 'LangExt',
                 'codesets' => { 'alpha'  => { 'desc'   => ['lc'], },
                               },
                },
   'langvar' => {
                 'default'  => 'alpha',
                 'modlabel' => 'LangVar',
                 'codesets' => { 'alpha'  => { 'desc'   => ['lc'], },
                               },
                },
   'langfam' => {
                 'default'  => 'alpha',
                 'modlabel' => 'LangFam',
                 'codesets' => { 'alpha'  => { 'desc'   => ['lc'], },
                               },
                },
  );

################################################################################

our ($max,$maxc);
gen_constants();
gen_mods();

################################################################################

sub gen_mods {
   foreach my $type (sort keys %type) {
      _gen_mod($type,'');
      _gen_mod($type,'pod');
   }
   foreach my $type (qw(country language currency script)) {
      _gen_mod($type,'',   'old');
      _gen_mod($type,'pod','old');
   }
}

sub _gen_mod {
   my($type,$pod,$old) = @_;
   my $mod   = (exists $type{$type}{'modlabel'}
                ? $type{$type}{'modlabel'}
                : _upcase_first($type) );
   my $dire  = ($old ? "lib/Locale" : "lib/Locale/Codes");
   my $f     = "$dire/$mod." . ($pod ? 'pod' : 'pm');
   my $o     = new IO::File;
   $o->open("> $f");
   if ($pod) {
      __gen_pod($o,$type,$mod,$old);
   } else {
      _header($o,$mod,$old);
      __gen_mod($o,$type,$mod);
   }
   $o->close();
}

sub __gen_pod {
   my($o,$type,$mod,$old) = @_;

   my $parent             = ($old ? "Locale" : "Locale::Codes");

   my $typelab            = ($type{$type}{'modlable'}
                             ? $type{$type}{'modlable'}
                             : ucfirst($type));

   print $o <<"EOS";
=pod

=head1 NAME

${parent}::${mod} - module for dealing with ${type} code sets

=head1 SYNOPSIS

   use ${parent}::${mod};

   \$name = code2${type}(CODE);
   \$code = ${type}2code(NAME);

   \@codes   = all_${type}_codes();
   \@names   = all_${type}_names();

=head1 DESCRIPTION

This module provides access to ${type} code sets.

Please refer to the L<Locale::Codes::Types> document for a description
of the code sets available.

Most of the routines take an optional additional argument which
specifies the code set to use. The code set can be specified using the
name of a code set, or the perl constant specified in the above
document.  If not specified, the default code set will be used.

=head1 ROUTINES

All routines in this module call the appropriate method in the
L<Locale::Codes> module, using an object of type: $type
Please refer to the documentation of the L<Locale::Codes> module
for details about each function.

The following functions are exported automatically:

=over 4

=item B<code2${type}(CODE [,CODESET] [,'retired'])>

See B<code2name> in L<Locale::Codes>

=item B<code2${type}s(CODE [,CODESET])>

See B<code2names> in L<Locale::Codes>

=item B<${type}2code(NAME [,CODESET] [,'retired'])>

See B<name2code> in L<Locale::Codes>

=item B<${type}_code2code(CODE ,CODESET ,CODESET2)>

See B<code2code> in L<Locale::Codes>

=item B<all_${type}_codes([CODESET] [,'retired'])>

See B<all_codes> in L<Locale::Codes>

=item B<all_${type}_names([CODESET] [,'retired'])>

See B<all_names> in L<Locale::Codes>

=back

The following functions are not exported and must be called fully
qualified with the package name:

=over 4

=item B<${parent}::${typelab}::show_errors(FLAG)>

By default, invalid input will produce empty results, but no errors.  By
passing in a non-zero value of FLAG, errors will be produced.

See B<show_errors> in L<Locale::Codes> but note that the default for
the non-OO modules are to NOT produce errors.

=item B<${parent}::${typelab}::rename_${type}(CODE ,NEW_NAME [,CODESET])>

See B<rename_code> in L<Locale::Codes>

=item B<${parent}::${typelab}::add_${type}(CODE ,NAME [,CODESET])>

See B<add_code> in L<Locale::Codes>

=item B<${parent}::${typelab}::delete_${type}(CODE [,CODESET])>

See B<delete_code> in L<Locale::Codes>

=item B<${parent}::${typelab}::add_${type}_alias(NAME ,NEW_NAME)>

See B<add_alias> in L<Locale::Codes>

=item B<${parent}::${typelab}::delete_${type}_alias(NAME)>

See B<delete_alias> in L<Locale::Codes>

=item B<${parent}::${typelab}::rename_${type}_code(CODE ,NEW_CODE [,CODESET])>

See B<replace_code> in L<Locale::Codes>

=item B<${parent}::${typelab}::add_${type}_code_alias(CODE ,NEW_CODE [,CODESET])>

See B<add_code_alias> in L<Locale::Codes>

=item B<${parent}::${typelab}::delete_${type}_code_alias(CODE [,CODESET])>

See B<delete_code_alias> in L<Locale::Codes>

=back

=head1 SEE ALSO

=over 4

=item L<Locale::Codes>

The Locale-Codes distribution.

=back

=head1 AUTHOR

See Locale::Codes for full author history.

Currently maintained by Sullivan Beck (sbeck\@cpan.org).

=head1 COPYRIGHT

   Copyright (c) 2011-2025 Sullivan Beck

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

=cut
EOS
}

sub __gen_mod {
   my($o,$type,$mod) = @_;
   my($TYPE)         = uc($type);

   print $o <<"EOS";
use if \$] >= 5.027007, 'deprecate';
use Locale::Codes;
use Locale::Codes::Constants;

\@EXPORT    = qw(
                code2${type}
                code2${type}s
                ${type}2code
                all_${type}_codes
                all_${type}_names
                ${type}_code2code
               );
push(\@EXPORT,\@Locale::Codes::Constants::CONSTANTS_${TYPE});

our \$obj = new Locale::Codes('$type');
\$obj->show_errors(0);

sub show_errors {
   my(\$val) = \@_;
   \$obj->show_errors(\$val);
}

sub code2${type} {
   return \$obj->code2name(\@_);
}

sub code2${type}s {
   return \$obj->code2names(\@_);
}

sub ${type}2code {
   return \$obj->name2code(\@_);
}

sub ${type}_code2code {
   return \$obj->code2code(\@_);
}

sub all_${type}_codes {
   return \$obj->all_codes(\@_);
}

sub all_${type}_names {
   return \$obj->all_names(\@_);
}

sub rename_${type} {
   return \$obj->rename_code(\@_);
}

sub add_${type} {
   return \$obj->add_code(\@_);
}

sub delete_${type} {
   return \$obj->delete_code(\@_);
}

sub add_${type}_alias {
   return \$obj->add_alias(\@_);
}

sub delete_${type}_alias {
   return \$obj->delete_alias(\@_);
}

sub rename_${type}_code {
   return \$obj->replace_code(\@_);
}

sub add_${type}_code_alias {
   return \$obj->add_code_alias(\@_);
}

sub delete_${type}_code_alias {
   return \$obj->delete_code_alias(\@_);
}

1;
EOS
}

sub get_vars {
   my $dir  = getcwd;
   $dir     =~ /Locale\-Codes\-(\d+\.\d+)/;
   $VERSION = $1;

   $YEAR    = (localtime(time))[5] + 1900;
}

sub gen_constants {
   my $f    = "lib/Locale/Codes/Constants.pm";
   my $o    = new IO::File;
   $o->open("> $f");
   _header($o,"Constants");
   _constants_defs($o);
   $o->close();
}

sub _constants_defs {
   my($o) = @_;

   $max   = 0;
   $maxc  = 0;
   foreach my $type (sort keys %type) {
      my @lab = (exists $type{$type}{'label'}
                 ? @{ $type{$type}{'label'} }
                 : _upcase($type) );
      foreach my $codeset (sort keys %{ $type{$type}{'codesets'} }) {
         my @const;
         if (exists $type{$type}{'codesets'}{$codeset}{'const'} ) {
            @const = @{ $type{$type}{'codesets'}{$codeset}{'const'} };
         } else {
            my @clab = (exists $type{$type}{'codesets'}{$codeset}{'label'}
                        ? @{ $type{$type}{'codesets'}{$codeset}{'label'} }
                        : _upcase($codeset) );
            foreach my $lab (@lab) {
               foreach my $clab (@clab) {
                  push(@const,"LOCALE_${lab}_${clab}");
               }
            }
         }
         foreach my $const (@const) {
            $type{$type}{'const'}{$const} = $codeset;
            if (length($const) > $max) {
               $max = length($const);
            }
         }
         if (length($codeset) > $maxc) {
            $maxc = length($codeset);
         }
      }
   }

   print $o <<"EOS";
our(\@CONSTANTS,\%ALL_CODESETS);

EOS

   foreach my $type (sort keys %type) {
      print $o "our(\@CONSTANTS_" . uc($type) . ") = qw(\n";
      foreach my $const (sort keys %{ $type{$type}{'const'} }) {
         print $o "                $const\n";
      }
      print $o ");\n";
      print $o "push(\@CONSTANTS,\@CONSTANTS_" . uc($type) . ");\n";
      print $o "\n";
   }

   print $o <<"EOS";
\@EXPORT    = (\@CONSTANTS,
               qw(
                \%ALL_CODESETS
               ));

EOS

   foreach my $type (sort keys %type) {
      foreach my $const (sort keys %{ $type{$type}{'const'} }) {
         my $codeset = $type{$type}{'const'}{$const};
         my $spc     = ' 'x($max - length($const));
         print $o "use constant $const$spc => '$codeset';\n"
      }
      my $def = $type{$type}{'default'};
      my $mod = (exists $type{$type}{'modlabel'}
                 ? $type{$type}{'modlabel'}
                 : _upcase_first($type) );
      print $o <<"EOS";

\$ALL_CODESETS{'$type'} =
   {
      'default'  => '$def',
      'module'   => '$mod',
      'codesets' => {
EOS
      foreach my $codeset (sort keys %{ $type{$type}{'codesets'} }) {
         my @desc = @{ $type{$type}{'codesets'}{$codeset}{'desc'} };
         foreach my $d (@desc) {
            if ($d !~ /^\d+$/) {
               $d = "'$d'";
            }
         }
         my $desc = join(',',@desc);
         my $spc  = ' 'x($maxc-length($codeset));
         print $o <<"EOS";
                     '$codeset'$spc => [$desc],
EOS
      }
      print $o <<"EOS";
                    }
   };

EOS
   }
   print $o "\n1;\n";
}

sub _upcase {
   my($string) = @_;
   $string     = uc($string);
   $string     =~ s/\-/_/g;
   return $string;
}

sub _upcase_first {
   my($string) = @_;
   $string     = ucfirst($string);
   $string     =~ s/\-/_/g;
   return $string;
}

sub _header {
   my($o,$package,$old) = @_;

   my $timestamp   = `date`;
   chomp($timestamp);

   my $parent      = ($old ? "Locale" : "Locale::Codes");

   print $o <<"EOS";
package ${parent}::$package;
# Copyright (C) 2001      Canon Research Centre Europe (CRE).
# Copyright (C) 2002-2009 Neil Bowers
# Copyright (c) 2010-$YEAR Sullivan Beck
# This program is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.

# This file was automatically generated.  Any changes to this file will
# be lost the next time 'gen_mods' is run.
#    Generated on: $timestamp

use strict;
use warnings;
require 5.006;
use Exporter qw(import);

our(\$VERSION,\@EXPORT);
\$VERSION   = '$VERSION';

################################################################################
EOS
}

# Local Variables:
# mode: cperl
# indent-tabs-mode: nil
# cperl-indent-level: 3
# cperl-continued-statement-offset: 2
# cperl-continued-brace-offset: 0
# cperl-brace-offset: 0
# cperl-brace-imaginary-offset: 0
# cperl-label-offset: 0
# End: