/[public]/psiconv/trunk/program/psiconv/general.c
ViewVC logotype

Annotation of /psiconv/trunk/program/psiconv/general.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 192 - (hide annotations)
Mon Feb 2 20:43:04 2004 UTC (20 years, 2 months ago) by frodo
File MIME type: text/plain
File size: 4291 byte(s)
(Frodo) Psiconv program update
  * Created html4 target
  * Update of xhtml target (print entities if ASCII, and others)
  * Made everything static that should not be exported
  * Renamed stuff to xhtml were appropriate
  * The fileformat data does now contain the supported Psion files to convert
  * This is also printed in the help text
  * ENCODING_ASCII_HTML introduced (only used internally)
  * Replaced debug, silent, verbose options with noise option
  * Default targets are XHTML and TIFF

1 frodo 185 /*
2     general.c - Part of psiconv, a PSION 5 file formats converter
3     Copyright (c) 2003 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 "psiconv.h"
24     #include "general.h"
25     #include <psiconv/list.h>
26     #include <psiconv/unicode.h>
27 frodo 189 #include <psiconv/error.h>
28     #include <stdlib.h>
29     #include <string.h>
30 frodo 185
31     /* Output a UCS2 character in one of the supported encodings. */
32 frodo 189 void output_char(psiconv_config config, psiconv_list list,
33 frodo 185 psiconv_ucs2 character, encoding enc)
34     {
35     psiconv_u8 temp;
36     int res;
37 frodo 192 #define TEMPSTR_LEN 80
38     char tempstr[TEMPSTR_LEN];
39 frodo 185
40     if (enc == ENCODING_UCS2) {
41     temp = character >> 8;
42 frodo 189 if ((res = psiconv_list_add(list,&temp))) {
43     fputs("Out of memory error\n",stderr);
44     exit(1);
45     }
46 frodo 185 temp = character & 0xff;
47 frodo 189 if ((res = psiconv_list_add(list,&temp))) {
48     fputs("Out of memory error\n",stderr);
49     exit(1);
50     }
51 frodo 185 } else if (enc == ENCODING_UTF8) {
52     if (character < 0x80) {
53     temp = character;
54 frodo 189 if ((res = psiconv_list_add(list,&temp))) {
55     fputs("Out of memory error\n",stderr);
56     exit(1);
57     }
58 frodo 185 } else if (character < 0x800) {
59     temp = 0xc0 + (character >> 6);
60 frodo 189 if ((res = psiconv_list_add(list,&temp))) {
61     fputs("Out of memory error\n",stderr);
62     exit(1);
63     }
64 frodo 185 temp = 0x80 + (character & 0x3f);
65 frodo 189 if ((res = psiconv_list_add(list,&temp))) {
66     fputs("Out of memory error\n",stderr);
67     exit(1);
68     }
69 frodo 185 } else {
70     temp = 0xe0 + (character >> 12);
71 frodo 189 if ((res = psiconv_list_add(list,&temp))) {
72     fputs("Out of memory error\n",stderr);
73     exit(1);
74     }
75 frodo 185 temp = 0x80 + ((character >> 6) & 0x3f);
76 frodo 189 if ((res = psiconv_list_add(list,&temp))) {
77     fputs("Out of memory error\n",stderr);
78     exit(1);
79     }
80 frodo 185 temp = 0x80 + (character & 0x3f);
81 frodo 189 if ((res = psiconv_list_add(list,&temp))) {
82     fputs("Out of memory error\n",stderr);
83     exit(1);
84     }
85 frodo 185 }
86     } else if (enc == ENCODING_ASCII) {
87     if (character == 0xa0)
88     temp = ' ';
89     else if (character >= 0x80)
90     temp = '?';
91     else
92     temp = character;
93 frodo 189 if ((res = psiconv_list_add(list,&temp))) {
94     fputs("Out of memory error\n",stderr);
95     exit(1);
96     }
97 frodo 192 } else if (enc == ENCODING_ASCII_HTML) {
98     if (character >= 0x80) {
99     snprintf(tempstr,TEMPSTR_LEN,"&#x%x;",character);
100     output_simple_chars(config,list,tempstr,enc);
101     } else {
102     temp = character;
103     if ((res = psiconv_list_add(list,&temp))) {
104     fputs("Out of memory error\n",stderr);
105     exit(1);
106     }
107     }
108 frodo 185 } else if (enc == ENCODING_PSION) {
109     temp = psiconv_unicode_to_char(config,character);
110 frodo 189 if ((res = psiconv_list_add(list,&temp))) {
111     fputs("Out of memory error\n",stderr);
112     exit(1);
113     }
114 frodo 185 }
115     }
116    
117 frodo 189 void output_string(psiconv_config config, psiconv_list list,
118 frodo 185 psiconv_ucs2 *string, encoding enc)
119     {
120     int i = 0;
121    
122     while (string[i]) {
123 frodo 189 output_char(config,list,string[i],enc);
124 frodo 185 i++;
125     }
126     }
127 frodo 189
128     void output_simple_chars(psiconv_config config, psiconv_list list,
129     char *string, encoding enc)
130     {
131     psiconv_ucs2 *ucs_string;
132     int i;
133    
134     if (!(ucs_string = malloc(sizeof(*ucs_string) * (strlen(string) + 1)))) {
135     fputs("Out of memory error",stderr);
136     exit(1);
137     }
138     for (i = 0; i < strlen(string); i++) {
139     if ((string[i] != '\n') && ((string[i] < 0x20) || (string[i] > 0x7e))) {
140     fprintf(stderr,"output_simple_chars unknown char: %02x",string[i]);
141     exit(1);
142     }
143     ucs_string[i] = string[i];
144     }
145     ucs_string[i] = string[i];
146     output_string(config,list,ucs_string,enc);
147     free(ucs_string);
148     }

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