1 |
/* |
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 |
#ifdef DMALLOC |
29 |
#include <dmalloc.h> |
30 |
#endif |
31 |
|
32 |
|
33 |
int psiconv_write(const psiconv_config config, psiconv_buffer *buf, |
34 |
const psiconv_file value) |
35 |
{ |
36 |
int res; |
37 |
|
38 |
if (!value) { |
39 |
psiconv_warn(config,0,0,"Can't parse to an empty buffer!"); |
40 |
return -PSICONV_E_OTHER; |
41 |
} |
42 |
if (!(*buf = psiconv_buffer_new())) |
43 |
return -PSICONV_E_NOMEM; |
44 |
|
45 |
if (value->type == psiconv_word_file) { |
46 |
if ((res = psiconv_write_header_section(config,*buf,PSICONV_ID_PSION5, |
47 |
PSICONV_ID_DATA_FILE, |
48 |
PSICONV_ID_WORD))) |
49 |
goto ERROR; |
50 |
if ((res =psiconv_write_word_file(config,*buf,(psiconv_word_f) (value->file)))) |
51 |
goto ERROR; |
52 |
} else if (value->type == psiconv_texted_file) { |
53 |
if ((res = psiconv_write_header_section(config,*buf,PSICONV_ID_PSION5, |
54 |
PSICONV_ID_DATA_FILE, |
55 |
PSICONV_ID_TEXTED))) |
56 |
goto ERROR; |
57 |
if ((res =psiconv_write_texted_file(config,*buf, |
58 |
(psiconv_texted_f) (value->file)))) |
59 |
goto ERROR; |
60 |
#if 0 |
61 |
} else if (value->type == psiconv_sketch_file) { |
62 |
if ((res =psiconv_write_sketch_file(config,*buf, |
63 |
(psiconv_sketch_f) (value->file)))) |
64 |
goto ERROR; |
65 |
} else if (value->type == psiconv_mbm_file) { |
66 |
if ((res =psiconv_write_mbm_file(config,*buf, |
67 |
(psiconv_mbm_f) (value->file)))) |
68 |
goto ERROR; |
69 |
} else if (value->type == psiconv_clipart_file) { |
70 |
if ((res =psiconv_write_clipart_file(config,*buf, |
71 |
(psiconv_clipart_f) (value->file)))) |
72 |
goto ERROR; |
73 |
#endif |
74 |
} else { |
75 |
psiconv_warn(config,0,0,"Unknown or unsupported file type"); |
76 |
res = -PSICONV_E_GENERATE; |
77 |
goto ERROR; |
78 |
} |
79 |
if ((res = psiconv_buffer_resolve(*buf))) |
80 |
goto ERROR; |
81 |
return -PSICONV_E_OK; |
82 |
|
83 |
ERROR: |
84 |
psiconv_buffer_free(*buf); |
85 |
return res; |
86 |
} |
87 |
|
88 |
int psiconv_write_texted_file(const psiconv_config config, |
89 |
psiconv_buffer buf,psiconv_texted_f value) |
90 |
{ |
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 |
psiconv_u32 section_table_id; |
97 |
psiconv_buffer buf_texted; |
98 |
|
99 |
if (!value) { |
100 |
psiconv_warn(config,0,0,"Null TextEd file"); |
101 |
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 |
if (!(base_char = psiconv_basic_character_layout())) { |
115 |
res = -PSICONV_E_NOMEM; |
116 |
goto ERROR3; |
117 |
} |
118 |
if (!(base_para = psiconv_basic_paragraph_layout())) { |
119 |
res = -PSICONV_E_NOMEM; |
120 |
goto ERROR4; |
121 |
} |
122 |
|
123 |
section_table_id = psiconv_buffer_unique_id(); |
124 |
if ((res = psiconv_write_offset(config,buf,section_table_id))) |
125 |
goto ERROR5; |
126 |
|
127 |
entry->id = PSICONV_ID_APPL_ID_SECTION; |
128 |
entry->offset = psiconv_buffer_unique_id(); |
129 |
if ((res = psiconv_list_add(section_table,entry))) |
130 |
goto ERROR5; |
131 |
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
132 |
goto ERROR5; |
133 |
if ((res=psiconv_write_application_id_section(config,buf, |
134 |
PSICONV_ID_TEXTED,"TextEd.app"))) |
135 |
goto ERROR5; |
136 |
|
137 |
entry->id = PSICONV_ID_PAGE_LAYOUT_SECTION; |
138 |
entry->offset = psiconv_buffer_unique_id(); |
139 |
if ((res = psiconv_list_add(section_table,entry))) |
140 |
goto ERROR5; |
141 |
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
142 |
goto ERROR5; |
143 |
if ((res = psiconv_write_page_layout_section(config,buf,value->page_sec))) |
144 |
goto ERROR5; |
145 |
|
146 |
entry->id = PSICONV_ID_TEXTED; |
147 |
entry->offset = psiconv_buffer_unique_id(); |
148 |
if ((res = psiconv_list_add(section_table,entry))) |
149 |
goto ERROR5; |
150 |
if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
151 |
goto ERROR5; |
152 |
if ((res = psiconv_write_texted_section(config,buf,value->texted_sec, |
153 |
base_char,base_para,&buf_texted))) |
154 |
goto ERROR5; |
155 |
|
156 |
if ((res = psiconv_buffer_concat(buf,buf_texted))) |
157 |
goto ERROR6; |
158 |
|
159 |
|
160 |
if ((res = psiconv_buffer_add_target(buf,section_table_id))) |
161 |
goto ERROR6; |
162 |
|
163 |
res = psiconv_write_section_table_section(config,buf,section_table); |
164 |
|
165 |
ERROR6: |
166 |
psiconv_buffer_free(buf_texted); |
167 |
ERROR5: |
168 |
psiconv_free_paragraph_layout(base_para); |
169 |
ERROR4: |
170 |
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 |
|
179 |
int psiconv_write_word_file(const psiconv_config config, |
180 |
psiconv_buffer buf,psiconv_word_f value) |
181 |
{ |
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 |
psiconv_warn(config,0,0,"Null Word file"); |
189 |
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 |
if ((res = psiconv_write_offset(config,buf,section_table_id))) |
204 |
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 |
if ((res=psiconv_write_application_id_section(config,buf, |
213 |
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 |
if ((res = psiconv_write_word_status_section(config,buf,value->status_sec))) |
223 |
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 |
if ((res = psiconv_write_page_layout_section(config,buf,value->page_sec))) |
232 |
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 |
if ((res = psiconv_write_word_styles_section(config,buf,value->styles_sec))) |
241 |
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 |
if ((res = psiconv_write_text_section(config,buf,value->paragraphs))) |
250 |
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 |
if ((res = psiconv_write_styled_layout_section(config,buf,value->paragraphs, |
259 |
value->styles_sec))) |
260 |
goto ERROR3; |
261 |
|
262 |
if ((res = psiconv_buffer_add_target(buf,section_table_id))) |
263 |
goto ERROR3; |
264 |
|
265 |
res = psiconv_write_section_table_section(config,buf,section_table); |
266 |
|
267 |
ERROR3: |
268 |
free(entry); |
269 |
ERROR2: |
270 |
psiconv_list_free(section_table); |
271 |
ERROR1: |
272 |
return res; |
273 |
} |