1 |
frodo |
73 |
/* |
2 |
|
|
generate_driver.c - Part of psiconv, a PSION 5 file formats converter |
3 |
|
|
Copyright (c) 2000 Frodo Looijaard <frodol@dds.nl> |
4 |
|
|
|
5 |
|
|
This program is free software; you can redistribute it and/or modify |
6 |
|
|
it under the terms of the GNU General Public License as published by |
7 |
|
|
the Free Software Foundation; either version 2 of the License, or |
8 |
|
|
(at your option) any later version. |
9 |
|
|
|
10 |
|
|
This program is distributed in the hope that it will be useful, |
11 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
|
|
GNU General Public License for more details. |
14 |
|
|
|
15 |
|
|
You should have received a copy of the GNU General Public License |
16 |
|
|
along with this program; if not, write to the Free Software |
17 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
18 |
|
|
*/ |
19 |
|
|
|
20 |
|
|
#include "config.h" |
21 |
|
|
#include "compat.h" |
22 |
|
|
|
23 |
|
|
#include <stdlib.h> |
24 |
|
|
|
25 |
|
|
#include "error.h" |
26 |
|
|
#include "generate_routines.h" |
27 |
|
|
|
28 |
frodo |
142 |
#ifdef DMALLOC |
29 |
|
|
#include <dmalloc.h> |
30 |
|
|
#endif |
31 |
frodo |
73 |
|
32 |
frodo |
142 |
|
33 |
frodo |
168 |
int psiconv_write(const psiconv_config config, psiconv_buffer *buf, |
34 |
|
|
const psiconv_file value) |
35 |
frodo |
73 |
{ |
36 |
|
|
int res; |
37 |
|
|
|
38 |
|
|
if (!value) { |
39 |
frodo |
168 |
psiconv_warn(config,0,0,"Can't parse to an empty buffer!"); |
40 |
frodo |
73 |
return -PSICONV_E_OTHER; |
41 |
|
|
} |
42 |
frodo |
80 |
if (!(*buf = psiconv_buffer_new())) |
43 |
frodo |
75 |
return -PSICONV_E_NOMEM; |
44 |
frodo |
73 |
|
45 |
|
|
if (value->type == psiconv_word_file) { |
46 |
frodo |
168 |
if ((res = psiconv_write_header_section(config,*buf,PSICONV_ID_PSION5, |
47 |
frodo |
166 |
PSICONV_ID_DATA_FILE, |
48 |
|
|
PSICONV_ID_WORD))) |
49 |
|
|
goto ERROR; |
50 |
frodo |
168 |
if ((res =psiconv_write_word_file(config,*buf,(psiconv_word_f) (value->file)))) |
51 |
frodo |
73 |
goto ERROR; |
52 |
frodo |
82 |
} else if (value->type == psiconv_texted_file) { |
53 |
frodo |
168 |
if ((res = psiconv_write_header_section(config,*buf,PSICONV_ID_PSION5, |
54 |
frodo |
166 |
PSICONV_ID_DATA_FILE, |
55 |
|
|
PSICONV_ID_TEXTED))) |
56 |
|
|
goto ERROR; |
57 |
frodo |
168 |
if ((res =psiconv_write_texted_file(config,*buf, |
58 |
frodo |
73 |
(psiconv_texted_f) (value->file)))) |
59 |
|
|
goto ERROR; |
60 |
|
|
#if 0 |
61 |
|
|
} else if (value->type == psiconv_sketch_file) { |
62 |
frodo |
168 |
if ((res =psiconv_write_sketch_file(config,*buf, |
63 |
frodo |
73 |
(psiconv_sketch_f) (value->file)))) |
64 |
|
|
goto ERROR; |
65 |
|
|
} else if (value->type == psiconv_mbm_file) { |
66 |
frodo |
168 |
if ((res =psiconv_write_mbm_file(config,*buf, |
67 |
frodo |
73 |
(psiconv_mbm_f) (value->file)))) |
68 |
|
|
goto ERROR; |
69 |
|
|
} else if (value->type == psiconv_clipart_file) { |
70 |
frodo |
168 |
if ((res =psiconv_write_clipart_file(config,*buf, |
71 |
frodo |
73 |
(psiconv_clipart_f) (value->file)))) |
72 |
|
|
goto ERROR; |
73 |
|
|
#endif |
74 |
|
|
} else { |
75 |
frodo |
168 |
psiconv_warn(config,0,0,"Unknown or unsupported file type"); |
76 |
frodo |
73 |
res = -PSICONV_E_GENERATE; |
77 |
|
|
goto ERROR; |
78 |
|
|
} |
79 |
frodo |
80 |
if ((res = psiconv_buffer_resolve(*buf))) |
80 |
|
|
goto ERROR; |
81 |
frodo |
73 |
return -PSICONV_E_OK; |
82 |
|
|
|
83 |
|
|
ERROR: |
84 |
frodo |
79 |
psiconv_buffer_free(*buf); |
85 |
frodo |
73 |
return res; |
86 |
|
|
} |
87 |
|
|
|
88 |
frodo |
168 |
int psiconv_write_texted_file(const psiconv_config config, |
89 |
|
|
psiconv_buffer buf,psiconv_texted_f value) |
90 |
frodo |
73 |
{ |
91 |
|
|
psiconv_character_layout base_char; |
92 |
|
|
psiconv_paragraph_layout base_para; |
93 |
|
|
int res; |
94 |
|
|
psiconv_section_table_section section_table; |
95 |
|
|
psiconv_section_table_entry entry; |
96 |
frodo |
80 |
psiconv_u32 section_table_id; |
97 |
|
|
psiconv_buffer buf_texted; |
98 |
frodo |
73 |
|
99 |
|
|
if (!value) { |
100 |
frodo |
168 |
psiconv_warn(config,0,0,"Null TextEd file"); |
101 |
frodo |
73 |
return -PSICONV_E_GENERATE; |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
if (!(section_table = psiconv_list_new(sizeof(*entry)))) { |
105 |
|
|
res = -PSICONV_E_NOMEM; |
106 |
|
|
goto ERROR1; |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
if (!(entry = malloc(sizeof(*entry)))) { |
110 |
|
|
res = -PSICONV_E_NOMEM; |
111 |
|
|
goto ERROR2; |
112 |
|
|
} |
113 |
|
|
|
114 |
frodo |
80 |
if (!(base_char = psiconv_basic_character_layout())) { |
115 |
frodo |
73 |
res = -PSICONV_E_NOMEM; |
116 |
|
|
goto ERROR3; |
117 |
|
|
} |
118 |
frodo |
80 |
if (!(base_para = psiconv_basic_paragraph_layout())) { |
119 |
frodo |
73 |
res = -PSICONV_E_NOMEM; |
120 |
|
|
goto ERROR4; |
121 |
|
|
} |
122 |
|
|
|
123 |
frodo |
80 |
section_table_id = psiconv_buffer_unique_id(); |
124 |
frodo |
168 |
if ((res = psiconv_write_offset(config,buf,section_table_id))) |
125 |
frodo |
80 |
goto ERROR5; |
126 |
frodo |
73 |
|
127 |
|
|
entry->id = PSICONV_ID_APPL_ID_SECTION; |
128 |
frodo |
80 |
entry->offset = psiconv_buffer_unique_id(); |
129 |
frodo |
76 |
if ((res = psiconv_list_add(section_table,entry))) |
130 |
frodo |
80 |
goto ERROR5; |
131 |
|
|
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
132 |
|
|
goto ERROR5; |
133 |
frodo |
168 |
if ((res=psiconv_write_application_id_section(config,buf, |
134 |
frodo |
73 |
PSICONV_ID_TEXTED,"TextEd.app"))) |
135 |
frodo |
80 |
goto ERROR5; |
136 |
frodo |
73 |
|
137 |
|
|
entry->id = PSICONV_ID_PAGE_LAYOUT_SECTION; |
138 |
frodo |
80 |
entry->offset = psiconv_buffer_unique_id(); |
139 |
frodo |
76 |
if ((res = psiconv_list_add(section_table,entry))) |
140 |
frodo |
80 |
goto ERROR5; |
141 |
|
|
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
142 |
|
|
goto ERROR5; |
143 |
frodo |
168 |
if ((res = psiconv_write_page_layout_section(config,buf,value->page_sec))) |
144 |
frodo |
80 |
goto ERROR5; |
145 |
frodo |
73 |
|
146 |
|
|
entry->id = PSICONV_ID_TEXTED; |
147 |
frodo |
80 |
entry->offset = psiconv_buffer_unique_id(); |
148 |
frodo |
76 |
if ((res = psiconv_list_add(section_table,entry))) |
149 |
frodo |
80 |
goto ERROR5; |
150 |
|
|
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
151 |
|
|
goto ERROR5; |
152 |
frodo |
168 |
if ((res = psiconv_write_texted_section(config,buf,value->texted_sec, |
153 |
frodo |
80 |
base_char,base_para,&buf_texted))) |
154 |
|
|
goto ERROR5; |
155 |
frodo |
73 |
|
156 |
frodo |
80 |
if ((res = psiconv_buffer_concat(buf,buf_texted))) |
157 |
frodo |
73 |
goto ERROR6; |
158 |
|
|
|
159 |
frodo |
80 |
|
160 |
|
|
if ((res = psiconv_buffer_add_target(buf,section_table_id))) |
161 |
frodo |
73 |
goto ERROR6; |
162 |
|
|
|
163 |
frodo |
168 |
res = psiconv_write_section_table_section(config,buf,section_table); |
164 |
frodo |
73 |
|
165 |
|
|
ERROR6: |
166 |
frodo |
80 |
psiconv_buffer_free(buf_texted); |
167 |
|
|
ERROR5: |
168 |
frodo |
73 |
psiconv_free_paragraph_layout(base_para); |
169 |
frodo |
80 |
ERROR4: |
170 |
frodo |
73 |
psiconv_free_character_layout(base_char); |
171 |
|
|
ERROR3: |
172 |
|
|
free(entry); |
173 |
|
|
ERROR2: |
174 |
|
|
psiconv_list_free(section_table); |
175 |
|
|
ERROR1: |
176 |
|
|
return res; |
177 |
|
|
} |
178 |
frodo |
82 |
|
179 |
frodo |
168 |
int psiconv_write_word_file(const psiconv_config config, |
180 |
|
|
psiconv_buffer buf,psiconv_word_f value) |
181 |
frodo |
82 |
{ |
182 |
|
|
int res; |
183 |
|
|
psiconv_section_table_section section_table; |
184 |
|
|
psiconv_section_table_entry entry; |
185 |
|
|
psiconv_u32 section_table_id; |
186 |
|
|
|
187 |
|
|
if (!value) { |
188 |
frodo |
168 |
psiconv_warn(config,0,0,"Null Word file"); |
189 |
frodo |
82 |
return -PSICONV_E_GENERATE; |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
if (!(section_table = psiconv_list_new(sizeof(*entry)))) { |
193 |
|
|
res = -PSICONV_E_NOMEM; |
194 |
|
|
goto ERROR1; |
195 |
|
|
} |
196 |
|
|
|
197 |
|
|
if (!(entry = malloc(sizeof(*entry)))) { |
198 |
|
|
res = -PSICONV_E_NOMEM; |
199 |
|
|
goto ERROR2; |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
section_table_id = psiconv_buffer_unique_id(); |
203 |
frodo |
168 |
if ((res = psiconv_write_offset(config,buf,section_table_id))) |
204 |
frodo |
82 |
goto ERROR3; |
205 |
|
|
|
206 |
|
|
entry->id = PSICONV_ID_APPL_ID_SECTION; |
207 |
|
|
entry->offset = psiconv_buffer_unique_id(); |
208 |
|
|
if ((res = psiconv_list_add(section_table,entry))) |
209 |
|
|
goto ERROR3; |
210 |
|
|
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
211 |
|
|
goto ERROR3; |
212 |
frodo |
168 |
if ((res=psiconv_write_application_id_section(config,buf, |
213 |
frodo |
82 |
PSICONV_ID_WORD,"Word.app"))) |
214 |
|
|
goto ERROR3; |
215 |
|
|
|
216 |
|
|
entry->id = PSICONV_ID_WORD_STATUS_SECTION; |
217 |
|
|
entry->offset = psiconv_buffer_unique_id(); |
218 |
|
|
if ((res = psiconv_list_add(section_table,entry))) |
219 |
|
|
goto ERROR3; |
220 |
|
|
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
221 |
|
|
goto ERROR3; |
222 |
frodo |
168 |
if ((res = psiconv_write_word_status_section(config,buf,value->status_sec))) |
223 |
frodo |
82 |
goto ERROR3; |
224 |
|
|
|
225 |
|
|
entry->id = PSICONV_ID_PAGE_LAYOUT_SECTION; |
226 |
|
|
entry->offset = psiconv_buffer_unique_id(); |
227 |
|
|
if ((res = psiconv_list_add(section_table,entry))) |
228 |
|
|
goto ERROR3; |
229 |
|
|
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
230 |
|
|
goto ERROR3; |
231 |
frodo |
168 |
if ((res = psiconv_write_page_layout_section(config,buf,value->page_sec))) |
232 |
frodo |
82 |
goto ERROR3; |
233 |
|
|
|
234 |
|
|
entry->id = PSICONV_ID_WORD_STYLES_SECTION; |
235 |
|
|
entry->offset = psiconv_buffer_unique_id(); |
236 |
|
|
if ((res = psiconv_list_add(section_table,entry))) |
237 |
|
|
goto ERROR3; |
238 |
|
|
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
239 |
|
|
goto ERROR3; |
240 |
frodo |
168 |
if ((res = psiconv_write_word_styles_section(config,buf,value->styles_sec))) |
241 |
frodo |
82 |
goto ERROR3; |
242 |
|
|
|
243 |
|
|
entry->id = PSICONV_ID_TEXT_SECTION; |
244 |
|
|
entry->offset = psiconv_buffer_unique_id(); |
245 |
|
|
if ((res = psiconv_list_add(section_table,entry))) |
246 |
|
|
goto ERROR3; |
247 |
|
|
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
248 |
|
|
goto ERROR3; |
249 |
frodo |
168 |
if ((res = psiconv_write_text_section(config,buf,value->paragraphs))) |
250 |
frodo |
82 |
goto ERROR3; |
251 |
|
|
|
252 |
|
|
entry->id = PSICONV_ID_LAYOUT_SECTION; |
253 |
|
|
entry->offset = psiconv_buffer_unique_id(); |
254 |
|
|
if ((res = psiconv_list_add(section_table,entry))) |
255 |
|
|
goto ERROR3; |
256 |
|
|
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
257 |
|
|
goto ERROR3; |
258 |
frodo |
168 |
if ((res = psiconv_write_styled_layout_section(config,buf,value->paragraphs, |
259 |
frodo |
82 |
value->styles_sec))) |
260 |
|
|
goto ERROR3; |
261 |
|
|
|
262 |
|
|
if ((res = psiconv_buffer_add_target(buf,section_table_id))) |
263 |
|
|
goto ERROR3; |
264 |
|
|
|
265 |
frodo |
168 |
res = psiconv_write_section_table_section(config,buf,section_table); |
266 |
frodo |
82 |
|
267 |
|
|
ERROR3: |
268 |
|
|
free(entry); |
269 |
|
|
ERROR2: |
270 |
|
|
psiconv_list_free(section_table); |
271 |
|
|
ERROR1: |
272 |
|
|
return res; |
273 |
|
|
} |