/[public]/psiconv/trunk/lib/psiconv/generate_simple.c
ViewVC logotype

Diff of /psiconv/trunk/lib/psiconv/generate_simple.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 79 Revision 232
1/* 1/*
2 generate_simple.c - Part of psiconv, a PSION 5 file formats converter 2 generate_simple.c - Part of psiconv, a PSION 5 file formats converter
3 Copyright (c) 2000 Frodo Looijaard <frodol@dds.nl> 3 Copyright (c) 2000-2004 Frodo Looijaard <frodol@dds.nl>
4 4
5 This program is free software; you can redistribute it and/or modify 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 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 7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version. 8 (at your option) any later version.
15 You should have received a copy of the GNU General Public License 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 16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/ 18*/
19 19
20
20#include "config.h" 21#include "config.h"
21#include "compat.h" 22#include "compat.h"
22 23
24#include <string.h>
25#include <stdlib.h>
26
23#include "generate_routines.h" 27#include "generate_routines.h"
24#include "error.h" 28#include "error.h"
25 29
26int psiconv_write_u8(psiconv_buffer buf,const psiconv_u8 value) 30#ifdef DMALLOC
31#include <dmalloc.h>
32#endif
33
34static int psiconv_write_string_aux(const psiconv_config config,
35 psiconv_buffer buf, int lev,
36 const psiconv_string_t value,int kind);
37
38int psiconv_write_u8(const psiconv_config config,psiconv_buffer buf,
39 int lev,const psiconv_u8 value)
27{ 40{
41 int res;
42 psiconv_progress(config,lev,0,"Writing u8");
43 psiconv_debug(config,lev+1,0,"Value: %02x",value);
28 return psiconv_buffer_add(buf,value); 44 res = psiconv_buffer_add(buf,value);
45 if (res)
46 psiconv_error(config,lev,0,"Out of memory error");
47 return res;
29} 48}
30 49
31int psiconv_write_u16(psiconv_buffer buf,const psiconv_u16 value) 50int psiconv_write_u16(const psiconv_config config,psiconv_buffer buf,
51 int lev,const psiconv_u16 value)
32{ 52{
33 int res; 53 int res;
54 psiconv_progress(config,lev,0,"Writing u16");
55 psiconv_debug(config,lev+1,0,"Value: %04x",value);
34 if ((res = psiconv_buffer_add(buf,value & 0xff))) 56 if ((res = psiconv_buffer_add(buf,value & 0xff)))
57 goto ERROR;
58 if ((res = psiconv_buffer_add(buf,(value & 0xff00) >> 8)))
59 goto ERROR;
60ERROR:
61 if (res)
62 psiconv_error(config,lev,0,"Out of memory error");
35 return res; 63 return res;
36 return psiconv_buffer_add(buf,(value & 0xff00) >> 8);
37} 64}
38 65
39int psiconv_write_u32(psiconv_buffer buf,const psiconv_u32 value) 66int psiconv_write_u32(const psiconv_config config,psiconv_buffer buf,
67 int lev,const psiconv_u32 value)
40{ 68{
41 int res; 69 int res;
70 psiconv_progress(config,lev,0,"Writing u32");
71 psiconv_debug(config,lev+1,0,"Value: %08x",value);
72
42 if ((res = psiconv_buffer_add(buf,value & 0xff))) 73 if ((res = psiconv_buffer_add(buf,value & 0xff)))
43 return res; 74 goto ERROR;
44 if ((res = psiconv_buffer_add(buf,(value & 0xff00) >> 8))) 75 if ((res = psiconv_buffer_add(buf,(value & 0xff00) >> 8)))
45 return res; 76 goto ERROR;
46 if ((res = psiconv_buffer_add(buf,(value & 0xff0000) >> 16))) 77 if ((res = psiconv_buffer_add(buf,(value & 0xff0000) >> 16)))
47 return res; 78 goto ERROR;
48 return psiconv_buffer_add(buf,(value & 0xff000000) >> 24); 79 if ((res = psiconv_buffer_add(buf,(value & 0xff000000) >> 24)))
80 goto ERROR;
81ERROR:
82 if (res)
83 psiconv_error(config,lev,0,"Out of memory error");
84 return res;
49} 85}
50 86
51int psiconv_write_S(psiconv_buffer buf, const psiconv_u32 value) 87int psiconv_write_S(const psiconv_config config,psiconv_buffer buf,
88 int lev,const psiconv_u32 value)
52{ 89{
90 int res;
91
92 psiconv_progress(config,lev,0,"Writing S");
93 psiconv_debug(config,lev+1,0,"Value: %08x",value);
53 if (value < 0x40) 94 if (value < 0x40)
54 return psiconv_write_u8(buf,value * 4 + 2); 95 res = psiconv_write_u8(config,buf,lev+2,value * 4 + 2);
55 else if (value < 0x2000) 96 else if (value < 0x2000)
56 return psiconv_write_u16(buf,value * 8 + 3); 97 res = psiconv_write_u16(config,buf,lev+2,value * 8 + 3);
57 else { 98 else {
58 psiconv_warn(0,psiconv_buffer_length(buf), 99 psiconv_error(config,0,psiconv_buffer_length(buf),
59 "Don't know how to write S value larger than 0x2000 " 100 "Don't know how to write S value larger than 0x2000 "
60 "(trying %x)",value); 101 "(trying %x)",value);
61 return -PSICONV_E_GENERATE; 102 res = -PSICONV_E_GENERATE;
62 } 103 }
104 if (res)
105 psiconv_error(config,lev,0,"Writing of S failed");
106 else
107 psiconv_progress(config,lev,0,"End of S");
108 return res;
63} 109}
64 110
65int psiconv_write_X(psiconv_buffer buf, const psiconv_u32 value) 111int psiconv_write_X(const psiconv_config config,psiconv_buffer buf,
112 int lev, const psiconv_u32 value)
66{ 113{
114 int res;
115 psiconv_progress(config,lev,0,"Writing X");
116 psiconv_debug(config,lev+1,0,"Value: %08x",value);
67 if (value < 0x80) 117 if (value < 0x80)
68 return psiconv_write_u8(buf,value * 2); 118 res = psiconv_write_u8(config,buf,lev+2,value * 2);
69 else if (value < 0x4000) 119 else if (value < 0x4000)
70 return psiconv_write_u16(buf,value * 4 + 1); 120 res = psiconv_write_u16(config,buf,lev+2,value * 4 + 1);
71 else if (value < 0x20000000) 121 else if (value < 0x20000000)
72 return psiconv_write_u16(buf,value * 8 + 3); 122 res = psiconv_write_u16(config,buf,lev+2,value * 8 + 3);
73 else { 123 else {
74 psiconv_warn(0,psiconv_buffer_length(buf), 124 psiconv_error(config,lev,0,
75 "Don't know how to write X value larger than 0x20000000 " 125 "Don't know how to write X value larger than 0x20000000 "
76 "(trying %x)",value); 126 "(trying %x)",value);
127 res = -PSICONV_E_GENERATE;
128 }
129 if (res)
130 psiconv_error(config,lev,0,"Writing of X failed");
131 else
132 psiconv_progress(config,lev,0,"End of X");
133 return res;
134}
135
136int psiconv_write_length(const psiconv_config config,psiconv_buffer buf,
137 int lev,const psiconv_length_t value)
138{
139 int res;
140 psiconv_progress(config,lev,0,"Writing length");
141 psiconv_debug(config,lev+1,0,"Value: %f",value);
142 res = psiconv_write_u32(config,buf,lev+2,value * (1440.0/2.54) + 0.5);
143 if (res)
144 psiconv_error(config,lev,0,"Writing of length failed");
145 else
146 psiconv_progress(config,lev,0,"End of length");
147 return res;
148}
149
150int psiconv_write_size(const psiconv_config config,psiconv_buffer buf,
151 int lev, psiconv_size_t value)
152{
153 int res;
154 psiconv_progress(config,lev,0,"Writing size");
155 psiconv_debug(config,lev+1,0,"Value: %f",value);
156 res = psiconv_write_u32(config,buf,lev+2,value * 20.0 + 0.5);
157 if (res)
158 psiconv_error(config,lev,0,"Writing of size failed");
159 else
160 psiconv_progress(config,lev,0,"End of size");
161 return res;
162}
163
164int psiconv_write_bool(const psiconv_config config,psiconv_buffer buf,
165 int lev,const psiconv_bool_t value)
166{
167 int res;
168 psiconv_progress(config,lev,0,"Writing bool");
169 psiconv_debug(config,lev+1,0,"Value: %s",
170 value == psiconv_bool_false?"False":"True");
171 if ((value != psiconv_bool_true) && (value != psiconv_bool_false))
172 psiconv_warn(config,0,psiconv_buffer_length(buf),
173 "Boolean has non-enum value (found %d, used true)",value);
174 res = psiconv_write_u8(config,buf,lev+2,value == psiconv_bool_false?0:1);
175 if (res)
176 psiconv_error(config,lev,0,"Writing of bool failed");
177 else
178 psiconv_progress(config,lev,0,"End of bool");
179 return res;
180}
181
182int psiconv_write_string(const psiconv_config config,psiconv_buffer buf,
183 int lev, const psiconv_string_t value)
184{
185 int res;
186 psiconv_progress(config,lev,0,"Writing string");
187 res = psiconv_write_string_aux(config,buf,lev+1,value,-1);
188 if (res)
189 psiconv_error(config,lev,0,"Writing of string failed");
190 else
191 psiconv_progress(config,lev,0,"End of string");
192 return res;
193}
194
195int psiconv_write_short_string(const psiconv_config config,psiconv_buffer buf,
196 int lev,const psiconv_string_t value)
197{
198 int res;
199 psiconv_progress(config,lev,0,"Writing short string");
200 res = psiconv_write_string_aux(config,buf,lev+1,value,-2);
201 if (res)
202 psiconv_error(config,lev,0,"Writing of short string failed");
203 else
204 psiconv_progress(config,lev,0,"End of short string");
205 return res;
206}
207
208int psiconv_write_charlist(const psiconv_config config,psiconv_buffer buf,
209 int lev,const psiconv_string_t value)
210{
211 int res;
212 psiconv_progress(config,lev,0,"Writing charlist");
213 res = psiconv_write_string_aux(config,buf,lev+1,value,0);
214 if (res)
215 psiconv_error(config,lev,0,"Writing of charlist failed");
216 else
217 psiconv_progress(config,lev,0,"End of charlist");
218 return res;
219}
220
221
222int psiconv_write_string_aux(const psiconv_config config,psiconv_buffer buf,
223 int lev, const psiconv_string_t value,int kind)
224{
225 int res,i,len;
226 char *printable;
227
228 len = psiconv_unicode_strlen(value);
229 if (!value) {
230 psiconv_error(config,lev,0, "NULL string");
77 return -PSICONV_E_GENERATE; 231 return -PSICONV_E_GENERATE;
78 } 232 }
79}
80 233
81int psiconv_write_offset(psiconv_buffer buf, const psiconv_u32 value) 234 if (!(printable = psiconv_make_printable(config,value))) {
82{ 235 psiconv_error(config,lev,0,"Out of memory error");
83 return psiconv_write_u32(buf,value + psiconv_buffer_base_offset(buf));
84}
85
86int psiconv_write_length(psiconv_buffer buf, const psiconv_length_t value)
87{
88 return psiconv_write_u32(buf,value * (1440.0/2.54) + 0.5);
89}
90
91int psiconv_write_size(psiconv_buffer buf, psiconv_size_t value)
92{
93 return psiconv_write_u32(buf,value * 20.0 + 0.5);
94}
95
96int psiconv_write_bool(psiconv_buffer buf, const psiconv_bool_t value)
97{
98 if ((value != psiconv_bool_true) && (value != psiconv_bool_false))
99 psiconv_warn(0,psiconv_buffer_length(buf),
100 "Boolean has non-enum value (found %d)",value);
101 return psiconv_write_u8(buf,value == psiconv_bool_false?0:1);
102}
103
104int psiconv_write_string(psiconv_buffer buf, const psiconv_string_t value)
105{
106 int res,i;
107 if (!value) {
108 psiconv_warn(0,psiconv_buffer_length(buf),
109 "NULL string");
110 return -PSICONV_E_GENERATE; 236 return -PSICONV_E_NOMEM;
111 } 237 }
112 if ((res = psiconv_write_S(buf,strlen(value)))) 238 psiconv_debug(config,lev+1,0,"Value: %s",printable);
239 free(printable);
240
241 if (kind == -1)
242 res = psiconv_write_S(config,buf,lev+2,len);
243 else if (kind == -2)
244 res = psiconv_write_u8(config,buf,lev+2,len);
245 else
246 res = 0;
247 if (res)
113 return res; 248 return res;
249
114 for (i = 0; i < strlen(value); i++) 250 for (i = 0; i < len; i++)
115 if ((res = psiconv_write_u8(buf,value[i]))) 251 if ((res = psiconv_unicode_write_char(config,buf,lev+2,value[i])))
116 return res; 252 return res;
117 return -PSICONV_E_OK; 253 return -PSICONV_E_OK;
118} 254}
255
256int psiconv_write_offset(const psiconv_config config,psiconv_buffer buf,
257 int lev,psiconv_u32 id)
258{
259 int res;
260 psiconv_progress(config,lev,0,"Writing offset");
261 psiconv_debug(config,lev+1,0,"ID: %08x",id);
262 res = psiconv_buffer_add_reference(buf,id);
263 if (res)
264 psiconv_error(config,lev,0,"Out of memory error");
265 return res;
266}

Legend:
Removed from v.79  
changed lines
  Added in v.232

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26