summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/bibtex/biber/lib/Biber/Config.pm
blob: 297e1f687872a22ae845ea1294ac540ce3a678c5 (plain)
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
package Biber::Config;
#use feature 'unicode_strings';
use Biber::Constants;
use IPC::Cmd qw( can_run run );
use Cwd qw( abs_path );
use Config::General qw( ParseConfig );
use Data::Dump;
use Carp;
use List::AllUtils qw(first);
use Log::Log4perl qw( :no_extra_logdie_message );
my $logger = Log::Log4perl::get_logger('main');

=encoding utf-8


=head1 NAME

Biber::Config - Configuration items which need to be saved across the
                lifetime of a Biber object

  This class contains a static object and static methods to access
  configuration and state data. There are several classes of data in here
  which have separate accessors:

  * Biber options
  * Biblatex options
  * State information used by Biber as it processes entries
  * displaymode date

=cut


# Static (class) data
our $CONFIG;
$CONFIG->{state}{crossrefkeys} = {};
$CONFIG->{state}{seennamehash} = {};
$CONFIG->{state}{keycase} = {};

# namehashcount holds a hash of namehashes and 
# occurences of unique names that generate the hash. For example:
# {AA => { Adams_A => 1, Allport_A => 2 }}
$CONFIG->{state}{namehashcount} = {};

# uniquenamecount holds a hash of lastnames and lastname/initials which point to a list
# of name(hashes) which contain them
$CONFIG->{state}{uniquenamecount} = {};
# Counter for tracking name/year combinations for extrayear
$CONFIG->{state}{seen_nameyear_extrayear} = {};
# Counter for the actual extrayear value
$CONFIG->{state}{seen_extrayear} = {};
# Counter for tracking name/year combinations for extraalpha
$CONFIG->{state}{seen_nameyear_extraalpha} = {};
# Counter for the actual extraalpha value
$CONFIG->{state}{seen_extraalpha} = {};
$CONFIG->{state}{seenkeys} = {};

# Location of the control file
$CONFIG->{state}{control_file_location} = '';

# Data files per section being used by biber
$CONFIG->{state}{datafiles} = [];

=head2 _init

    Reset internal hashes to defaults. This is needed for tests when
    prepare() is used more than once

=cut

sub _init {
  $CONFIG->{options}{biblatex}{PER_ENTRY} = {};
  $CONFIG->{state}{control_file_location} = '';
  $CONFIG->{state}{seennamehash} = {};
  $CONFIG->{state}{crossrefkeys} = {};
  $CONFIG->{state}{namehashcount} = {};
  $CONFIG->{state}{uniquenamecount} = {};
  $CONFIG->{state}{seen_nameyear_extrayear} = {};
  $CONFIG->{state}{seen_extrayear} = {};
  $CONFIG->{state}{seen_nameyear_extraalpha} = {};
  $CONFIG->{state}{seen_extraalpha} = {};
  $CONFIG->{state}{seenkeys} = {};
  $CONFIG->{state}{keycase} = {};
  $CONFIG->{state}{datafiles} = [];

  return;
}

=head2 _initopts

    Initialise default options, optionally with config file as argument

=cut

sub _initopts {
  shift; # class method so don't care about class name
  my $conffile = shift;
  my $noconf = shift;
  my %LOCALCONF = ();

  # For testing, need to be able to force ignore of conf file in case user
  # already has one which interferes with test settings.
  unless ($noconf) {
    # if a config file was given as cmd-line arg, it overrides all other
    # config file locations
    unless ( defined $conffile and -f $conffile ) {
      $conffile = config_file();
    }

    if (defined $conffile) {
      %LOCALCONF = ParseConfig(-LowerCaseNames => 1,
                               -MergeDuplicateBlocks => 1,
                               -AllowMultiOptions => 1,
                               -ConfigFile => $conffile,
                               -UTF8 => 1) or
        $logger->logcarp("Failure to read config file " . $conffile . "\n $@");
    }
  }

  # Config file overrides defaults for biber
  my %BIBER_CONFIG = (%CONFIG_DEFAULT_BIBER, %LOCALCONF);

  # Set options internally from config for biber options
  foreach (keys %BIBER_CONFIG) {
    Biber::Config->setoption($_, $BIBER_CONFIG{$_});
  }

  # Set options internally from config for biblatex options
  foreach (keys %CONFIG_DEFAULT_BIBLATEX) {
    Biber::Config->setblxoption($_, $CONFIG_DEFAULT_BIBLATEX{$_});
  }

  return;
}

