summaryrefslogtreecommitdiffstats
path: root/ldap/admin/src/Inf.pm
blob: 41d6458a82211a76d679f51cb0f5fc2b6cda1f95 (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
#!perl
#
# BEGIN COPYRIGHT BLOCK
# Copyright 2001 Sun Microsystems, Inc.
# Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
# All rights reserved.
# END COPYRIGHT BLOCK
#

package NSSetupSDK::Inf;

use NSSetupSDK::Base;

%otherEnc = ('local' => "utf8", utf8 => "local");
# mapping of encoding to the subroutine which converts from that encoding
%convertEnc = ('local' => \&toUTF8, utf8 => \&toLocal);

#############################################################################
# Creator, the argument (optional) is the INF file to read
#
sub new {
	my ($class, $filename) = @_;
	my $self = {};

	bless $self, $class;

	if ($filename) {
		$self->read($filename);
	}

	return $self;
}

#############################################################################
# Read in and initialize ourself with the given INF file.  The file will be
# encoded in either local or utf8 encoding.  The type of encoding is given
# in the $enc argument.  We first read in the values in the given encoding,
# then convert the file to the other encoding, then read in and initialize
# the values for the other encoding
#
sub read {
	my ($self, $filename, $enc) = @_;
	my $inf = {}; # the contents of the inf file
	my @sectionList = (); # a list of section names, in order
	my $sectionName = "General"; # default section name

	open INF, $filename or return undef;

	push @sectionList, $sectionName;
	while (<INF>) {
		next if /^\s*#/; # skip comments
		next if /^\s*$/; # skip blank lines
		if (/^\s*\[([^\]]+)\]/) {
			$sectionName = $1; # new section
			if ($sectionName cmp "General") {
				# General is already in the list
				push @sectionList, $sectionName;
			}
		} else {
			chop;
			($name, $value) = split(/\s*=\s*/, $_, 2);
#			print "name=$name value=$value\n";
			$inf->{$sectionName}{$enc}{$name} = $value;
			$inf->{$sectionName}{$otherEnc{$enc}}{$name} =
				&{$convertEnc{$enc}}($value);
		}
	}
	close INF;

	$self->{inf} = $inf;
	$self->{sections} = [ @sectionList ];

#	foreach $section (keys %inf) {
#		print '[', $section, ']', "\n";
#		foreach $name (keys %{ $inf{$section} }) {
#			print "local $name=$inf{$section}{local}{$name}\n";
#			print "UTF8 $name=$inf{$section}{utf8}{$name}\n";
#		}
#	}

	return 1;
}

sub readLocal {
	my $self = shift;
	return $self->read(@_, 'local');
}

sub readUTF8 {
	my $self = shift;
	return $self->read(@_, 'utf8');
}

#############################################################################
# Init from a hash; used to create a subsection as another inf
#
sub init {
	my ($self, $hashRef) = @_;
	my $inf = {};
	$inf->{General} = $hashRef;
	$self->{inf} = $inf;
	$self->{sections} = [ "General" ];

	return 1;
}

#############################################################################
# return the number of sections
#
sub numSections {
	my $self = shift;
	return scalar(@{$self->{sections}});
}

#############################################################################
# return the section corresponding to the given name or number
#
sub getSection {
	my ($self, $section) = @_;
	if ($section =~ /\d+/) { # section is a number
		$section = $self->{sections}->[$section];
	}

	my $newSec = new Inf;
	$newSec->init($self->{inf}->{$section});
	return $newSec;
}

#############################################################################
# return the value of the given name in local encoding
#
sub getLocal {
	my ($self, $name) = @_;
	return getFromSection($self, "General", $name, "local");
}

#############################################################################
# return the value of the given name in UTF8 encoding
#
sub getUTF8 {
	my ($self, $name) = @_;
	return getFromSection($self, "General", $name, "utf8");
}

#############################################################################
# return the value of the given name in UTF8 encoding
#
sub get {
	my ($self, $name) = @_;
	return getFromSection($self, "General", $name, "utf8");
}

#############################################################################
# return the value of the given name in the given section
#
sub getFromSection {
	my ($self, $section, $name, $enc) = @_;
#	print "self inf = ", %{ $self->{inf} }, "\n";
#	print "self inf section = ", %{ $self->{inf}->{$section} }, "\n";
	return $self->{inf}->{$section}{$enc}{$name};
}

#############################################################################
# set the value
#
sub setInSection {
	my ($self, $section, $name, $value, $enc) = @_;
	if (!$enc) {
		$enc = 'utf8';
	}
	$self->{inf}->{$section}{$enc}{$name} = $value;
	$self->{inf}->{$section}{$otherEnc{$enc}}{$name} =
		&{$convertEnc{$enc}}($value);
}

#############################################################################
# set the value; value is locally encoded
#
sub setLocal {
	my ($self, $name, $value) = @_;
	setInSection($self, "General", $name, $value, "local");
}

#############################################################################
# set the value; value is UTF-8 encoded
#
sub setUTF8 {
	my ($self, $name, $value) = @_;
	setInSection($self, "General", $name, $value, "utf8");
}

#############################################################################
# set the value, assume UTF-8 encoding
#
sub set {
	my ($self, $name, $value) = @_;
	setInSection($self, "General", $name, $value, "utf8");
}

sub write {
	my ($self, $ref, $enc) = @_;
	my $needClose = undef;
	if (!$enc) {
		$enc = "local"; # write file in local encoding by default
	}
	if (!$ref) {
		# no filehandle given
		$ref = \*STDOUT;
	} elsif (!ref($ref)) { # not a ref, assume scalar filename
		# filename
		open(OUTPUT, ">$ref") or die "Error: could not write file $ref: $!";
		$ref = \*OUTPUT;
		$needClose = 1; # be sure to close
	} elsif (ref($ref) eq 'SCALAR') {
		# filename
		open(OUTPUT, ">$$ref") or die "Error: could not write file $$ref: $!";
		$ref = \*OUTPUT;
		$needClose = 1; # be sure to close
	} # else already a file handle ref
	foreach $secName (@{ $self->{sections} }) {
		print $ref "[", $secName, "]\n";
		foreach $name (keys %{ $self->{inf}->{$secName}{$enc} }) {
			$value = $self->{inf}->{$secName}{$enc}{$name};
			print $ref $name, "=", $value, "\n";
		}
		print $ref "\n";
	}
	if ($needClose) {
		close $ref;
	}
}

sub writeLocal {
	my ($self, $ref) = @_;
	$self->write($ref, 'local');
}

sub writeUTF8 {
	my ($self, $ref) = @_;
	$self->write($ref, 'utf8');
}

1; # the mandatory TRUE return from the package