From 703fc676365e4e7737ac201b2dd9d0346bde4cb1 Mon Sep 17 00:00:00 2001 From: ehofman Date: Thu, 28 Aug 2003 12:36:31 +0000 Subject: [PATCH] Make reads() more robust and easier to follow --- src/Prep/DemRaw2ascii/rawdem.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Prep/DemRaw2ascii/rawdem.c b/src/Prep/DemRaw2ascii/rawdem.c index 888bdbc8..d54c0737 100644 --- a/src/Prep/DemRaw2ascii/rawdem.c +++ b/src/Prep/DemRaw2ascii/rawdem.c @@ -60,19 +60,21 @@ double round( double a ) { int reads(int fd, char *buf, unsigned int len) { - char c; + unsigned int i = 0; int res; - unsigned int i; - for (i=0; - (i < (len-1)) && ((res = read(fd, &c, 1)) != 0) - && ((c != '\n') && (c != '\r')); - i++) - buf[i] = c; + char c; - if (buf[i] == '\r') - buf[i] = '\n'; + len--; + while ( (i < len) && ((res = read(fd, &c, 1)) != 0) + && ((c != '\n') && (c != '\r')) ) + { + buf[i++] = c; + } - buf[++i] = '\0'; + if ((c == '\r') || (c == '\n')) + buf[i++] = '\n'; + + buf[i] = '\0'; return res; }