=head2 config_file

Returns the full path of the B<Biber> configuration file.
If returns the first file found among:

=over 4

=item * C<biber.conf> in the current directory

=item * C<$HOME/.biber.conf>

=item * C<$ENV{XDG_CONFIG_HOME}/biber/biber.conf>

=item * C<$HOME/Library/biber/biber.conf> (Mac OSX only)

=item * C<$ENV{APPDATA}/biber.conf> (Windows only)

=item * the output of C<kpsewhich biber.conf> (if available on the system).

=back

If no file is found, it returns C<undef>.

=cut

sub config_file {
  my $biberconf;
  if ( -f $BIBER_CONF_NAME ) {
    $biberconf = abs_path($BIBER_CONF_NAME);
  } elsif ( -f File::Spec->catfile($ENV{HOME}, ".$BIBER_CONF_NAME" ) ) {
    $biberconf = File::Spec->catfile($ENV{HOME}, ".$BIBER_CONF_NAME" );
  } elsif ( defined $ENV{XDG_CONFIG_HOME} and
    -f File::Spec->catfile($ENV{XDG_CONFIG_HOME}, "biber", $BIBER_CONF_NAME) ) {
    $biberconf = File::Spec->catfile($ENV{XDG_CONFIG_HOME}, "biber", $BIBER_CONF_NAME);
  } elsif ( $^O =~ /(?:Mac|darwin)/ and
    -f File::Spec->catfile($ENV{HOME}, "Library", "biber", $BIBER_CONF_NAME) ) {
    $biberconf = File::Spec->catfile($ENV{HOME}, "Library", "biber", $BIBER_CONF_NAME);
  } elsif ( $^O =~ /Win/ and
    defined $ENV{APPDATA} and
    -f File::Spec->catfile($ENV{APPDATA}, "biber", $BIBER_CONF_NAME) ) {
    $biberconf = File::Spec->catfile($ENV{APPDATA}, "biber", $BIBER_CONF_NAME);
  } elsif ( can_run('kpsewhich') ) {
    scalar run( command => [ 'kpsewhich', $BIBER_CONF_NAME ],
      verbose => 0,
      buffer => \$biberconf );
  } else {
    $biberconf = undef;
  }
  return $biberconf;
}

##############################
# Biber options static methods
##############################

=head2 postprocess_biber_opts

    Place to postprocess biber options when they have been
    gathered from all the possible places that set them

=cut

sub postprocess_biber_opts {
  shift; # class method so don't care about class name

  # Turn sortcase and sortupper into booleans if they are not already
  # They are not booleans on the command-line/config file so that they
  # mirror biblatex option syntax for users
  if (exists($CONFIG->{options}{biber}{sortcase})) {
    if ($CONFIG->{options}{biber}{sortcase} eq 'true') {
      $CONFIG->{options}{biber}{sortcase} = 1;
    } elsif ($CONFIG->{options}{biber}{sortcase} eq 'false') {
      $CONFIG->{options}{biber}{sortcase} = 0;
    }
    unless ($CONFIG->{options}{biber}{sortcase} eq '1' or
            $CONFIG->{options}{biber}{sortcase} eq '0') {
      $logger->logdie("Invalid value for option 'sortcase'");
    }
  }

  if (exists($CONFIG->{options}{biber}{sortupper})) {
    if ($CONFIG->{options}{biber}{sortupper} eq 'true') {
      $CONFIG->{options}{biber}{sortupper} = 1;
    } elsif ($CONFIG->{options}{biber}{sortupper} eq 'false') {
      $CONFIG->{options}{biber}{sortupper} = 0;
    }
    unless ($CONFIG->{options}{biber}{sortupper} eq '1' or
            $CONFIG->{options}{biber}{sortupper} eq '0') {
      $logger->logdie("Invalid value for option 'sortupper'");
    }
  }
}

