Changeset View
Changeset View
Standalone View
Standalone View
tools/scan-build/CommonStuff.pm
#!/usr/bin/env perl | |||||
# | |||||
# The LLVM Compiler Infrastructure | |||||
# | |||||
# This file is distributed under the University of Illinois Open Source | |||||
# License. See LICENSE.TXT for details. | |||||
# | |||||
##===----------------------------------------------------------------------===## | |||||
# | |||||
# A set of routines common to scan-build and [ccc/c++]-analyzer scripts. | |||||
# | |||||
##===----------------------------------------------------------------------===## | |||||
package CommonStuff; | |||||
use strict; | |||||
# Reads a section from INI (https://en.wikipedia.org/wiki/INI_file) file. | |||||
# $INIFile - path to the file to read options from. | |||||
# $SectionToRead - name of the section to read options from. | |||||
# $OptionsOut - array reference to keep output, is being filled with | |||||
# {Option =>, Value =>} elements as options are parsed. | |||||
# Preserves an order in which the options occur in config file. | |||||
sub ReadINI { | |||||
my ($INIFile, $SectionToRead, $OptionsOut) = @_; | |||||
my $CurrSection = ""; | |||||
my $ErrMsg = undef; | |||||
my %OptionNames = (); # Just to check for duplicated options in a section. | |||||
my $SectionFound = 0; | |||||
return "$!" unless open(IN, "<", $INIFile); | |||||
while(<IN>) { | |||||
s/^\s+|\s+$//og; # remove leading and trailing whitespaces | |||||
next if (/^$/o); # skip empty lines | |||||
next if (/^[#;]/o); # skip comments | |||||
# Parse section name. | |||||
if (/^\[([a-zA-Z0-9\-_]+)\]$/o) { | |||||
$CurrSection = $1; | |||||
$SectionFound |= $CurrSection eq $SectionToRead; | |||||
next; | |||||
} | |||||
# Parse options. | |||||
elsif (/^([a-zA-Z0-9\-_]+)\s*=\s*(.*)$/o) { | |||||
next if $CurrSection ne $SectionToRead; | |||||
if (exists $OptionNames{$1}) { | |||||
$ErrMsg = "Duplicate option '$1' in section '[$CurrSection]'."; | |||||
last; | |||||
} | |||||
$OptionNames{$1} = 1; | |||||
# Fil output array with options with nonempty values. | |||||
push @$OptionsOut, { Option => $1, Value => $2 } if $2 ne ""; | |||||
} | |||||
# Bad syntax. | |||||
else { | |||||
$ErrMsg = "Error parsing line '$_'."; | |||||
last; | |||||
} | |||||
} | |||||
$ErrMsg = "Section '[$SectionToRead]' not found." | |||||
unless $ErrMsg || $SectionFound; | |||||
close (IN); | |||||
return $ErrMsg; | |||||
} | |||||
1; |