SHOW:
|
|
- or go back to the newest paste.
| 1 | File = {
| |
| 2 | new = function( self, filePath ) | |
| 3 | if self.index then | |
| 4 | - | if self.__index then |
| 4 | + | |
| 5 | else | |
| 6 | return setmetatable( { fPath = filePath, buffer = "", index = true }, { __index = File } )
| |
| 7 | - | return setmetatable( { fPath = filePath, buffer = "" }, { __index = File } )
|
| 7 | + | |
| 8 | end, | |
| 9 | ||
| 10 | exist = function( self, filePath ) | |
| 11 | if self.index then | |
| 12 | - | if self.__index then |
| 12 | + | |
| 13 | return fileExists( self.fPath ) | |
| 14 | end | |
| 15 | return false | |
| 16 | else | |
| 17 | return fileExists( filePath ) | |
| 18 | end | |
| 19 | end, | |
| 20 | ||
| 21 | rename = function( self, filePath, newFilePath ) | |
| 22 | if self.index then | |
| 23 | - | if self.__index then |
| 23 | + | |
| 24 | local res = fileRename( self.fPath, filePath ) | |
| 25 | self.fPath = filePath | |
| 26 | return res | |
| 27 | end | |
| 28 | return false | |
| 29 | else | |
| 30 | return fileRename( filePath, newFilePath ) | |
| 31 | end | |
| 32 | end, | |
| 33 | ||
| 34 | delete = function( self, filePath ) | |
| 35 | if self.index then | |
| 36 | - | if self.__index then |
| 36 | + | |
| 37 | return fileDelete( self.fPath ) | |
| 38 | end | |
| 39 | return false | |
| 40 | else | |
| 41 | return fileDelete( filePath ) | |
| 42 | end | |
| 43 | end, | |
| 44 | ||
| 45 | create = function( self, filePath ) | |
| 46 | if not self.file then | |
| 47 | self.fPath = filePath or self.fPath | |
| 48 | self.file = fileCreate( self.fPath ) | |
| 49 | return self.file ~= false | |
| 50 | end | |
| 51 | return false | |
| 52 | end, | |
| 53 | ||
| 54 | open = function( self, readOnly, filePath ) | |
| 55 | if not self.file then | |
| 56 | self.fPath = filePath or self.fPath | |
| 57 | self.file = fileOpen( self.fPath, readOnly or false ) | |
| 58 | return self.file ~= false | |
| 59 | end | |
| 60 | return false | |
| 61 | end, | |
| 62 | ||
| 63 | close = function( self ) | |
| 64 | if self.file then | |
| 65 | local res = fileClose( self.file ) | |
| 66 | self.file = nil | |
| 67 | self.buffer = "" | |
| 68 | return res | |
| 69 | end | |
| 70 | return false | |
| 71 | end, | |
| 72 | ||
| 73 | pos = function( self, offset ) | |
| 74 | if self.file then | |
| 75 | if offset then | |
| 76 | return fileSetPos( self.file, offset ) | |
| 77 | else | |
| 78 | return fileGetPos( self.file ) | |
| 79 | end | |
| 80 | end | |
| 81 | return false | |
| 82 | end, | |
| 83 | ||
| 84 | size = function( self, filePath ) | |
| 85 | if self.index then | |
| 86 | - | if filePath then |
| 86 | + | if self.file then |
| 87 | return fileGetPos( self.file ) | |
| 88 | end | |
| 89 | return false | |
| 90 | elseif filePath then | |
| 91 | local hFile = fileOpen( filePath ) | |
| 92 | if hFile then | |
| 93 | - | elseif self.file then |
| 93 | + | |
| 94 | - | return fileGetPos( self.file ) |
| 94 | + | |
| 95 | return fSize | |
| 96 | end | |
| 97 | end | |
| 98 | return false | |
| 99 | end, | |
| 100 | ||
| 101 | flush = function( self ) | |
| 102 | if self.file then | |
| 103 | return fileFlush( self.file ) | |
| 104 | end | |
| 105 | return false | |
| 106 | end, | |
| 107 | ||
| 108 | write = function( self, string1, ... ) | |
| 109 | if self.file then | |
| 110 | return fileWrite( self.file, string1, ... ) | |
| 111 | end | |
| 112 | return false | |
| 113 | end, | |
| 114 | ||
| 115 | read = function( self, count ) | |
| 116 | if self.file then | |
| 117 | return fileRead( self.file, count ) | |
| 118 | end | |
| 119 | return false | |
| 120 | end, | |
| 121 | ||
| 122 | readLine = function( self, count ) | |
| 123 | if self.file then | |
| 124 | while true do | |
| 125 | local endpos = string.find( self.buffer, "\n" ) | |
| 126 | ||
| 127 | if not endpos then | |
| 128 | if fileIsEOF( self.file ) then | |
| 129 | if self.buffer ~= "" then | |
| 130 | local line = self.buffer | |
| 131 | self.buffer = "" | |
| 132 | return line | |
| 133 | else | |
| 134 | return false | |
| 135 | end | |
| 136 | - | else |
| 136 | + | |
| 137 | self.buffer = self.buffer .. fileRead( self.file, count or 500 ) | |
| 138 | else | |
| 139 | local line = string.sub( self.buffer, 1, endpos - 1 ) | |
| 140 | self.buffer = string.sub( self.buffer, endpos + 1 ) | |
| 141 | return line | |
| 142 | end | |
| 143 | end | |
| 144 | end | |
| 145 | return false | |
| 146 | end, | |
| 147 | ||
| 148 | EOF = function( self ) | |
| 149 | if self.file then | |
| 150 | return fileIsEOF( self.file ) | |
| 151 | end | |
| 152 | return false | |
| 153 | end | |
| 154 | } |