Menu

[r16]: / branches / spikephpcoverage-0.8 / src / util / Utility.php  Maximize  Restore  History

Download this file

226 lines (199 with data), 6.9 kB

  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
<?php
/*
* $Id: Utility.php 14663 2005-03-23 19:27:27Z npac $
*
* Copyright(c) 2004-2006, SpikeSource Inc. All Rights Reserved.
* Licensed under the Open Software License version 2.1
* (See http://www.spikesource.com/license.html)
*/
?>
<?php
if(!defined("__PHPCOVERAGE_HOME")) {
define("__PHPCOVERAGE_HOME", dirname(dirname(__FILE__)));
}
require_once __PHPCOVERAGE_HOME . "/conf/phpcoverage.conf.php";
// include our dummy implementation
require_once 'CoverageLogger.php';
/**
* Utility functions
*
* @author Nimish Pachapurkar <npac@spikesource.com>
* @version $Revision: $
* @package SpikePHPCoverage_Util
*/
class Utility {
public static $logger;
/*{{{ public function getTimeStamp() */
/**
* Return the current timestamp in human readable format.
* Thursday March 17, 2005 19:10:47
*
* @return Readable timestamp
* @access public
*/
public function getTimeStamp() {
$ts = getdate();
return $ts["weekday"] . " " . $ts["month"] . " " . $ts["mday"]
. ", " . $ts["year"] . " " . sprintf("%02d:%02d:%02d", $ts["hours"], $ts["minutes"], $ts["seconds"]);
}
/*}}}*/
/*{{{ public function shortenFilename() */
/**
* Shorten the filename to some maximum characters
*
* @param $filename Complete file path
* @param $maxlength=150 Maximum allowable length of the shortened
* filepath
* @return Shortened file path
* @access public
*/
public function shortenFilename($filename, $maxlength=80) {
$length = strlen($filename);
if($length < $maxlength) {
return $filename;
}
// trim the first few characters
$filename = substr($filename, $length-$maxlength);
// If there is a path separator slash in first n characters,
// trim upto that point.
$n = 20;
$firstSlash = strpos($filename, "/");
if($firstSlash === false || $firstSlash > $n) {
$firstSlash = strpos($filename, "\\");
if($firstSlash === false || $firstSlash > $n) {
return "..." . $filename;
}
return "..." . substr($filename, $firstSlash);
}
return "..." . substr($filename, $firstSlash);
}
/*}}}*/
/*{{{ public function writeError() */
/**
* Write error log if debug is on
*
* @param $str Error string
* @access public
*/
public function writeError($str) {
if(__PHPCOVERAGE_DEBUG) {
error_log($str);
}
}
/*}}}*/
/*{{{ public function unixifyPath() */
/**
* Convert Windows paths to Unix paths
*
* @param $path File path
* @return String Unixified file path
* @access public
*/
public function unixifyPath($path) {
// Remove the drive-letter:
if(strpos($path, ":") == 1) {
$path = substr($path, 2);
}
$path = $this->replaceBackslashes($path);
return $path;
}
/*}}}*/
/*{{{ public function replaceBackslashes() */
/**
* Convert the back slash path separators with forward slashes.
*
* @param $path Windows path with backslash path separators
* @return String Path with back slashes replaced with forward slashes.
* @access public
*/
public function replaceBackslashes($path) {
$path = str_replace("\\", "/", $path);
return $this->capitalizeDriveLetter($path);
}
/*}}}*/
/*{{{ public function capitalizeDriveLetter() */
/**
* Convert the drive letter to upper case
*
* @param $path Windows path with "c:<blah>"
* @return String Path with driver letter capitalized.
* @access public
*/
public function capitalizeDriveLetter($path) {
if(strpos($path, ":") === 1) {
$path = strtoupper(substr($path, 0, 1)) . substr($path, 1);
}
return $path;
}
/*}}}*/
/*{{{ public function makeDirRecursive() */
/**
* Make directory recursively.
* (Taken from: http://aidan.dotgeek.org/lib/?file=function.mkdirr.php)
*
* @param $dir Directory path to create
* @param $mode=0755
* @return True on success, False on failure
* @access public
*/
public function makeDirRecursive($dir, $mode=0755) {
// Check if directory already exists
if (is_dir($dir) || empty($dir)) {
return true;
}
// Ensure a file does not already exist with the same name
if (is_file($dir)) {
$this->getLogger()->debug("File already exists: " . $dir,
__FILE__, __LINE__);
return false;
}
$dir = $this->replaceBackslashes($dir);
// Crawl up the directory tree
$next_pathname = substr($dir, 0, strrpos($dir, "/"));
if ($this->makeDirRecursive($next_pathname, $mode)) {
if (!file_exists($dir)) {
return mkdir($dir, $mode);
}
}
return false;
}
/*}}}*/
/*{{{ public function getOS() */
/**
* Returns the current OS code
* WIN - Windows, LIN -Linux, etc.
*
* @return String 3 letter code for current OS
* @access public
* @since 0.6.6
*/
public function getOS() {
return strtoupper(substr(PHP_OS, 0, 3));
}
/*}}}*/
/*{{{ public function getTmpDir() */
public function getTmpDir() {
global $spc_config;
$OS = $this->getOS();
switch($OS) {
case "WIN":
return $spc_config['windows_tmpdir'];
default:
return $spc_config['tmpdir'];
}
}
/*}}}*/
/*{{{ public function getLogger() */
public function getLogger($package=false) {
global $spc_config;
if(!isset($this->logger) || $this->logger == NULL) {
$this->logger =& new CoverageLogger();
$this->logger->setLevel($spc_config["log_level"]);
}
return $this->logger;
}
/*}}}*/
}
$util = new Utility();
global $util;
?>
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.