… | |
… | |
28 | #ifdef DMALLOC |
28 | #ifdef DMALLOC |
29 | #include <dmalloc.h> |
29 | #include <dmalloc.h> |
30 | #endif |
30 | #endif |
31 | |
31 | |
32 | |
32 | |
33 | int psiconv_write(psiconv_buffer *buf,const psiconv_file value) |
33 | int psiconv_write(const psiconv_config config, psiconv_buffer *buf, |
|
|
34 | const psiconv_file value) |
34 | { |
35 | { |
35 | int res; |
36 | int res; |
36 | |
37 | |
37 | if (!value) { |
38 | if (!value) { |
38 | psiconv_warn(0,0,"Can't parse to an empty buffer!"); |
39 | psiconv_warn(config,0,0,"Can't parse to an empty buffer!"); |
39 | return -PSICONV_E_OTHER; |
40 | return -PSICONV_E_OTHER; |
40 | } |
41 | } |
41 | if (!(*buf = psiconv_buffer_new())) |
42 | if (!(*buf = psiconv_buffer_new())) |
42 | return -PSICONV_E_NOMEM; |
43 | return -PSICONV_E_NOMEM; |
43 | |
44 | |
44 | 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; |
45 | 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)))) |
46 | goto ERROR; |
51 | goto ERROR; |
47 | } else 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; |
48 | if ((res =psiconv_write_texted_file(*buf, |
57 | if ((res =psiconv_write_texted_file(config,*buf, |
49 | (psiconv_texted_f) (value->file)))) |
58 | (psiconv_texted_f) (value->file)))) |
50 | goto ERROR; |
59 | goto ERROR; |
51 | #if 0 |
60 | #if 0 |
52 | } else if (value->type == psiconv_sketch_file) { |
61 | } else if (value->type == psiconv_sketch_file) { |
53 | if ((res =psiconv_write_sketch_file(*buf, |
62 | if ((res =psiconv_write_sketch_file(config,*buf, |
54 | (psiconv_sketch_f) (value->file)))) |
63 | (psiconv_sketch_f) (value->file)))) |
55 | goto ERROR; |
64 | goto ERROR; |
56 | } else if (value->type == psiconv_mbm_file) { |
65 | } else if (value->type == psiconv_mbm_file) { |
57 | if ((res =psiconv_write_mbm_file(*buf, |
66 | if ((res =psiconv_write_mbm_file(config,*buf, |
58 | (psiconv_mbm_f) (value->file)))) |
67 | (psiconv_mbm_f) (value->file)))) |
59 | goto ERROR; |
68 | goto ERROR; |
60 | } else if (value->type == psiconv_clipart_file) { |
69 | } else if (value->type == psiconv_clipart_file) { |
61 | if ((res =psiconv_write_clipart_file(*buf, |
70 | if ((res =psiconv_write_clipart_file(config,*buf, |
62 | (psiconv_clipart_f) (value->file)))) |
71 | (psiconv_clipart_f) (value->file)))) |
63 | goto ERROR; |
72 | goto ERROR; |
64 | #endif |
73 | #endif |
65 | } else { |
74 | } else { |
66 | psiconv_warn(0,0,"Unknown or unsupported file type"); |
75 | psiconv_warn(config,0,0,"Unknown or unsupported file type"); |
67 | res = -PSICONV_E_GENERATE; |
76 | res = -PSICONV_E_GENERATE; |
68 | goto ERROR; |
77 | goto ERROR; |
69 | } |
78 | } |
70 | if ((res = psiconv_buffer_resolve(*buf))) |
79 | if ((res = psiconv_buffer_resolve(*buf))) |
71 | goto ERROR; |
80 | goto ERROR; |
… | |
… | |
74 | ERROR: |
83 | ERROR: |
75 | psiconv_buffer_free(*buf); |
84 | psiconv_buffer_free(*buf); |
76 | return res; |
85 | return res; |
77 | } |
86 | } |
78 | |
87 | |
79 | 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) |
80 | { |
90 | { |
81 | psiconv_character_layout base_char; |
91 | psiconv_character_layout base_char; |
82 | psiconv_paragraph_layout base_para; |
92 | psiconv_paragraph_layout base_para; |
83 | int res; |
93 | int res; |
84 | psiconv_section_table_section section_table; |
94 | psiconv_section_table_section section_table; |
85 | psiconv_section_table_entry entry; |
95 | psiconv_section_table_entry entry; |
86 | psiconv_u32 section_table_id; |
96 | psiconv_u32 section_table_id; |
87 | psiconv_buffer buf_texted; |
97 | psiconv_buffer buf_texted; |
88 | |
98 | |
89 | if (!value) { |
99 | if (!value) { |
90 | psiconv_warn(0,0,"Null TextEd file"); |
100 | psiconv_warn(config,0,0,"Null TextEd file"); |
91 | return -PSICONV_E_GENERATE; |
101 | return -PSICONV_E_GENERATE; |
92 | } |
102 | } |
93 | |
103 | |
94 | if (!(section_table = psiconv_list_new(sizeof(*entry)))) { |
104 | if (!(section_table = psiconv_list_new(sizeof(*entry)))) { |
95 | res = -PSICONV_E_NOMEM; |
105 | res = -PSICONV_E_NOMEM; |
… | |
… | |
108 | if (!(base_para = psiconv_basic_paragraph_layout())) { |
118 | if (!(base_para = psiconv_basic_paragraph_layout())) { |
109 | res = -PSICONV_E_NOMEM; |
119 | res = -PSICONV_E_NOMEM; |
110 | goto ERROR4; |
120 | goto ERROR4; |
111 | } |
121 | } |
112 | |
122 | |
113 | if ((res = psiconv_write_header_section(buf,PSICONV_ID_PSION5, |
|
|
114 | PSICONV_ID_DATA_FILE, |
|
|
115 | PSICONV_ID_TEXTED))) |
|
|
116 | goto ERROR5; |
|
|
117 | |
|
|
118 | section_table_id = psiconv_buffer_unique_id(); |
123 | section_table_id = psiconv_buffer_unique_id(); |
119 | if ((res = psiconv_write_offset(buf,section_table_id))) |
124 | if ((res = psiconv_write_offset(config,buf,section_table_id))) |
120 | goto ERROR5; |
125 | goto ERROR5; |
121 | |
126 | |
122 | entry->id = PSICONV_ID_APPL_ID_SECTION; |
127 | entry->id = PSICONV_ID_APPL_ID_SECTION; |
123 | entry->offset = psiconv_buffer_unique_id(); |
128 | entry->offset = psiconv_buffer_unique_id(); |
124 | if ((res = psiconv_list_add(section_table,entry))) |
129 | if ((res = psiconv_list_add(section_table,entry))) |
125 | goto ERROR5; |
130 | goto ERROR5; |
126 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
131 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
127 | goto ERROR5; |
132 | goto ERROR5; |
128 | if ((res=psiconv_write_application_id_section(buf, |
133 | if ((res=psiconv_write_application_id_section(config,buf, |
129 | PSICONV_ID_TEXTED,"TextEd.app"))) |
134 | PSICONV_ID_TEXTED,"TextEd.app"))) |
130 | goto ERROR5; |
135 | goto ERROR5; |
131 | |
136 | |
132 | entry->id = PSICONV_ID_PAGE_LAYOUT_SECTION; |
137 | entry->id = PSICONV_ID_PAGE_LAYOUT_SECTION; |
133 | entry->offset = psiconv_buffer_unique_id(); |
138 | entry->offset = psiconv_buffer_unique_id(); |
134 | if ((res = psiconv_list_add(section_table,entry))) |
139 | if ((res = psiconv_list_add(section_table,entry))) |
135 | goto ERROR5; |
140 | goto ERROR5; |
136 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
141 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
137 | goto ERROR5; |
142 | goto ERROR5; |
138 | if ((res = psiconv_write_page_layout_section(buf,value->page_sec))) |
143 | if ((res = psiconv_write_page_layout_section(config,buf,value->page_sec))) |
139 | goto ERROR5; |
144 | goto ERROR5; |
140 | |
145 | |
141 | entry->id = PSICONV_ID_TEXTED; |
146 | entry->id = PSICONV_ID_TEXTED; |
142 | entry->offset = psiconv_buffer_unique_id(); |
147 | entry->offset = psiconv_buffer_unique_id(); |
143 | if ((res = psiconv_list_add(section_table,entry))) |
148 | if ((res = psiconv_list_add(section_table,entry))) |
144 | goto ERROR5; |
149 | goto ERROR5; |
145 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
150 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
146 | goto ERROR5; |
151 | goto ERROR5; |
147 | if ((res = psiconv_write_texted_section(buf,value->texted_sec, |
152 | if ((res = psiconv_write_texted_section(config,buf,value->texted_sec, |
148 | base_char,base_para,&buf_texted))) |
153 | base_char,base_para,&buf_texted))) |
149 | goto ERROR5; |
154 | goto ERROR5; |
150 | |
155 | |
151 | if ((res = psiconv_buffer_concat(buf,buf_texted))) |
156 | if ((res = psiconv_buffer_concat(buf,buf_texted))) |
152 | goto ERROR6; |
157 | goto ERROR6; |
153 | |
158 | |
154 | |
159 | |
155 | if ((res = psiconv_buffer_add_target(buf,section_table_id))) |
160 | if ((res = psiconv_buffer_add_target(buf,section_table_id))) |
156 | goto ERROR6; |
161 | goto ERROR6; |
157 | |
162 | |
158 | res = psiconv_write_section_table_section(buf,section_table); |
163 | res = psiconv_write_section_table_section(config,buf,section_table); |
159 | |
164 | |
160 | ERROR6: |
165 | ERROR6: |
161 | psiconv_buffer_free(buf_texted); |
166 | psiconv_buffer_free(buf_texted); |
162 | ERROR5: |
167 | ERROR5: |
163 | psiconv_free_paragraph_layout(base_para); |
168 | psiconv_free_paragraph_layout(base_para); |
… | |
… | |
169 | psiconv_list_free(section_table); |
174 | psiconv_list_free(section_table); |
170 | ERROR1: |
175 | ERROR1: |
171 | return res; |
176 | return res; |
172 | } |
177 | } |
173 | |
178 | |
174 | int psiconv_write_word_file(psiconv_buffer buf,psiconv_word_f value) |
179 | int psiconv_write_word_file(const psiconv_config config, |
|
|
180 | psiconv_buffer buf,psiconv_word_f value) |
175 | { |
181 | { |
176 | int res; |
182 | int res; |
177 | psiconv_section_table_section section_table; |
183 | psiconv_section_table_section section_table; |
178 | psiconv_section_table_entry entry; |
184 | psiconv_section_table_entry entry; |
179 | psiconv_u32 section_table_id; |
185 | psiconv_u32 section_table_id; |
180 | |
186 | |
181 | if (!value) { |
187 | if (!value) { |
182 | psiconv_warn(0,0,"Null Word file"); |
188 | psiconv_warn(config,0,0,"Null Word file"); |
183 | return -PSICONV_E_GENERATE; |
189 | return -PSICONV_E_GENERATE; |
184 | } |
190 | } |
185 | |
191 | |
186 | if (!(section_table = psiconv_list_new(sizeof(*entry)))) { |
192 | if (!(section_table = psiconv_list_new(sizeof(*entry)))) { |
187 | res = -PSICONV_E_NOMEM; |
193 | res = -PSICONV_E_NOMEM; |
… | |
… | |
191 | if (!(entry = malloc(sizeof(*entry)))) { |
197 | if (!(entry = malloc(sizeof(*entry)))) { |
192 | res = -PSICONV_E_NOMEM; |
198 | res = -PSICONV_E_NOMEM; |
193 | goto ERROR2; |
199 | goto ERROR2; |
194 | } |
200 | } |
195 | |
201 | |
196 | if ((res = psiconv_write_header_section(buf,PSICONV_ID_PSION5, |
|
|
197 | PSICONV_ID_DATA_FILE, |
|
|
198 | PSICONV_ID_WORD))) |
|
|
199 | goto ERROR3; |
|
|
200 | |
|
|
201 | section_table_id = psiconv_buffer_unique_id(); |
202 | section_table_id = psiconv_buffer_unique_id(); |
202 | if ((res = psiconv_write_offset(buf,section_table_id))) |
203 | if ((res = psiconv_write_offset(config,buf,section_table_id))) |
203 | goto ERROR3; |
204 | goto ERROR3; |
204 | |
205 | |
205 | entry->id = PSICONV_ID_APPL_ID_SECTION; |
206 | entry->id = PSICONV_ID_APPL_ID_SECTION; |
206 | entry->offset = psiconv_buffer_unique_id(); |
207 | entry->offset = psiconv_buffer_unique_id(); |
207 | if ((res = psiconv_list_add(section_table,entry))) |
208 | if ((res = psiconv_list_add(section_table,entry))) |
208 | goto ERROR3; |
209 | goto ERROR3; |
209 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
210 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
210 | goto ERROR3; |
211 | goto ERROR3; |
211 | if ((res=psiconv_write_application_id_section(buf, |
212 | if ((res=psiconv_write_application_id_section(config,buf, |
212 | PSICONV_ID_WORD,"Word.app"))) |
213 | PSICONV_ID_WORD,"Word.app"))) |
213 | goto ERROR3; |
214 | goto ERROR3; |
214 | |
215 | |
215 | entry->id = PSICONV_ID_WORD_STATUS_SECTION; |
216 | entry->id = PSICONV_ID_WORD_STATUS_SECTION; |
216 | entry->offset = psiconv_buffer_unique_id(); |
217 | entry->offset = psiconv_buffer_unique_id(); |
217 | if ((res = psiconv_list_add(section_table,entry))) |
218 | if ((res = psiconv_list_add(section_table,entry))) |
218 | goto ERROR3; |
219 | goto ERROR3; |
219 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
220 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
220 | goto ERROR3; |
221 | goto ERROR3; |
221 | if ((res = psiconv_write_word_status_section(buf,value->status_sec))) |
222 | if ((res = psiconv_write_word_status_section(config,buf,value->status_sec))) |
222 | goto ERROR3; |
223 | goto ERROR3; |
223 | |
224 | |
224 | entry->id = PSICONV_ID_PAGE_LAYOUT_SECTION; |
225 | entry->id = PSICONV_ID_PAGE_LAYOUT_SECTION; |
225 | entry->offset = psiconv_buffer_unique_id(); |
226 | entry->offset = psiconv_buffer_unique_id(); |
226 | if ((res = psiconv_list_add(section_table,entry))) |
227 | if ((res = psiconv_list_add(section_table,entry))) |
227 | goto ERROR3; |
228 | goto ERROR3; |
228 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
229 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
229 | goto ERROR3; |
230 | goto ERROR3; |
230 | if ((res = psiconv_write_page_layout_section(buf,value->page_sec))) |
231 | if ((res = psiconv_write_page_layout_section(config,buf,value->page_sec))) |
231 | goto ERROR3; |
232 | goto ERROR3; |
232 | |
233 | |
233 | entry->id = PSICONV_ID_WORD_STYLES_SECTION; |
234 | entry->id = PSICONV_ID_WORD_STYLES_SECTION; |
234 | entry->offset = psiconv_buffer_unique_id(); |
235 | entry->offset = psiconv_buffer_unique_id(); |
235 | if ((res = psiconv_list_add(section_table,entry))) |
236 | if ((res = psiconv_list_add(section_table,entry))) |
236 | goto ERROR3; |
237 | goto ERROR3; |
237 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
238 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
238 | goto ERROR3; |
239 | goto ERROR3; |
239 | if ((res = psiconv_write_word_styles_section(buf,value->styles_sec))) |
240 | if ((res = psiconv_write_word_styles_section(config,buf,value->styles_sec))) |
240 | goto ERROR3; |
241 | goto ERROR3; |
241 | |
242 | |
242 | entry->id = PSICONV_ID_TEXT_SECTION; |
243 | entry->id = PSICONV_ID_TEXT_SECTION; |
243 | entry->offset = psiconv_buffer_unique_id(); |
244 | entry->offset = psiconv_buffer_unique_id(); |
244 | if ((res = psiconv_list_add(section_table,entry))) |
245 | if ((res = psiconv_list_add(section_table,entry))) |
245 | goto ERROR3; |
246 | goto ERROR3; |
246 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
247 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
247 | goto ERROR3; |
248 | goto ERROR3; |
248 | if ((res = psiconv_write_text_section(buf,value->paragraphs))) |
249 | if ((res = psiconv_write_text_section(config,buf,value->paragraphs))) |
249 | goto ERROR3; |
250 | goto ERROR3; |
250 | |
251 | |
251 | entry->id = PSICONV_ID_LAYOUT_SECTION; |
252 | entry->id = PSICONV_ID_LAYOUT_SECTION; |
252 | entry->offset = psiconv_buffer_unique_id(); |
253 | entry->offset = psiconv_buffer_unique_id(); |
253 | if ((res = psiconv_list_add(section_table,entry))) |
254 | if ((res = psiconv_list_add(section_table,entry))) |
254 | goto ERROR3; |
255 | goto ERROR3; |
255 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
256 | if ((res = psiconv_buffer_add_target(buf,entry->offset))) |
256 | goto ERROR3; |
257 | goto ERROR3; |
257 | if ((res = psiconv_write_styled_layout_section(buf,value->paragraphs, |
258 | if ((res = psiconv_write_styled_layout_section(config,buf,value->paragraphs, |
258 | value->styles_sec))) |
259 | value->styles_sec))) |
259 | goto ERROR3; |
260 | goto ERROR3; |
260 | |
261 | |
261 | if ((res = psiconv_buffer_add_target(buf,section_table_id))) |
262 | if ((res = psiconv_buffer_add_target(buf,section_table_id))) |
262 | goto ERROR3; |
263 | goto ERROR3; |
263 | |
264 | |
264 | res = psiconv_write_section_table_section(buf,section_table); |
265 | res = psiconv_write_section_table_section(config,buf,section_table); |
265 | |
266 | |
266 | ERROR3: |
267 | ERROR3: |
267 | free(entry); |
268 | free(entry); |
268 | ERROR2: |
269 | ERROR2: |
269 | psiconv_list_free(section_table); |
270 | psiconv_list_free(section_table); |