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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 351 - (show annotations)
Wed Oct 22 19:53:40 2014 UTC (6 years ago) by frodo
File MIME type: text/plain
File size: 4691 byte(s)
(Frodo) Update copyright year in all source files

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

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