2 # PhysicsFS test program - mimics real physfs_test
8 puts "#{msg} - reason: #{PhysicsFS.last_error}"
12 # parse line to command and args
15 return false if line.nil?
17 if line.strip =~ /^(.*?) (?: (?:\s+(.*)) | $)/x
30 dquoted = /^ " (.*?) "/x
31 squoted = /^ ' (.*?) '/x
32 unquoted = /^([^\s\'\"]+)/
34 regexps = [dquoted, squoted, unquoted]
50 def usage cmd, prefix = "usage: "
52 args = Commands::HELP[cmd]
55 args.scan(/\w+/).each {|x|
60 puts %|#{cmd} (no arguments)|
68 "addarchive" => "archiveLocation append",
69 "removearchive" => "archiveLocation",
70 "enumerate" => "dirToEnumerate",
71 "ls" => "dirToEnumerate",
72 "setwritedir" => "newWriteDir",
73 "permitsymlinks" => "1or0",
74 "setsaneconfig" => "org appName arcExt includeCdRoms archivesFirst",
76 "delete" => "dirToDelete",
77 "getrealdir" => "fileToFind",
78 "exists" => "fileToCheck",
79 "isdir" => "fileToCheck",
80 "issymlink" => "fileToCheck",
82 "filelength" => "fileToCheck",
83 "append" => "fileToAppend",
84 "write" => "fileToCreateOrTrash",
85 "getlastmodtime" => "fileToExamine"
95 commands = ::Commands.instance_methods.grep(/_cmd$/).sort
98 usage c.sub("_cmd", ""), " - "
108 puts "Failure. reason: #{PhysicsFS.last_error}"
114 e PhysicsFS.init(arg)
121 def addarchive_cmd archive, append
122 e PhysicsFS.add_to_search_path(archive, append)
125 def removearchive_cmd archive
126 e PhysicsFS.remove_from_search_path archive
129 def enumerate_cmd path
130 entries = PhysicsFS.enumerate(path)
137 alias ls_cmd enumerate_cmd
140 puts "Last error is [#{PhysicsFS.last_error}]"
145 puts "Directory separator is [#{PhysicsFS.dir_separator}]"
150 dirs = PhysicsFS.cdrom_dirs
154 puts " total [#{dirs.length}] drives."
158 def getsearchpath_cmd
159 spath = PhysicsFS.search_path
163 puts "total [#{spath.length}] directories."
168 dir = PhysicsFS.base_dir
174 puts PhysicsFS.user_dir
179 dir = PhysicsFS.write_dir
181 puts "Write directory is [#{dir}]."
183 puts "No write directory defined."
188 def setwritedir_cmd dir
189 e(PhysicsFS.write_dir = dir)
192 def permitsymlinks_cmd val
194 PhysicsFS.permit_symlinks true
195 puts "Symlinks are now permitted"
197 PhysicsFS.permit_symlinks false
198 puts "Symlinks are now forbidden"
203 def setsaneconfig_cmd org, appname, ext, includeCdroms, archivesFirst
204 includeCdroms = includeCdroms.to_i == 1
205 archiveFirst = archivesFirst == 1
206 e PhysicsFS.set_sane_config(org, appname, ext, includeCdroms, archivesFirst)
210 e PhysicsFS.mkdir(dir)
214 e PhysicsFS.delete(dir)
217 def getrealdir_cmd file
218 dir = PhysicsFS.real_dir file
220 puts "Found at [#{dir}]"
228 if PhysicsFS.exists? file
231 puts "File does not exist"
237 if PhysicsFS.is_directory? file
238 puts "File is a directory"
240 puts "File is NOT a directory"
245 def issymlink_cmd file
246 if PhysicsFS.is_symlink? file
247 puts "File is a symlink"
249 puts "File is NOT a symlink"
255 file = PhysicsFS.open_read filename
257 puts "failed to open. reason: #{PhysicsFS.last_error}"
265 def filelength_cmd filename
266 file = PhysicsFS.open_read filename
268 puts "failed to open. reason: #{PhysicsFS.last_error}"
277 WRITE_STR = "Rubyfied PhysicsFS works just fine.\n\n"
279 def append_cmd filename
280 file = PhysicsFS.open_append filename
282 puts "failed to open. reason: #{PhysicsFS.last_error}"
286 file.write WRITE_STR, 1, WRITE_STR.length
291 def write_cmd filename
292 file = PhysicsFS.open_write filename
294 puts "failed to open. reason: #{PhysicsFS.last_error}"
298 file.write_str WRITE_STR
303 def getlastmodtime_cmd filename
304 t = PhysicsFS.last_mod_time filename
306 puts "failed to determin. reason: #{PhysicsFS.last_error}"
308 puts "Last modified: #{Time.at(t)}"
316 def run command, args
318 args = parse_args args
324 cmd = method "#{command}_cmd"
325 if args.length == cmd.arity
326 return cmd.call *args
332 puts 'Unknown command. Enter "help" for instructions.'
339 PhysicsFS.init($0) or die "PhysicsFS init failed"
341 puts "PhysicsFS version: #{PhysicsFS.version}"
344 puts "Supported archives: "
345 puts PhysicsFS.supported_archives
348 puts 'Enter commands. Enter "help" for instructions.'
351 line = Readline::readline "physfs_rb> ", true
352 break unless parse line