=head2 set_structure

    Sets the structure information object

=cut

sub set_structure {
  shift;
  my $obj = shift;
  $CONFIG->{structure} = $obj;
  return;
}

=head2 get_structure

    Gets the structure information object

=cut

sub get_structure {
  shift;
  return $CONFIG->{structure};
}

=head2 set_ctrlfile_path

    Stores the path to the control file

=cut

sub set_ctrlfile_path {
  shift;
  $CONFIG->{control_file_location} = shift;
  return;
}

=head2 get_ctrlfile_path

    Retrieved the path to the control file

=cut

sub get_ctrlfile_path {
  shift;
  return $CONFIG->{control_file_location};
}

=head2 setoption

    Store a Biber config option

=cut

sub setoption {
  shift; # class method so don't care about class name
  my ($opt, $val) = @_;
  $CONFIG->{options}{biber}{$opt} = $val;
  return;
}

=head2 getoption

    Get a Biber option

=cut

sub getoption {
  shift; # class method so don't care about class name
  my $opt = shift;
  return $CONFIG->{options}{biber}{$opt};
}

=head2 setcmdlineoption

    Store a Biber command-line option

=cut

sub setcmdlineoption {
  shift; # class method so don't care about class name
  my ($opt, $val) = @_;
  # Command line options are also options ...
  $CONFIG->{options}{biber}{$opt} = $CONFIG->{cmdlineoptions}{$opt} = $val;
  return;
}

=head2 getcmdlineoption

    Get a Biber command lineoption

=cut

sub getcmdlineoption {
  shift; # class method so don't care about class name
  my $opt = shift;
  return $CONFIG->{cmdlineoptions}{$opt};
}

#################################
# BibLaTeX options static methods
#################################


=head2 setblxoption

    Set a biblatex option on the global or per entry-type scope

=cut

sub setblxoption {
  shift; # class method so don't care about class name
  my ($opt, $val, $scope, $scopeval) = @_;
  if (not defined($scope)) { # global is the default
    if ($CONFIG_SCOPE_BIBLATEX{$opt}->{GLOBAL}) {
      $CONFIG->{options}{biblatex}{GLOBAL}{$opt} = $val;
    }
  }
  else { # Per-type/entry options need to specify type/entry too
    $scopeval = lc($scopeval) if $scope eq 'PER_ENTRY';
    if ($CONFIG_SCOPE_BIBLATEX{$opt}->{$scope}) {
      $CONFIG->{options}{biblatex}{$scope}{$scopeval}{$opt} = $val;
    }
  }
  return;
}

=head2 getblxoption

    Get a biblatex option from the global or per entry-type scope

    getblxoption('option', ['entrytype'], ['citekey'])

    Returns the value of option. In order of decreasing preference, returns:
    1. Biblatex option defined for entry
    2. Biblatex option defined for entry type
    3. Biblatex option defined globally

=cut

sub getblxoption {
  shift; # class method so don't care about class name
  my ($opt, $entrytype, $citekey) = @_;
  if ( defined($citekey) and
       $CONFIG_SCOPE_BIBLATEX{$opt}->{PER_ENTRY} and
       defined $CONFIG->{options}{biblatex}{PER_ENTRY}{lc($citekey)} and
       defined $CONFIG->{options}{biblatex}{PER_ENTRY}{lc($citekey)}{$opt}) {
    return $CONFIG->{options}{biblatex}{PER_ENTRY}{lc($citekey)}{$opt};
  }
  elsif (defined($entrytype) and
         $CONFIG_SCOPE_BIBLATEX{$opt}->{PER_TYPE} and
         defined $CONFIG->{options}{biblatex}{PER_TYPE}{$entrytype} and
         defined $CONFIG->{options}{biblatex}{PER_TYPE}{$entrytype}{$opt}) {
    return $CONFIG->{options}{biblatex}{PER_TYPE}{$entrytype}{$opt};
  }
  elsif ($CONFIG_SCOPE_BIBLATEX{$opt}->{GLOBAL}) {
    return $CONFIG->{options}{biblatex}{GLOBAL}{$opt};
  }
}


