Inspired in the new utf8 man page (thanks tedu@) I think I found a
solution to the charset issue.
New version:
/*
* MIME encode mail header quoted-printable.
*
*/
#include <stdio.h>
#define ASCII 0x7f
#define IN 1
#define OUT 0
#define MAX 1024
int
main()
{
int c, i, n, nl, count, isutf8, eightbit, encode, body;
unsigned char s[MAX];
i = n = nl = count = 0;
encode = eightbit = body = isutf8 = OUT;
while ((c = getchar()) != EOF) {
if (body == IN)
putchar(c);
else if (c == '\n' || c == ' ') {
s[i] = '\0';
if (eightbit == IN) {
if (encode == IN)
printf("=20?= ");
if (isutf8 == OUT)
printf("=?ISO-8859-1?Q?");
else
printf("=?UTF-8?Q?");
while (n < i) {
if (s[n] > ASCII ||
s[n] == '=' ||
s[n] == '?' ||
s[n] == '\t')
printf("=%02X", s[n++]);
else
printf("%c", s[n++]);
}
n = 0;
encode = IN;
if (c == '\n') {
printf("?=");
putchar(c);
encode = OUT;
}
eightbit = OUT;
} else {
if (encode == IN)
printf("?= ");
printf("%s", s);
putchar(c);
encode = OUT;
}
i = 0;
if (c == '\n')
++nl;
else
nl = 0;
if (nl > 1)
body = IN;
} else {
if (c > ASCII) {
eightbit = IN;
++count;
if (count == 1) {
if (c != 0xc2 &&
c != 0xc3 &&
c != 0xe2)
isutf8 = OUT;
else
isutf8 = IN;
}
} else
count = 0;
if (i >= MAX - 1) {
s[i] = '\0';
printf("%s", s);
i = 0;
}
s[i++] = c;
}
}
return 0;
}
No comments:
Post a Comment