Initial import package mtools: Programs for accessing MS-DOS disks without mounting...
[profile/ivi/mtools.git] / stream.c
1 /*  Copyright 1996,1997,1999,2001,2002,2008,2009 Alain Knaff.
2  *  This file is part of mtools.
3  *
4  *  Mtools is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  Mtools is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "sysincludes.h"
19 #include "msdos.h"
20 #include "stream.h"
21
22 int batchmode = 0;
23
24 int flush_stream(Stream_t *Stream)
25 {
26         int ret=0;
27         if(!batchmode) {
28                 if(Stream->Class->flush)
29                         ret |= Stream->Class->flush(Stream);
30                 if(Stream->Next)
31                         ret |= flush_stream(Stream->Next);
32         }
33         return ret;
34 }
35
36 Stream_t *copy_stream(Stream_t *Stream)
37 {
38         if(Stream)
39                 Stream->refs++;
40         return Stream;
41 }
42
43 int free_stream(Stream_t **Stream)
44 {
45         int ret=0;
46
47         if(!*Stream)
48                 return -1;
49         if(! --(*Stream)->refs){
50                 if((*Stream)->Class->flush)
51                         ret |= (*Stream)->Class->flush(*Stream);
52                 if((*Stream)->Class->freeFunc)
53                         ret |= (*Stream)->Class->freeFunc(*Stream);
54                 if((*Stream)->Next)
55                         ret |= free_stream(&(*Stream)->Next);
56                 Free(*Stream);
57         } else if ( (*Stream)->Next )
58                 ret |= flush_stream((*Stream)->Next);           
59         *Stream = NULL;
60         return ret;
61 }
62
63
64 #define GET_DATA(stream, date, size, type, address) \
65 (stream)->Class->get_data( (stream), (date), (size), (type), (address) )
66
67
68 int get_data_pass_through(Stream_t *Stream, time_t *date, mt_size_t *size,
69                           int *type, int *address)
70 {
71        return GET_DATA(Stream->Next, date, size, type, address);
72 }
73
74 int read_pass_through(Stream_t *Stream, char *buf, mt_off_t start, size_t len)
75 {
76         return READS(Stream->Next, buf, start, len);
77 }
78
79 int write_pass_through(Stream_t *Stream, char *buf, mt_off_t start, size_t len)
80 {
81         return WRITES(Stream->Next, buf, start, len);
82 }
83
84 doscp_t *get_dosConvert_pass_through(Stream_t *Stream)
85 {
86         return GET_DOSCONVERT(Stream->Next);
87 }