1 | /* ************************************************************************** */
|
---|
2 | /* * For conditions of distribution and use, * */
|
---|
3 | /* * see copyright notice in libmng.h * */
|
---|
4 | /* ************************************************************************** */
|
---|
5 | /* * * */
|
---|
6 | /* * project : libmng * */
|
---|
7 | /* * file : libmng_write.c copyright (c) 2000-2004 G.Juyn * */
|
---|
8 | /* * version : 1.0.9 * */
|
---|
9 | /* * * */
|
---|
10 | /* * purpose : Write management (implementation) * */
|
---|
11 | /* * * */
|
---|
12 | /* * author : G.Juyn * */
|
---|
13 | /* * * */
|
---|
14 | /* * comment : implementation of the write management routines * */
|
---|
15 | /* * * */
|
---|
16 | /* * changes : 0.5.1 - 05/08/2000 - G.Juyn * */
|
---|
17 | /* * - changed strict-ANSI stuff * */
|
---|
18 | /* * 0.5.1 - 05/12/2000 - G.Juyn * */
|
---|
19 | /* * - changed trace to macro for callback error-reporting * */
|
---|
20 | /* * 0.5.1 - 05/16/2000 - G.Juyn * */
|
---|
21 | /* * - moved the actual write_graphic functionality from * */
|
---|
22 | /* * mng_hlapi to its appropriate function here * */
|
---|
23 | /* * * */
|
---|
24 | /* * 0.9.1 - 07/19/2000 - G.Juyn * */
|
---|
25 | /* * - fixed writing of signature * */
|
---|
26 | /* * * */
|
---|
27 | /* * 0.9.2 - 08/05/2000 - G.Juyn * */
|
---|
28 | /* * - changed file-prefixes * */
|
---|
29 | /* * * */
|
---|
30 | /* * 1.0.5 - 08/19/2002 - G.Juyn * */
|
---|
31 | /* * - B597134 - libmng pollutes the linker namespace * */
|
---|
32 | /* * * */
|
---|
33 | /* * 1.0.8 - 07/06/2004 - G.R-P * */
|
---|
34 | /* * - added conditionals around openstream/closestream * */
|
---|
35 | /* * - defend against using undefined Open/Closestream function * */
|
---|
36 | /* * 1.0.8 - 08/02/2004 - G.Juyn * */
|
---|
37 | /* * - added conditional to allow easier writing of large MNG's * */
|
---|
38 | /* * * */
|
---|
39 | /* * 1.0.9 - 09/25/2004 - G.Juyn * */
|
---|
40 | /* * - replaced MNG_TWEAK_LARGE_FILES with permanent solution * */
|
---|
41 | /* * 1.0.9 - 12/20/2004 - G.Juyn * */
|
---|
42 | /* * - cleaned up macro-invocations (thanks to D. Airlie) * */
|
---|
43 | /* * * */
|
---|
44 | /* ************************************************************************** */
|
---|
45 |
|
---|
46 | #include "libmng.h"
|
---|
47 | #include "libmng_data.h"
|
---|
48 | #include "libmng_error.h"
|
---|
49 | #include "libmng_trace.h"
|
---|
50 | #ifdef __BORLANDC__
|
---|
51 | #pragma hdrstop
|
---|
52 | #endif
|
---|
53 | #include "libmng_memory.h"
|
---|
54 | #include "libmng_chunks.h"
|
---|
55 | #include "libmng_chunk_io.h"
|
---|
56 | #include "libmng_write.h"
|
---|
57 |
|
---|
58 | #if defined(__BORLANDC__) && defined(MNG_STRICT_ANSI)
|
---|
59 | #pragma option -A /* force ANSI-C */
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | /* ************************************************************************** */
|
---|
63 |
|
---|
64 | #if defined(MNG_SUPPORT_READ) || defined(MNG_SUPPORT_WRITE)
|
---|
65 | mng_retcode mng_drop_chunks (mng_datap pData)
|
---|
66 | {
|
---|
67 | mng_chunkp pChunk;
|
---|
68 | mng_chunkp pNext;
|
---|
69 | mng_cleanupchunk fCleanup;
|
---|
70 |
|
---|
71 | #ifdef MNG_SUPPORT_TRACE
|
---|
72 | MNG_TRACE (pData, MNG_FN_DROP_CHUNKS, MNG_LC_START);
|
---|
73 | #endif
|
---|
74 |
|
---|
75 | pChunk = pData->pFirstchunk; /* and get first stored chunk (if any) */
|
---|
76 |
|
---|
77 | while (pChunk) /* more chunks to discard ? */
|
---|
78 | {
|
---|
79 | pNext = ((mng_chunk_headerp)pChunk)->pNext;
|
---|
80 | /* call appropriate cleanup */
|
---|
81 | fCleanup = ((mng_chunk_headerp)pChunk)->fCleanup;
|
---|
82 | fCleanup (pData, pChunk);
|
---|
83 |
|
---|
84 | pChunk = pNext; /* neeeext */
|
---|
85 | }
|
---|
86 |
|
---|
87 | pData->pFirstchunk = MNG_NULL;
|
---|
88 | pData->pLastchunk = MNG_NULL;
|
---|
89 |
|
---|
90 | #ifdef MNG_SUPPORT_TRACE
|
---|
91 | MNG_TRACE (pData, MNG_FN_DROP_CHUNKS, MNG_LC_END);
|
---|
92 | #endif
|
---|
93 |
|
---|
94 | return MNG_NOERROR;
|
---|
95 | }
|
---|
96 | #endif /* MNG_SUPPORT_READ || MNG_SUPPORT_WRITE */
|
---|
97 |
|
---|
98 | /* ************************************************************************** */
|
---|
99 |
|
---|
100 | #ifdef MNG_INCLUDE_WRITE_PROCS
|
---|
101 |
|
---|
102 | /* ************************************************************************** */
|
---|
103 |
|
---|
104 | mng_retcode mng_write_graphic (mng_datap pData)
|
---|
105 | {
|
---|
106 | mng_chunkp pChunk;
|
---|
107 | mng_retcode iRetcode;
|
---|
108 | mng_uint32 iWritten;
|
---|
109 |
|
---|
110 | #ifdef MNG_SUPPORT_TRACE
|
---|
111 | MNG_TRACE (pData, MNG_FN_WRITE_GRAPHIC, MNG_LC_START);
|
---|
112 | #endif
|
---|
113 |
|
---|
114 | pChunk = pData->pFirstchunk; /* we'll start with the first, thank you */
|
---|
115 |
|
---|
116 | if (pChunk) /* is there anything to write ? */
|
---|
117 | { /* open the file */
|
---|
118 | if (!pData->bWriting)
|
---|
119 | {
|
---|
120 | #ifndef MNG_NO_OPEN_CLOSE_STREAM
|
---|
121 | if (pData->fOpenstream && !pData->fOpenstream ((mng_handle)pData))
|
---|
122 | MNG_ERROR (pData, MNG_APPIOERROR);
|
---|
123 | #endif
|
---|
124 | {
|
---|
125 | pData->bWriting = MNG_TRUE; /* indicate writing */
|
---|
126 | pData->iWritebufsize = 32768; /* get a temporary write buffer */
|
---|
127 | /* reserve 12 bytes for length, chunkname & crc */
|
---|
128 | MNG_ALLOC (pData, pData->pWritebuf, pData->iWritebufsize+12);
|
---|
129 |
|
---|
130 | /* write the signature */
|
---|
131 | if (((mng_chunk_headerp)pChunk)->iChunkname == MNG_UINT_IHDR)
|
---|
132 | mng_put_uint32 (pData->pWritebuf, PNG_SIG);
|
---|
133 | else
|
---|
134 | if (((mng_chunk_headerp)pChunk)->iChunkname == MNG_UINT_JHDR)
|
---|
135 | mng_put_uint32 (pData->pWritebuf, JNG_SIG);
|
---|
136 | else
|
---|
137 | mng_put_uint32 (pData->pWritebuf, MNG_SIG);
|
---|
138 |
|
---|
139 | mng_put_uint32 (pData->pWritebuf+4, POST_SIG);
|
---|
140 |
|
---|
141 | if (!pData->fWritedata ((mng_handle)pData, pData->pWritebuf, 8, &iWritten))
|
---|
142 | {
|
---|
143 | MNG_FREE (pData, pData->pWritebuf, pData->iWritebufsize+12);
|
---|
144 | MNG_ERROR (pData, MNG_APPIOERROR);
|
---|
145 | }
|
---|
146 |
|
---|
147 | if (iWritten != 8) /* disk full ? */
|
---|
148 | {
|
---|
149 | MNG_FREE (pData, pData->pWritebuf, pData->iWritebufsize+12);
|
---|
150 | MNG_ERROR (pData, MNG_OUTPUTERROR);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | while (pChunk) /* so long as there's something to write */
|
---|
156 | { /* let's call its output routine */
|
---|
157 | iRetcode = ((mng_chunk_headerp)pChunk)->fWrite (pData, pChunk);
|
---|
158 | if (iRetcode) /* on error bail out */
|
---|
159 | return iRetcode;
|
---|
160 | /* neeeext */
|
---|
161 | pChunk = ((mng_chunk_headerp)pChunk)->pNext;
|
---|
162 | }
|
---|
163 |
|
---|
164 | if (!pData->bCreating)
|
---|
165 | { /* free the temporary buffer */
|
---|
166 | MNG_FREE (pData, pData->pWritebuf, pData->iWritebufsize+12);
|
---|
167 |
|
---|
168 | pData->bWriting = MNG_FALSE; /* done writing */
|
---|
169 | /* close the stream now */
|
---|
170 | #ifndef MNG_NO_OPEN_CLOSE_STREAM
|
---|
171 | if (pData->fClosestream && !pData->fClosestream ((mng_handle)pData))
|
---|
172 | MNG_ERROR (pData, MNG_APPIOERROR);
|
---|
173 | #endif
|
---|
174 |
|
---|
175 | } else {
|
---|
176 | /* cleanup the written chunks */
|
---|
177 | iRetcode = mng_drop_chunks (pData);
|
---|
178 | if (iRetcode) /* on error bail out */
|
---|
179 | return iRetcode;
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | #ifdef MNG_SUPPORT_TRACE
|
---|
184 | MNG_TRACE (pData, MNG_FN_WRITE_GRAPHIC, MNG_LC_END);
|
---|
185 | #endif
|
---|
186 |
|
---|
187 | return MNG_NOERROR;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /* ************************************************************************** */
|
---|
191 |
|
---|
192 | #endif /* MNG_INCLUDE_WRITE_PROCS */
|
---|
193 |
|
---|
194 | /* ************************************************************************** */
|
---|
195 | /* * end of file * */
|
---|
196 | /* ************************************************************************** */
|
---|
197 |
|
---|
198 |
|
---|