libsent/src/util/readfile.c

Go to the documentation of this file.
00001 
00027 /*
00028  * Copyright (c) 1991-2007 Kawahara Lab., Kyoto University
00029  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00030  * Copyright (c) 2005-2007 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 
00114 char *
00115 getl_fd(char *buf, int maxlen, int fd)
00116 {
00117   int cnt;
00118   char *p;
00119   p = buf;
00120   while(1) {
00121     cnt = read(fd, p, 1);
00122     if (cnt <= 0) return NULL;          /* eof or error */
00123     if (*p == '\n') {
00124       *p = '\0';
00125       if (p - 1 >= buf && *(p-1) == '\r') *(p-1) = '\0';
00126       if (buf[0] == '\0') {
00127         p = buf;
00128         continue;
00129       } else {
00130         break;
00131       }
00132     } else {
00133       if (++p >= buf + maxlen) {
00134         jlog("Error: readfile: line too long (> %d)\n", maxlen);
00135         return NULL;
00136       }
00137     }
00138   }
00139   return buf;
00140 }
00141 
00152 char *
00153 getl_sd(char *buf, int maxlen, int sd)
00154 {
00155   int cnt;
00156   char *p;
00157   p = buf;
00158   while(1) {
00159     cnt = recv(sd, p, 1, 0);
00160     if (cnt <= 0) return NULL;                /* eof or error */
00161     if (*p == '\n') {
00162       *p = '\0';
00163       if (p - 1 >= buf && *(p-1) == '\r') *(p-1) = '\0';
00164       if (buf[0] == '\0') {
00165       p = buf;
00166       continue;
00167       } else {
00168       break;
00169       }
00170     } else {
00171       if (++p >= buf + maxlen) {
00172         jlog("Error: readfile: line too long (> %d)\n", maxlen);
00173         return NULL;
00174       }
00175     }
00176   }
00177   return buf;
00178 }
00179 
00180 /* get 1 line input from stdin with prompt */
00181 /* return value: newly allocated buffer */
00182 /* repeat if no input, and */
00183 /* returns NULL on EOF */
00193 char *
00194 get_line_from_stdin(char *buf, int buflen, char *prompt)
00195 {
00196   char *p;
00197 
00198   do {
00199     fprintf(stderr, "%s",prompt);
00200     if (fgets(buf, buflen, stdin) == NULL) {
00201       return(NULL);
00202     }
00203   } while (!buf[0]);            /* loop till some input */
00204   /* chop last newline */
00205   p = buf + strlen(buf) - 1;
00206   if (p >= buf && *p == '\n') {
00207     *(p --) = '\0';
00208   }
00209   if (p >= buf && *p == '\r') {
00210     *(p --) = '\0';
00211   }
00212   /* chop last space */
00213   while(p >= buf && *p == ' ') {
00214     *(p --) = '\0';
00215   }
00216 
00217   return(buf);
00218 }

Generated on Tue Dec 18 15:59:56 2007 for Julius by  doxygen 1.5.4