Menu

[r55]: / trunk / PhpCheckstyle / src / util / Utility.php  Maximize  Restore  History

Download this file

182 lines (162 with data), 4.6 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
<?php
/*
* $Id: Utility.php 27242 2005-07-21 01:21:42Z hkodungallur $
*
* Copyright(c) 2004-2005, SpikeSource Inc. All Rights Reserved.
* Licensed under the Open Source License version 2.1
* (See http://www.spikesource.com/license.html)
*/
/**
* Utility functions
*
* @author Nimish Pachapurkar <npac@spikesource.com>
* @version $Revision: $
* @package SpikePHPCheckstyle_Util
*/
class Utility {
/**
* 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"]);
}
/**
* 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);
}
/**
* 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;
}
/**
* 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);
}
/**
* 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;
}
/**
* 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)) {
error_log("File already exists: ".$dir);
return false;
}
$dir = $this->replaceBackslashes($dir);
// Crawl up the directory tree
$nextPathname = substr($dir, 0, strrpos($dir, "/"));
if ($this->makeDirRecursive($nextPathname, $mode)) {
if (!file_exists($dir)) {
return mkdir($dir, $mode);
}
}
return false;
}
/**
* Copy a file, or recursively copy a folder and its contents
* This function is taken from the following website:
* http://aidan.dotgeek.org/lib/?file=function.copyr.php
*
* @author Aidan Lister <aidan@php.net>
* @version 1.0.1
* @param string $source Source path
* @param string $dest Destination path
* @return bool Returns TRUE on success, FALSE on failure
*/
public function copyr($source, $dest) {
// Simple copy for a file
if (is_file($source)) {
return copy($source, $dest);
}
// Make destination directory
if (!is_dir($dest)) {
mkdir($dest);
}
// Loop through the folder
$dir = dir($source);
while (false !== ($entry = $dir->read())) {
// Skip pointers
if ($entry == '.' || $entry == '..') {
continue;
}
// Deep copy directories
if ($dest !== $source."/".$entry) {
$this->copyr($source."/".$entry, $dest."/".$entry);
}
}
// Clean up
$dir->close();
return true;
}
}
$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.