Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | \ ========== Copyright Header Begin ========================================== |
2 | \ | |
3 | \ Hypervisor Software File: filetool.fth | |
4 | \ | |
5 | \ Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
6 | \ | |
7 | \ - Do no alter or remove copyright notices | |
8 | \ | |
9 | \ - Redistribution and use of this software in source and binary forms, with | |
10 | \ or without modification, are permitted provided that the following | |
11 | \ conditions are met: | |
12 | \ | |
13 | \ - Redistribution of source code must retain the above copyright notice, | |
14 | \ this list of conditions and the following disclaimer. | |
15 | \ | |
16 | \ - Redistribution in binary form must reproduce the above copyright notice, | |
17 | \ this list of conditions and the following disclaimer in the | |
18 | \ documentation and/or other materials provided with the distribution. | |
19 | \ | |
20 | \ Neither the name of Sun Microsystems, Inc. or the names of contributors | |
21 | \ may be used to endorse or promote products derived from this software | |
22 | \ without specific prior written permission. | |
23 | \ | |
24 | \ This software is provided "AS IS," without a warranty of any kind. | |
25 | \ ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, | |
26 | \ INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A | |
27 | \ PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN | |
28 | \ MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR | |
29 | \ ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR | |
30 | \ DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN | |
31 | \ OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR | |
32 | \ FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE | |
33 | \ DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, | |
34 | \ ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF | |
35 | \ SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. | |
36 | \ | |
37 | \ You acknowledge that this software is not designed, licensed or | |
38 | \ intended for use in the design, construction, operation or maintenance of | |
39 | \ any nuclear facility. | |
40 | \ | |
41 | \ ========== Copyright Header End ============================================ | |
42 | \ @(#)filetool.fth 2.8 03/07/17 | |
43 | \ Copyright 1985-1990 Bradley Forthware | |
44 | \ Copyright 1994-2003 Sun Microsystems, Inc. All Rights Reserved. | |
45 | \ Copyright Use is subject to license terms. | |
46 | ||
47 | \ Some convenience words for dealing with files. | |
48 | ||
49 | decimal | |
50 | ||
51 | \ Relative seek | |
52 | : +fseek ( loffset fd -- ) | |
53 | tuck ftell ( fd loffset lpos ) | |
54 | + swap fseek | |
55 | ; | |
56 | \ Relative seek from end of file. loffset should be negative. | |
57 | : fseek-from-end ( loffset fd -- ) | |
58 | tuck fsize ( fd loffset lsize ) | |
59 | + ( fd lposition ) | |
60 | 0 max swap fseek | |
61 | ; | |
62 | ||
63 | \ linefeed constant newline | |
64 | ||
65 | \ Handy file descriptor variables | |
66 | variable ifd | |
67 | variable ofd | |
68 | ||
69 | : $read-open ( name$ -- ) | |
70 | 2dup r/o open-file if ( name$ x ) | |
71 | drop ." Can't open " type ." for reading." cr abort | |
72 | then ( name$ fd ) | |
73 | ifd ! ( name$ ) | |
74 | 2drop | |
75 | ; | |
76 | : reading ( "filename" -- ) safe-parse-word $read-open ; | |
77 | ||
78 | : $write-open ( name$ -- ) | |
79 | 2dup r/w open-file if ( name x ) | |
80 | drop ." Can't open " type ." for writing." cr abort | |
81 | then ( name$ fd ) | |
82 | ofd ! ( name$ ) | |
83 | 2drop | |
84 | ; | |
85 | : $new-file ( name$ -- ) | |
86 | 2dup r/w create-file if ( name$ x ) | |
87 | drop ." Can't create " type cr abort | |
88 | then ( name$ fd ) | |
89 | ofd ! ( name$ ) | |
90 | 2drop | |
91 | ; | |
92 | : writing ( "filename" -- ) safe-parse-word $new-file ; | |
93 | ||
94 | : $append-open ( name$ -- ) | |
95 | 2dup r/w open-file if ( name$ ior ) | |
96 | \ We have to make the file | |
97 | drop $new-file ( ) | |
98 | else \ The file already exists, so seek to the end ( name$ fd ) | |
99 | ofd ! 2drop ( ) | |
100 | 0 ofd @ fseek-from-end ( ) | |
101 | then | |
102 | ; | |
103 | : appending ( "filename" -- ) safe-parse-word $append-open ; | |
104 | ||
105 | : $file-exists? ( name$ -- flag ) \ True if the named file already exists | |
106 | r/o open-file if drop false else close-file drop true then | |
107 | ; | |
108 | ||
109 | : $file, ( adr len -- ) | |
110 | r/o bin or open-file abort" Can't open file" ifd ! | |
111 | ||
112 | here ifd @ fsize dup allot ( adr len ) | |
113 | 2dup ifd @ fgets over <> abort" Short read" ( adr len ) | |
114 | ifd @ fclose ( adr len ) | |
115 | note-string 2drop \ Mark as a sequence of bytes | |
116 | ; | |
117 | ||
118 | \ Backwards compatibility ... | |
119 | ||
120 | : read-open ( name-pstr -- ) count $read-open ; | |
121 | : write-open ( name-pstr -- ) count $write-open ; | |
122 | : new-file ( name-pstr -- ) count $new-file ; | |
123 | : append-open ( name-pstr -- ) count $append-open ; | |
124 | : file-exists? ( name-pstr -- flag ) \ True if the named file already exists | |
125 | read fopen ( fd ) dup if fclose true then | |
126 | ; | |
127 | ||
128 | headers | |
129 |