3 * Copyright (C) 1998-2001 A.J. van Os; Released under GPL
6 * Functions to save the results as a textfile or a drawfile
16 bWrite2File(void *pvBytes
, size_t tSize
, FILE *pFile
, const char *szFilename
)
18 if (fwrite(pvBytes
, sizeof(char), tSize
, pFile
) != tSize
) {
19 werr(0, "I can't write to '%s'", szFilename
);
23 } /* end of bWrite2File */
26 * bText2File - Save the generated draw file to a Text file
29 bText2File(char *szFilename
, void *pvHandle
)
33 draw_textstrhdr tText
;
35 int iToGo
, iSize
, iX
, iYtopPrev
, iHeight
, iLines
;
36 BOOL bFirst
, bIndent
, bSuccess
;
38 fail(szFilename
== NULL
|| szFilename
[0] == '\0');
39 fail(pvHandle
== NULL
);
41 DBG_MSG("bText2File");
44 pDiag
= (diagram_type
*)pvHandle
;
45 pFile
= fopen(szFilename
, "w");
47 werr(0, "I can't open '%s' for writing", szFilename
);
52 iHeight
= (int)lWord2DrawUnits20(DEFAULT_FONT_SIZE
);
54 iToGo
= pDiag
->tInfo
.length
- sizeof(draw_fileheader
);
56 pcTmp
= pDiag
->tInfo
.data
+ sizeof(draw_fileheader
);
57 while (iToGo
> 0 && bSuccess
) {
58 tText
= *(draw_textstrhdr
*)pcTmp
;
60 case draw_OBJFONTLIST
:
61 /* These are not relevant in a textfile */
62 iSize
= ((draw_fontliststrhdr
*)pcTmp
)->size
;
67 /* Compute the number of lines */
69 (iYtopPrev
- tText
.bbox
.y1
+ iHeight
/ 2) / iHeight
;
71 bIndent
= iLines
> 0 || bFirst
;
73 /* Print the newlines */
74 while (iLines
> 0 && bSuccess
) {
75 bSuccess
= bWrite2File("\n",
76 1, pFile
, szFilename
);
79 /* Print the indentation */
80 if (bIndent
&& bSuccess
) {
81 for (iX
= draw_screenToDraw(8);
82 iX
<= tText
.bbox
.x0
&& bSuccess
;
83 iX
+= draw_screenToDraw(16)) {
84 bSuccess
= bWrite2File(" ",
85 1, pFile
, szFilename
);
91 /* Print the text object */
92 pcTmp
+= sizeof(tText
);
93 bSuccess
= bWrite2File(pcTmp
,
94 strlen(pcTmp
), pFile
, szFilename
);
95 pcTmp
+= tText
.size
- sizeof(tText
);
96 /* Setup for the next text object */
98 iYtopPrev
= tText
.bbox
.y1
;
99 iHeight
= tText
.bbox
.y1
- tText
.bbox
.y0
;
102 /* These are not relevant in a textfile */
103 iSize
= ((draw_pathstrhdr
*)pcTmp
)->size
;
109 /* These are not relevant in a textfile */
110 iSize
= ((draw_spristrhdr
*)pcTmp
)->size
;
120 DBG_DEC_C(iToGo
!= 0, iToGo
);
122 bSuccess
= bWrite2File("\n", 1, pFile
, szFilename
);
126 vSetFiletype(szFilename
, FILETYPE_TEXT
);
128 (void)remove(szFilename
);
129 werr(0, "Unable to save textfile '%s'", szFilename
);
132 } /* end of bText2File */
138 vSaveTextfile(diagram_type
*pDiagram
)
141 int iRecLen
, iNbrRecs
, iEstSize
;
143 fail(pDiagram
== NULL
);
145 DBG_MSG("vSaveTextfile");
146 iRecLen
= sizeof(draw_textstrhdr
) + DEFAULT_SCREEN_WIDTH
* 2 / 3;
147 iNbrRecs
= pDiagram
->tInfo
.length
/ iRecLen
+ 1;
148 iEstSize
= iNbrRecs
* DEFAULT_SCREEN_WIDTH
* 2 / 3;
151 tMask
= event_getmask();
153 saveas(FILETYPE_TEXT
, "WordText",
154 iEstSize
, bText2File
,
155 NULL
, NULL
, pDiagram
);
156 event_setmask(tMask
);
157 } /* end of vSaveTextfile */
160 * bDraw2File - Save the generated draw file to a Draw file
162 * Remark: This is not a simple copy action. The origin of the
163 * coordinates (0,0) must move from the top-left corner to the
164 * bottom-left corner.
167 bDraw2File(char *szFilename
, void *pvHandle
)
170 diagram_type
*pDiagram
;
171 draw_fileheader tHdr
;
172 draw_textstrhdr tText
;
173 draw_pathstrhdr tPath
;
174 draw_spristrhdr tSprite
;
175 draw_jpegstrhdr tJpeg
;
178 int iYadd
, iToGo
, iSize
;
181 fail(szFilename
== NULL
|| szFilename
[0] == '\0');
182 fail(pvHandle
== NULL
);
184 DBG_MSG("bDraw2File");
185 NO_DBG_MSG(szFilename
);
187 pDiagram
= (diagram_type
*)pvHandle
;
188 pFile
= fopen(szFilename
, "wb");
190 werr(0, "I can't open '%s' for writing", szFilename
);
193 iToGo
= pDiagram
->tInfo
.length
;
195 pcTmp
= pDiagram
->tInfo
.data
;
196 tHdr
= *(draw_fileheader
*)pcTmp
;
197 iYadd
= -tHdr
.bbox
.y0
;
198 tHdr
.bbox
.y0
+= iYadd
;
199 tHdr
.bbox
.y1
+= iYadd
;
200 bSuccess
= bWrite2File(&tHdr
, sizeof(tHdr
), pFile
, szFilename
);
201 iToGo
-= sizeof(tHdr
);
203 pcTmp
+= sizeof(tHdr
);
204 while (iToGo
> 0 && bSuccess
) {
205 tText
= *(draw_textstrhdr
*)pcTmp
;
207 case draw_OBJFONTLIST
:
208 iSize
= ((draw_fontliststrhdr
*)pcTmp
)->size
;
209 bSuccess
= bWrite2File(pcTmp
,
210 iSize
, pFile
, szFilename
);
215 /* First correct the coordinates */
216 tText
.bbox
.y0
+= iYadd
;
217 tText
.bbox
.y1
+= iYadd
;
218 tText
.coord
.y
+= iYadd
;
219 /* Now write the information to file */
220 bSuccess
= bWrite2File(&tText
,
221 sizeof(tText
), pFile
, szFilename
);
222 pcTmp
+= sizeof(tText
);
223 iSize
= tText
.size
- sizeof(tText
);
225 bSuccess
= bWrite2File(pcTmp
,
226 iSize
, pFile
, szFilename
);
232 tPath
= *(draw_pathstrhdr
*)pcTmp
;
233 /* First correct the coordinates */
234 tPath
.bbox
.y0
+= iYadd
;
235 tPath
.bbox
.y1
+= iYadd
;
236 /* Now write the information to file */
237 bSuccess
= bWrite2File(&tPath
,
238 sizeof(tPath
), pFile
, szFilename
);
239 pcTmp
+= sizeof(tPath
);
240 iSize
= tPath
.size
- sizeof(tPath
);
241 fail(iSize
< 14 * sizeof(int));
242 /* Second correct the path coordinates */
243 piPath
= xmalloc(iSize
);
244 memcpy(piPath
, pcTmp
, iSize
);
250 bSuccess
= bWrite2File(piPath
,
251 iSize
, pFile
, szFilename
);
254 piPath
= xfree(piPath
);
258 tSprite
= *(draw_spristrhdr
*)pcTmp
;
259 /* First correct the coordinates */
260 tSprite
.bbox
.y0
+= iYadd
;
261 tSprite
.bbox
.y1
+= iYadd
;
262 /* Now write the information to file */
263 bSuccess
= bWrite2File(&tSprite
,
264 sizeof(tSprite
), pFile
, szFilename
);
265 pcTmp
+= sizeof(tSprite
);
266 iSize
= tSprite
.size
- sizeof(tSprite
);
268 bSuccess
= bWrite2File(pcTmp
,
269 iSize
, pFile
, szFilename
);
272 iToGo
-= tSprite
.size
;
275 tJpeg
= *(draw_jpegstrhdr
*)pcTmp
;
276 /* First correct the coordinates */
277 tJpeg
.bbox
.y0
+= iYadd
;
278 tJpeg
.bbox
.y1
+= iYadd
;
279 tJpeg
.trfm
[5] += iYadd
;
280 /* Now write the information to file */
281 bSuccess
= bWrite2File(&tJpeg
,
282 sizeof(tJpeg
), pFile
, szFilename
);
283 pcTmp
+= sizeof(tJpeg
);
284 iSize
= tJpeg
.size
- sizeof(tJpeg
);
286 bSuccess
= bWrite2File(pcTmp
,
287 iSize
, pFile
, szFilename
);
298 DBG_DEC_C(iToGo
!= 0, iToGo
);
301 vSetFiletype(szFilename
, FILETYPE_DRAW
);
303 (void)remove(szFilename
);
304 werr(0, "Unable to save drawfile '%s'", szFilename
);
307 } /* end of bDraw2File */
313 vSaveDrawfile(diagram_type
*pDiagram
)
318 fail(pDiagram
== NULL
);
320 DBG_MSG("vSaveDrawfile");
321 iEstSize
= pDiagram
->tInfo
.length
;
324 tMask
= event_getmask();
326 saveas(FILETYPE_DRAW
, "WordDraw",
327 iEstSize
, bDraw2File
,
328 NULL
, NULL
, pDiagram
);
329 event_setmask(tMask
);
330 } /* end of vSaveDrawfile */