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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 72 - (hide annotations)
Sat Dec 23 20:21:40 2000 UTC (23 years, 4 months ago) by frodo
File MIME type: text/plain
File size: 3641 byte(s)
(Frodo) New generation routines in generate_layout

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

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