##############################
# Biber state static methods
##############################

#============================
#        seenkey
#============================

=head2 get_seenkey

    Get the count of a key

    Biber::Config->get_seenkey($hash);

=cut

sub get_seenkey {
  shift; # class method so don't care about class name
  my $key = shift;
  my $section = shift; # If passed, return count for just this section
  if (defined($section)) {
    return $CONFIG->{state}{seenkeys}{$section}{lc($key)};
  }
  else {
    my $count;
    foreach my $section (keys %{$CONFIG->{state}{seenkeys}}) {
      $count += $CONFIG->{state}{seenkeys}{$section}{lc($key)};
    }
    return $count;
  }
}

=head2 get_keycase

    Return a key in the original case it was cited with so we
    can return mismatched cite key errors

    Biber::Config->get_keycase($key);

=cut

sub get_keycase {
  shift; # class method so don't care about class name
  my $key = shift;
  return $CONFIG->{state}{keycase}{lc($key)};
}

=head2 incr_seenkey

    Increment the seen count of a key

    Biber::Config->incr_seenkey($ay);

=cut

sub incr_seenkey {
  shift; # class method so don't care about class name
  my $key = shift;
  my $section = shift;
  $CONFIG->{state}{keycase}{lc($key)} = $key;
  $CONFIG->{state}{seenkeys}{$section}{lc($key)}++;
  return;
}


=head2 reset_seen_extra

    Reset the counters for extrayear and extraalpha

    Biber::Config->reset_extra;

=cut

sub reset_seen_extra {
  shift; # class method so don't care about class name
  my $ay = shift;
  $CONFIG->{state}{seen_extrayear}= {};
  $CONFIG->{state}{seen_extraalpha}= {};
  return;
}

#============================
#        seen_extrayear
#============================

=head2 incr_seen_extrayear

    Increment and return the counter for extrayear

    Biber::Config->incr_seen_extrayear($ay);

=cut

sub incr_seen_extrayear {
  shift; # class method so don't care about class name
  my $ay = shift;
  return ++$CONFIG->{state}{seen_extrayear}{$ay};
}


#============================
#       seen_nameyear_extrayear
#============================

=head2 get_seen_nameyear_extrayear

    Get the count of an labelname/labelyear combination for tracking
    extrayear. It uses labelyear plus name as we need to disambiguate
    entries with different labelyear (like differentiating 1984--1986 from
    just 1984)

    Biber::Config->get_seen_nameyear_extrayear($ny);

=cut

sub get_seen_nameyear_extrayear {
  shift; # class method so don't care about class name
  my $ny = shift;
  return $CONFIG->{state}{seen_nameyear_extrayear}{$ny};
}

=head2 incr_seen_nameyear_extrayear

    Increment the count of an labelname/labelyear combination for extrayear

    Biber::Config->incr_seen_nameyear_extrayear($ns, $ys);

    We pass in the name and year strings seperately as we have to
    be careful and only increment this counter beyond 1 if there is
    both a name and year component. Otherwise, extrayear gets defined for all
    entries with no name but the same year etc.

=cut

