xpm_c_to_perl.pl 994 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date : 21 February 2013
  5. # https://github.com/trizen
  6. # XPM to Perl data.
  7. # for file in `find /usr/share/pixmaps/ -maxdepth 1`; do perl -X xpm_c_to_perl.pl $file > $(basename $file); done
  8. use strict;
  9. use Data::Dump qw(dump);
  10. $Data::Dump::INDENT = '';
  11. sub parse_xpm_file {
  12. my ($file) = @_;
  13. open my $fh, '<', $file
  14. or die "Can't open file '$file': $!";
  15. my @data;
  16. while (<$fh>) {
  17. if (/^"(.*?)",?\s*(\};\s*)?$/s) {
  18. push @data, $1;
  19. }
  20. else {
  21. #print STDERR $_;
  22. }
  23. }
  24. close $fh;
  25. my $dumped = dump \@data;
  26. # In list context returns the dumped data and the array itself.
  27. # In scalar context returns only the dumped data
  28. return wantarray ? ($dumped, \@data) : $dumped;
  29. }
  30. my $xpm_file = shift // die "usage: $0 [xpm_file]\n";
  31. $xpm_file =~ /\.xpm\z/i or die "Not a XPM file: $xpm_file\n";
  32. my $data = parse_xpm_file($xpm_file);
  33. print $data;