PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/clp/mls/modules/wx/ds_system.lua

http://microlua-sim.googlecode.com/
Lua | 250 lines | 103 code | 36 blank | 111 comment | 16 complexity | 9b5c1f4ab933492e9a0ca61713e61b06 MD5 | raw file
Possible License(s): GPL-3.0
  1. -------------------------------------------------------------------------------
  2. -- Micro Lua ds_system module simulation, based on wxWidgets.
  3. --
  4. -- @class module
  5. -- @name clp.mls.modules.wx.ds_system
  6. -- @author Ced-le-pingouin <Ced.le.pingouin@gmail.com>
  7. -------------------------------------------------------------------------------
  8. -- Copyright (C) 2009-2011 C?Šdric FLOQUET
  9. --
  10. -- This file is part of Micro Lua DS Simulator.
  11. --
  12. -- Micro Lua DS Simulator is free software: you can redistribute it and/or modify
  13. -- it under the terms of the GNU General Public License as published by
  14. -- the Free Software Foundation, either version 3 of the License, or
  15. -- (at your option) any later version.
  16. --
  17. -- Micro Lua DS Simulator is distributed in the hope that it will be useful,
  18. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. -- GNU General Public License for more details.
  21. --
  22. -- You should have received a copy of the GNU General Public License
  23. -- along with Micro Lua DS Simulator. If not, see <http://www.gnu.org/licenses/>.
  24. require "wx"
  25. local Class = require "clp.Class"
  26. local Sys = require "clp.mls.Sys"
  27. local M = Class.new()
  28. --- Module initialization function.
  29. --
  30. -- @param emulateLibs (boolean) True if libs.lua must be emulated
  31. function M:initModule(emulateLibs)
  32. if emulateLibs then
  33. M.changeDirectory = M.changeCurrentDirectory
  34. -- argh, ds_system and System both have a function of the same name, but
  35. -- they're different
  36. System.listDirectory = M._listDirectoryFull
  37. end
  38. self:resetModule()
  39. end
  40. --- Resets the module state (e.g. for use with a new script)
  41. function M:resetModule()
  42. M._currentDirectoryList = nil
  43. end
  44. --- Gets the current working directory [ML 2+ API].
  45. --
  46. -- @return (string)
  47. function M.currentDirectory()
  48. local dir = ( Sys.convertFakeRootToRoot(wx.wxGetCwd().."/") )
  49. return dir:gsub("//", "/")
  50. end
  51. --- Changes the current working directory [ML 2+ API].
  52. --
  53. -- @param path (string) The path of the directory
  54. function M.changeCurrentDirectory(path)
  55. Mls.logger:debug("changing current directory to "..path, "system")
  56. wx.wxSetWorkingDirectory(Sys.getFile(path))
  57. end
  58. --- Removes a file or an empty folder [ML 2+ API].
  59. --
  60. -- @param name (string) The name of the file or directory to remove
  61. function M.remove(name)
  62. Mls.logger:debug("deleting file/dir "..name, "system")
  63. os.remove(name)
  64. end
  65. --- Renames file or an empty folder [ML 2+ API].
  66. --
  67. -- @param oldName (string) The name of the file or directory to rename
  68. -- @param newName (string) The new name of the file or directory
  69. --
  70. -- @todo The ML doc says it should rename files or *empty* folders.
  71. -- Since it's a rename and not a remove, does the folder have to be empty?
  72. -- Or is it a copy-paste gone wrong from the remove() function ?
  73. -- I don't know, I haven't tested in real ML
  74. function M.rename(oldName, newName)
  75. Mls.logger:debug("renaming "..oldName.." to "..newName, "system")
  76. os.rename(oldName, newName)
  77. end
  78. --- Creates a new directory [ML 2+ API].
  79. --
  80. -- @param name (string) The path and name of the directory
  81. --
  82. -- @todo On some systems, you can set the permissions for the created dir,
  83. -- I don't set it, so it's 0777 by default. Maybe I should set a more
  84. -- restrictive default ?
  85. function M.makeDirectory(name)
  86. Mls.logger:debug("creating directory "..name, "system")
  87. wx.wxMkdir(Sys.getFile(name))
  88. end
  89. --- Lists the next entry in a directory listing [ML 2+ API].
  90. --
  91. -- If the function has been called on a directory, and not all the entries have
  92. -- been returned by successive calls yet, the listing continues, ignoring the
  93. -- path parameter. Otherwise the function returns the first entry in the
  94. -- directory represented in path.
  95. --
  96. -- @param path (string) The path of the directory to list
  97. --
  98. -- @return (string) The next entry for the directory, prefixed with "*" if it
  99. -- is itself a directory.
  100. -- If there is no more entries for the directory that was
  101. -- currently being listed, returns "##"
  102. --
  103. -- @see _listDirectoryFull
  104. function M.listDirectory(path)
  105. local dirList = M._currentDirectoryList
  106. -- no listing was in progress, so start one
  107. if not dirList then
  108. dirList = M._listDirectoryFull(path)
  109. end
  110. -- pop the first element in dir list
  111. local fileEntry = table.remove(dirList, 1)
  112. local file
  113. if fileEntry then
  114. file = (fileEntry.isDir and "*" or "")..fileEntry.name
  115. M._currentDirectoryList = dirList
  116. else
  117. file = "##"
  118. M._currentDirectoryList = nil
  119. end
  120. return file
  121. end
  122. --- Lists all files and folders of a directory [ML 2+ API].
  123. --
  124. -- NOTE: this is the "libs.lua emulated" version of System.listDirectory(), and
  125. -- ds_system.listDirectory() should always be available as the original
  126. -- version, even when libs emulation is enabled
  127. --
  128. -- @param path (string) The path of the directory to list
  129. --
  130. -- @return (table) A table listing the directory content, each entry being
  131. -- itself a table of files or directories, with key/value items.
  132. -- These keys are "name" (string, the file/directory name) and
  133. -- "isDir" (boolean, tells if an entry is a directory)
  134. function M._listDirectoryFull(path)
  135. path = Sys.getFile(path)
  136. local dotTable = {}
  137. local dirTable = {}
  138. local fileTable = {}
  139. local fileEntry
  140. local dir = wx.wxDir(path)
  141. local found, file
  142. -- I tried to use wx.wxDir.GetAllFiles() instead of this GetFirst/GetNext
  143. -- stuff, but the flags to get the "dots" directories, or prevent recursive
  144. -- directory listing, don't seem to work in the Lua wx module for
  145. -- GetAllFiles() :(. They work with GetFirst(), though
  146. -- WARNING: I know I shouldn't make a sum out of these constants, and do
  147. -- bitwise ops instead, but here it works since their values are 1,2,4,8
  148. found, file = dir:GetFirst("", wx.wxDIR_DOTDOT
  149. + wx.wxDIR_FILES
  150. + wx.wxDIR_DIRS
  151. + wx.wxDIR_HIDDEN)
  152. if found then
  153. repeat
  154. fileEntry = {
  155. name = file,
  156. isDir = wx.wxDirExists(wx.wxFileName(path, file):GetFullPath())
  157. }
  158. if file == "." or file == ".." then
  159. table.insert(dotTable, fileEntry)
  160. elseif fileEntry.isDir then
  161. table.insert(dirTable, fileEntry)
  162. else
  163. table.insert(fileTable, fileEntry)
  164. end
  165. found, file = dir:GetNext()
  166. until not found
  167. end
  168. -- this forces dir to be closed by wxWidgets, since there's no
  169. -- wxDir::Close() function. On Mac OS X, the uLua 3,0 shell, which makes
  170. -- dozens of calls per second (!!!) to listDirectory(), causes error
  171. -- messages if we don't do this ("error 24: Too many open files")
  172. dir = nil
  173. collectgarbage("collect")
  174. local fullTable = dotTable
  175. for _, entry in ipairs(dirTable) do table.insert(fullTable, entry) end
  176. for _, entry in ipairs(fileTable) do table.insert(fullTable, entry) end
  177. return fullTable
  178. end
  179. --- Gets a "part" of current time (i.e. year, month etc).
  180. --
  181. -- @param whichPart (number) A numeric value that defines which part of the time
  182. -- you want to get. The values are:
  183. -- 0 = the year
  184. -- 1 = the month
  185. -- 2 = the hour
  186. -- 3 = the day
  187. -- 4 = the minute
  188. -- 5 = the second
  189. --
  190. -- @return (number) The part you asked for
  191. function M.getCurrentTime(whichPart)
  192. local time = os.date("*t")
  193. if whichPart == 0 then -- TIME_YEAR
  194. return time.year
  195. elseif whichPart == 1 then -- TIME_MONTH
  196. return time.month
  197. elseif whichPart == 2 then -- TIME_DAY
  198. return time.day
  199. elseif whichPart == 3 then -- TIME_HOUR
  200. return time.hour
  201. elseif whichPart == 4 then -- TIME_MINUTE
  202. return time.min
  203. elseif whichPart == 5 then -- TIME_SECOND
  204. return time.sec
  205. --[[
  206. elseif whichPart == 6 then -- TIME_WEEKDAY
  207. return time.wday
  208. elseif whichPart == 7 then -- TIME_YEARDAY
  209. return time.yday
  210. --]]
  211. end
  212. error("Bad parameter")
  213. end
  214. return M