diff options
author | goodale <goodale@17b73243-c579-4c4c-a9d2-2d5706c11dac> | 2002-05-22 01:15:34 +0000 |
---|---|---|
committer | goodale <goodale@17b73243-c579-4c4c-a9d2-2d5706c11dac> | 2002-05-22 01:15:34 +0000 |
commit | f5380b07c235c29f7d2e57ecf7e050780a6996cc (patch) | |
tree | f831caf757dc104eb71d9742331b211e8570c4fc /lib/sbin/CSTUtils.pl | |
parent | 359ca45e4c0ef42658a525e108e25186f3e003f4 (diff) |
New routine which splits a string on = or space, ignoring their presence in
a string.
Tom
git-svn-id: http://svn.cactuscode.org/flesh/trunk@2844 17b73243-c579-4c4c-a9d2-2d5706c11dac
Diffstat (limited to 'lib/sbin/CSTUtils.pl')
-rw-r--r-- | lib/sbin/CSTUtils.pl | 131 |
1 files changed, 130 insertions, 1 deletions
diff --git a/lib/sbin/CSTUtils.pl b/lib/sbin/CSTUtils.pl index bb0d22d4..ecbc1d3e 100644 --- a/lib/sbin/CSTUtils.pl +++ b/lib/sbin/CSTUtils.pl @@ -1,11 +1,20 @@ #/*@@ +# @file CSTUtils.pl +# @date 4 July 1999 +# @author Gabrielle Allen +# @desc +# Various utility routines. +# @enddesc +# @version $Header$ +#@@*/ + +#/*@@ # @routine CST_error # @date 4 July 1999 # @author Gabrielle Allen # @desc # Print an error or warning message # @enddesc -# @version $Id$ #@@*/ sub CST_error @@ -244,4 +253,124 @@ sub TestName return $valid; } +#/*@@ +# @routine SplitWithStrings +# @date Tue May 21 23:45:54 2002 +# @author Tom Goodale +# @desc +# Splits a string on spaces and = ignoring +# any occurence of these in strings. +# @enddesc +# @calls +# @calledby +# @history +# +# @endhistory +# +# @var expression +# @vdesc Expression to split +# @vtype string +# @vio in +# @endvar +# +# @returntype list +# @returndesc +# Split representation of input expression. +# @endreturndesc +#@@*/ +sub SplitWithStrings +{ + my ($expression) = @_; + + my $insstring = 0; + my $indstring = 0; + my $escaping = 0; + + my @tokens = (); + + my $token=""; + + # First split the string into string tokens and split tokens we are + # allowed to split. + + for $i (split(//,$expression)) + { + if($i eq '\\') + { + if($escaping) + { + $token .= $i; + } + + $escaping = 1 - $escaping; + } + elsif($i eq '"' && ! $insstring && ! $escaping) + { + if(length $token > 0 || $indstring) + { + push(@tokens, $token); + } + + $token = ""; + $indstring = 1 - $indstring; + } + elsif($i eq "'" && ~ $indstring && ! $escaping) + { + if(length $token > 0 || $insstring) + { + push(@tokens, $token); + } + + $token = ""; + + $insstring = 1 - $insstring; + } + elsif($i =~ /^\s+$/ && ! $insstring && ! $indstring && ! $escaping) + { + if(length $token > 0 || $insstring) + { + push(@tokens, $token); + } + + $token = ""; + } + elsif($i eq '=' && ! $insstring && ! $indstring && ! $escaping) + { + if(length $token > 0 || $insstring) + { + push(@tokens, $token); + } + + $token = ""; + } + else + { + if($escaping) + { + $token .= "\\"; + $escaping = 0; + } + $token .= "$i"; + } + } + + if($insstring || $indstring) + { + print "Error: Unterminated string\n" + } + + if($escaping) + { + $token .= '\\'; + } + + if(length $token > 0) + { + push(@tokens, $token); + } + + return @tokens; + +} + 1; |