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