1 /*===========================================================================
2 *
3 * PUBLIC DOMAIN NOTICE
4 * National Center for Biotechnology Information
5 *
6 * This software/database is a "United States Government Work" under the
7 * terms of the United States Copyright Act. It was written as part of
8 * the author's official duties as a United States Government employee and
9 * thus cannot be copyrighted. This software/database is freely available
10 * to the public for use. The National Library of Medicine and the U.S.
11 * Government have not placed any restriction on its use or reproduction.
12 *
13 * Although all reasonable efforts have been taken to ensure the accuracy
14 * and reliability of the software and data, the NLM and the U.S.
15 * Government do not and cannot warrant the performance or results that
16 * may be obtained by using this software or data. The NLM and the U.S.
17 * Government disclaim all warranties, express or implied, including
18 * warranties of performance, merchantability or fitness for any particular
19 * purpose.
20 *
21 * Please cite the author in any work or product based on this material.
22 *
23 * ===========================================================================
24 *
25 */
26
27
28 typedef struct KStreamFromFiles KStreamFromFiles;
29 #define KSTREAM_IMPL KStreamFromFiles
30
31 #include <kns/extern.h>
32 #include <kns/stream.h>
33 #include <kns/impl.h>
34 #include <kfs/file.h>
35 #include <kfs/impl.h>
36 #include <klib/rc.h>
37
38 #include <sysalloc.h>
39
40 #include <assert.h>
41 #include <stdlib.h>
42
43 #include "stream-priv.h"
44
45
46 /* the object structure for THIS implementation */
47 struct KStreamFromFiles
48 {
49 /* THIS MUST COME FIRST */
50 KStream dad;
51
52 uint64_t in_pos;
53 uint64_t out_pos;
54
55 const KFile *in;
56 KFile *out;
57 };
58
59
60 /* the required methods */
61
62 static
KStreamFromFilesWhack(KStreamFromFiles * self)63 rc_t CC KStreamFromFilesWhack ( KStreamFromFiles *self )
64 {
65 KFileRelease ( self -> in );
66 KFileRelease ( self -> out );
67 free ( self );
68 return 0;
69 }
70
71 static
KStreamFromFilesRead(const KStreamFromFiles * cself,void * buffer,size_t bsize,size_t * num_read)72 rc_t CC KStreamFromFilesRead ( const KStreamFromFiles *cself,
73 void *buffer, size_t bsize, size_t *num_read )
74 {
75 rc_t rc = KFileRead ( cself -> in, cself -> in_pos, buffer, bsize, num_read );
76 if ( rc == 0 )
77 {
78 KStreamFromFiles *self = ( KStreamFromFiles* ) cself;
79 self -> in_pos += * num_read;
80 }
81
82 return rc;
83 }
84
85 static
KStreamFromFilesWrite(KStreamFromFiles * self,const void * buffer,size_t size,size_t * num_writ)86 rc_t CC KStreamFromFilesWrite ( KStreamFromFiles *self,
87 const void *buffer, size_t size, size_t *num_writ )
88 {
89 rc_t rc = KFileWrite ( self -> out, self -> out_pos, buffer, size, num_writ );
90 if ( rc == 0 )
91 self -> out_pos += * num_writ;
92
93 return rc;
94 }
95
96 /* the vtable */
97 static KStream_vt_v1 vtKStreamFromFiles =
98 {
99 1, 0,
100 KStreamFromFilesWhack,
101 KStreamFromFilesRead,
102 KStreamFromFilesWrite
103 };
104
105
106 /* FromKFilePair
107 * create a KStream from a pair of KFiles
108 * maintains a "pos" marker for input and output files
109 *
110 * "strm" [ OUT ] - resultant KStream
111 *
112 * "read" [ IN, NULL OKAY ] - file to use for stream reading
113 *
114 * "write" [ IN, NULL OKAY ] - file to use for stream writing
115 *
116 * NB - EITHER "read" or "write" may be NULL, but not both.
117 */
KStreamFromKFilePair(KStream ** strm,const KFile * read,KFile * write)118 LIB_EXPORT rc_t CC KStreamFromKFilePair ( KStream **strm,
119 const KFile *read, KFile *write )
120 {
121 rc_t rc;
122
123 if ( strm == NULL )
124 rc = RC ( rcNS, rcStream, rcConstructing, rcParam, rcNull );
125 else
126 {
127 bool can_read = ( read == NULL ) ? false : read -> read_enabled;
128 bool can_write = ( write == NULL ) ? false : write -> write_enabled;
129
130 if ( ! can_read && ! can_write )
131 rc = RC ( rcNS, rcStream, rcConstructing, rcFile, rcNoPerm );
132 else
133 {
134 KStreamFromFiles *sff = calloc ( 1, sizeof *sff );
135 if ( sff == NULL )
136 rc = RC ( rcNS, rcStream, rcConstructing, rcMemory, rcExhausted );
137 else
138 {
139 rc = KStreamInit ( & sff -> dad, ( const KStream_vt* ) & vtKStreamFromFiles,
140 "KStreamFromFilePair", "adapter", can_read, can_write );
141 if ( rc == 0 )
142 {
143 rc = KFileAddRef ( read );
144 if ( rc == 0 )
145 {
146 rc = KFileAddRef ( write );
147 if ( rc == 0 )
148 {
149 sff -> in = read;
150 sff -> out = write;
151 *strm = & sff -> dad;
152
153 return 0;
154 }
155
156 KFileRelease ( read );
157 }
158 }
159
160 free ( sff );
161 }
162 }
163
164 *strm = NULL;
165 }
166
167 return rc;
168 }
169