Fix CursorMove command to correctly honour EdgeScroll settings.
[fvwm.git] / perllib / General / Parse.pm
blobd8101fb370131b5ed1680fabd70570ece2d0ba2b
1 # Copyright (c) 2002-2009, Mikhael Goikhman
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 package General::Parse;
18 use strict;
20 use vars qw(@ISA @EXPORT);
21 require Exporter;
22 @ISA = qw(Exporter);
23 @EXPORT = qw(
24 get_token cut_token get_tokens cut_tokens eqi nei
27 # currently backslashes are ignored and this is a bit buggy
28 sub get_token ($) {
29 my $line = shift;
30 my $token;
32 $line =~ s/^\s+//;
33 my $quote = '\s';
34 $quote = substr($line, 0, 1) if $line =~ /^["'`]/;
36 $line =~ s/^$quote//;
37 if ($line =~ /^(.*?)$quote\s*(.*)$/) {
38 $token = $1;
39 $line = $2;
40 } elsif ($line eq "") {
41 $token = undef;
42 $line = "";
43 } else {
44 $token = $line;
45 $line = "";
47 return wantarray ? ($token, $line) : $token;
50 # returns the next quoted token, modifies the parameter (ref to line)
51 sub cut_token ($) {
52 my $line_ref = shift;
53 my ($token, $rest) = get_token($$line_ref);
54 $$line_ref = $rest;
55 return $token;
58 sub cut_tokens ($$) {
59 my $line_ref = shift;
60 my $limit = shift;
61 my @tokens = ();
63 $$line_ref =~ s/^\s+//;
64 while ($$line_ref ne "" && $limit-- > 0) {
65 push @tokens, cut_token($line_ref);
67 return wantarray ? @tokens : \@tokens;
70 sub get_tokens ($;$) {
71 my $line = shift;
72 my $limit = shift || 1000;
74 return cut_tokens(\$line, $limit);
77 sub eqi ($$) {
78 my ($a, $b) = @_;
79 return lc($a) eq lc($b);
82 sub nei ($$) {
83 my ($a, $b) = @_;
84 return lc($a) ne lc($b);
87 # ----------------------------------------------------------------------------
89 =head1 NAME
91 General::Parse - parsing functions
93 =head1 SYNOPSIS
95 use General::Parse;
97 my $string = q{Some "not very long" string of 6 tokens.};
98 my $token1 = cut_token(\$string); # $token1 = "Some";
99 my ($token2, $token3) = cut_tokens(\$string, 2);
100 my @subtokens = get_tokens($token2); # ("not", "very", "long")
101 my $subtoken1 = get_token($token2); # the same as $subtokens[0]
102 my $ending_array_ref = get_tokens($string); # ["of", "6", "tokens."]
104 =head1 DESCRIPTION
106 This package may be used for parsing a string into tokens (quoted words).
108 =head1 FUNCTIONS
111 =head2 get_token
113 =over 4
115 =item description
117 Returns the first token of the given string without changing the string itself.
119 If the string is empty or consists of only spaces the returned token is undef.
121 If the caller expects a scalar - the token is returned. If it expects an
122 array - 2 values returned, the token and the rest of the string.
124 =item parameters
126 String (scalar) to be parsed.
128 =item returns
130 Token (scalar).
131 Or, in array context, array of 2 scalars: token and the rest of string.
133 =back
136 =head2 cut_token
138 =over 4
140 =item description
142 Returns the first token of the given string, the input string is cut to
143 contain tokens starting from the second one.
145 If the string is empty or consists of only spaces the returned token is undef
146 and the string is changed to an empty string.
148 =item parameters
150 String (scalar) to be parsed.
152 =item returns
154 Token (scalar).
156 =back
159 =head2 get_tokens
161 =over 4
163 =item description
165 Returns all or the requested number of tokens of the given string without
166 changing the string itself.
168 The returned array may contain less tokens than requested if the string
169 is too short. Particularly, the returned array is empty if the string is empty
170 or consists of only spaces.
172 If the caller expects a scalar - a reference to the token array is returned.
173 If it expects an array - the token array is returned.
175 =item parameters
177 String (scalar) to be parsed, and optional limit (integer) for number of
178 returned tokens.
180 =item returns
182 Tokens (array of scalars or array ref of scalars depending on context).
184 =back
187 =head2 cut_tokens
189 =over 4
191 =item description
193 Returns the requested number of tokens of the given string, the string is cut
194 to contain the tokens starting from the first non returned token.
196 The returned array may contain less tokens than requested if the string
197 is too short. Particularly, the returned array is empty if the string is empty
198 or consists of only spaces.
200 If the caller expects a scalar - a reference to the token array is returned.
201 If it expects an array - the token array is returned.
203 =item parameters
205 String (scalar) to be parsed, and limit (integer) for number of
206 returned tokens.
208 =item returns
210 Tokens (array of scalars or array ref of scalars depending on context).
212 =back
215 =head2 eqi
217 =over 4
219 =item description
221 Similar to B<eq>, but case-insensitive, gets 2 strings, returns boolean.
223 =back
226 =head2 nei
228 =over 4
230 =item description
232 Similar to B<ne>, but case-insensitive, gets 2 strings, returns boolean.
234 =back
237 =head1 AUTHOR
239 Mikhael Goikhman <migo@homemail.com>
241 =cut
242 # ============================================================================