37#define ERR(m) LT_ERROR(NECHAR,m)
38#define ERR1(m,x) LT_ERROR1(NECHAR,m,x)
39#define ERR2(m,x,y) LT_ERROR2(NECHAR,m,x,y)
42#define Realloc srealloc
50#define ERR(m) fprintf(stderr,m)
51#define ERR1(m,x) fprintf(stderr,m,x)
52#define ERR2(m,x,y) fprintf(stderr,m,x,y)
68#define BufferSize 4096
71typedef int WriteProc(
FILE16 *
file,
const unsigned char *
buf,
int count);
85 CharacterEncoding enc;
89#define FILE16_read 0x01
90#define FILE16_write 0x02
91#define FILE16_close_underlying 0x04
94static int FileWrite(
FILE16 *
file,
const unsigned char *
buf,
int count);
100static int StringWrite(
FILE16 *
file,
const unsigned char *
buf,
int count);
106#ifdef SOCKETS_IMPLEMENTED
123FILE16 *Stdin, *Stdout, *Stderr;
125void init_stdio16(
void) {
126 Stdin = MakeFILE16FromFILE(
stdin,
"r");
127 SetFileEncoding(Stdin, CE_ISO_8859_1);
128 Stdout = MakeFILE16FromFILE(
stdout,
"w");
129 SetFileEncoding(Stdout, CE_ISO_8859_1);
130 Stderr = MakeFILE16FromFILE(
stderr,
"w");
131 SetFileEncoding(Stderr, CE_ISO_8859_1);
134static int ConvertASCII(
const char8 *
buf,
int count,
FILE16 *
file);
135static int ConvertUTF16(
const char16 *
buf,
int count,
FILE16 *
file);
145static int ConvertASCII(
const char8 *
buf,
int count,
FILE16 *
file)
147 unsigned char outbuf[BufferSize*2];
161 case CE_unspecified_ascii_superset:
162 return file->write(
file, (
unsigned char *)
buf, count);
165 for(i=
j=0; i<count; i++)
167 unsigned char c =
buf[i];
173 outbuf[
j++] = 0x80 + (c & 0x3f);
179 case CE_ISO_10646_UCS_2B:
180 for(i=
j=0; i<count; i++)
188 case CE_ISO_10646_UCS_2L:
189 for(i=
j=0; i<count; i++)
197 ERR2(
"Bad output character encoding %d (%s)\n",
199 file->enc < CE_enum_count ? CharacterEncodingName[
file->enc] :
208static int ConvertUTF16(
const char16 *
buf,
int count,
FILE16 *
file)
211 unsigned char outbuf[BufferSize*3 + 1];
219 case CE_unspecified_ascii_superset:
220 for(i=0; i<count; i++)
240 for(i=0; i<count; i++)
250 for(i=
j=0; i<count; i++)
254 else if(
buf[i] < 0x800)
259 else if(
buf[i] >= 0xd800 &&
buf[i] <= 0xdbff)
261 else if(
buf[i] >= 0xdc00 &&
buf[i] <= 0xdfff)
264 ((
file->save - 0xd800) << 10) + (
buf[i] - 0xdc00);
280 case CE_ISO_10646_UCS_2B:
281 for(i=
j=0; i<count; i++)
290 case CE_ISO_10646_UCS_2L:
291 for(i=
j=0; i<count; i++)
300 ERR2(
"Bad output character encoding %d (%s)\n",
302 file->enc < CE_enum_count ? CharacterEncodingName[
file->enc] :
341 if(
file->read == FileRead)
350 file->flags |= FILE16_close_underlying;
352 file->flags &= ~FILE16_close_underlying;
355void SetFileEncoding(
FILE16 *
file, CharacterEncoding encoding)
357 file->enc = encoding;
360CharacterEncoding GetFileEncoding(
FILE16 *
file)
372int Printf(
const char *
format, ...)
376 return Vfprintf(Stdout,
format, args);
379int Sprintf(
void *
buf, CharacterEncoding enc,
const char *
format, ...)
388 return Vfprintf(Stdout,
format, args);
391int Vsprintf(
void *
buf, CharacterEncoding enc,
const char *
format,
395 FILE16 file = {0, 0, -1, StringRead, StringWrite, StringSeek, StringFlush, StringClose, FILE16_write};
406#define put(x) {nchars++; if(count == sizeof(buf)) {if(ConvertASCII(buf, count, file) == -1) return -1; count = 0;} buf[count++] = x;}
410 char8
buf[BufferSize];
412 int c, i, n, width,
prec;
464 width =
va_arg(args,
int);
467 else if(c >=
'0' && c <=
'9')
470 while((c = *
format++) >=
'0' && c <=
'9')
471 width = width * 10 + c -
'0';
482 else if(c >=
'0' && c <=
'9')
485 while((c = *
format++) >=
'0' && c <=
'9')
502#ifdef HAVE_LONG_DOUBLE
512 ERR(
"Printf: format specifier too long");
547#ifdef HAVE_LONG_DOUBLE
558 if(ConvertASCII(
buf, count,
file) == -1)
562 if(ConvertUTF16(
cbuf, 1,
file) == -1)
580 static char16
sNULL[] = {
'(',
'N',
'U',
'L',
'L',
')',0};
590 if(n < width && !
mflag)
591 for(i=width-n; i>0; i--)
593 if(ConvertASCII(
buf, count,
file) == -1)
600 if(ConvertUTF16(
q, n > BufferSize ? BufferSize : n,
file) == -1)
611 p =
va_arg(args, char8 *);
617 if(n < width && !
mflag)
618 for(i=width-n; i>0; i--)
623 if(n < width &&
mflag)
624 for(i=width-n; i>0; i--)
630 ERR1(
"unknown format character %c\n", c);
637 if(ConvertASCII(
buf, count,
file) == -1)
643static FILE16 *MakeFILE16(
const char *type)
652 file->flags |= FILE16_read;
654 file->flags |= FILE16_write;
656 file->enc = InternalCharacterEncoding;
661FILE16 *MakeFILE16FromFILE(
FILE *f,
const char *type)
665 if(!(
file = MakeFILE16(type)))
668 file->read = FileRead;
669 file->write = FileWrite;
670 file->seek = FileSeek;
671 file->close = FileClose;
672 file->flush = FileFlush;
685 return ferror(f) ? -1 : count;
688static int FileWrite(
FILE16 *
file,
const unsigned char *
buf,
int count)
694 return fwrite(
buf, 1, count, f) == 0 ? -1 : 0;
708 return (
file->flags & FILE16_close_underlying) ?
fclose(f) : 0;
718FILE16 *MakeFILE16FromString(
void *
buf,
long size,
const char *type)
722 if(!(
file = MakeFILE16(type)))
725 file->read = StringRead;
726 file->write = StringWrite;
727 file->seek = StringSeek;
728 file->close = StringClose;
729 file->flush = StringFlush;
733 file->handle3 = size;
740 char *p = (
char *)
file->handle +
file->handle2;
754static int StringWrite(
FILE16 *
file,
const unsigned char *
buf,
int count)
756 char *p = (
char *)
file->handle +
file->handle2;
758 if(
file->handle3 >= 0 &&
file->handle2 + count >
file->handle3)
761 memcpy(p,
buf, count);
762 file->handle2 += count;
772 offset =
file->handle2 + offset;
775 if(
file->handle3 < 0)
777 offset =
file->handle3 + offset;
781 if(
file->handle3 >= 0 && offset >
file->handle3)
784 file->handle2 = offset;
791 static char8 null = 0;
793 if(
file->flags & FILE16_write)
794 ConvertASCII(&null, 1,
file);
796 if(
file->flags & FILE16_close_underlying)
797 Free((
char *)
file->handle);
809#ifdef SOCKETS_IMPLEMENTED
815 if(!(
file = MakeFILE16(type)))
830 int f = (int)
file->handle2;
872 if(!(
file = MakeFILE16(type)))
880 file->handle = (
void *)f;
925 return (
file->flags & FILE16_close_underlying) ?
gzclose(f) : 0;
941 char16 S[] = {
'w',
'o',
'r',
'l',
'd',
' ',
'£' & 0xff, 0xd841, 0xdc42, 0};
943 n=Printf(
argv[1], s, 98765432, &c, 5.3, 3.2L,
"÷hello", S);
944 printf(
"\nreturned %d, c=%d\n", n, c);
945 n=Printf(
argv[1], s, 98765432, &c, 5.3, 3.2L,
"÷hello", S);
946 printf(
"\nreturned %d, c=%d\n", n, c);
947 n=Printf(
argv[1], s, 98765432, &c, 5.3, 3.2L,
"÷hello", S);
948 printf(
"\nreturned %d, c=%d\n", n, c);
949 n=Printf(
argv[1], s, 98765432, &c, 5.3, 3.2L,
"÷hello", S);
950 printf(
"\nreturned %d, c=%d\n", n, c);