sub incr_seen_nameyear_extrayear {
  shift; # class method so don't care about class name
  my ($ns, $ys) = @_;
  $tmp = "$ns,$ys";
  # We can always increment this to 1
  unless ($CONFIG->{state}{seen_nameyear_extrayear}{$tmp}) {
    $CONFIG->{state}{seen_nameyear_extrayear}{$tmp}++;
  }
  # But beyond that only if we have a labelname and labelyear in the entry since
  # this counter is used to create extrayear which doesn't mean anything for
  # entries with only one of these.
  else {
    if ($ns and $ys) {
      $CONFIG->{state}{seen_nameyear_extrayear}{$tmp}++;
    }
  }
  return;
}

#============================
#        seen_extraalpha
#============================

=head2 incr_seen_extraalpha

    Increment and return the counter for extraalpha

    Biber::Config->incr_seen_extraalpha($ay);

=cut

sub incr_seen_extraalpha {
  shift; # class method so don't care about class name
  my $ay = shift;
  return ++$CONFIG->{state}{seen_extraalpha}{$ay};
}


#============================
#       seen_nameyear_extraalpha
#============================

=head2 get_seen_nameyear_extraalpha

    Get the count of an labelname/labelyear combination for tracking
    extraalpha. It uses labelyear plus name as we need to disambiguate
    entries with different labelyear (like differentiating 1984--1986 from
    just 1984)

    Biber::Config->get_seen_nameyear_extraalpha($ny);

=cut

sub get_seen_nameyear_extraalpha {
  shift; # class method so don't care about class name
  my $ny = shift;
  return $CONFIG->{state}{seen_nameyear_extraalpha}{$ny};
}

=head2 incr_seen_nameyear_extraalpha

    Increment the count of an labelname/labelyear combination for extraalpha

    Biber::Config->incr_seen_nameyear_extraalpha($ns, $ys);

    We pass in the name and year strings seperately as we have to
    be careful and only increment this counter beyond 1 if there is
    both a name and year component. Otherwise, extraalpha gets defined for all
    entries with no name but the same year etc.

=cut

sub incr_seen_nameyear_extraalpha {
  shift; # class method so don't care about class name
  my ($ns, $ys) = @_;
  $tmp = "$ns,$ys";
  # We can always increment this to 1
  unless ($CONFIG->{state}{seen_nameyear_extraalpha}{$tmp}) {
    $CONFIG->{state}{seen_nameyear_extraalpha}{$tmp}++;
  }
  # But beyond that only if we have a labelname and labelyear in the entry since
  # this counter is used to create extraalpha which doesn't mean anything for
  # entries with only one of these.
  else {
    if ($ns and $ys) {
      $CONFIG->{state}{seen_nameyear_extraalpha}{$tmp}++;
    }
  }
  return;
}

#============================
#       uniquenamecount
#============================

=head2 get_numofuniquenames

    Get the number of uniquenames entries for a name

    Biber::Config->get_numofuniquenames($name);

=cut

sub get_numofuniquenames {
  shift; # class method so don't care about class name
  my $name = shift;
  return $#{$CONFIG->{state}{uniquenamecount}{$name}} + 1;
}

=head2 add_uniquenamecount

    Add a hash to the list of name(hashes) which have the name part in it

    Biber::Config->add_uniquenamecount($name, $hash);

=cut

sub add_uniquenamecount {
  shift; # class method so don't care about class name
  my $namestring = shift;
  my $hash = shift;
  # name(hash) already recorded as containing namestring
  if (first {$hash eq $_} @{$CONFIG->{state}{uniquenamecount}{$namestring}}) {
    return;
  }
  # Record name(hash) as containing namestring
  else {
    push @{$CONFIG->{state}{uniquenamecount}{$namestring}}, $hash;
  }
  return;
}


=head2 _get_uniquename

    Get the uniquename hash of a lastname/hash combination
    Mainly for use in tests

    Biber::Config->get_uniquename($name);

=cut

sub _get_uniquename {
  shift; # class method so don't care about class name
  my $name = shift;
  my @list = sort @{$CONFIG->{state}{uniquenamecount}{$name}};
  return \@list;
}


#============================
#       namehashcount
#============================


=head2 get_numofnamehashes

    Get the number of name hashes

    Biber::Config->get_numofnamehashes($hash);

