[BACK]Return to stdio.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / parse

Annotation of OpenXM_contrib2/asir2000/parse/stdio.c, Revision 1.1

1.1     ! noro        1: /* $OpenXM: OpenXM/src/asir99/parse/stdio.c,v 1.1.1.1 1999/11/10 08:12:34 noro Exp $ */
        !             2: #include <stdio.h>
        !             3: #include <stdlib.h>
        !             4: #include <string.h>
        !             5: #include <stdarg.h>
        !             6: #include "w_stdio.h"
        !             7:
        !             8: static struct w_buf {
        !             9:   int inlen, inpos;
        !            10:   int outpos;
        !            11:   char inbuf[2*BUFSIZ];
        !            12:   char outbuf[2*BUFSIZ];
        !            13: } W_BUF;
        !            14:
        !            15: static int noflush;
        !            16:
        !            17: #define INBUF_EMPTY (W_BUF.inpos >= W_BUF.inlen)
        !            18:
        !            19: int w_filbuf(void)
        !            20: {
        !            21:   int status = get_line(W_BUF.inbuf);
        !            22:   W_BUF.inpos = 0;
        !            23:   W_BUF.inlen = strlen(W_BUF.inbuf);
        !            24:   return status;
        !            25: }
        !            26:
        !            27: int w_flushbuf(void) {
        !            28:   W_BUF.outbuf[W_BUF.outpos] = 0;
        !            29:   W_BUF.outpos = 0;
        !            30:   put_line(W_BUF.outbuf);
        !            31:   return 0;
        !            32: }
        !            33:
        !            34: int w_fflush(FILE *file)
        !            35: {
        !            36:   if ( file == stdout || file == stderr )
        !            37:     return w_flushbuf();
        !            38:   else
        !            39:     return fflush(file);
        !            40: }
        !            41:
        !            42: void w_noflush_stderr(int flag)
        !            43: {
        !            44:   noflush = flag;
        !            45: }
        !            46:
        !            47: int get_char(void)
        !            48: {
        !            49:   int status = 0;
        !            50:
        !            51:   if ( INBUF_EMPTY )
        !            52:     status = w_filbuf();
        !            53:   if ( status )
        !            54:     return EOF;
        !            55:   else
        !            56:     return (int)W_BUF.inbuf[W_BUF.inpos++];
        !            57: }
        !            58:
        !            59: char *get_string(char *s, int n)
        !            60: {
        !            61:   int i;
        !            62:   char *p;
        !            63:   int status = 0;
        !            64:
        !            65:   if ( INBUF_EMPTY )
        !            66:     status = w_filbuf();
        !            67:   if ( status )
        !            68:     return NULL;
        !            69:   for ( i = 0, p = s; i < n && !INBUF_EMPTY && W_BUF.inbuf[i]; i++ )
        !            70:     *p++ = W_BUF.inbuf[W_BUF.inpos++];
        !            71:   if ( i < n )
        !            72:    *p = 0;
        !            73:   return s;
        !            74: }
        !            75:
        !            76: int w_fgetc(FILE *file)
        !            77: {
        !            78:   if ( file == stdin )
        !            79:     return get_char();
        !            80:   else
        !            81:     return fgetc(file);
        !            82: }
        !            83:
        !            84: int unget_char(int c)
        !            85: {
        !            86:   W_BUF.inbuf[--W_BUF.inpos] = (char )c;
        !            87:   return c;
        !            88: }
        !            89:
        !            90: int put_char(int c,FILE *file)
        !            91: {
        !            92:   static char s[2];
        !            93:
        !            94:   s[0] = (char)c;
        !            95:   put_string(s,file);
        !            96:   return c;
        !            97: }
        !            98:
        !            99: int put_string(char *s,FILE *file)
        !           100: {
        !           101:   char c;
        !           102:
        !           103:   while ( 1 ) {
        !           104:     for ( ; (c = *s) && W_BUF.outpos < BUFSIZ; s++ ) {
        !           105:       W_BUF.outbuf[W_BUF.outpos++] = c;
        !           106:       if ( c == '\n' ) {
        !           107:         W_BUF.outbuf[W_BUF.outpos-1] = '\r';
        !           108:         W_BUF.outbuf[W_BUF.outpos++] = '\n';
        !           109:         w_flushbuf();
        !           110:       }
        !           111:     }
        !           112:     if ( !c )
        !           113:       break;
        !           114:     else if ( W_BUF.outpos == BUFSIZ )
        !           115:       w_flushbuf();
        !           116:   }
        !           117:   if (file == stderr && !noflush)
        !           118:       w_flushbuf();
        !           119:   return 0;
        !           120: }
        !           121:
        !           122: char *w_fgets(char *s,int n, FILE *file)
        !           123: {
        !           124:   if ( file == stdin )
        !           125:     return get_string(s,n);
        !           126:   else
        !           127:     return fgets(s,n,file);
        !           128: }
        !           129:
        !           130: int w_ungetc(int c, FILE *file)
        !           131: {
        !           132:   if ( file == stdin )
        !           133:     return unget_char(c);
        !           134:   else
        !           135:     return ungetc(c,file);
        !           136: }
        !           137:
        !           138: int w_fputc(int c, FILE *file)
        !           139: {
        !           140:   if ( file == stdout || file == stderr )
        !           141:     return put_char(c,file);
        !           142:   else
        !           143:     return fputc(c,file);
        !           144: }
        !           145:
        !           146: int w_fputs(char *s, FILE *file)
        !           147: {
        !           148:   int status;
        !           149:
        !           150:   if ( file == stdout || file == stderr )
        !           151:     return put_string(s,file);
        !           152:   else
        !           153:     return fputs(s,file);
        !           154: }
        !           155:
        !           156: int w_printf(char *format, ...)
        !           157: {
        !           158:   va_list ap;
        !           159:   int status;
        !           160:   char buf[BUFSIZ];
        !           161:
        !           162:   va_start(ap,format);
        !           163:   status = vsprintf(buf,format,ap);
        !           164:   put_string(buf,stdout);
        !           165:   return status;
        !           166: }
        !           167:
        !           168: int w_fprintf(FILE *file, char *format, ...)
        !           169: {
        !           170:   va_list ap;
        !           171:   int status;
        !           172:   char buf[BUFSIZ];
        !           173:
        !           174:   va_start(ap,format);
        !           175:   if ( file == stdout || file == stderr ) {
        !           176:     status = vsprintf(buf,format,ap);
        !           177:     put_string(buf,file);
        !           178:   } else {
        !           179:     status = vfprintf(file,format,ap);
        !           180:   }
        !           181:   va_end(ap);
        !           182:   return status;
        !           183: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>