Debian Codegraph @

gcc/m2/mc/mcStream.mod

raw
1 (* mcStream.mod provides an interface to create a file from fragments.
2
3 Copyright (C) 2015-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. *)
21
22 IMPLEMENTATION MODULE mcStream ;
23
24
25 FROM FIO IMPORT File, OpenToWrite, OpenToRead, EOF, ReadNBytes, WriteNBytes, Close, getFileName, IsNoError ;
26 FROM libc IMPORT unlink, printf, getpid, exit ;
27 FROM Indexing IMPORT InitIndex, InBounds, HighIndice, LowIndice, PutIndice, GetIndice, Index, ForeachIndiceInIndexDo ;
28 FROM DynamicStrings IMPORT String, InitString, InitStringCharStar, string ;
29 FROM FormatStrings IMPORT Sprintf2 ;
30 FROM SYSTEM IMPORT ADR ;
31 FROM Storage IMPORT ALLOCATE ;
32
33 IMPORT alists ;
34 IMPORT SFIO ;
35
36
37 TYPE
38 ptrToFile = POINTER TO File ;
39
40 VAR
41 listOfFiles: alists.alist ;
42 frag : Index ;
43 destFile : File ;
44 seenDest : BOOLEAN ;
45
46
47 (*
48 removeLater -
49 *)
50
51 PROCEDURE removeLater (filename: String) : String ;
52 BEGIN
53 alists.includeItemIntoList (listOfFiles, filename) ;
54 RETURN filename
55 END removeLater ;
56
57
58 (*
59 removeNow - removes a single file, s.
60 *)
61
62 PROCEDURE removeNow (s: String) ;
63 BEGIN
64 IF unlink (string (s)) # 0
65 THEN
66 END
67 END removeNow ;
68
69
70 (*
71 removeFiles - remove any fragment.
72 *)
73
74 PROCEDURE removeFiles ;
75 BEGIN
76 alists.foreachItemInListDo (listOfFiles, removeNow) ;
77 alists.killList (listOfFiles) ;
78 listOfFiles := alists.initList ()
79 END removeFiles ;
80
81
82 (*
83 createTemporaryFile -
84 *)
85
86 PROCEDURE createTemporaryFile (id: CARDINAL) : File ;
87 VAR
88 s: String ;
89 f: File ;
90 p: INTEGER ;
91 BEGIN
92 s := InitString ('/tmp/frag-%d-%d.frag') ;
93 p := getpid () ;
94 s := removeLater (Sprintf2 (s, p, id)) ;
95 f := SFIO.OpenToWrite (s) ;
96 RETURN f
97 END createTemporaryFile ;
98
99
100 (*
101 openFrag - create and open fragment, id, and return the file.
102 The file should not be closed by the user.
103 *)
104
105 PROCEDURE openFrag (id: CARDINAL) : File ;
106 VAR
107 f: File ;
108 p: ptrToFile ;
109 BEGIN
110 f := createTemporaryFile (id) ;
111 NEW (p) ;
112 p^ := f ;
113 PutIndice (frag, id, p) ;
114 RETURN f
115 END openFrag ;
116
117
118 (*
119 copy - copies contents of f to the destination file.
120 *)
121
122 PROCEDURE copy (p: ptrToFile) ;
123 CONST
124 maxBuffer = 4096 ;
125 VAR
126 buffer: ARRAY [0..maxBuffer] OF CHAR ;
127 b : CARDINAL ;
128 s : String ;
129 f : File ;
130 BEGIN
131 IF p # NIL
132 THEN
133 f := p^ ;
134 s := InitStringCharStar(getFileName (f)) ;
135 Close (f) ;
136 f := SFIO.OpenToRead (s) ;
137 WHILE (NOT EOF (f)) AND IsNoError (f) DO
138 b := ReadNBytes (f, HIGH (buffer), ADR (buffer)) ;
139 IF IsNoError (f)
140 THEN
141 b := WriteNBytes (destFile, b, ADR (buffer))
142 ELSIF NOT EOF (f)
143 THEN
144 printf ("mcStream.mod:copy: error seen when reading file fragment: %s\n",
145 string (s)) ;
146 exit (1)
147 END
148 END ;
149 Close (f)
150 END
151 END copy ;
152
153
154 (*
155 setDest - informs the stream module and all fragments must be copied
156 info, f.
157 *)
158
159 PROCEDURE setDest (f: File) ;
160 BEGIN
161 seenDest := TRUE ;
162 destFile := f
163 END setDest ;
164
165
166 (*
167 combine - closes all fragments and then writes them in
168 order to the destination file. The dest file
169 is returned.
170 *)
171
172 PROCEDURE combine () : File ;
173 BEGIN
174 IF NOT seenDest
175 THEN
176 HALT
177 END ;
178 ForeachIndiceInIndexDo (frag, copy) ;
179 removeFiles ;
180 RETURN destFile
181 END combine ;
182
183
184 BEGIN
185 listOfFiles := alists.initList () ;
186 seenDest := FALSE ;
187 frag := InitIndex (1)
188 END mcStream.