File: cov.php

package info (click to toggle)
ggcov 0.8.2-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,172 kB
  • ctags: 3,299
  • sloc: cpp: 15,896; ansic: 7,066; sh: 4,851; php: 1,644; perl: 401; makefile: 218
file content (301 lines) | stat: -rw-r--r-- 7,093 bytes parent folder | download
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
//
// ggcov - A GTK frontend for exploring gcov coverage data
// Copyright (c) 2005 Greg Banks <gnb@users.sourceforge.net>
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// $Id: cov.php,v 1.9 2010/05/09 05:37:14 gnb Exp $
//

// Status defines
define('COV_COVERED', 0);
define('COV_PARTCOVERED', 1);
define('COV_UNCOVERED', 2);
define('COV_UNINSTRUMENTED', 3);
define('COV_SUPPRESSED', 4);

$cov_rgb = array(
	    array(0,192,0),
	    array(160,160,0),
	    array(192,0,0),
	    array(0,0,0),
	    array(0,0,192));

class cov_callbacks
{
    function fatal($msg)
    {
	echo "<p style=\"color:red;\">$msg</p>\n";
	exit;
    }

    function url_base($base)
    {
	return $base;
    }

    function add_state($query)
    {
	return $query;
    }
}

class cov
{
    var $db_ = false;		    // db resource
    var $cb_;			    // cov_callbacks ref
    var $base_directory_;	    // string
    var $file_index_;		    // array
    var $global_function_list_;	    // array
    var $global_function_index_;    // array
    var $callnode_index_;	    // array
    var $report_index_;		    // array
    var $diagram_index_;	    // array

    function cov($cb)
    {
	if ($cb === null)
	    $cb = new cov_callbacks;
	$this->cb_ = $cb;
    }

    function attach($dir)
    {
	$this->base_directory_ = $dir;
    }

    function detach()
    {
	if ($this->db_ !== false)
	{
	    dba_close($this->db_);
	    $this->db_ = false;
	}
    }

    // static method to check for existance of an openable webdb
    function database_exists($dir)
    {
	return file_exists($dir . '/ggcov.webdb');
    }

    function fetch($key)
    {
	if ($this->db_ === false)
	{
	    $webdb_file = $this->base_directory_ . '/ggcov.webdb';
	    $this->db_ = dba_open($webdb_file, 'rd', 'db4', 0);
	    if ($this->db_ === false)
		$this->cb_->fatal("Couldn't open $webdb_file");
	}

	$s = dba_fetch($key, $this->db_);
	if ($s === false)
	    $this->cb_->fatal("Couldn't find key \"$key\"");
	return unserialize($s);
    }

    function file_index()
    {
	if ($this->file_index_ === null)
	    $this->file_index_ = $this->fetch('FI');
	return $this->file_index_;
    }

    function global_function_list()
    {
	if ($this->global_function_list_ === null)
	    $this->global_function_list_ = $this->fetch('UL');
	return $this->global_function_list_;
    }

    function global_function_index()
    {
	if ($this->global_function_index_ === null)
	    $this->global_function_index_ = $this->fetch('UI');
	return $this->global_function_index_;
    }

    function callnode_index()
    {
	if ($this->callnode_index_ === null)
	    $this->callnode_index_ = $this->fetch('NI');
	return $this->callnode_index_;
    }

    function report_index()
    {
	if ($this->report_index_ === null)
	    $this->report_index_ = $this->fetch('RI');
	return $this->report_index_;
    }

    function diagram_index()
    {
	if ($this->diagram_index_ === null)
	    $this->diagram_index_ = $this->fetch('GI');
	return $this->diagram_index_;
    }

