summaryrefslogtreecommitdiff
path: root/LedgerSMB/Inifile.pm
blob: 9922c63cf9f0e0b941269a283501528d7ed7c31b (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. # http://www.ledgersmb.org/
  5. #
  6. # Copyright (C) 2006
  7. # This work contains copyrighted information from a number of sources all used
  8. # with permission.
  9. #
  10. # This file contains source code included with or based on SQL-Ledger which
  11. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  12. # under the GNU General Public License version 2 or, at your option, any later
  13. # version. For a full list including contact information of contributors,
  14. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  15. #
  16. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  17. # Copyright (C) 2002
  18. #
  19. # Author: DWS Systems Inc.
  20. # Web: http://www.sql-ledger.org
  21. #
  22. # Contributors:
  23. # Tony Fraser <tony@sybaspace.com>
  24. #
  25. #======================================================================
  26. #
  27. # This file has undergone whitespace cleanup.
  28. #
  29. #======================================================================
  30. #
  31. # routines to retrieve / manipulate win ini style files
  32. # ORDER is used to keep the elements in the order they appear in .ini
  33. #
  34. #=====================================================================
  35. package Inifile;
  36. sub new {
  37. my ($type, $file) = @_;
  38. warn "$type has no copy constructor! creating a new object."
  39. if ref($type);
  40. $type = ref($type) || $type;
  41. my $self = bless {}, $type;
  42. $self->add_file($file) if defined $file;
  43. return $self;
  44. }
  45. sub add_file {
  46. my ($self, $file) = @_;
  47. my $id = "";
  48. my %menuorder = ();
  49. for (@{$self->{ORDER}}) { $menuorder{$_} = 1 }
  50. open FH, "$file" or Form->error("$file : $!");
  51. while (<FH>) {
  52. next if /^(#|;|\s)/;
  53. last if /^\./;
  54. chop;
  55. # strip comments
  56. s/\s*(#|;).*//g;
  57. # remove any trailing whitespace
  58. s/^\s*(.*?)\s*$/$1/;
  59. if (/^\[/) {
  60. s/(\[|\])//g;
  61. $id = $_;
  62. push @{$self->{ORDER}}, $_ if ! $menuorder{$_};
  63. $menuorder{$_} = 1;
  64. next;
  65. }
  66. # add key=value to $id
  67. my ($key, $value) = split /=/, $_, 2;
  68. $self->{$id}{$key} = $value;
  69. }
  70. close FH;
  71. }
  72. 1;