34 #include "lt-memory.h" 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 70 typedef int ReadProc(FILE16 *file,
unsigned char *buf,
int max_count);
71 typedef int WriteProc(FILE16 *file,
const unsigned char *buf,
int count);
72 typedef int SeekProc(FILE16 *file,
long offset,
int ptrname);
89 #define FILE16_read 0x01 90 #define FILE16_write 0x02 91 #define FILE16_close_underlying 0x04 93 static int FileRead(FILE16 *file,
unsigned char *buf,
int max_count);
94 static int FileWrite(FILE16 *file,
const unsigned char *buf,
int count);
95 static int FileSeek(FILE16 *file,
long offset,
int ptrname);
96 static int FileClose(FILE16 *file);
97 static int FileFlush(FILE16 *file);
99 static int StringRead(FILE16 *file,
unsigned char *buf,
int max_count);
100 static int StringWrite(FILE16 *file,
const unsigned char *buf,
int count);
101 static int StringSeek(FILE16 *file,
long offset,
int ptrname);
102 static int StringClose(FILE16 *file);
103 static int StringFlush(FILE16 *file);
106 #ifdef SOCKETS_IMPLEMENTED 107 static int WinsockRead(FILE16 *file,
unsigned char *buf,
int max_count);
108 static int WinsockWrite(FILE16 *file,
const unsigned char *buf,
int count);
109 static int WinsockSeek(FILE16 *file,
long offset,
int ptrname);
110 static int WinsockClose(FILE16 *file);
111 static int WinsockFlush(FILE16 *file);
116 static int GzipRead(FILE16 *file,
unsigned char *buf,
int max_count);
117 static int GzipWrite(FILE16 *file,
const unsigned char *buf,
int count);
118 static int GzipSeek(FILE16 *file,
long offset,
int ptrname);
119 static int GzipClose(FILE16 *file);
120 static int GzipFlush(FILE16 *file);
134 static int ConvertASCII(
const char8 *buf,
int count, FILE16 *file);
135 static int ConvertUTF16(
const char16 *buf,
int count, FILE16 *file);
145 static int ConvertASCII(
const char8 *buf,
int count, FILE16 *file)
162 return file->write(file, (
unsigned char *)buf, count);
165 for(i=j=0; i<count; i++)
167 unsigned char c = buf[i];
172 outbuf[j++] = 0xc0 + (c >> 6);
173 outbuf[j++] = 0x80 + (c & 0x3f);
176 return file->write(file, outbuf, j);
180 for(i=j=0; i<count; i++)
183 outbuf[j++] = buf[i];
185 return file->write(file, outbuf, count*2);
189 for(i=j=0; i<count; i++)
191 outbuf[j++] = buf[i];
194 return file->write(file, outbuf, count*2);
197 ERR2(
"Bad output character encoding %d (%s)\n",
208 static int ConvertUTF16(
const char16 *buf,
int count, FILE16 *file)
212 int i, j, tablenum,
max;
220 for(i=0; i<count; i++)
223 outbuf[i] = (
unsigned char)buf[i];
227 return file->write(file, outbuf, count);
240 for(i=0; i<count; i++)
243 outbuf[i] = (
unsigned char)from_unicode[buf[i]];
247 return file->write(file, outbuf, count);
250 for(i=j=0; i<count; i++)
253 outbuf[j++] = (
unsigned char)buf[i];
254 else if(buf[i] < 0x800)
256 outbuf[j++] = 0xc0 + (buf[i] >> 6);
257 outbuf[j++] = 0x80 + (buf[i] & 0x3f);
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);
265 outbuf[j++] = 0xf0 + (big >> 18);
266 outbuf[j++] = 0x80 + ((big >> 12) & 0x3f);
267 outbuf[j++] = 0x80 + ((big >> 6) & 0x3f);
268 outbuf[j++] = 0x80 + (big & 0x3f);
272 outbuf[j++] = 0xe0 + (buf[i] >> 12);
273 outbuf[j++] = 0x80 + ((buf[i] >> 6) & 0x3f);
274 outbuf[j++] = 0x80 + (buf[i] & 0x3f);
277 return file->write(file, outbuf, j);
281 for(i=j=0; i<count; i++)
283 outbuf[j++] = (buf[i] >> 8);
284 outbuf[j++] = (buf[i] & 0xff);
287 return file->write(file, outbuf, count*2);
291 for(i=j=0; i<count; i++)
293 outbuf[j++] = (buf[i] & 0xff);
294 outbuf[j++] = (buf[i] >> 8);
297 return file->write(file, outbuf, count*2);
300 ERR2(
"Bad output character encoding %d (%s)\n",
309 int Readu(FILE16 *file,
unsigned char *buf,
int max_count)
311 return file->read(file, buf, max_count);
314 int Writeu(FILE16 *file,
unsigned char *buf,
int count)
316 return file->write(file, buf, count);
323 ret = file->close(file);
329 int Fseek(FILE16 *file,
long offset,
int ptrname)
331 return file->seek(file, offset, ptrname);
336 return file->flush(file);
341 if(file->read == FileRead)
342 return (FILE *)file->handle;
365 int Fprintf(FILE16 *file,
const char *format, ...)
369 va_start(args, format);
370 nchars =
Vfprintf(file, format, args);
379 va_start(args, format);
389 va_start(args, format);
390 nchars =
Vsprintf(buf, enc, format, args);
404 FILE16 file = {0, 0, -1, StringRead, StringWrite, StringSeek, StringFlush, StringClose,
FILE16_write, 0,0};
409 nchars =
Vfprintf(&file, format, args);
415 #define put(x) {nchars++; if(count == sizeof(buf)) {if(ConvertASCII(buf, count, file) == -1) return -1; count = 0;} buf[count++] = x;} 417 int Vfprintf(FILE16 *file,
const char *format, va_list args)
421 int c, i, n, width, prec;
430 #ifdef HAVE_LONG_DOUBLE 436 while((c = *format++))
449 #ifdef HAVE_LONG_DOUBLE 455 switch(c = *format++)
473 width = va_arg(args,
int);
476 else if(c >=
'0' && c <=
'9')
479 while((c = *format++) >=
'0' && c <=
'9')
480 width = width * 10 + c -
'0';
488 prec = va_arg(args,
int);
491 else if(c >=
'0' && c <=
'9')
494 while((c = *format++) >=
'0' && c <=
'9')
495 prec = prec * 10 + c -
'0';
511 #ifdef HAVE_LONG_DOUBLE 519 if(format - start + 1 > (
int)
sizeof(fmt))
521 ERR(
"Printf: format specifier too long");
526 strncpy(fmt, start, format - start);
527 fmt[format -
start] =
'\0';
534 *va_arg(args,
int *) = nchars;
543 sprintf(val, fmt, va_arg(args,
int));
545 sprintf(val, fmt, va_arg(args,
long));
547 sprintf(val, fmt, va_arg(args,
int));
556 #ifdef HAVE_LONG_DOUBLE 558 sprintf(val, fmt, va_arg(args,
long double));
561 sprintf(val, fmt, va_arg(args,
double));
567 if(ConvertASCII(buf, count, file) == -1)
570 cbuf[0] = va_arg(args,
int);
571 if(ConvertUTF16(cbuf, 1, file) == -1)
575 put(va_arg(args,
int));
579 sprintf(val, fmt, va_arg(args,
void *));
589 static char16 sNULL[] = {
'(',
'N',
'U',
'L',
'L',
')',0};
593 q = va_arg(args,
char16 *);
597 if(prec >= 0 && n > prec)
599 if(n < width && !mflag)
600 for(i=width-n; i>0; i--)
602 if(ConvertASCII(buf, count, file) == -1)
620 p = va_arg(args,
char8 *);
624 if(prec >= 0 && n > prec)
626 if(n < width && !mflag)
627 for(i=width-n; i>0; i--)
632 if(n < width && mflag)
633 for(i=width-n; i>0; i--)
639 ERR1(
"unknown format character %c\n", c);
646 if(ConvertASCII(buf, count, file) == -1)
652 static FILE16 *MakeFILE16(
const char *type)
656 if(!(file =
Malloc(
sizeof(*file))))
674 if(!(file = MakeFILE16(type)))
677 file->read = FileRead;
678 file->write = FileWrite;
679 file->seek = FileSeek;
680 file->close = FileClose;
681 file->flush = FileFlush;
687 static int FileRead(FILE16 *file,
unsigned char *buf,
int max_count)
689 FILE *
f = file->handle;
692 count = fread(buf, 1, max_count, f);
694 return ferror(f) ? -1 : count;
697 static int FileWrite(FILE16 *file,
const unsigned char *buf,
int count)
699 FILE *
f = file->handle;
703 return fwrite(buf, 1, count, f) == 0 ? -1 : 0;
706 static int FileSeek(FILE16 *file,
long offset,
int ptrname)
708 FILE *
f = file->handle;
710 return fseek(f, offset, ptrname);
713 static int FileClose(FILE16 *file)
715 FILE *
f = file->handle;
720 static int FileFlush(FILE16 *file)
722 FILE *
f = file->handle;
731 if(!(file = MakeFILE16(type)))
734 file->read = StringRead;
735 file->write = StringWrite;
736 file->seek = StringSeek;
737 file->close = StringClose;
738 file->flush = StringFlush;
742 file->handle3 = size;
747 static int StringRead(FILE16 *file,
unsigned char *buf,
int max_count)
749 char *p = (
char *)file->handle + file->handle2;
751 if(file->handle3 >= 0 && file->handle2 + max_count > file->handle3)
752 max_count = file->handle3 - file->handle2;
757 memcpy(buf, p, max_count);
758 file->handle2 += max_count;
763 static int StringWrite(FILE16 *file,
const unsigned char *buf,
int count)
765 char *p = (
char *)file->handle + file->handle2;
767 if(file->handle3 >= 0 && file->handle2 + count > file->handle3)
770 memcpy(p, buf, count);
771 file->handle2 += count;
776 static int StringSeek(FILE16 *file,
long offset,
int ptrname)
781 offset = file->handle2 + offset;
784 if(file->handle3 < 0)
786 offset = file->handle3 + offset;
790 if(file->handle3 >= 0 && offset > file->handle3)
793 file->handle2 = offset;
798 static int StringClose(FILE16 *file)
800 static char8 null = 0;
803 ConvertASCII(&null, 1, file);
806 Free((
char *)file->handle);
811 static int StringFlush(FILE16 *file)
819 #ifdef SOCKETS_IMPLEMENTED 821 FILE16 *MakeFILE16FromWinsock(
int sock,
const char *type)
825 if(!(file = MakeFILE16(type)))
828 file->read = WinsockRead;
829 file->write = WinsockWrite;
830 file->seek = WinsockSeek;
831 file->close = WinsockClose;
832 file->flush = WinsockFlush;
833 file->handle2 = sock;
838 static int WinsockRead(FILE16 *file,
unsigned char *buf,
int max_count)
840 int f = (
int)file->handle2;
844 count = recv(f, buf, max_count, 0);
849 static int WinsockWrite(FILE16 *file,
const unsigned char *buf,
int count)
856 static int WinsockSeek(FILE16 *file,
long offset,
int ptrname)
861 static int WinsockClose(FILE16 *file)
868 static int WinsockFlush(FILE16 *file)
878 FILE16 *MakeFILE16FromGzip(gzFile
f,
const char *type)
882 if(!(file = MakeFILE16(type)))
885 file->read = GzipRead;
886 file->write = GzipWrite;
887 file->seek = GzipSeek;
888 file->close = GzipClose;
889 file->flush = GzipFlush;
890 file->handle = (
void *)
f;
895 static int GzipRead(FILE16 *file,
unsigned char *buf,
int max_count)
897 gzFile
f = (gzFile)file->handle;
900 const char *errorString;
902 count = gzread(f, buf, max_count);
904 errorString = gzerror(f, &gzerr);
905 if(gzerr != 0 && gzerr != Z_STREAM_END)
911 static int GzipWrite(FILE16 *file,
const unsigned char *buf,
int count)
913 gzFile
f = (gzFile)file->handle;
915 const char *errorString;
917 count = gzwrite(f, (
char *)buf, count);
919 errorString = gzerror(f, &gzerr);
920 if(gzerr != 0 && gzerr != Z_STREAM_END)
926 static int GzipSeek(FILE16 *file,
long offset,
int ptrname)
931 static int GzipClose(FILE16 *file)
933 gzFile
f = (gzFile)file->handle;
938 static int GzipFlush(FILE16 *file)
947 int main(
int argc,
char **argv)
951 char16 S[] = {
'w',
'o',
'r',
'l',
'd',
' ',
'£' & 0xff, 0xd841, 0xdc42, 0};
953 n=
Printf(argv[1], s, 98765432, &c, 5.3, 3.2L,
"÷hello", S);
954 printf(
"\nreturned %d, c=%d\n", n, c);
955 n=
Printf(argv[1], s, 98765432, &c, 5.3, 3.2L,
"÷hello", S);
956 printf(
"\nreturned %d, c=%d\n", n, c);
957 n=
Printf(argv[1], s, 98765432, &c, 5.3, 3.2L,
"÷hello", S);
958 printf(
"\nreturned %d, c=%d\n", n, c);
959 n=
Printf(argv[1], s, 98765432, &c, 5.3, 3.2L,
"÷hello", S);
960 printf(
"\nreturned %d, c=%d\n", n, c);
FILE * GetFILE(FILE16 *file)
int CloseProc(FILE16 *file)
int Fprintf(FILE16 *file, const char *format,...)
int SeekProc(FILE16 *file, long offset, int ptrname)
int Writeu(FILE16 *file, unsigned char *buf, int count)
STD_API const char8 * CharacterEncodingName[CE_enum_count]
STD_API char8 * unicode_to_iso[8]
bool save(Lattice &lattice, EST_String filename)
CharacterEncoding GetFileEncoding(FILE16 *file)
#define FILE16_close_underlying
int Sprintf(void *buf, CharacterEncoding enc, const char *format,...)
float max(float a, float b)
int Printf(const char *format,...)
int Vprintf(const char *format, va_list args)
STD_API CharacterEncoding InternalCharacterEncoding
int main(int argc, char **argv)
int Vfprintf(FILE16 *file, const char *format, va_list args)
STD_API int iso_max_val[8]
STD_API size_t strlen16(const char16 *)
FILE16 * MakeFILE16FromString(void *buf, long size, const char *type)
void SetFileEncoding(FILE16 *file, CharacterEncoding encoding)
FILE16 * MakeFILE16FromFILE(FILE *f, const char *type)
int ReadProc(FILE16 *file, unsigned char *buf, int max_count)
int WriteProc(FILE16 *file, const unsigned char *buf, int count)
int Readu(FILE16 *file, unsigned char *buf, int max_count)
CharacterEncoding encoding
int Fseek(FILE16 *file, long offset, int ptrname)
float start(const EST_Item &item)
void SetCloseUnderlying(FILE16 *file, int cu)
enum character_encoding CharacterEncoding
int FlushProc(FILE16 *file)
int Vsprintf(void *buf, CharacterEncoding enc, const char *format, va_list args)