cvs-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Cvs-cvs] ccvs/src ChangeLog subr.c subr.h


From: Derek Robert Price
Subject: [Cvs-cvs] ccvs/src ChangeLog subr.c subr.h
Date: Mon, 03 Jul 2006 11:54:42 +0000

CVSROOT:        /cvsroot/cvs
Module name:    ccvs
Changes by:     Derek Robert Price <dprice>     06/07/03 11:54:42

Modified files:
        src            : ChangeLog subr.c subr.h 

Log message:
        * subr.c (get_file): Factor out...
        (get_stream): ...this function.  Simplify.
        * subr.h (get_stream): New proto.
        * sanity.sh: Update to compensate.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/ccvs/src/ChangeLog?cvsroot=cvs&r1=1.3463&r2=1.3464
http://cvs.savannah.gnu.org/viewcvs/ccvs/src/subr.c?cvsroot=cvs&r1=1.152&r2=1.153
http://cvs.savannah.gnu.org/viewcvs/ccvs/src/subr.h?cvsroot=cvs&r1=1.10&r2=1.11

Patches:
Index: ChangeLog
===================================================================
RCS file: /cvsroot/cvs/ccvs/src/ChangeLog,v
retrieving revision 1.3463
retrieving revision 1.3464
diff -u -b -r1.3463 -r1.3464
--- ChangeLog   30 Jun 2006 00:38:39 -0000      1.3463
+++ ChangeLog   3 Jul 2006 11:54:41 -0000       1.3464
@@ -1,5 +1,12 @@
 2006-06-29  Derek Price  <address@hidden>
 
+       * subr.c (get_file): Factor out...
+       (get_stream): ...this function.  Simplify.
+       * subr.h (get_stream): New proto.
+       * sanity.sh: Update to compensate.
+
+2006-06-29  Derek Price  <address@hidden>
+
        * client.c (is_arg_a_parent_or_listed_dir): Strip trailing slashes from
        dir name defore searching for it.  Partially addresses TODO #205.
        Avoid false positives.

Index: subr.c
===================================================================
RCS file: /cvsroot/cvs/ccvs/src/subr.c,v
retrieving revision 1.152
retrieving revision 1.153
diff -u -b -r1.152 -r1.153
--- subr.c      23 Jun 2006 20:46:39 -0000      1.152
+++ subr.c      3 Jul 2006 11:54:42 -0000       1.153
@@ -650,26 +650,24 @@
 
 
 
-/* Read the entire contents of the file NAME into *BUF.
-   If NAME is NULL, read from stdin.  *BUF
-   is a pointer returned from malloc (or NULL), pointing to *BUFSIZE
-   bytes of space.  The actual size is returned in *LEN.  On error,
-   give a fatal error.  The name of the file to use in error messages
-   (typically will include a directory if we have changed directory)
-   is FULLNAME.  MODE is "r" for text or "rb" for binary.  */
+/* Read the entire contents of the file pointer IN into *BUF.  *BUF
+ * is a pointer returned from malloc (or NULL), pointing to *BUFSIZE
+ * bytes of space.  The actual size is returned in *LEN.  On error,
+ * give a fatal error.  The name of the file to use in error messages
+ * (typically will include a directory if we have changed directory)
+ * is FULLNAME.  MODE is "r" for text or "rb" for binary.
+ */
 void
-get_file (const char *name, const char *fullname, const char *mode, char **buf,
-         size_t *bufsize, size_t *len)
+get_stream (FILE *in, const char *fullname, char **buf, size_t *bufsize,
+           size_t *len)
 {
     struct stat s;
-    size_t nread;
-    char *tobuf;
-    FILE *e;
+    size_t off;
     size_t filesize;
 
-    if (name == NULL)
+    if (in == stdin)
     {
-       e = stdin;
+       fullname = "stdin";
        filesize = 100; /* force allocation of minimum buffer */
     }
     else
@@ -680,62 +678,67 @@
           be of arbitrary size, so I think we better do all that
           extra allocation.  */
 
-       if (stat (name, &s) < 0)
-           error (1, errno, "can't stat %s", fullname);
+       if (fstat (fileno (in), &s) < 0)
+           error (1, errno, "can't stat `%s'", fullname);
 
        /* Convert from signed to unsigned.  */
        filesize = s.st_size;
 
-       e = xfopen (name, mode);
+       /* Don't assume the file is positioned at the start.  */
+       rewind (in);
     }
 