=cut

sub get_numofnamehashes {
  shift; # class method so don't care about class name
  my $hash = shift;
  return scalar keys %{$CONFIG->{state}{namehashcount}{$hash}};
}

=head2 namehashexists

    Check if there is an entry for a namehash

    Biber::Config->namehashexists($hash);

=cut

sub namehashexists {
  shift; # class method so don't care about class name
  my $hash = shift;
  return exists($CONFIG->{state}{namehashcount}{$hash}) ? 1 : 0;
}


=head2 get_namehashcount

    Get the count of a name hash and name id

    Biber::Config->get_namehashcount($hash, $id);

=cut

sub get_namehashcount {
  shift; # class method so don't care about class name
  my ($hash, $id) = @_;
  return $CONFIG->{state}{namehashcount}{$hash}{$id};
}

=head2 set_namehashcount

    Set the count of a name hash and name id

    Biber::Config->set_namehashcount($hash, $id, $num);

=cut

sub set_namehashcount {
  shift; # class method so don't care about class name
  my ($hash, $id, $num) = @_;
  $CONFIG->{state}{namehashcount}{$hash}{$id} = $num;
  return;
}


=head2 del_namehash

    Delete the count information for a name hash

    Biber::Config->del_namehashcount($hash);

=cut

sub del_namehash {
  shift; # class method so don't care about class name
  my $hash = shift;
  if (exists($CONFIG->{state}{namehashcount}{$hash})) {
    delete $CONFIG->{state}{namehashcount}{$hash};
  }
  return;
}

#============================
#       seennamehash
#============================


=head2 get_seennamehash

    Get the count of a seen name hash

    Biber::Config->get_seennamehash($hash);

=cut

sub get_seennamehash {
  shift; # class method so don't care about class name
  my $hash = shift;
  return $CONFIG->{state}{seennamehash}{$hash};
}


=head2 incr_seennamehash

    Increment the count of a seen name hash

    Biber::Config->incr_seennamehash($hash);

=cut

sub incr_seennamehash {
  shift; # class method so don't care about class name
  my $hash = shift;
  $CONFIG->{state}{seennamehash}{$hash}++;
  return;
}


#============================
#       crossrefkeys
#============================


=head2 get_crossrefkeys

    Return ref to array of keys which are crossref targets

    Biber::Config->get_crossrefkeys();

=cut

sub get_crossrefkeys {
  shift; # class method so don't care about class name
  return [ keys %{$CONFIG->{state}{crossrefkeys}} ];
}

=head2 get_crossrefkey

    Return an integer representing the number of times a
    crossref target key has been ref'ed

    Biber::Config->get_crossrefkey($key);

=cut

sub get_crossrefkey {
  shift; # class method so don't care about class name
  my $k = shift;
  return $CONFIG->{state}{crossrefkeys}{lc($k)};
}

=head2 del_crossrefkey

    Remove a crossref target key from the crossrefkeys state

    Biber::Config->del_crossrefkey($key);

=cut

sub del_crossrefkey {
  shift; # class method so don't care about class name
  my $k = shift;
  if (exists($CONFIG->{state}{crossrefkeys}{lc($k)})) {
    delete $CONFIG->{state}{crossrefkeys}{lc($k)};
  }
  return;
}

=head2 incr_crossrefkey

    Increment the crossreferences count for a target crossref key

    Biber::Config->incr_crossrefkey($key);

=cut

sub incr_crossrefkey {
  shift; # class method so don't care about class name
  my $k = shift;
  $CONFIG->{state}{crossrefkeys}{lc($k)}++;
  return;
}


############################
# Displaymode static methods
############################

=head2 set_displaymode

    Set the display mode for a field.
    setdisplaymode(['entrytype'], ['field'], ['citekey'], $value)

    This sets the desired displaymode to use for some data in the bib.
    Of course, this is entirey seperate semantically from the
    displaymodes *defined* in the bib which just tell you what to return
    for a particular displaymode request for some data.

