my
$filename
= $0;
$filename
=~ s/\.PL$//;
open
OUT,
">$filename"
or
die
"Can't create $filename: $!"
;
chmod
(0755,
$filename
);
print
"Extracting $filename (with #! substitution)\n"
;
print
OUT
<<"EOHEADER";
$Config{'startperl'} -w
eval 'exec perl -S \$0 "\$@"'
if 0;
EOHEADER
print
OUT <<
'EOBODY'
;
use
vars
qw( $running_under_some_shell )
;
my
%opts
= (
'd'
=>
'[ \t]+'
,
);
getopt(
'dfce'
, \
%opts
);
if
(
defined
$opts
{
'h'
})
{
print
STDERR
<<"EOF";
This is cssort version $Cz::Sort::VERSION.
Usage info: cssort [ -clist | -flist [-dregexp]] [files ...]
-c Columns
-f Field numbers
-d Delimiter, field separator
Lists are comma separated lists of field (column) numbers or ranges.
Example: cssort -c10-15,50-,25-45 cssort -f3,5-6 -s:
EOF
exit
(1);
}
my
$switch
=
'c'
;
my
$option
=
$opts
{
$switch
};
if
(not
defined
$option
)
{
$switch
=
'f'
;
$option
=
$opts
{
$switch
};
}
if
(not
defined
$option
)
{
$switch
=
undef
;
}
my
$conversion
;
if
(
defined
$opts
{
'e'
})
{
$conversion
= new Cz::Cstocs
$opts
{
'e'
},
'il2'
;
}
if
(
defined
$switch
)
{
my
(
@starts
,
@lengths
,
@array
);
for
(
split
/,/,
$option
)
{
if
(/^\d+$/)
{
push
@starts
,
$_
- 1;
push
@lengths
, 1; }
elsif
(/^(\d+)-(\d+)$/)
{
push
@starts
, $1 - 1;
push
@lengths
, ($2 - $1 + 1); }
elsif
(/^(\d+)-$/)
{
push
@starts
, $1 - 1;
push
@lengths
,
undef
; }
else
{
die
"Cssort: wrong option '$_' for switch -$switch\n"
; }
}
if
(
$switch
eq
'c'
)
{
while
(<>)
{
chomp
;
my
$line
= [
$_
];
my
$i
;
for
(
$i
= 0;
$i
<
@starts
;
$i
++)
{
if
(
$starts
[
$i
] >=
length
$_
)
{
push
@$line
,
undef
; }
elsif
(
defined
$lengths
[
$i
])
{
push
@$line
,
substr
$_
,
$starts
[
$i
],
$lengths
[
$i
]; }
else
{
push
@$line
,
substr
$_
,
$starts
[
$i
]; }
}
push
@array
,
$line
;
}
}
else
{
my
$regexp
=
$opts
{
'd'
};
while
(<>)
{
chomp
;
my
@items
=
split
/
$regexp
/so;
my
$line
= [
$_
];
my
$i
;
for
(
$i
= 0;
$i
<
@starts
;
$i
++)
{
push
@$line
,
@items
[
$starts
[
$i
] .. (
defined
$lengths
[
$i
] ?
$starts
[
$i
] +
$lengths
[
$i
] - 1 :
$#items
)];
}
push
@array
,
$line
;
}
}
print
map
{
$_
->[0] .
"\n"
}
sort
{
my
$len
= (
@$a
>=
@$b
?
@$a
:
@$b
);
my
$i
;
for
(
$i
= 1;
$i
<
$len
;
$i
++)
{
if
(not
defined
$a
->[
$i
])
{
return
0
if
not
defined
$b
->[
$i
];
return
-1;
}
if
(not
defined
$b
->[
$i
])
{
return
1;
}
my
$result
= czcmp(
$a
->[
$i
],
$b
->[
$i
]);
return
$result
if
$result
!= 0;
}
return
0;
}
@array
;
}
else
{
print
czsort <>; }
EOBODY