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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 196 - (show annotations)
Wed Feb 4 12:19:09 2004 UTC (16 years, 4 months ago) by frodo
File MIME type: text/plain
File size: 6268 byte(s)
(Frodo) Copyright dates update

1 /*
2 generate_simple.c - Part of psiconv, a PSION 5 file formats converter
3 Copyright (c) 2000-2004 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
21 #include "config.h"
22 #include "compat.h"
23
24 #include <string.h>
25 #include <stdlib.h>
26
27 #include "generate_routines.h"
28 #include "error.h"
29
30 #ifdef DMALLOC
31 #include <dmalloc.h>
32 #endif
33
34 static int psiconv_write_string_aux(const psiconv_config config,
35 psiconv_buffer buf, int lev,
36 const psiconv_string_t value,int kind);
37
38 int psiconv_write_u8(const psiconv_config config,psiconv_buffer buf,
39 int lev,const psiconv_u8 value)
40 {
41 psiconv_progress(config,lev,0,"Writing u8");
42 return psiconv_buffer_add(buf,value);
43 }
44
45 int psiconv_write_u16(const psiconv_config config,psiconv_buffer buf,
46 int lev,const psiconv_u16 value)
47 {
48 int res;
49 psiconv_progress(config,lev,0,"Writing u16");
50 if ((res = psiconv_buffer_add(buf,value & 0xff)))
51 return res;
52 return psiconv_buffer_add(buf,(value & 0xff00) >> 8);
53 }
54
55 int psiconv_write_u32(const psiconv_config config,psiconv_buffer buf,
56 int lev,const psiconv_u32 value)
57 {
58 int res;
59 psiconv_progress(config,lev,0,"Writing u32");
60 if ((res = psiconv_buffer_add(buf,value & 0xff)))
61 return res;
62 if ((res = psiconv_buffer_add(buf,(value & 0xff00) >> 8)))
63 return res;
64 if ((res = psiconv_buffer_add(buf,(value & 0xff0000) >> 16)))
65 return res;
66 return psiconv_buffer_add(buf,(value & 0xff000000) >> 24);
67 }
68
69 int psiconv_write_S(const psiconv_config config,psiconv_buffer buf,
70 int lev,const psiconv_u32 value)
71 {
72 psiconv_progress(config,lev,0,"Writing S");
73 if (value < 0x40)
74 return psiconv_write_u8(config,buf,lev+1,value * 4 + 2);
75 else if (value < 0x2000)
76 return psiconv_write_u16(config,buf,lev+1,value * 8 + 3);
77 else {
78 psiconv_error(config,0,psiconv_buffer_length(buf),
79 "Don't know how to write S value larger than 0x2000 "
80 "(trying %x)",value);
81 return -PSICONV_E_GENERATE;
82 }
83 }
84
85 int psiconv_write_X(const psiconv_config config,psiconv_buffer buf,
86 int lev, const psiconv_u32 value)
87 {
88 psiconv_progress(config,lev,0,"Writing X");
89 if (value < 0x80)
90 return psiconv_write_u8(config,buf,lev+1,value * 2);
91 else if (value < 0x4000)
92 return psiconv_write_u16(config,buf,lev+1,value * 4 + 1);
93 else if (value < 0x20000000)
94 return psiconv_write_u16(config,buf,lev+1,value * 8 + 3);
95 else {
96 psiconv_error(config,0,psiconv_buffer_length(buf),
97 "Don't know how to write X value larger than 0x20000000 "
98 "(trying %x)",value);
99 return -PSICONV_E_GENERATE;
100 }
101 }
102
103 int psiconv_write_length(const psiconv_config config,psiconv_buffer buf,
104 int lev,const psiconv_length_t value)
105 {
106 psiconv_progress(config,lev,0,"Writing length");
107 return psiconv_write_u32(config,buf,lev+1,value * (1440.0/2.54) + 0.5);
108 }
109
110 int psiconv_write_size(const psiconv_config config,psiconv_buffer buf,
111 int lev, psiconv_size_t value)
112 {
113 psiconv_progress(config,lev,0,"Writing size");
114 return psiconv_write_u32(config,buf,lev+1,value * 20.0 + 0.5);
115 }
116
117 int psiconv_write_bool(const psiconv_config config,psiconv_buffer buf,
118 int lev,const psiconv_bool_t value)
119 {
120 psiconv_progress(config,lev,0,"Writing bool");
121 if ((value != psiconv_bool_true) && (value != psiconv_bool_false))
122 psiconv_warn(config,0,psiconv_buffer_length(buf),
123 "Boolean has non-enum value (found %d)",value);
124 return psiconv_write_u8(config,buf,lev+1,value == psiconv_bool_false?0:1);
125 }
126
127 int psiconv_write_string(const psiconv_config config,psiconv_buffer buf,
128 int lev, const psiconv_string_t value)
129 {
130 psiconv_progress(config,lev,0,"Writing string");
131 return psiconv_write_string_aux(config,buf,lev+1,value,-1);
132 }
133
134 int psiconv_write_short_string(const psiconv_config config,psiconv_buffer buf,
135 int lev,const psiconv_string_t value)
136 {
137 psiconv_progress(config,lev,0,"Writing short string");
138 return psiconv_write_string_aux(config,buf,lev+1,value,-2);
139 }
140
141 int psiconv_write_charlist(const psiconv_config config,psiconv_buffer buf,
142 int lev,const psiconv_string_t value)
143 {
144 psiconv_progress(config,lev,0,"Writing short string");
145 return psiconv_write_string_aux(config,buf,lev+1,value,0);
146 }
147
148
149 int psiconv_write_string_aux(const psiconv_config config,psiconv_buffer buf,
150 int lev, const psiconv_string_t value,int kind)
151 {
152 int res,i,len;
153 psiconv_u8 *output;
154
155 psiconv_progress(config,lev,0,"Writing string (auxiliary)");
156 len = psiconv_unicode_strlen(value);
157 if (!value) {
158 psiconv_error(config,0,psiconv_buffer_length(buf),
159 "NULL string");
160 return -PSICONV_E_GENERATE;
161 }
162 if (kind == -1)
163 res = psiconv_write_S(config,buf,lev+1,len);
164 else if (kind == -2)
165 res = psiconv_write_u8(config,buf,lev+1,len);
166 else
167 res = 0;
168 if (res)
169 return res;
170
171 if ((res = psiconv_unicode_to_chars(config,value,&output)))
172 return res;
173
174 for (i = 0; i < len; i++)
175 if ((res = psiconv_write_u8(config,buf,lev+1,output[i]))) {
176 free(output);
177 return res;
178 }
179 free(output);
180 return -PSICONV_E_OK;
181 }
182
183 int psiconv_write_offset(const psiconv_config config,psiconv_buffer buf,
184 int lev,psiconv_u32 id)
185 {
186 psiconv_progress(config,lev,0,"Writing offset");
187 return psiconv_buffer_add_reference(buf,id);
188 }

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