1 #!/usr/bin/perl -w 2 3 # Copyright (C) 2008 Julien Chaffraix <jchaffraix@webkit.org> 4 # 5 # Redistribution and use in source and binary forms, with or without 6 # modification, are permitted provided that the following conditions 7 # are met: 8 # 1. Redistributions of source code must retain the above copyright 9 # notice, this list of conditions and the following disclaimer. 10 # 2. Redistributions in binary form must reproduce the above copyright 11 # notice, this list of conditions and the following disclaimer in the 12 # documentation and/or other materials provided with the distribution. 13 # 14 # THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 15 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 18 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22 # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 # 26 27 use strict; 28 29 package InFilesParser; 30 31 my $isParsingCommonParameters; 32 my $hasStartedParsing; 33 34 # Helper functions 35 36 sub trimComment 37 { 38 my $string = shift; 39 $string =~ s/#.+$//; 40 chomp($string); 41 return $string; 42 } 43 44 sub trimWS 45 { 46 my $string = shift; 47 $string =~ s/^\s+//; 48 $string =~ s/\s+$//; 49 chomp($string); 50 return $string; 51 } 52 53 sub trimQuoteAndWS 54 { 55 my $string = shift; 56 $string =~ s/\"([^\"]+)\"/$1/; 57 return trimWS($string); 58 } 59 60 # Default constructor 61 62 sub new 63 { 64 my $object = shift; 65 my $reference = { }; 66 67 # Initialize the parser. 68 $isParsingCommonParameters = 1; 69 $hasStartedParsing = 0; 70 71 bless($reference, $object); 72 return $reference; 73 } 74 75 # parse take 3 attributes: 76 # - the filestream to read from (the caller has to open / close it). 77 # - the commonParameterHandler called when parsing the first part of the file with the parameter and the value. 78 # - the perTagHandler called for each optional parameter with the element name, the parameter and its value. 79 # If no parameter were provided, it is called once with an empty parameter and value. 80 sub parse($) 81 { 82 my $object = shift; 83 my $fileStream = shift; # IO::File only 84 my $commonParameterHandler = shift; 85 my $perTagHandler = shift; 86 87 foreach (<$fileStream>) { 88 # Empty line, change from common parameter part 89 # to per tag part if we have started parsing. 90 if (/^$/) { 91 if ($hasStartedParsing) { 92 $isParsingCommonParameters = 0; 93 } 94 next; 95 } 96 97 # There may be a few empty lines at the beginning of the file 98 # so detect the first non empty line which starts the common 99 # parameters part. 100 $hasStartedParsing = 1; 101 102 if (/^#/) { 103 next; 104 } 105 106 $_ = trimComment($_); 107 108 if ($isParsingCommonParameters) { 109 my ($name, $value) = split '=', $_; 110 111 $name = trimWS($name); 112 if (defined($value)) { 113 $value = trimQuoteAndWS($value); 114 } else { 115 # We default to 1 as it eases the syntax. 116 $value = "1"; 117 } 118 119 &$commonParameterHandler($name, $value); 120 } else { 121 # Parsing per-tag parameters. 122 123 # Split the tag name ($1) from the optionnal parameter(s) ($2) 124 /^(\S+)\s*(.*)$/; 125 my $elementName = $1; 126 127 if ($2) { 128 my @options = split "," , $2; 129 my ($option, $value); 130 for (my $i = 0; $i < @options; ++$i) { 131 ($option, $value) = split "=", $options[$i]; 132 $option = trimWS($option); 133 if (defined($value)) { 134 $value = trimQuoteAndWS($value); 135 } else { 136 # We default to 1 as it eases the syntax. 137 $value = "1"; 138 } 139 140 &$perTagHandler($elementName, $option, $value); 141 } 142 } else { 143 # No parameter was given so call it with empty strings. 144 &$perTagHandler($elementName, "", ""); 145 } 146 } 147 } 148 } 149 150 1; 151