1 {
2 Vampyre Imaging Library
3 by Marek Mauder
4 http://imaginglib.sourceforge.net
6 The contents of this file are used with permission, subject to the Mozilla
7 Public License Version 1.1 (the "License"); you may not use this file except
8 in compliance with the License. You may obtain a copy of the License at
9 http://www.mozilla.org/MPL/MPL-1.1.html
11 Software distributed under the License is distributed on an "AS IS" basis,
12 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
13 the specific language governing rights and limitations under the License.
15 Alternatively, the contents of this file may be used under the terms of the
16 GNU Lesser General Public License (the "LGPL License"), in which case the
17 provisions of the LGPL License are applicable instead of those above.
18 If you wish to allow use of your version of this file only under the terms
19 of the LGPL License and not to allow others to use your version of this file
20 under the MPL, indicate your decision by deleting the provisions above and
21 replace them with the notice and other provisions required by the LGPL
22 License. If you do not delete the provisions above, a recipient may use
23 your version of this file under either the MPL or the LGPL License.
25 For more information about the LGPL: http://www.gnu.org/copyleft/lesser.html
26 }
28 { This unit contains default IO functions for reading from/writting to
29 files, streams and memory.}
32 {$I ImagingOptions.inc}
34 interface
36 uses
39 type
47 var
53 { Helper function that returns size of input (from current position to the end)
54 represented by Handle (and opened and operated on by members of IOFunctions).}
56 { Helper function that initializes TMemoryIORec with given params.}
58 { Reads one text line from input (CR+LF, CR, or LF as line delimiter).}
61 { Writes one text line to input with optional line delimiter.}
65 implementation
67 const
70 type
71 { Based on TaaBufferedStream
72 Copyright (c) Julian M Bucknall 1997, 1999 }
74 private
88 public
101 begin
114 begin
116 begin
117 Commit;
125 begin
130 begin
135 begin
140 var
142 begin
152 var
155 begin
165 begin
167 begin
168 WriteBuffer;
174 var
177 begin
178 // Calculate the actual number of bytes we can read - this depends on
179 // the current position and size of the stream as well as the number
180 // of bytes requested.
186 begin
188 Exit;
190 // Remember to return the result of our calculation
195 ReadBuffer;
196 // Calculate the number of bytes we can read prior to the loop
200 // Copy from the stream buffer to the caller's buffer
202 // Calculate the number of bytes still to read}
205 // while we have bytes to read, read them
207 begin
209 // As we've exhausted this buffer-full, advance to the next, check
210 // to see whether we need to write the buffer out first
212 begin
213 WriteBuffer;
218 ReadBuffer;
219 // Calculate the number of bytes we can read in this cycle
223 // Ccopy from the stream buffer to the caller's buffer
225 // Calculate the number of bytes still to read
228 // Remember our new position
231 begin
239 var
241 begin
242 // Calculate the new position
247 else
252 begin
253 //NewPos := ClampInt(NewPos, 0, FSize); don't do this - for writing
255 // Calculate which page of the file we need to be at
257 // If the new page is different than the old, mark the buffer as being
258 // ready to be replenished, and if need be write out any dirty data
260 begin
262 begin
263 WriteBuffer;
269 // Save the new position
275 var
278 begin
279 // When we write to this stream we always assume that we can write the
280 // requested number of bytes: if we can't (eg, the disk is full) we'll
281 // get an exception somewhere eventually.
283 // Remember to return the result of our calculation
288 ReadBuffer;
289 // Calculate the number of bytes we can write prior to the loop
293 // Copy from the caller's buffer to the stream buffer
295 // Mark our stream buffer as requiring a save to the actual stream,
296 // note that this will suffice for the rest of the routine as well: no
297 // inner routine will turn off the dirty flag.
299 // Calculate the number of bytes still to write
302 // While we have bytes to write, write them
304 begin
306 // As we've filled this buffer, write it out to the actual stream
307 // and advance to the next buffer, reading it if required
309 WriteBuffer;
314 ReadBuffer;
315 // Calculate the number of bytes we can write in this cycle
319 // Copy from the caller's buffer to our buffer
321 // Calculate the number of bytes still to write
324 // Remember our new position
326 // Make sure the count of valid bytes is correct
329 // Make sure the stream size is correct
332 // If we're at the end of the buffer, write it out and advance to the
333 // start of the next page
335 begin
336 WriteBuffer;
344 { File IO functions }
347 var
349 begin
355 omReadWrite:
356 begin
359 else
369 var
371 begin
378 begin
384 begin
389 begin
395 begin
401 begin
405 { Stream IO functions }
408 begin
413 begin
417 begin
423 begin
428 begin
434 begin
440 begin
444 { Memory IO functions }
447 begin
452 begin
456 begin
462 begin
469 //Result := ClampInt(Result, 0, PMemoryIORec(Handle).Size); don't do this - some file formats use it
474 begin
480 var
482 begin
493 var
495 begin
504 { Helper IO functions }
507 var
509 begin
517 begin
525 const
527 var
532 begin
541 begin
545 begin
546 Break;
550 begin
552 begin
554 Exit;
555 end
556 else
557 begin
561 end
563 begin
570 begin
571 Break;
581 var
583 begin
588 initialization
613 ResetFileIO;
615 {
616 File Notes:
618 -- TODOS ----------------------------------------------------
619 - nothing now
621 -- 0.77.1 ---------------------------------------------------
622 - Updated IO Open functions according to changes in ImagingTypes.
623 - Added ReadLine and WriteLine functions.
625 -- 0.23 Changes/Bug Fixes -----------------------------------
626 - Added merge between buffered read-only and write-only file
627 stream adapters - TIFF saving needed both reading and writing.
628 - Fixed bug causing wrong value of TBufferedWriteFile.Size
629 (needed to add buffer pos to size).
631 -- 0.21 Changes/Bug Fixes -----------------------------------
632 - Removed TMemoryIORec.Written, use Position to get proper memory
633 position (Written didn't take Seeks into account).
634 - Added TBufferedReadFile and TBufferedWriteFile classes for
635 buffered file reading/writting. File IO functions now use these
636 classes resulting in performance increase mainly in file formats
637 that read/write many small chunks.
638 - Added fmShareDenyWrite to FileOpenRead. You can now read
639 files opened for reading by Imaging from other apps.
640 - Added GetInputSize and PrepareMemIO helper functions.
642 -- 0.19 Changes/Bug Fixes -----------------------------------
643 - changed behaviour of MemorySeek to act as TStream
644 based Seeks
645 }