summaryrefslogtreecommitdiff
path: root/LedgerSMB/Inifile.pm
blob: f3683cabf473d40c9d71de08bf1b5fba3b36aaa2 (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. # http://sourceforge.net/projects/ledger-smb/
  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 NOT 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." if ref($type);
  39. $type = ref($type) || $type;
  40. my $self = bless {}, $type;
  41. $self->add_file($file) if defined $file;
  42. return $self;
  43. }
  44. sub add_file {
  45. my ($self, $file) = @_;
  46. my $id = "";
  47. my %menuorder = ();
  48. for (@{$self->{ORDER}}) { $menuorder{$_} = 1 }
  49. open FH, "$file" or Form->error("$file : $!");
  50. while (<FH>) {
  51. next if /^(#|;|\s)/;
  52. last if /^\./;
  53. chop;
  54. # strip comments
  55. s/\s*(#|;).*//g;
  56. # remove any trailing whitespace
  57. s/^\s*(.*?)\s*$/$1/;
  58. if (/^\[/) {
  59. s/(\[|\])//g;
  60. $id = $_;
  61. push @{$self->{ORDER}}, $_ if ! $menuorder{$_};
  62. $menuorder{$_} = 1;
  63. next;
  64. }
  65. # add key=value to $id
  66. my ($key, $value) = split /=/, $_, 2;
  67. $self->{$id}{$key} = $value;
  68. }
  69. close FH;
  70. }
  71. 1;