    function _url($args, $g)
    {
	$u = $this->cb_->url_base($args[0]);
	for ($i = 1 ; $i < count($args) ; $i+=2)
	{
	    $k = $args[$i];
	    $v = $args[$i+1];
	    $g[$k] = $v;
	}
	$g = $this->cb_->add_state($g);
	if (count($g))
	{
	    $c = '?';
	    foreach ($g as $k => $v)
	    {
		$u = $u . $c . $k . '=' . urlencode($v);
		$c = '&';
	    }
	}
	return $u;
    }
    function url()
    {
	return htmlentities($this->_url(func_get_args(), $_GET));
    }
    function curl()
    {
	return htmlentities($this->_url(func_get_args(), array()));
    }
    function credirect()
    {
	header('Location: ' . $this->_url(func_get_args(), array()));
	exit;
    }

    function color_by_status($st)
    {
	global $cov_rgb;

	$rgb = $cov_rgb[$st];
	return sprintf('#%02x%02x%02x', $rgb[0], $rgb[1], $rgb[2]);
    }

    function status_by_values($values)
    {
	$numerator = $values[COV_COVERED] + $values[COV_PARTCOVERED];
	$denominator = $numerator + $values[COV_UNCOVERED];

	if ($denominator == 0)
	    $st = COV_UNINSTRUMENTED;
	else if ($numerator == 0)
	    $st = COV_UNCOVERED;
	else if ($values[COV_COVERED] == $denominator)
	    $st = COV_COVERED;
	else
	    $st = COV_PARTCOVERED;

	return $st;
    }

    function status_by_stats($stats)
    {
	return cov::status_by_values($stats[0]);
    }

    // Factory method to create and return a page object
    // best suited for the given URL.
    function create_page($url)
    {
	$url = basename($url);
	$url = preg_replace('/\?.*$/', '', $url);
	$map = array(
	    'callbutterfly.php' =>  'cov_callbutterfly_page',
	    'callgraph.php'	=>  'cov_callgraph_page',
	    'calls.php'		=>  'cov_calls_page',
	    'covbar.php'	=>  'cov_covbar_page',
	    'files.php'		=>  'cov_files_page',
	    'functions.php'	=>  'cov_functions_page',
	    'reports.php'	=>  'cov_reports_page',
	    'source.php'	=>  'cov_source_page',
	    'summary.php'	=>  'cov_summary_page',
	    'diagram.php'	=>  'cov_diagram_page',
	    'drender.php'	=>  'cov_drender_page',
	    'dviewport.php'	=>  'cov_dviewport_page'
	);

	if (!array_key_exists($url, $map))
	    $this->cb_->fatal("create_page: unknown url");
	$classname = $map[$url];
	require_once "ggcov/lib/$url";
	return new $classname($this);
    }
}

class cov_page
{
    function content_type()
    {
	return 'text/html';
    }

    function render_state_to_form($prefix)
    {
	$state = $this->env_->cb_->add_state(array());
	foreach ($state as $k => $v)
	{
	    $v = htmlentities($v);
	    echo "$prefix<input type=\"hidden\" name=\"$k\" value=\"$v\">\n";
	}
    }
}

// Class whose static methods provide input filtering and validation
class cov_valid
{
    function integer($s)
    {
	return preg_match('/^[0-9]+$/', $s);
    }
    function floating($s)
    {
	return preg_match('/^[0-9.]+$/', $s);
    }
    function callnode($s)
    {
	return preg_match('/^[a-zA-Z_][a-zA-Z_0-9]*$/', $s);
    }
    function funcname($s)
    {
	return (preg_match('/^[a-zA-Z_][a-zA-Z_0-9]*$/', $s) ||
		preg_match('/^[a-zA-Z_][a-zA-Z_0-9]* \[[a-zA-Z0-9_.\/-]+\]$/', $s));
    }
    function report($s)
    {
	return preg_match('/^[a-z_]+$/', $s);
    }
    function filename($s)
    {
	// Note, we don't check for naughty stuff like "../" here
	// because each use of a filename goes through the file index
	return preg_match('/^[a-zA-Z0-9_.\/-]+$/', $s);
    }
    function scope($s)
    {
	return preg_match('/^(overall|file|function)$/', $s);
    }
}

?>