diff src/misc2.c @ 2229:d45902a5c61c vim73

Fix a few more things for persistent undo.
author Bram Moolenaar <bram@vim.org>
date Sat, 29 May 2010 15:11:47 +0200
parents cccb71c2c5c1
children aa6412cab544
line wrap: on
line diff
--- a/src/misc2.c
+++ b/src/misc2.c
@@ -6125,3 +6125,165 @@ emsgn(s, n)
     vim_snprintf((char *)IObuff, IOSIZE, (char *)s, n);
     return emsg(IObuff);
 }
+
+#if defined(FEAT_SPELL) || defined(FEAT_PERSISTENT_UNDO) || defined(PROTO)
+/*
+ * Read 2 bytes from "fd" and turn them into an int, MSB first.
+ */
+    int
+get2c(fd)
+    FILE	*fd;
+{
+    long	n;
+
+    n = getc(fd);
+    n = (n << 8) + getc(fd);
+    return n;
+}
+
+/*
+ * Read 3 bytes from "fd" and turn them into an int, MSB first.
+ */
+    int
+get3c(fd)
+    FILE	*fd;
+{
+    long	n;
+
+    n = getc(fd);
+    n = (n << 8) + getc(fd);
+    n = (n << 8) + getc(fd);
+    return n;
+}
+
+/*
+ * Read 4 bytes from "fd" and turn them into an int, MSB first.
+ */
+    int
+get4c(fd)
+    FILE	*fd;
+{
+    long	n;
+
+    n = getc(fd);
+    n = (n << 8) + getc(fd);
+    n = (n << 8) + getc(fd);
+    n = (n << 8) + getc(fd);
+    return n;
+}
+
+/*
+ * Read 8 bytes from "fd" and turn them into a time_t, MSB first.
+ */
+    time_t
+get8ctime(fd)
+    FILE	*fd;
+{
+    time_t	n = 0;
+    int		i;
+
+    for (i = 0; i < 8; ++i)
+	n = (n << 8) + getc(fd);
+    return n;
+}
+
+/*
+ * Read a string of length "cnt" from "fd" into allocated memory.
+ * Returns NULL when out of memory or unable to read that many bytes.
+ */
+    char_u *
+read_string(fd, cnt)
+    FILE	*fd;
+    int		cnt;
+{
+    char_u	*str;
+    int		i;
+    int		c;
+
+    /* allocate memory */
+    str = alloc((unsigned)cnt + 1);
+    if (str != NULL)
+    {
+	/* Read the string.  Quit when running into the EOF. */
+	for (i = 0; i < cnt; ++i)
+	{
+	    c = getc(fd);
+	    if (c == EOF)
+	    {
+		vim_free(str);
+		return NULL;
+	    }
+	    str[i] = c;
+	}
+	str[i] = NUL;
+    }
+    return str;
+}
+
+/*
+ * Write a number to file "fd", MSB first, in "len" bytes.
+ */
+    int
+put_bytes(fd, nr, len)
+    FILE    *fd;
+    long_u  nr;
+    int	    len;
+{
+    int	    i;
+
+    for (i = len - 1; i >= 0; --i)
+	if (putc((int)(nr >> (i * 8)), fd) == EOF)
+	    return FAIL;
+    return OK;
+}
+
+#ifdef _MSC_VER
+# if (_MSC_VER <= 1200)
+/* This line is required for VC6 without the service pack.  Also see the
+ * matching #pragma below. */
+ #  pragma optimize("", off)
+# endif
+#endif
+
+/*
+ * Write time_t to file "fd" in 8 bytes.
+ */
+    void
+put_time(fd, the_time)
+    FILE	*fd;
+    time_t	the_time;
+{
+    int		c;
+    int		i;
+    time_t	wtime = the_time;
+
+    /* time_t can be up to 8 bytes in size, more than long_u, thus we
+     * can't use put_bytes() here.
+     * Another problem is that ">>" may do an arithmetic shift that keeps the
+     * sign.  A cast to long_u may truncate if time_t is 8 bytes.  So only use
+     * a cast when it is 4 bytes, it's safe to assume that long_u is 4 bytes
+     * or more and when using 8 bytes the top bit won't be set. */
+    for (i = 7; i >= 0; --i)
+    {
+	if (i + 1 > (int)sizeof(time_t))
+	    /* ">>" doesn't work well when shifting more bits than avail */
+	    putc(0, fd);
+	else
+	{
+	    /* use "i" in condition to avoid compiler warning */
+	    if (i >= 0 && sizeof(time_t) > 4)
+		c = wtime >> (i * 8);
+	    else
+		c = (long_u)wtime >> (i * 8);
+	    putc(c, fd);
+	}
+    }
+}
+
+#ifdef _MSC_VER
+# if (_MSC_VER <= 1200)
+ #  pragma optimize("", on)
+# endif
+#endif
+
+#endif