| 1 |
-------------------------------------------------------------------------------- |
|---|
| 2 |
-- Title: LUFile.lua |
|---|
| 3 |
-- Description: Like a square peg in a round hole |
|---|
| 4 |
-- Author: Raphaël Szwarc http://alt.textdrive.com/lua/ |
|---|
| 5 |
-- Creation Date: February 1, 2005 |
|---|
| 6 |
-- Legal: Copyright (C) 2005 Raphaël Szwarc |
|---|
| 7 |
-------------------------------------------------------------------------------- |
|---|
| 8 |
|
|---|
| 9 |
-- import dependencies |
|---|
| 10 |
local LUObject = require( "LUObject" ) |
|---|
| 11 |
local LUList = require( "LUList" ) |
|---|
| 12 |
local LUString = require( "LUString" ) |
|---|
| 13 |
local io = require( "io" ) |
|---|
| 14 |
local math = require( "math" ) |
|---|
| 15 |
local os = require( "os" ) |
|---|
| 16 |
|
|---|
| 17 |
-- define the class |
|---|
| 18 |
local super = LUObject |
|---|
| 19 |
local self = super() |
|---|
| 20 |
|
|---|
| 21 |
-- class variable(s) |
|---|
| 22 |
local _separator = nil |
|---|
| 23 |
local _tmpDir = nil |
|---|
| 24 |
|
|---|
| 25 |
-- initialization method |
|---|
| 26 |
function self:init( aDirectory, aFile ) |
|---|
| 27 |
self = super.init( self ) |
|---|
| 28 |
|
|---|
| 29 |
if aDirectory ~= nil then |
|---|
| 30 |
aDirectory = tostring( aDirectory ) |
|---|
| 31 |
end |
|---|
| 32 |
|
|---|
| 33 |
if aFile ~= nil then |
|---|
| 34 |
aFile = tostring( aFile ) |
|---|
| 35 |
end |
|---|
| 36 |
|
|---|
| 37 |
self._directory = aDirectory |
|---|
| 38 |
self._file = aFile |
|---|
| 39 |
|
|---|
| 40 |
return self |
|---|
| 41 |
end |
|---|
| 42 |
|
|---|
| 43 |
-- method to access this file content as a "lua chunck" |
|---|
| 44 |
function self:chunk() |
|---|
| 45 |
local aContent, aStatus = self:content() |
|---|
| 46 |
|
|---|
| 47 |
if aContent ~= nil then |
|---|
| 48 |
local aChunk, aStatus = loadstring( aContent ) |
|---|
| 49 |
|
|---|
| 50 |
if aChunk ~= nil then |
|---|
| 51 |
return aChunk |
|---|
| 52 |
else |
|---|
| 53 |
return nil, aStatus |
|---|
| 54 |
end |
|---|
| 55 |
end |
|---|
| 56 |
|
|---|
| 57 |
return nil, aStatus |
|---|
| 58 |
end |
|---|
| 59 |
|
|---|
| 60 |
-- method to access this file content |
|---|
| 61 |
function self:content() |
|---|
| 62 |
local aPath = self:path() |
|---|
| 63 |
local aFile, aStatus = io.open( aPath, "rb" ) |
|---|
| 64 |
|
|---|
| 65 |
if aFile ~= nil then |
|---|
| 66 |
local aContent = aFile:read( "*all" ) |
|---|
| 67 |
|
|---|
| 68 |
aFile:close() |
|---|
| 69 |
|
|---|
| 70 |
return aContent |
|---|
| 71 |
end |
|---|
| 72 |
|
|---|
| 73 |
return nil, aStatus |
|---|
| 74 |
end |
|---|
| 75 |
|
|---|
| 76 |
-- method to set this file content |
|---|
| 77 |
function self:setContent( aContent ) |
|---|
| 78 |
local aPath = self:path() |
|---|
| 79 |
local aFile, aStatus = io.open( aPath, "wb" ) |
|---|
| 80 |
|
|---|
| 81 |
if aFile ~= nil then |
|---|
| 82 |
aFile:write( aContent or "" ) |
|---|
| 83 |
aFile:close() |
|---|
| 84 |
|
|---|
| 85 |
return self |
|---|
| 86 |
end |
|---|
| 87 |
|
|---|
| 88 |
return nil, aStatus |
|---|
| 89 |
end |
|---|
| 90 |
|
|---|
| 91 |
-- method to copy this file |
|---|
| 92 |
function self:copy( aFile, shouldDelete ) |
|---|
| 93 |
if aFile ~= nil and self ~= aFile then |
|---|
| 94 |
local aContent, aStatus = self:content() |
|---|
| 95 |
|
|---|
| 96 |
if aContent ~= nil then |
|---|
| 97 |
local aFile, aStatus = aFile:setContent( aContent ) |
|---|
| 98 |
|
|---|
| 99 |
if aFile ~= nil and aFile:exists() == true then |
|---|
| 100 |
if shouldDelete == true then |
|---|
| 101 |
return self:delete() |
|---|
| 102 |
end |
|---|
| 103 |
|
|---|
| 104 |
return true |
|---|
| 105 |
end |
|---|
| 106 |
|
|---|
| 107 |
return false, aStatus |
|---|
| 108 |
end |
|---|
| 109 |
|
|---|
| 110 |
return false, aStatus |
|---|
| 111 |
end |
|---|
| 112 |
|
|---|
| 113 |
return false, "nil file" |
|---|
| 114 |
end |
|---|
| 115 |
|
|---|
| 116 |
-- method to delete this file |
|---|
| 117 |
function self:delete() |
|---|
| 118 |
local aPath = self:path() |
|---|
| 119 |
local aValue, aStatus = os.remove( aPath ) |
|---|
| 120 |
|
|---|
| 121 |
if aStatus == nil then |
|---|
| 122 |
return true |
|---|
| 123 |
end |
|---|
| 124 |
|
|---|
| 125 |
return false, aStatus |
|---|
| 126 |
end |
|---|
| 127 |
|
|---|
| 128 |
-- method to access the directory of this file |
|---|
| 129 |
function self:directory() |
|---|
| 130 |
if self._directory == nil then |
|---|
| 131 |
self._directory = "." |
|---|
| 132 |
end |
|---|
| 133 |
|
|---|
| 134 |
return self._directory |
|---|
| 135 |
end |
|---|
| 136 |
|
|---|
| 137 |
-- method to compare the given object with this object for equality |
|---|
| 138 |
function self:equals( anObject ) |
|---|
| 139 |
if self:isKindOf( anObject ) == true then |
|---|
| 140 |
if self:path() == anObject:path() then |
|---|
| 141 |
return true |
|---|
| 142 |
end |
|---|
| 143 |
end |
|---|
| 144 |
|
|---|
| 145 |
return false |
|---|
| 146 |
end |
|---|
| 147 |
|
|---|
| 148 |
-- method to check if this file exists |
|---|
| 149 |
function self:exists() |
|---|
| 150 |
local aPath = self:path() |
|---|
| 151 |
local aFile, aStatus = io.open( aPath, "r" ) |
|---|
| 152 |
|
|---|
| 153 |
if aFile ~= nil then |
|---|
| 154 |
aFile:close() |
|---|
| 155 |
|
|---|
| 156 |
return true |
|---|
| 157 |
end |
|---|
| 158 |
|
|---|
| 159 |
return false, aStatus |
|---|
| 160 |
end |
|---|
| 161 |
|
|---|
| 162 |
-- method to access this file extension |
|---|
| 163 |
function self:extension() |
|---|
| 164 |
local aName = self:pathComponents():last() |
|---|
| 165 |
local anExtension = aName:match( "^.+%.(%w+)$" ) |
|---|
| 166 |
|
|---|
| 167 |
return anExtension |
|---|
| 168 |
end |
|---|
| 169 |
|
|---|
| 170 |
-- method to check if this file path is absolute |
|---|
| 171 |
function self:isAbsolute() |
|---|
| 172 |
if LUString:startsWith( self:directory(), "." ) == false then |
|---|
| 173 |
return true |
|---|
| 174 |
end |
|---|
| 175 |
|
|---|
| 176 |
return false |
|---|
| 177 |
end |
|---|
| 178 |
|
|---|
| 179 |
-- method to check if this file is a directory |
|---|
| 180 |
function self:isDirectory() |
|---|
| 181 |
if self._file == nil then |
|---|
| 182 |
return true |
|---|
| 183 |
end |
|---|
| 184 |
|
|---|
| 185 |
return false |
|---|
| 186 |
end |
|---|
| 187 |
|
|---|
| 188 |
-- method to check if this file is a file |
|---|
| 189 |
function self:isFile() |
|---|
| 190 |
if self._file ~= nil then |
|---|
| 191 |
return true |
|---|
| 192 |
end |
|---|
| 193 |
|
|---|
| 194 |
return false |
|---|
| 195 |
end |
|---|
| 196 |
|
|---|
| 197 |
-- method to move this file |
|---|
| 198 |
function self:move( aFile ) |
|---|
| 199 |
return self:copy( aFile, true ) |
|---|
| 200 |
end |
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
-- method to access the name of this file |
|---|
| 204 |
function self:name() |
|---|
| 205 |
if self._name == nil then |
|---|
| 206 |
if self._file ~= nil then |
|---|
| 207 |
self._name = self._file |
|---|
| 208 |
else |
|---|
| 209 |
self._name = self:pathComponents():last() |
|---|
| 210 |
end |
|---|
| 211 |
end |
|---|
| 212 |
|
|---|
| 213 |
return self._name |
|---|
| 214 |
end |
|---|
| 215 |
|
|---|
| 216 |
-- method to access this directory parent |
|---|
| 217 |
function self:parentDirectory() |
|---|
| 218 |
local aSeparator = self:separator() |
|---|
| 219 |
local aPath = self:path() |
|---|
| 220 |
|
|---|
| 221 |
if aPath ~= aSeparator then |
|---|
| 222 |
local anIndex = aPath:reverse():find( aSeparator, 1, true ) |
|---|
| 223 |
|
|---|
| 224 |
if anIndex ~= nil then |
|---|
| 225 |
aPath = aPath:sub( 1, aPath:len() - anIndex ) |
|---|
| 226 |
|
|---|
| 227 |
if aPath:len() == 0 then |
|---|
| 228 |
aPath = aSeparator |
|---|
| 229 |
end |
|---|
| 230 |
|
|---|
| 231 |
return aPath |
|---|
| 232 |
end |
|---|
| 233 |
end |
|---|
| 234 |
|
|---|
| 235 |
return nil |
|---|
| 236 |
end |
|---|
| 237 |
|
|---|
| 238 |
-- method to access the path of this file |
|---|
| 239 |
function self:path() |
|---|
| 240 |
local aSeparator = self:separator() |
|---|
| 241 |
local aPath = self:directory() |
|---|
| 242 |
|
|---|
| 243 |
if self._file ~= nil then |
|---|
| 244 |
aPath = aPath .. aSeparator |
|---|
| 245 |
aPath = aPath .. self._file |
|---|
| 246 |
end |
|---|
| 247 |
|
|---|
| 248 |
aPath = aPath:gsub( "%.%.", "" ) |
|---|
| 249 |
aPath = aPath:gsub( "%" .. aSeparator .. "%" .. aSeparator, aSeparator ) |
|---|
| 250 |
|
|---|
| 251 |
return aPath |
|---|
| 252 |
end |
|---|
| 253 |
|
|---|
| 254 |
-- method to access the path components of this file |
|---|
| 255 |
function self:pathComponents( aPath, aSeparator ) |
|---|
| 256 |
aPath = aPath or self:path() |
|---|
| 257 |
aSeparator = aSeparator or self:separator() |
|---|
| 258 |
|
|---|
| 259 |
return LUString:components( aPath, aSeparator ) |
|---|
| 260 |
end |
|---|
| 261 |
|
|---|
| 262 |
-- method to rename this file |
|---|
| 263 |
function self:rename( aFile ) |
|---|
| 264 |
local aPath = tostring( aFile ) |
|---|
| 265 |
local aValue, aStatus = os.rename( self:path(), aPath ) |
|---|
| 266 |
|
|---|
| 267 |
if aStatus == nil then |
|---|
| 268 |
return true |
|---|
| 269 |
end |
|---|
| 270 |
|
|---|
| 271 |
return false, aStatus |
|---|
| 272 |
end |
|---|
| 273 |
|
|---|
| 274 |
-- method to compute this file size |
|---|
| 275 |
function self:size() |
|---|
| 276 |
local aFile, aStatus = io.open ( self:path(), "rb" ) |
|---|
| 277 |
|
|---|
| 278 |
if aFile ~= nil then |
|---|
| 279 |
local aSize = aFile:seek( "end" ) |
|---|
| 280 |
|
|---|
| 281 |
aFile:close() |
|---|
| 282 |
|
|---|
| 283 |
if aSize ~= nil then |
|---|
| 284 |
return aSize |
|---|
| 285 |
end |
|---|
| 286 |
end |
|---|
| 287 |
|
|---|
| 288 |
return -1, aStatus |
|---|
| 289 |
end |
|---|
| 290 |
|
|---|
| 291 |
-- method for string representation |
|---|
| 292 |
function self:toString() |
|---|
| 293 |
return self:path() |
|---|
| 294 |
end |
|---|
| 295 |
|
|---|
| 296 |
-- method for URI representation |
|---|
| 297 |
function self:toURI() |
|---|
| 298 |
local LUURI = require( "LUURI" ) |
|---|
| 299 |
local aPath = LUURI:pathWithComponents( self:pathComponents(), true, self:isDirectory() ) |
|---|
| 300 |
|
|---|
| 301 |
return LUURI( "file://" .. aPath ) |
|---|
| 302 |
end |
|---|
| 303 |
|
|---|
| 304 |
-- method to access the system dependent name separator |
|---|
| 305 |
function self:separator() |
|---|
| 306 |
if _separator == nil then |
|---|
| 307 |
_separator = package.path:match( "(%p)%?%." ) or "/" |
|---|
| 308 |
end |
|---|
| 309 |
|
|---|
| 310 |
return _separator |
|---|
| 311 |
end |
|---|
| 312 |
|
|---|
| 313 |
-- method to access the system dependent temporary directory |
|---|
| 314 |
function self:tmpDir() |
|---|
| 315 |
if _tmpDir == nil then |
|---|
| 316 |
_tmpDir = self:new( os.tmpname() ):parentDirectory() |
|---|
| 317 |
end |
|---|
| 318 |
|
|---|
| 319 |
return _tmpDir |
|---|
| 320 |
end |
|---|
| 321 |
|
|---|
| 322 |
-- method to generate a temporary file name |
|---|
| 323 |
function self:tmpName( aPrefix, aSuffix ) |
|---|
| 324 |
local aComponent = self:pathComponents( os.tmpname() ):last() |
|---|
| 325 |
local aBuffer = LUList() |
|---|
| 326 |
|
|---|
| 327 |
math.random( math.pi ) |
|---|
| 328 |
|
|---|
| 329 |
if aPrefix ~= nil then |
|---|
| 330 |
aBuffer:add( aPrefix ) |
|---|
| 331 |
end |
|---|
| 332 |
|
|---|
| 333 |
aBuffer:add( aComponent ) |
|---|
| 334 |
aBuffer:add( ( "%x" ):format( math.random( ( os.clock() + 1 ) * math.pi ) ) ) |
|---|
| 335 |
aBuffer:add( ( "%x" ):format( math.random( os.time() + 1 ) ) ) |
|---|
| 336 |
|
|---|
| 337 |
if aSuffix ~= nil then |
|---|
| 338 |
aBuffer:add( aSuffix ) |
|---|
| 339 |
end |
|---|
| 340 |
|
|---|
| 341 |
return aBuffer:join( "." ) |
|---|
| 342 |
end |
|---|
| 343 |
|
|---|
| 344 |
-- method to access a temporary file |
|---|
| 345 |
function self:tmpFile( aPrefix, aSuffix ) |
|---|
| 346 |
return self:new( self:tmpDir(), self:tmpName( aPrefix, aSuffix ) ) |
|---|
| 347 |
end |
|---|
| 348 |
|
|---|
| 349 |
return self |
|---|