Annotation of OpenXM_contrib2/asir2000/parse/stdio.c, Revision 1.2
1.2 ! noro 1: /*
! 2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
! 3: * All rights reserved.
! 4: *
! 5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
! 6: * non-exclusive and royalty-free license to use, copy, modify and
! 7: * redistribute, solely for non-commercial and non-profit purposes, the
! 8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
! 9: * conditions of this Agreement. For the avoidance of doubt, you acquire
! 10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
! 11: * third party developer retains all rights, including but not limited to
! 12: * copyrights, in and to the SOFTWARE.
! 13: *
! 14: * (1) FLL does not grant you a license in any way for commercial
! 15: * purposes. You may use the SOFTWARE only for non-commercial and
! 16: * non-profit purposes only, such as academic, research and internal
! 17: * business use.
! 18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
! 19: * international copyright treaties. If you make copies of the SOFTWARE,
! 20: * with or without modification, as permitted hereunder, you shall affix
! 21: * to all such copies of the SOFTWARE the above copyright notice.
! 22: * (3) An explicit reference to this SOFTWARE and its copyright owner
! 23: * shall be made on your publication or presentation in any form of the
! 24: * results obtained by use of the SOFTWARE.
! 25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
! 26: * e-mail at risa-admin@flab.fujitsu.co.jp of the detailed specification
! 27: * for such modification or the source code of the modified part of the
! 28: * SOFTWARE.
! 29: *
! 30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
! 31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
! 32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
! 33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
! 34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
! 35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
! 36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
! 37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
! 38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
! 39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
! 40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
! 41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
! 42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
! 43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
! 44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
! 45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
! 46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
! 47: *
! 48: * $OpenXM: OpenXM_contrib2/asir2000/parse/stdio.c,v 1.1.1.1 1999/12/03 07:39:12 noro Exp $
! 49: */
1.1 noro 50: #include <stdio.h>
51: #include <stdlib.h>
52: #include <string.h>
53: #include <stdarg.h>
54: #include "w_stdio.h"
55:
56: static struct w_buf {
57: int inlen, inpos;
58: int outpos;
59: char inbuf[2*BUFSIZ];
60: char outbuf[2*BUFSIZ];
61: } W_BUF;
62:
63: static int noflush;
64:
65: #define INBUF_EMPTY (W_BUF.inpos >= W_BUF.inlen)
66:
67: int w_filbuf(void)
68: {
69: int status = get_line(W_BUF.inbuf);
70: W_BUF.inpos = 0;
71: W_BUF.inlen = strlen(W_BUF.inbuf);
72: return status;
73: }
74:
75: int w_flushbuf(void) {
76: W_BUF.outbuf[W_BUF.outpos] = 0;
77: W_BUF.outpos = 0;
78: put_line(W_BUF.outbuf);
79: return 0;
80: }
81:
82: int w_fflush(FILE *file)
83: {
84: if ( file == stdout || file == stderr )
85: return w_flushbuf();
86: else
87: return fflush(file);
88: }
89:
90: void w_noflush_stderr(int flag)
91: {
92: noflush = flag;
93: }
94:
95: int get_char(void)
96: {
97: int status = 0;
98:
99: if ( INBUF_EMPTY )
100: status = w_filbuf();
101: if ( status )
102: return EOF;
103: else
104: return (int)W_BUF.inbuf[W_BUF.inpos++];
105: }
106:
107: char *get_string(char *s, int n)
108: {
109: int i;
110: char *p;
111: int status = 0;
112:
113: if ( INBUF_EMPTY )
114: status = w_filbuf();
115: if ( status )
116: return NULL;
117: for ( i = 0, p = s; i < n && !INBUF_EMPTY && W_BUF.inbuf[i]; i++ )
118: *p++ = W_BUF.inbuf[W_BUF.inpos++];
119: if ( i < n )
120: *p = 0;
121: return s;
122: }
123:
124: int w_fgetc(FILE *file)
125: {
126: if ( file == stdin )
127: return get_char();
128: else
129: return fgetc(file);
130: }
131:
132: int unget_char(int c)
133: {
134: W_BUF.inbuf[--W_BUF.inpos] = (char )c;
135: return c;
136: }
137:
138: int put_char(int c,FILE *file)
139: {
140: static char s[2];
141:
142: s[0] = (char)c;
143: put_string(s,file);
144: return c;
145: }
146:
147: int put_string(char *s,FILE *file)
148: {
149: char c;
150:
151: while ( 1 ) {
152: for ( ; (c = *s) && W_BUF.outpos < BUFSIZ; s++ ) {
153: W_BUF.outbuf[W_BUF.outpos++] = c;
154: if ( c == '\n' ) {
155: W_BUF.outbuf[W_BUF.outpos-1] = '\r';
156: W_BUF.outbuf[W_BUF.outpos++] = '\n';
157: w_flushbuf();
158: }
159: }
160: if ( !c )
161: break;
162: else if ( W_BUF.outpos == BUFSIZ )
163: w_flushbuf();
164: }
165: if (file == stderr && !noflush)
166: w_flushbuf();
167: return 0;
168: }
169:
170: char *w_fgets(char *s,int n, FILE *file)
171: {
172: if ( file == stdin )
173: return get_string(s,n);
174: else
175: return fgets(s,n,file);
176: }
177:
178: int w_ungetc(int c, FILE *file)
179: {
180: if ( file == stdin )
181: return unget_char(c);
182: else
183: return ungetc(c,file);
184: }
185:
186: int w_fputc(int c, FILE *file)
187: {
188: if ( file == stdout || file == stderr )
189: return put_char(c,file);
190: else
191: return fputc(c,file);
192: }
193:
194: int w_fputs(char *s, FILE *file)
195: {
196: int status;
197:
198: if ( file == stdout || file == stderr )
199: return put_string(s,file);
200: else
201: return fputs(s,file);
202: }
203:
204: int w_printf(char *format, ...)
205: {
206: va_list ap;
207: int status;
208: char buf[BUFSIZ];
209:
210: va_start(ap,format);
211: status = vsprintf(buf,format,ap);
212: put_string(buf,stdout);
213: return status;
214: }
215:
216: int w_fprintf(FILE *file, char *format, ...)
217: {
218: va_list ap;
219: int status;
220: char buf[BUFSIZ];
221:
222: va_start(ap,format);
223: if ( file == stdout || file == stderr ) {
224: status = vsprintf(buf,format,ap);
225: put_string(buf,file);
226: } else {
227: status = vfprintf(file,format,ap);
228: }
229: va_end(ap);
230: return status;
231: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>