argh, clean out copy
[supertux.git] / src / unison / physfs-1.1.1 / extras / physfs_rb / physfs / test / test_physfs.rb
1 #
2 # PhysicsFS test program - mimics real physfs_test
3 #
4 require 'readline'
5 require 'physfs'
6
7 def die msg
8   puts "#{msg} - reason: #{PhysicsFS.last_error}"
9 end
10
11 #
12 # parse line to command and args
13
14 def parse line
15   return false if line.nil?
16   
17   if line.strip =~ /^(.*?) (?: (?:\s+(.*)) | $)/x
18     run $1, $2
19   else
20     false
21   end
22 end
23
24 #
25 # parse command args
26
27 def parse_args args
28   args.strip!
29
30   dquoted  = /^ " (.*?) "/x
31   squoted  = /^ ' (.*?) '/x
32   unquoted = /^([^\s\'\"]+)/
33   
34   regexps = [dquoted, squoted, unquoted]
35   
36   result = []
37   while args != ""
38     regexps.each do |r|
39       if args =~ r
40         result << $1
41         args.sub! r, ""
42         args.sub!(/\s+/, "")
43         break
44       end
45     end
46   end
47   result
48 end
49
50 def usage cmd, prefix = "usage: "
51   print prefix
52   args = Commands::HELP[cmd]
53   if args
54     print cmd
55     args.scan(/\w+/).each {|x|
56       print " <#{x}>"
57     }
58     puts
59   else
60     puts %|#{cmd} (no arguments)|
61   end
62 end
63   
64 # commands go below
65 module Commands
66   HELP = {
67     "init"           => "argv0",
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",
75     "mkdir"          => "dirToMk",
76     "delete"         => "dirToDelete",
77     "getrealdir"     => "fileToFind",
78     "exists"         => "fileToCheck",
79     "isdir"          => "fileToCheck",
80     "issymlink"      => "fileToCheck",
81     "cat"            => "fileToCat",
82     "filelength"     => "fileToCheck",
83     "append"         => "fileToAppend",
84     "write"          => "fileToCreateOrTrash",
85     "getlastmodtime" => "fileToExamine"
86   }
87
88   def quit_cmd
89     exit
90   end
91
92   alias q_cmd quit_cmd
93
94   def help_cmd
95     commands = ::Commands.instance_methods.grep(/_cmd$/).sort
96     puts "Commands:"
97     commands.each do |c|
98       usage c.sub("_cmd", ""), "  - "
99     end
100
101     true
102   end
103
104   def e val
105     if val
106       puts "Successful."
107     else
108       puts "Failure. reason: #{PhysicsFS.last_error}"
109     end
110     true
111   end
112
113   def init_cmd arg
114     e PhysicsFS.init(arg)
115   end
116
117   def deinit_cmd
118     e PhysicsFS.deinit
119   end
120
121   def addarchive_cmd archive, append
122     e PhysicsFS.add_to_search_path(archive, append)
123   end
124
125   def removearchive_cmd archive
126     e PhysicsFS.remove_from_search_path archive
127   end
128
129   def enumerate_cmd path
130     entries = PhysicsFS.enumerate(path)
131     entries.each {|x|
132       puts x
133     }
134     true
135   end
136
137   alias ls_cmd enumerate_cmd
138
139   def getlasterror_cmd
140     puts "Last error is [#{PhysicsFS.last_error}]"
141     true
142   end
143
144   def getdirsep_cmd
145     puts "Directory separator is [#{PhysicsFS.dir_separator}]"
146     true
147   end
148
149   def getcdromdirs_cmd
150     dirs = PhysicsFS.cdrom_dirs
151     dirs.each {|x|
152       puts x
153     }
154     puts " total [#{dirs.length}] drives."
155     true
156   end
157
158   def getsearchpath_cmd
159     spath = PhysicsFS.search_path
160     spath.each {|x|
161       puts x
162     }
163     puts "total [#{spath.length}] directories."
164     true
165   end
166
167   def getbasedir_cmd
168     dir = PhysicsFS.base_dir
169     puts dir if dir
170     true
171   end
172
173   def getuserdir_cmd
174     puts PhysicsFS.user_dir
175     true
176   end
177
178   def getwritedir_cmd
179     dir = PhysicsFS.write_dir
180     if dir
181       puts "Write directory is [#{dir}]."
182     else
183       puts "No write directory defined."
184     end
185     true
186   end
187
188   def setwritedir_cmd dir
189     e(PhysicsFS.write_dir = dir)
190   end
191
192   def permitsymlinks_cmd val
193     if val.to_i == 1
194       PhysicsFS.permit_symlinks true
195       puts "Symlinks are now permitted"
196     else
197       PhysicsFS.permit_symlinks false
198       puts "Symlinks are now forbidden"
199     end
200     true
201   end
202
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)
207   end
208
209   def mkdir_cmd dir
210     e PhysicsFS.mkdir(dir)
211   end
212
213   def delete_cmd dir
214     e PhysicsFS.delete(dir)
215   end
216
217   def getrealdir_cmd file
218     dir = PhysicsFS.real_dir file
219     if dir
220       puts "Found at [#{dir}]"
221     else
222       puts "Not found."
223     end
224     true
225   end
226
227   def exists_cmd file
228     if PhysicsFS.exists? file
229       puts "File exists"
230     else
231       puts "File does not exist"
232     end
233     true
234   end
235
236   def isdir_cmd file
237     if PhysicsFS.is_directory? file
238       puts "File is a directory"
239     else
240       puts "File is NOT a directory"
241     end
242     true
243   end
244
245   def issymlink_cmd file
246     if PhysicsFS.is_symlink? file
247       puts "File is a symlink"
248     else
249       puts "File is NOT a symlink"
250     end
251     true
252   end
253
254   def cat_cmd filename
255     file = PhysicsFS.open_read filename
256     if file.nil?
257       puts "failed to open. reason: #{PhysicsFS.last_error}"
258       return true
259     end
260
261     puts file.cat
262     true
263   end
264
265   def filelength_cmd filename
266     file = PhysicsFS.open_read filename
267     if file.nil?
268       puts "failed to open. reason: #{PhysicsFS.last_error}"
269       return true
270     end
271
272     puts file.length
273     file.close
274     true
275   end
276
277   WRITE_STR = "Rubyfied PhysicsFS works just fine.\n\n"
278   
279   def append_cmd filename
280     file = PhysicsFS.open_append filename
281     if file.nil?
282       puts "failed to open. reason: #{PhysicsFS.last_error}"
283       return true
284     end
285
286     file.write WRITE_STR, 1, WRITE_STR.length
287     file.close
288     true
289   end
290
291   def write_cmd filename
292     file = PhysicsFS.open_write filename
293     if file.nil?
294       puts "failed to open. reason: #{PhysicsFS.last_error}"
295       return true
296     end
297
298     file.write_str WRITE_STR
299     file.close
300     true
301   end
302
303   def getlastmodtime_cmd filename
304     t = PhysicsFS.last_mod_time filename
305     if t == -1
306       puts "failed to determin. reason: #{PhysicsFS.last_error}"
307     else
308       puts "Last modified: #{Time.at(t)}"
309     end
310     true
311   end
312 end
313
314 include Commands
315
316 def run command, args
317   if args
318     args = parse_args args
319   else
320     args = []
321   end
322
323   begin
324     cmd = method "#{command}_cmd"
325     if args.length == cmd.arity
326       return cmd.call *args
327     else
328       usage command
329       true
330     end
331   rescue NameError
332     puts 'Unknown command. Enter "help" for instructions.'
333     true
334   end
335 end
336
337 if __FILE__ == $0
338   
339   PhysicsFS.init($0) or die "PhysicsFS init failed"
340   
341   puts "PhysicsFS version: #{PhysicsFS.version}"
342   puts
343
344   puts "Supported archives: "
345   puts PhysicsFS.supported_archives
346   puts
347
348   puts 'Enter commands. Enter "help" for instructions.'
349
350   loop {
351     line = Readline::readline "physfs_rb> ", true
352     break unless parse line
353   }
354 end
355
356
357
358