Ignore:
Timestamp:
Apr 20, 2008, 1:59:47 PM (12 years ago)
Author:
Bruno Cornec
Message:

Split again function in modules to allow for usage with pbinit and easier reuse.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • devel/pb-modules/lib/ProjectBuilder/Base.pm

    r402 r405  
    3333
    3434our @ISA = qw(Exporter);
    35 our @EXPORT = qw(pb_conf_read pb_conf_read_if pb_mkdir_p pb_system pb_rm_rf pb_get_date pb_log pb_log_init pb_get_uri pb_get_content pb_display_file pb_syntax_init pb_syntax pb_temp_init $debug $LOG);
     35our @EXPORT = qw(pb_mkdir_p pb_system pb_rm_rf pb_get_date pb_log pb_log_init pb_get_uri pb_get_content pb_display_file pb_syntax_init pb_syntax pb_temp_init $debug $LOG);
    3636
    3737=pod
     
    6363  #
    6464  pb_system("ls -l", "Printing directory content");
    65 
    66   #
    67   # Read hash codes of values from a configuration file and return table of pointers
    68   #
    69   my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","key1","key2");
    70   my ($k) = pb_conf_read("$ENV{'HOME'}/.pbrc","key");
    7165
    7266  #
     
    162156}
    163157
    164 =item B<pb_conf_read_if>
    165 
    166 This function returns a table of pointers on hashes
    167 corresponding to the keys in a configuration file passed in parameter.
    168 If that file doesn't exist, it returns undef.
    169 
    170 The format of the configuration file is as follows:
    171 
    172 key tag = value1,value2,...
    173 
    174 Supposing the file is called "$ENV{'HOME'}/.pbrc", containing the following:
    175 
    176   $ cat $HOME/.pbrc
    177   pbver pb = 3
    178   pbver default = 1
    179   pblist pb = 12,25
    180 
    181 calling it like this:
    182 
    183   my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","pbver","pblist");
    184 
    185 will allow to get the mapping:
    186 
    187   $k1->{'pb'}  contains 3
    188   $ka->{'default'} contains 1
    189   $k2->{'pb'} contains 12,25
    190 
    191 Valid chars for keys and tags are letters, numbers, '-' and '_'.
    192 
    193 =cut
    194 
    195 sub pb_conf_read_if {
    196 
    197 my $conffile = shift;
    198 my @param = @_;
    199 
    200 open(CONF,$conffile) || return((undef));
    201 close(CONF);
    202 return(pb_conf_read($conffile,@param));
    203 }
    204 
    205 =item B<pb_conf_read>
    206 
    207 This function is similar to B<pb_conf_read_if> except that it dies when the file in parameter doesn't exist.
    208 
    209 =cut
    210 
    211 sub pb_conf_read {
    212 
    213 my $conffile = shift;
    214 my @param = @_;
    215 my $trace;
    216 my @ptr;
    217 my %h;
    218 
    219 open(CONF,$conffile) || die "Unable to open $conffile";
    220 while(<CONF>) {
    221     if (/^\s*([A-z0-9-_]+)\s+([[A-z0-9-_]+)\s*=\s*(.+)$/) {
    222         pb_log(3,"DEBUG: 1:$1 2:$2 3:$3\n");
    223         $h{$1}{$2}=$3;
    224     }
    225 }
    226 close(CONF);
    227 
    228 for my $param (@param) {
    229     push @ptr,$h{$param};
    230 }
    231 return(@ptr);
    232 }
    233 
    234158=item B<pb_get_uri>
    235159
Note: See TracChangeset for help on using the changeset viewer.