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