-
Notifications
You must be signed in to change notification settings - Fork 7
/
check_formatting.php
executable file
·137 lines (127 loc) · 3.04 KB
/
check_formatting.php
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/*
This script does the following:
- Checks the lineterm characters of each file and reportsif the formats are NOT in UNIX format (\n)
- Rewrites files with UNIX lineterms only.
*/
# -- GLOBAL VARIABLES --
$g_flag = '';
# - ---
function print_result( $p_result ) {
# switch( $p_result ) {
# }
}
# - ---
# process one file for problems
function process_file( $p_file ) {
global $g_flag;
$line_arr = file( $p_file );
$counter = 0;
foreach ( $line_arr as $line ) {
$line = trim( $line );
$counter++;
switch ( $g_flag ) {
case '-f':
# check for no braces for if
$pos = strpos( $line, 'if' );
if ( 0 === $pos ) {
$pos2 = strpos( $line, '{' );
if ( FALSE === $pos2 ) {
print "$p_file : $counter\n";
}
}
# check for no braces for while
$pos = strpos( $line, 'while' );
if ( 0 === $pos ) {
$pos2 = strpos( $line, '{' );
if ( FALSE === $pos2 ) {
print "$p_file : $counter\n";
}
}
break;
case '-b':
# check for spaces in brackets []
$pos = strpos( $line, "[ '" );
if ( $pos > 0 ) {
print "$p_file : $counter\n";
}
$pos = strpos( $line, "[ \$" );
if ( $pos > 0 ) {
print "$p_file : $counter\n";
}
break;
case '-p':
# parenthesis
break;
case '-c':
# check for order of comparison ( constant == variable )
break;
case '-m':
# check for // comment
$pos = strpos( $line, '//' );
if ( FALSE !== $pos ) {
print "$p_file : $counter\n";
}
break;
case '-n':
# check for ! with space
$pos = strpos( $line, '! ' );
if ( FALSE !== $pos ) {
print "$p_file : $counter\n";
}
break;
} # end switch
}
}
# - ---
# read in all files
function process_files( $p_dir ) {
$cwd = getcwd();
$cwd .= '\\'.$p_dir;
chdir( $cwd );
if ( $handle = opendir( $cwd ) ) {
#echo "Directory: ".getcwd()."\n";
$file_arr = array();
while (false !== ( $file = readdir( $handle ) )) {
$file_arr[] = $file;
}
closedir( $handle );
foreach( $file_arr as $file ) {
#echo "file: $file\n";
if (( '.' == $file )||( '..' == $file )||( 'CVS' == $file )) {
continue;
}
if ( TRUE == is_dir( $file ) ) {
# directory
#process_files( $file );
} else {
process_file( $file );
#echo "Processing: ".getcwd()."\\".$file."\n";
}
}
}
chdir( '..' );
}
# - ---
function print_usage() {
echo "\nUsage:\n php -q check_formatting.php <option> <path/folder>";
echo "\nOptions:";
echo "\n -f brace check {}";
echo "\n -b bracket check []";
echo "\n -p parenthesis check ()";
echo "\n -c comparison check ( value == variable )";
echo "\n -m comment check # or //";
echo "\n -n not check !";
}
# - ---
# -- MAIN --
$argv = $_SERVER['argv'];
$argc = $_SERVER['argc'];
# too few arguments?
if ( $argc != 3 ) {
print_usage();
exit;
}
$g_flag = $argv[1];
process_files( $argv[2] );
?>