| 1 |
/* |
| 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 |
|
| 28 |
/* Output a UCS2 character in one of the supported encodings. */ |
| 29 |
int output_char(psiconv_config config, psiconv_list list, |
| 30 |
psiconv_ucs2 character, encoding enc) |
| 31 |
{ |
| 32 |
psiconv_u8 temp; |
| 33 |
int res; |
| 34 |
|
| 35 |
if (enc == ENCODING_UCS2) { |
| 36 |
temp = character >> 8; |
| 37 |
if ((res = psiconv_list_add(list,&temp))) |
| 38 |
return res; |
| 39 |
temp = character & 0xff; |
| 40 |
if ((res = psiconv_list_add(list,&temp))) |
| 41 |
return res; |
| 42 |
} else if (enc == ENCODING_UTF8) { |
| 43 |
if (character < 0x80) { |
| 44 |
temp = character; |
| 45 |
if ((res = psiconv_list_add(list,&temp))) |
| 46 |
return res; |
| 47 |
} else if (character < 0x800) { |
| 48 |
temp = 0xc0 + (character >> 6); |
| 49 |
if ((res = psiconv_list_add(list,&temp))) |
| 50 |
return res; |
| 51 |
temp = 0x80 + (character & 0x3f); |
| 52 |
if ((res = psiconv_list_add(list,&temp))) |
| 53 |
return res; |
| 54 |
} else { |
| 55 |
temp = 0xe0 + (character >> 12); |
| 56 |
if ((res = psiconv_list_add(list,&temp))) |
| 57 |
return res; |
| 58 |
temp = 0x80 + ((character >> 6) & 0x3f); |
| 59 |
if ((res = psiconv_list_add(list,&temp))) |
| 60 |
return res; |
| 61 |
temp = 0x80 + (character & 0x3f); |
| 62 |
if ((res = psiconv_list_add(list,&temp))) |
| 63 |
return res; |
| 64 |
} |
| 65 |
} else if (enc == ENCODING_ASCII) { |
| 66 |
if (character == 0xa0) |
| 67 |
temp = ' '; |
| 68 |
else if (character >= 0x80) |
| 69 |
temp = '?'; |
| 70 |
else |
| 71 |
temp = character; |
| 72 |
if ((res = psiconv_list_add(list,&temp))) |
| 73 |
return res; |
| 74 |
} else if (enc == ENCODING_PSION) { |
| 75 |
temp = psiconv_unicode_to_char(config,character); |
| 76 |
if ((res = psiconv_list_add(list,&temp))) |
| 77 |
return res; |
| 78 |
} |
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
int output_string(psiconv_config config, psiconv_list list, |
| 83 |
psiconv_ucs2 *string, encoding enc) |
| 84 |
{ |
| 85 |
int i = 0; |
| 86 |
int res; |
| 87 |
|
| 88 |
while (string[i]) { |
| 89 |
if ((res = output_char(config,list,string[i],enc))) |
| 90 |
return res; |
| 91 |
i++; |
| 92 |
} |
| 93 |
return 0; |
| 94 |
} |