File: FileType.pm

package info (click to toggle)
acheck 0.5.13
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 292 kB
  • sloc: perl: 1,327; makefile: 33
file content (112 lines) | stat: -rw-r--r-- 2,118 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package ACheck::FileType;


use strict;
use Exporter;
use locale;
use File::Spec::Functions qw(rel2abs splitpath splitdir);
use ACheck::Common;

use vars  qw(@ISA @EXPORT);

@ISA	= qw(Exporter);
@EXPORT	= qw(
	file_type
	);

my %FILE_TYPES = (						# filetypes
	'\.wml(\.\w+)?$'	=> "wml",
	'\.po(\.\w+)?$'		=> "po",
	'\.te?xt(\.\w+)?$'	=> "text"
	);


# file_type
#
# find file type
#
# input:
#   filename
#   array reference of file lines
# return:
#   file type
sub file_type ($$) {
	my $filename	= shift || "-";				# filename
	my $lines	= shift;				# array of lines

	debug 1;
	debug 2, "filename:     ".($filename || "STDIN")."\n";

	my $type;					# file type
	my $file   =  "";				# file name
	my $dir    =  "";				# directory
	my @dirs   = ("");				# path

	unless ($filename eq "-") {
		$filename = rel2abs($filename) unless $filename eq "-";
		(undef, $dir, $file) = splitpath($filename);
		@dirs = splitdir($dir);
	}

	#
	# file name tests
	#
	if ($dirs[-1] eq "debian" &&
	    $file     eq "control"  ) {
		$type = "debian-control";
	}

	if ($type) {
		debug 3, "filetype:     $type\n";
		return $type;
	}


	#
	# file extension tests
	#
	foreach (keys %FILE_TYPES) {
		next unless $file =~ /$_/;
		$type = $FILE_TYPES{$_};
		last;
	}

	if ($type) {
		debug 3, "filetype:     $type\n";
		return $type;
	}


	#
	# content tests
	#
	if	($$lines[0] =~ /^--- \w+/    &&		# unified diff tests
		 $$lines[1] =~ /^\+\+\+ \w+/ &&
		 $$lines[2] =~ /^@@ /       ) {
		$type   = "udiff-wml" if $$lines[0] =~ /^\-\-\- \w+\.wml\b/;
		$type   = "udiff-wml" if $$lines[1] =~ /^\+\+\+ \w+\.wml\b/;
		$type ||= "udiff";
	} elsif ($$lines[0] =~ /^#use wml\b/) {
		$type = "wml";
	} else {					# otherwise try to guess from file lines
		my $i;
		for ($i = 0; $i < @{ $lines }; $i++) {
			last unless $$lines[$i] =~ /^#/;
		}
		if	($i == @{ $lines }) {
		} elsif	($$lines[$i  ] =~ /^msgid ""$/ &&
			 $$lines[$i+1] =~ /^msgstr "/    ) {
			$type = "po";
		} elsif ($$lines[$i  ] =~ /^Description: /) {
			$type = "ddts";
		}
	}

	$type ||= "text";				# default to text

	debug 3, "filetype:     $type\n";

	return $type;
}

1;