summaryrefslogtreecommitdiff
path: root/LedgerSMB/Inifile.pm
blob: 0b5055df0ef736bae95604365e18eed07dcd4421 (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. #
  5. # See COPYRIGHT file for copyright information
  6. #======================================================================
  7. #
  8. # This file has NOT undergone whitespace cleanup.
  9. #
  10. #======================================================================
  11. #
  12. # routines to retrieve / manipulate win ini style files
  13. # ORDER is used to keep the elements in the order they appear in .ini
  14. #
  15. #=====================================================================
  16. package Inifile;
  17. sub new {
  18. my ($type, $file) = @_;
  19. warn "$type has no copy constructor! creating a new object." if ref($type);
  20. $type = ref($type) || $type;
  21. my $self = bless {}, $type;
  22. $self->add_file($file) if defined $file;
  23. return $self;
  24. }
  25. sub add_file {
  26. my ($self, $file) = @_;
  27. my $id = "";
  28. my %menuorder = ();
  29. for (@{$self->{ORDER}}) { $menuorder{$_} = 1 }
  30. open FH, "$file" or Form->error("$file : $!");
  31. while (<FH>) {
  32. next if /^(#|;|\s)/;
  33. last if /^\./;
  34. chop;
  35. # strip comments
  36. s/\s*(#|;).*//g;
  37. # remove any trailing whitespace
  38. s/^\s*(.*?)\s*$/$1/;
  39. if (/^\[/) {
  40. s/(\[|\])//g;
  41. $id = $_;
  42. push @{$self->{ORDER}}, $_ if ! $menuorder{$_};
  43. $menuorder{$_} = 1;
  44. next;
  45. }
  46. # add key=value to $id
  47. my ($key, $value) = split /=/, $_, 2;
  48. $self->{$id}{$key} = $value;
  49. }
  50. close FH;
  51. }
  52. 1;