1 |
/* |
2 |
generate_simple.c - Part of psiconv, a PSION 5 file formats converter |
3 |
Copyright (c) 2000 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 |
#include <string.h> |
21 |
|
22 |
#include "config.h" |
23 |
#include "compat.h" |
24 |
|
25 |
#include "generate_routines.h" |
26 |
#include "error.h" |
27 |
|
28 |
#ifdef DMALLOC |
29 |
#include <dmalloc.h> |
30 |
#endif |
31 |
|
32 |
|
33 |
int psiconv_write_u8(const psiconv_config config,psiconv_buffer buf,const psiconv_u8 value) |
34 |
{ |
35 |
return psiconv_buffer_add(buf,value); |
36 |
} |
37 |
|
38 |
int psiconv_write_u16(const psiconv_config config,psiconv_buffer buf,const psiconv_u16 value) |
39 |
{ |
40 |
int res; |
41 |
if ((res = psiconv_buffer_add(buf,value & 0xff))) |
42 |
return res; |
43 |
return psiconv_buffer_add(buf,(value & 0xff00) >> 8); |
44 |
} |
45 |
|
46 |
int psiconv_write_u32(const psiconv_config config,psiconv_buffer buf,const psiconv_u32 value) |
47 |
{ |
48 |
int res; |
49 |
if ((res = psiconv_buffer_add(buf,value & 0xff))) |
50 |
return res; |
51 |
if ((res = psiconv_buffer_add(buf,(value & 0xff00) >> 8))) |
52 |
return res; |
53 |
if ((res = psiconv_buffer_add(buf,(value & 0xff0000) >> 16))) |
54 |
return res; |
55 |
return psiconv_buffer_add(buf,(value & 0xff000000) >> 24); |
56 |
} |
57 |
|
58 |
int psiconv_write_S(const psiconv_config config,psiconv_buffer buf, const psiconv_u32 value) |
59 |
{ |
60 |
if (value < 0x40) |
61 |
return psiconv_write_u8(config,buf,value * 4 + 2); |
62 |
else if (value < 0x2000) |
63 |
return psiconv_write_u16(config,buf,value * 8 + 3); |
64 |
else { |
65 |
psiconv_warn(config,0,psiconv_buffer_length(buf), |
66 |
"Don't know how to write S value larger than 0x2000 " |
67 |
"(trying %x)",value); |
68 |
return -PSICONV_E_GENERATE; |
69 |
} |
70 |
} |
71 |
|
72 |
int psiconv_write_X(const psiconv_config config,psiconv_buffer buf, const psiconv_u32 value) |
73 |
{ |
74 |
if (value < 0x80) |
75 |
return psiconv_write_u8(config,buf,value * 2); |
76 |
else if (value < 0x4000) |
77 |
return psiconv_write_u16(config,buf,value * 4 + 1); |
78 |
else if (value < 0x20000000) |
79 |
return psiconv_write_u16(config,buf,value * 8 + 3); |
80 |
else { |
81 |
psiconv_warn(config,0,psiconv_buffer_length(buf), |
82 |
"Don't know how to write X value larger than 0x20000000 " |
83 |
"(trying %x)",value); |
84 |
return -PSICONV_E_GENERATE; |
85 |
} |
86 |
} |
87 |
|
88 |
int psiconv_write_length(const psiconv_config config,psiconv_buffer buf, const psiconv_length_t value) |
89 |
{ |
90 |
return psiconv_write_u32(config,buf,value * (1440.0/2.54) + 0.5); |
91 |
} |
92 |
|
93 |
int psiconv_write_size(const psiconv_config config,psiconv_buffer buf, psiconv_size_t value) |
94 |
{ |
95 |
return psiconv_write_u32(config,buf,value * 20.0 + 0.5); |
96 |
} |
97 |
|
98 |
int psiconv_write_bool(const psiconv_config config,psiconv_buffer buf, const psiconv_bool_t value) |
99 |
{ |
100 |
if ((value != psiconv_bool_true) && (value != psiconv_bool_false)) |
101 |
psiconv_warn(config,0,psiconv_buffer_length(buf), |
102 |
"Boolean has non-enum value (found %d)",value); |
103 |
return psiconv_write_u8(config,buf,value == psiconv_bool_false?0:1); |
104 |
} |
105 |
|
106 |
int psiconv_write_string(const psiconv_config config,psiconv_buffer buf, const psiconv_string_t value) |
107 |
{ |
108 |
int res,i; |
109 |
if (!value) { |
110 |
psiconv_warn(config,0,psiconv_buffer_length(buf), |
111 |
"NULL string"); |
112 |
return -PSICONV_E_GENERATE; |
113 |
} |
114 |
if ((res = psiconv_write_S(config,buf,strlen(value)))) |
115 |
return res; |
116 |
for (i = 0; i < strlen(value); i++) |
117 |
if ((res = psiconv_write_u8(config,buf,value[i]))) |
118 |
return res; |
119 |
return -PSICONV_E_OK; |
120 |
} |
121 |
|
122 |
int psiconv_write_offset(const psiconv_config config,psiconv_buffer buf, psiconv_u32 id) |
123 |
{ |
124 |
return psiconv_buffer_add_reference(buf,id); |
125 |
} |