… | |
… | |
23 | #include <stdlib.h> |
23 | #include <stdlib.h> |
24 | |
24 | |
25 | #include "error.h" |
25 | #include "error.h" |
26 | #include "generate_routines.h" |
26 | #include "generate_routines.h" |
27 | |
27 | |
|
|
28 | #ifdef DMALLOC |
|
|
29 | #include <dmalloc.h> |
|
|
30 | #endif |
28 | |
31 | |
29 | int psiconv_write(psiconv_buffer *buf,const psiconv_file value) |
32 | |
|
|
33 | int psiconv_write(const psiconv_config config, psiconv_buffer *buf, |
|
|
34 | const psiconv_file value) |
30 | { |
35 | { |
31 | int res; |
36 | int res; |
32 | |
37 | |
33 | if (!value) { |
38 | if (!value) { |
34 | psiconv_warn(0,0,"Can't parse to an empty buffer!"); |
39 | psiconv_warn(config,0,0,"Can't parse to an empty buffer!"); |
35 | return -PSICONV_E_OTHER; |
40 | return -PSICONV_E_OTHER; |
36 | } |
41 | } |
37 | if (!(*buf = psiconv_new_buffer())) |
42 | if (!(*buf = psiconv_buffer_new())) |
38 | return -PSICONV_E_NOMEM; |
43 | return -PSICONV_E_NOMEM; |
39 | |
44 | |
40 | #if 0 |
|
|
41 | if (value->type == psiconv_word_file) { |
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; |
42 | if ((res =psiconv_write_word_file(*buf,(psiconv_word_f) (value->file)))) |
50 | if ((res =psiconv_write_word_file(config,*buf,(psiconv_word_f) (value->file)))) |
43 | goto ERROR; |
51 | goto ERROR; |
44 | } else |
|
|
45 | #endif |
|
|
46 | if (value->type == psiconv_texted_file) { |
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; |
47 | if ((res =psiconv_write_texted_file(*buf, |
57 | if ((res =psiconv_write_texted_file(config,*buf, |
48 | (psiconv_texted_f) (value->file)))) |
58 | (psiconv_texted_f) (value->file)))) |
49 | goto ERROR; |
59 | goto ERROR; |
50 | #if 0 |
60 | #if 0 |
51 | } else if (value->type == psiconv_sketch_file) { |
61 | } else if (value->type == psiconv_sketch_file) { |
52 | if ((res =psiconv_write_texted_file(*buf, |
62 | if ((res =psiconv_write_sketch_file(config,*buf, |
53 | (psiconv_sketch_f) (value->file)))) |
63 | (psiconv_sketch_f) (value->file)))) |
54 | goto ERROR; |
64 | goto ERROR; |
55 | } else if (value->type == psiconv_mbm_file) { |
65 | } else if (value->type == psiconv_mbm_file) { |
56 | if ((res =psiconv_write_texted_file(*buf, |
66 | if ((res =psiconv_write_mbm_file(config,*buf, |
57 | (psiconv_mbm_f) (value->file)))) |
67 | (psiconv_mbm_f) (value->file)))) |
58 | goto ERROR; |
68 | goto ERROR; |
59 | } else if (value->type == psiconv_clipart_file) { |
69 | } else if (value->type == psiconv_clipart_file) { |
60 | if ((res =psiconv_write_texted_file(*buf, |
70 | if ((res =psiconv_write_clipart_file(config,*buf, |
61 | (psiconv_clipart_f) (value->file)))) |
71 | (psiconv_clipart_f) (value->file)))) |
62 | goto ERROR; |
72 | goto ERROR; |
63 | #endif |
73 | #endif |
64 | } else { |
74 | } else { |
65 | psiconv_warn(0,0,"Unknown or unsupported file type"); |
75 | psiconv_warn(config,0,0,"Unknown or unsupported file type"); |
66 | res = -PSICONV_E_GENERATE; |
76 | res = -PSICONV_E_GENERATE; |
67 | goto ERROR; |
77 | goto ERROR; |
68 | } |
78 | } |
|
|
79 | if ((res = psiconv_buffer_resolve(*buf))) |
|
|
80 | goto ERROR; |
69 | return -PSICONV_E_OK; |
81 | return -PSICONV_E_OK; |
70 | |
82 | |
71 | ERROR: |
83 | ERROR: |
72 | psiconv_free_buffer(*buf); |
84 | psiconv_buffer_free(*buf); |
73 | return res; |
85 | return res; |
74 | } |
86 | } |
75 | |
87 | |
76 | int psiconv_write_texted_file(psiconv_buffer buf,psiconv_texted_f value) |
88 | int psiconv_write_texted_file(const psiconv_config config, |
|
|
89 | psiconv_buffer buf,psiconv_texted_f value) |
77 | { |
90 | { |
78 | psiconv_character_layout base_char; |
91 | psiconv_character_layout base_char; |
79 | psiconv_paragraph_layout base_para; |
92 | psiconv_paragraph_layout base_para; |
80 | int res; |
93 | int res; |
81 | psiconv_buffer extra_buf; |
|
|
82 | psiconv_section_table_section section_table; |
94 | psiconv_section_table_section section_table; |
83 | psiconv_section_table_entry entry; |
95 | psiconv_section_table_entry entry; |
|
|
96 | psiconv_u32 section_table_id; |
|
|
97 | psiconv_buffer buf_texted; |
84 | |
98 | |
85 | if (!value) { |
99 | if (!value) { |
86 | psiconv_warn(0,0,"Null TextEd file"); |
100 | psiconv_warn(config,0,0,"Null TextEd file"); |
87 | return -PSICONV_E_GENERATE; |
101 | return -PSICONV_E_GENERATE; |
88 | } |
102 | } |
89 | |
103 | |
90 | if (!(section_table = psiconv_list_new(sizeof(*entry)))) { |
104 | if (!(section_table = psiconv_list_new(sizeof(*entry)))) { |
91 | res = -PSICONV_E_NOMEM; |
105 | res = -PSICONV_E_NOMEM; |
… | |
… | |
95 | if (!(entry = malloc(sizeof(*entry)))) { |
109 | if (!(entry = malloc(sizeof(*entry)))) { |
96 | res = -PSICONV_E_NOMEM; |
110 | res = -PSICONV_E_NOMEM; |
97 | goto ERROR2; |
111 | goto ERROR2; |
98 | } |
112 | } |
99 | |
113 | |
100 | if (!(extra_buf = psiconv_new_buffer())) { |
|
|
101 | res = -PSICONV_E_NOMEM; |
|
|
102 | goto ERROR3; |
|
|
103 | } |
|
|
104 | |
|
|
105 | if (!(base_char = psiconv_basic_character_layout())) { |
114 | if (!(base_char = psiconv_basic_character_layout())) { |
106 | res = -PSICONV_E_NOMEM; |
115 | res = -PSICONV_E_NOMEM; |
|
|
116 | goto ERROR3; |
|
|
117 | } |
|
|
118 | if (!(base_para = psiconv_basic_paragraph_layout())) { |
|
|
119 | res = -PSICONV_E_NOMEM; |
107 | goto ERROR4; |
120 | goto ERROR4; |
108 | } |
121 | } |
109 | if (!(base_para = psiconv_basic_paragraph_layout())) { |
|
|
110 | res = -PSICONV_E_NOMEM; |
|
|
111 | goto ERROR5; |
|
|
112 | } |
|
|
113 | |
122 | |
114 | if ((res = psiconv_write_header_section(buf,PSICONV_ID_PSION5, |
123 | section_table_id = psiconv_buffer_unique_id(); |
115 | PSICONV_ID_DATA_FILE, |
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, |
116 | PSICONV_ID_TEXTED))) |
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))) |
117 | goto ERROR6; |
157 | goto ERROR6; |
118 | |
158 | |
119 | |
159 | |
120 | entry->id = PSICONV_ID_APPL_ID_SECTION; |
160 | if ((res = psiconv_buffer_add_target(buf,section_table_id))) |
121 | entry->offset = psiconv_list_length(extra_buf) + 0x14; |
|
|
122 | if ((res = psiconv_list_add(section_table,&entry))) |
|
|
123 | goto ERROR6; |
161 | goto ERROR6; |
124 | if ((res=psiconv_write_application_id_section(extra_buf, |
|
|
125 | PSICONV_ID_TEXTED,"TextEd.app"))) |
|
|
126 | goto ERROR6; |
|
|
127 | |
|
|
128 | entry->id = PSICONV_ID_PAGE_LAYOUT_SECTION; |
|
|
129 | entry->offset = psiconv_list_length(extra_buf) + 0x14; |
|
|
130 | if ((res = psiconv_list_add(section_table,&entry))) |
|
|
131 | goto ERROR6; |
|
|
132 | if ((res = psiconv_write_page_layout_section(extra_buf,value->page_sec))) |
|
|
133 | goto ERROR6; |
|
|
134 | |
162 | |
135 | entry->id = PSICONV_ID_TEXTED; |
|
|
136 | entry->offset = psiconv_list_length(extra_buf) + 0x14; |
|
|
137 | if ((res = psiconv_list_add(section_table,&entry))) |
|
|
138 | goto ERROR6; |
|
|
139 | if ((res = psiconv_write_texted_section(extra_buf,value->texted_sec, |
|
|
140 | base_char,base_para))) |
|
|
141 | goto ERROR6; |
|
|
142 | |
|
|
143 | if ((res = psiconv_write_u32(buf,psiconv_list_length(extra_buf) + 0x14))) |
|
|
144 | goto ERROR6; |
|
|
145 | |
|
|
146 | if ((res = psiconv_list_concat(buf,extra_buf))) |
|
|
147 | goto ERROR6; |
|
|
148 | |
|
|
149 | res = psiconv_write_section_table_section(buf,section_table); |
163 | res = psiconv_write_section_table_section(config,buf,section_table); |
150 | |
164 | |
151 | ERROR6: |
165 | ERROR6: |
|
|
166 | psiconv_buffer_free(buf_texted); |
|
|
167 | ERROR5: |
152 | psiconv_free_paragraph_layout(base_para); |
168 | psiconv_free_paragraph_layout(base_para); |
153 | ERROR5: |
169 | ERROR4: |
154 | psiconv_free_character_layout(base_char); |
170 | psiconv_free_character_layout(base_char); |
155 | ERROR4: |
|
|
156 | psiconv_free_buffer(extra_buf); |
|
|
157 | ERROR3: |
171 | ERROR3: |
158 | free(entry); |
172 | free(entry); |
159 | ERROR2: |
173 | ERROR2: |
160 | psiconv_list_free(section_table); |
174 | psiconv_list_free(section_table); |
161 | ERROR1: |
175 | ERROR1: |
162 | return res; |
176 | return res; |
163 | } |
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 | } |