History

My first initial commit with all of mine experimental changes.

Dmitry Kabak (userdima committed 13 years ago

Commit d37b88d1 parent 3f5b46618352 files · +2598093 −-8690Browse files

Showing the changes to java/pkg2/java/io/File_L.b (1 of 18352 files). Show all

Unified · Split

java/pkg2/java/io/File_L.b+332−0
@@ -0,0 +1,332 @@
1implement File_L;
2
3# javal v1.3 generated file: edit with care
4
5include "jni.m";
6 jni : JNI;
7 ClassModule,
8 JString,
9 JArray,
10 JArrayI,
11 JArrayC,
12 JArrayB,
13 JArrayS,
14 JArrayJ,
15 JArrayF,
16 JArrayD,
17 JArrayZ,
18 JArrayJObject,
19 JArrayJClass,
20 JArrayJString,
21 JClass,
22 JThread,
23 JObject : import jni;
24
25#>> extra pre includes here
26 sys : import jni;
27 str : import jni;
28 FALSE : import jni;
29 TRUE : import jni;
30#<<
31
32include "File_L.m";
33
34#>> extra post includes here
35include "workdir.m";
36include "regex.m";
37str_mod : String;
38#<<
39
40init( jni_p : JNI )
41{
42 # save java native inteface mod instance
43 jni = jni_p;
44 #>>extra initialization here
45 sys = jni->sys;
46 str_mod = jni->str;
47 #<<
48}
49
50exists0_Z( this : ref File_obj) : int
51{#>>
52 (ok, nil) := sys->stat(this.path.str);
53 if (ok == -1)
54 return (FALSE);
55 return (TRUE);
56}#<<
57
58canWrite0_Z( this : ref File_obj) : int
59{#>>
60 # this routine is real cludgy...try to open the file for writing
61 # to see if you have WRITE access rights or not.
62
63 local_fd := sys->open(this.path.str, sys->OWRITE);
64 if (local_fd == nil)
65 return (FALSE);
66 return (TRUE);
67}#<<
68
69canRead0_Z( this : ref File_obj) : int
70{#>>
71 # this routine is real cludgy...try to open the file for reading
72 # to see if you have READ access rights or not.
73
74 local_fd := sys->open(this.path.str, sys->OREAD);
75 if (local_fd == nil)
76 return (FALSE);
77 return (TRUE);
78}#<<
79
80isFile0_Z( this : ref File_obj) : int
81{#>>
82 (ok, dir) := sys->stat(this.path.str);
83 if(ok >= 0 && !(dir.mode & Sys->CHDIR))
84 return (TRUE);
85 return (FALSE);
86}#<<
87
88isDirectory0_Z( this : ref File_obj) : int
89{#>>
90 (ok, dir) := sys->stat(this.path.str);
91 if(ok >= 0 && (dir.mode & Sys->CHDIR))
92 return (TRUE);
93 return (FALSE);
94}#<<
95
96lastModified0_J( this : ref File_obj) : big
97{#>>
98 (ok, dir) := sys->stat(this.path.str);
99 if(ok >= 0)
100 return (big dir.mtime);
101 return (big 0);
102}#<<
103
104length0_J( this : ref File_obj) : big
105{#>>
106 (ok, dir) := sys->stat(this.path.str);
107 if(ok >= 0)
108 return (big dir.length);
109 return (big 0);
110}#<<
111
112mkdir0_Z( this : ref File_obj) : int
113{#>>
114 (ok, nil) := sys->stat(this.path.str);
115 if(ok >= 0)
116 return (FALSE);
117 fd := sys->create(this.path.str, sys->OREAD, sys->CHDIR + 8r777);
118 if(fd == nil)
119 return (FALSE);
120 fd = nil;
121 return (TRUE);
122}#<<
123
124renameTo0_rFile_Z( this : ref File_obj, p0 : ref File_obj) : int
125{#>>
126 # this routine is based on the appl/cmd/mv command in inferno....
127
128 #put from and to file names into complete path names
129 frompath := pathname(this.path.str);
130 topath := pathname(p0.path.str);
131
132 #seperate the name of the file from the directory the files reside in
133 (fromdir, fromname) := split(frompath);
134 (todir, toname) := split(topath);
135
136 (i, dirf) := sys->stat(this.path.str);
137 if ( i == -1 )
138 return FALSE;
139
140 # are the files in the same directory????
141 if ( samefile(fromdir, todir) ) {
142 if ( toname == fromname )
143 return TRUE;
144 (j, dirt) := sys->stat(p0.path.str);
145 if ( j == 0 ) {
146 if ( (dirf.mode | dirt.mode) & Sys->CHDIR )
147 return FALSE;
148 hardremove(topath);
149 }
150 dirf.name = toname;
151 if ( sys->wstat(this.path.str, dirf) >= 0 )
152 return TRUE;
153 }
154 #open the from file....
155 if ( (fdf := sys->open(this.path.str, Sys->OREAD)) == nil )
156 return FALSE;
157
158 #open the to file....
159 if ( (fdt := sys->create(p0.path.str, Sys->OWRITE, dirf.mode)) == nil )
160 return FALSE;;
161
162 #copy data from from file to to file....
163 if ( (stat := copy1(fdf, fdt)) == TRUE ) {
164 fdf = nil;
165 if ( sys->remove(this.path.str) < 0 )
166 return FALSE;
167 }
168 else {
169 fdt = nil;
170 sys->remove(p0.path.str);
171 }
172 return stat;
173}#<<
174
175delete0_Z( this : ref File_obj) : int
176{#>>
177 if (isFile0_Z(this) == TRUE)
178 return (remove0_b(this));
179 return (FALSE);
180}#<<
181
182rmdir0_Z( this : ref File_obj) : int
183{#>>
184 if (isDirectory0_Z(this) == TRUE)
185 return (remove0_b(this));
186 return (FALSE);
187}#<<
188
189list0_aString( this : ref File_obj) : JArrayJString
190{#>>
191 fd := sys->open(this.path.str, sys->OREAD);
192 if(fd == nil) {
193 return nil;
194 }
195
196 dir_array := array[200] of Sys->Dir;
197 n := 0;
198 for(;;){
199 if(len dir_array - n == 0){
200 # expand dir_array
201 nd := array[2 * len dir_array] of Sys->Dir;
202 nd[0:] = dir_array;
203 dir_array = nd;
204 }
205 nr := sys->dirread(fd, dir_array[n:]);
206 if(nr < 0)
207 return nil;
208 if(nr == 0)
209 break;
210 n += nr;
211 }
212 file_name := array[n] of string;
213
214 for(i := 0; i < n; i++)
215 file_name[i] = dir_array[i].name;
216
217 return (jni->MkAString(file_name));
218}#<<
219
220canonPath_rString_rString( this : ref File_obj, p0 : JString) : JString
221{#>>
222 TRACE_1 := this; # to remove warning msg at compile time;
223
224 path := pathname(p0.str) + "/";
225
226 regex := load Regex Regex->PATH;
227 if ( regex == nil )
228 jni->InitError( sys->sprint( "java.io.File: could not load %s: %r", Regex->PATH ) );
229
230 path = squeeze_out(regex, regex->compile("//", 0), "/", path);
231 path = squeeze_out(regex, regex->compile("/\\./", 0), "/", path);
232 path = squeeze_out(regex, regex->compile("/[^/]+/\\.\\./", 0), "/", path);
233
234 if ( len path > 1 && path[len path - 1] == '/' )
235 path = path[0:len path - 1];
236
237 return (jni->NewString( path));
238}#<<
239
240isAbsolute_Z( this : ref File_obj) : int
241{#>>
242 if (str_mod->prefix("/", this.path.str))
243 return (TRUE);
244 return (FALSE);
245}#<<
246
247
248
249
250
251remove0_b( this : ref File_obj) : int
252{
253 if (sys->remove(this.path.str) < 0)
254 return (FALSE);
255 return (TRUE);
256}
257
258copy1(fdf, fdt : ref Sys->FD): int
259{
260 n : int;
261 buf := array[8192] of byte;
262
263 for ( ; ; ) {
264 n = sys->read(fdf, buf, len buf);
265 if (n<=0)
266 break;
267 n1 := sys->write(fdt, buf, n);
268 if ( n1 != n )
269 return FALSE;
270 }
271 if ( n < 0 )
272 return FALSE;
273 return TRUE;
274}
275
276#convert a file name into complete path name
277pathname(p : string) : string
278{
279 if ( p == nil || p[0] == '/' )
280 return p;
281 gwd := load Workdir Workdir->PATH;
282 if ( gwd == nil )
283 jni->InitError( sys->sprint( "java.io.File: could not load %s: %r", Workdir->PATH ) );
284
285 wd := gwd->init();
286 if ( len wd == 0 )
287 return p;
288 return wd + "/" + p;
289}
290
291#pull out the name of the file only
292split(name : string): (string,string)
293{
294 (d,t) := str_mod->splitr(name, "/");
295 if ( d != nil )
296 return(d,t);
297 else if ( name == ".." )
298 return("../",".");
299 else
300 return("./",name);
301}
302
303# return TRUE if file a is same as file b
304samefile(a,b : string): int
305{
306 if ( a == b )
307 return 1;
308 (i, file_a) := sys->stat(a);
309 (j, file_b) := sys->stat(b);
310 if ( i < 0 || j < 0 )
311 return 0;
312 i = ((file_a.qid.path == file_b.qid.path) && (file_a.qid.vers == file_b.qid.vers) &&
313 (file_a.dev == file_b.dev) && (file_a.dtype == file_b.dtype));
314 return i;
315}
316
317hardremove(a: string)
318{
319 do; while ( sys->remove(a) != -1 );
320}
321
322squeeze_out(r : Regex, pat : Regex->Re, repl, str :string) : string
323{
324 while ( str_mod->prefix("/../", str) )
325 str = str[3:];
326 a := r->execute(pat, str);
327 if (a == nil)
328 return str;
329 (beg, end) := a[0];
330 str = str[0:beg] + repl + str[end:];
331 return squeeze_out(r, pat, repl, str);
332}