Julius 4.2
libsent/src/util/readfile.c
説明を見る。
00001 
00027 /*
00028  * Copyright (c) 1991-2011 Kawahara Lab., Kyoto University
00029  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00030  * Copyright (c) 2005-2011 Julius project team, Nagoya Institute of Technology
00031  * All rights reserved
00032  */
00033 
00034 #include <sent/stddefs.h>
00035 #include <sent/tcpip.h>
00036 
00037 #ifdef HAVE_ZLIB
00038 #include <zlib.h>
00039 #endif
00040 
00041 
00052 char *
00053 getl(char *buf, int maxlen, FILE *fp)
00054 {
00055   int newline;
00056 
00057   while(
00058 #ifdef HAVE_ZLIB
00059         gzgets((gzFile)fp, buf, maxlen) != Z_NULL
00060 #else
00061         fgets(buf, maxlen, fp) != NULL
00062 #endif
00063         ) {
00064     newline = strlen(buf)-1;    /* chop newline */
00065     if (buf[newline] == '\n') {
00066       buf[newline] = '\0';
00067       newline--;
00068     }
00069     if (newline >= 0 && buf[newline] == '\r') buf[newline] = '\0';
00070     if (buf[0] == '\0') continue; /* if blank line, read next */
00071     return buf;
00072   }
00073   return NULL;
00074 }
00075 
00086 char *
00087 getl_fp(char *buf, int maxlen, FILE *fp)
00088 {
00089   int newline;
00090 
00091   while(fgets(buf, maxlen, fp) != NULL) {
00092     newline = strlen(buf)-1;    /* chop newline */
00093     if (buf[newline] == '\n') {
00094       buf[newline] = '\0';
00095       newline--;
00096     }
00097     if (newline >= 0 && buf[newline] == '\r') buf[newline] = '\0';
00098     if (buf[0] == '\0') continue; /* if blank line, read next */
00099     return buf;
00100   }
00101   return NULL;
00102 }
00103 
00104 /* get 1 line input from stdin with prompt */
00105 /* return value: newly allocated buffer */
00106 /* repeat if no input, and */
00107 /* returns NULL on EOF */
00117 char *
00118 get_line_from_stdin(char *buf, int buflen, char *prompt)
00119 {
00120   char *p;
00121 
00122   do {
00123     fprintf(stderr, "%s",prompt);
00124     if (fgets(buf, buflen, stdin) == NULL) {
00125       return(NULL);
00126     }
00127     /* chop last newline */
00128     p = buf + strlen(buf) - 1;
00129     if (p >= buf && *p == '\n') {
00130       *(p --) = '\0';
00131     }
00132     if (p >= buf && *p == '\r') {
00133       *(p --) = '\0';
00134     }
00135     /* chop last space */
00136     while(p >= buf && *p == ' ') {
00137       *(p --) = '\0';
00138     }
00139   } while (!buf[0]);            /* loop till some input */
00140 
00141   return(buf);
00142 }