-    if (*buf == NULL || *bufsize <= filesize)
-    {
-       *bufsize = filesize + 1;
-       *buf = xrealloc (*buf, *bufsize);
-    }
+    if (*bufsize < filesize)
+       *bufsize = filesize;
 
-    tobuf = *buf;
-    nread = 0;
-    while (1)
+    off = 0;
+    while (true)
     {
        size_t got;
 
-       got = fread (tobuf, 1, *bufsize - (tobuf - *buf), e);
-       if (ferror (e))
-           error (1, errno, "can't read %s", fullname);
-       nread += got;
-       tobuf += got;
+       /* Allocates *more* than *BUFSIZE and updates *BUFSIZE.  */
+       *buf = x2realloc (*buf, bufsize);
 
-       if (feof (e))
+       if (feof (in))
            break;
 
-       /* Allocate more space if needed.  */
-       if (tobuf == *buf + *bufsize)
-       {
-           int c;
-           long off;
-
-           c = getc (e);
-           if (c == EOF)
-               break;
-           off = tobuf - *buf;
-           expand_string (buf, bufsize, *bufsize + 100);
-           tobuf = *buf + off;
-           *tobuf++ = c;
-           ++nread;
-       }
+       got = fread (*buf + off, 1, *bufsize - off, in);
+       if (ferror (in))
+           error (1, errno, "can't read `%s'", fullname);
+       off += got;
     }
 
-    if (e != stdin && fclose (e) < 0)
-       error (0, errno, "cannot close %s", fullname);
-
-    *len = nread;
+    *len = off;
 
     /* Force *BUF to be large enough to hold a null terminator. */
-    if (nread == *bufsize)
-       expand_string (buf, bufsize, *bufsize + 1);
-    (*buf)[nread] = '\0';
+    if (off == *bufsize)
+       *buf = x2realloc (*buf, bufsize);
+    (*buf)[off] = '\0';
+}
+
+
+
+/* Read the entire contents of the file NAME into *BUF.  If NAME is NULL, read
+ * from stdin.  *BUF is a pointer returned from malloc (or NULL), pointing to
+ * *BUFSIZE bytes of space.  The actual size is returned in *LEN.  On error,
+ * give a fatal error.  The name of the file to use in error messages
+ * (typically will include a directory if we have changed directory)
+ * is FULLNAME.  MODE is "r" for text or "rb" for binary.
+ */
+void
+get_file (const char *name, const char *fullname, const char *mode, char **buf,
+         size_t *bufsize, size_t *len)
+{
+    FILE *in;
+
+    if (name)
+       in = xfopen (name, mode);
+    else
+       in = stdin;
+
+    get_stream (in, fullname, buf, bufsize, len);
+    if (in != stdin && fclose (in) < 0)
+       error (0, errno, "cannot close `%s'", fullname);
 }
 
 

Index: subr.h
===================================================================
RCS file: /cvsroot/cvs/ccvs/src/subr.h,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -b -r1.10 -r1.11
--- subr.h      20 May 2006 07:19:03 -0000      1.10
+++ subr.h      3 Jul 2006 11:54:42 -0000       1.11
@@ -50,6 +50,7 @@
 char *make_message_rcsvalid (const char *message);
 int file_has_markers (const struct file_info *);
 bool file_contains_keyword (const struct file_info *finfo);
+void get_stream (FILE *, const char *, char **, size_t *, size_t *);
 void get_file (const char *, const char *, const char *,
                char **, size_t *, size_t *);
 void force_write_file (const char *file, const char *data, size_t len);




reply via email to

[Prev in Thread] Current Thread [Next in Thread]