=cut

sub set_displaymode {
  shift; # class method so don't care about class name
  my ($val, $entrytype, $fieldtype, $citekey) = @_;
  if ($citekey) {
    my $key = lc($citekey);
    if ($fieldtype) {
      $CONFIG->{displaymodes}{PER_FIELD}{$key}{$fieldtype} = $val;
    }
    else {
      $CONFIG->{displaymodes}{PER_ENTRY}{$key} = $val;
    }
  }
  elsif ($fieldtype) {
    $CONFIG->{displaymodes}{PER_FIELDTYPE}{$fieldtype} = $val;
  }
  elsif ($entrytype) {
    $CONFIG->{displaymodes}{PER_ENTRYTYPE}{$entrytype} = $val;
  }
  else {
    $CONFIG->{displaymodes}{GLOBAL} = $val ;
  }
}

=head2 get_displaymode

    Get the display mode for a field.
    getdisplaymode(['entrytype'], ['field'], ['citekey'])

    Returns the displaymode. In order of decreasing preference, returns:
    1. Mode defined for a specific field in a specific citekey
    2. Mode defined for a citekey
    3. Mode defined for a fieldtype (any citekey)
    4. Mode defined for an entrytype (any citekey)
    5. Mode defined globally (any citekey)

=cut

sub get_displaymode {
  shift; # class method so don't care about class name
  my ($entrytype, $fieldtype, $citekey) = @_;
  my $dm;
  if ($citekey) {
    my $key = lc($citekey);
    if ($fieldtype and
      defined($CONFIG->{displaymodes}{PER_FIELD}) and
      defined($CONFIG->{displaymodes}{PER_FIELD}{$key}) and
      defined($CONFIG->{displaymodes}{PER_FIELD}{$key}{$fieldtype})) {
      $dm = $CONFIG->{displaymodes}{PER_FIELD}{$key}{$fieldtype};
    }
    elsif (defined($CONFIG->{displaymodes}{PER_ENTRY}) and
      defined($CONFIG->{displaymodes}{PER_ENTRY}{$key})) {
      $dm = $CONFIG->{displaymodes}{PER_ENTRY}{$key};
    }
  }
  elsif ($fieldtype and
    defined($CONFIG->{displaymodes}{PER_FIELDTYPE}) and
    defined($CONFIG->{displaymodes}{PER_FIELDTYPE}{$fieldtype})) {
    $dm = $CONFIG->{displaymodes}{PER_FIELDTYPE}{$fieldtype};
  }
  elsif ($entrytype and
    defined($CONFIG->{displaymodes}{PER_ENTRYTYPE}) and
    defined($CONFIG->{displaymodes}{PER_ENTRYTYPE}{$entrytype})) {
    $dm = $CONFIG->{displaymodes}{PER_ENTRYTYPE}{$entrytype};
  }
  $dm = $CONFIG->{displaymodes}{GLOBAL} unless $dm; # Global if nothing else;
  $dm = $DISPLAYMODE_DEFAULT unless $dm; # fall back to this constant
  if ( ref $dm eq 'ARRAY') {
    return $dm;
  }
  else {
    return $DISPLAYMODES{$dm};
  }
}

=head2 dump

    Dump config information (for debugging)

=cut

sub dump {
  shift; # class method so don't care about class name
  dd($CONFIG);
}

=head1 AUTHORS

François Charette, C<< <firmicus at gmx.net> >>
Philip Kime C<< <philip at kime.org.uk> >>

=head1 BUGS

Please report any bugs or feature requests on our sourceforge tracker at
L<https://sourceforge.net/tracker2/?func=browse&group_id=228270>.

=head1 COPYRIGHT & LICENSE

Copyright 2009-2011 François Charette and Philip Kime, all rights reserved.

This module is free software.  You can redistribute it and/or
modify it under the terms of the Artistic License 2.0.

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.

=cut

1;

# vim: set tabstop=2 shiftwidth=2 expandtab: