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