[vte] [stream] Add a reset() method



commit 68cce180dd903aacbbec11dca8f89be0cf278e92
Author: Behdad Esfahbod <behdad behdad org>
Date:   Mon Sep 14 15:04:51 2009 -0400

    [stream] Add a reset() method

 src/vtestream-base.h |    7 +++
 src/vtestream-file.h |  104 ++++++++++++++++++++++++++++++--------------------
 src/vtestream.h      |    1 +
 3 files changed, 71 insertions(+), 41 deletions(-)
---
diff --git a/src/vtestream-base.h b/src/vtestream-base.h
index 1562083..8d5e86f 100644
--- a/src/vtestream-base.h
+++ b/src/vtestream-base.h
@@ -31,6 +31,7 @@ struct _VteStream {
 typedef struct _VteStreamClass {
 	GObjectClass parent_class;
 
+	void (*reset) (VteStream *stream, gsize offset);
 	gsize (*append) (VteStream *stream, const char *data, gsize len);
 	void (*read) (VteStream *stream, gsize offset, char *data, gsize len);
 	void (*truncate) (VteStream *stream, gsize offset);
@@ -53,6 +54,12 @@ _vte_stream_init (VteStream *stream)
 {
 }
 
+void
+_vte_stream_reset (VteStream *stream, gsize offset)
+{
+	return VTE_STREAM_GET_CLASS (stream)->reset (stream, offset);
+}
+
 gsize
 _vte_stream_append (VteStream *stream, const char *data, gsize len)
 {
diff --git a/src/vtestream-file.h b/src/vtestream-file.h
index eefc28d..160915e 100644
--- a/src/vtestream-file.h
+++ b/src/vtestream-file.h
@@ -22,6 +22,61 @@
 #include <unistd.h>
 #include <errno.h>
 
+static gsize
+_xread (int fd, char *data, gsize len)
+{
+	gsize ret, total = 0;
+
+	g_assert (fd);
+
+	while (len) {
+		ret = read (fd, data, len);
+		if (G_UNLIKELY (ret == (gsize) -1)) {
+			if (errno == EINTR)
+				continue;
+			else
+				break;
+		}
+		data += ret;
+		len -= ret;
+		total += ret;
+	}
+	return total;
+}
+
+static void
+_xwrite (int fd, const char *data, gsize len)
+{
+	gsize ret;
+
+	g_assert (fd);
+
+	while (len) {
+		ret = write (fd, data, len);
+		if (G_UNLIKELY (ret == (gsize) -1)) {
+			if (errno == EINTR)
+				continue;
+			else
+				break;
+		}
+		data += ret;
+		len -= ret;
+	}
+}
+
+static void
+_xtruncate (gint fd, gsize offset)
+{
+	int ret;
+
+	g_assert (fd);
+
+	do {
+		ret = ftruncate (fd, offset);
+	} while (ret == -1 && errno == EINTR);
+}
+
+
 /*
  * VteFileStream: A POSIX file-based stream
  */
@@ -83,20 +138,14 @@ _vte_file_stream_ensure_fd0 (VteFileStream *stream)
 }
 
 static void
-_xwrite (int fd, const char *data, gsize len)
+_vte_file_stream_reset (VteStream *astream, gsize offset)
 {
-	gsize ret;
-	while (len) {
-		ret = write (fd, data, len);
-		if (G_UNLIKELY (ret == (gsize) -1)) {
-			if (errno == EINTR)
-				continue;
-			else
-				break;
-		}
-		data += ret;
-		len -= ret;
-	}
+	VteFileStream *stream = (VteFileStream *) astream;
+
+	if (stream->fd[0]) _xtruncate (stream->fd[0], 0);
+	if (stream->fd[1]) _xtruncate (stream->fd[1], 0);
+
+	stream->offset[0] = stream->offset[1] = offset;
 }
 
 static gsize
@@ -113,25 +162,6 @@ _vte_file_stream_append (VteStream *astream, const char *data, gsize len)
 	return stream->offset[0] + ret;
 }
 
-static gsize
-_xread (int fd, char *data, gsize len)
-{
-	gsize ret, total = 0;
-	while (len) {
-		ret = read (fd, data, len);
-		if (G_UNLIKELY (ret == (gsize) -1)) {
-			if (errno == EINTR)
-				continue;
-			else
-				break;
-		}
-		data += ret;
-		len -= ret;
-		total += ret;
-	}
-	return total;
-}
-
 static void
 _vte_file_stream_read (VteStream *astream, gsize offset, char *data, gsize len)
 {
@@ -166,15 +196,6 @@ _vte_file_stream_swap_fds (VteFileStream *stream)
 }
 
 static void
-_xtruncate (gint fd, gsize offset)
-{
-	int ret;
-	do {
-		ret = ftruncate (fd, offset);
-	} while (ret == -1 && errno == EINTR);
-}
-
-static void
 _vte_file_stream_truncate (VteStream *astream, gsize offset)
 {
 	VteFileStream *stream = (VteFileStream *) astream;
@@ -211,6 +232,7 @@ _vte_file_stream_class_init (VteFileStreamClass *klass)
 
 	gobject_class->finalize = _vte_file_stream_finalize;
 
+	klass->reset = _vte_file_stream_reset;
 	klass->append = _vte_file_stream_append;
 	klass->read = _vte_file_stream_read;
 	klass->truncate = _vte_file_stream_truncate;
diff --git a/src/vtestream.h b/src/vtestream.h
index 8843350..649e8fe 100644
--- a/src/vtestream.h
+++ b/src/vtestream.h
@@ -27,6 +27,7 @@ G_BEGIN_DECLS
 
 typedef struct _VteStream VteStream;
 
+void _vte_stream_reset (VteStream *stream, gsize offset);
 gsize _vte_stream_append (VteStream *stream, const char *data, gsize len);
 void _vte_stream_read (VteStream *stream, gsize offset, char *data, gsize len);
 void _vte_stream_truncate (VteStream *stream, gsize offset);



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