[gmime] fixed binary vs text mode in Windows files



commit 51b9a2aa4999291d32ce6843dfded4a28bd748d5
Author: Jeffrey Stedfast <fejj gnome org>
Date:   Thu May 7 22:31:11 2009 -0400

    fixed binary vs text mode in Windows files
    
    2009-05-07  Jeffrey Stedfast  <fejj novell com>
    
    	* gmime/gmime-stream-fs.c (stream_read): Removed gross hack.
    	(stream_write): Same.
    	(g_mime_stream_fs_new): On Windows systems, set the translation
    	mode to BINARY.
    	(g_mime_stream_fs_new_with_bounds): Same.
    
    	* gmime/gmime-stream-file.c: Same as GMimeStreamFs
---
 ChangeLog                 |   10 ++++++++++
 gmime/gmime-stream-file.c |   40 ++++++++++++++--------------------------
 gmime/gmime-stream-fs.c   |   30 ++++++++----------------------
 3 files changed, 32 insertions(+), 48 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index a13405a..9abe5df 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2009-05-07  Jeffrey Stedfast  <fejj novell com>
+
+	* gmime/gmime-stream-fs.c (stream_read): Removed gross hack.
+	(stream_write): Same.
+	(g_mime_stream_fs_new): On Windows systems, set the translation
+	mode to BINARY.
+	(g_mime_stream_fs_new_with_bounds): Same.
+
+	* gmime/gmime-stream-file.c: Same as GMimeStreamFs
+
 2009-05-02  Jeffrey Stedfast  <fejj novell com>
 
 	* gmime/gmime-stream-buffer.c (stream_seek_cache_read): Fixed a
diff --git a/gmime/gmime-stream-file.c b/gmime/gmime-stream-file.c
index 2fcb57c..d907d8b 100644
--- a/gmime/gmime-stream-file.c
+++ b/gmime/gmime-stream-file.c
@@ -23,6 +23,10 @@
 #include <config.h>
 #endif
 
+#ifdef G_OS_WIN32
+#include <fcntl.h>
+#include <io.h>
+#endif
 #include <errno.h>
 
 #include "gmime-stream-file.h"
@@ -149,20 +153,8 @@ stream_read (GMimeStream *stream, char *buf, size_t len)
 	/* make sure we are at the right position */
 	fseek (fstream->fp, (long) stream->position, SEEK_SET);
 	
-	if ((nread = fread (buf, 1, len, fstream->fp)) > 0) {
-#ifdef G_OS_WIN32
-		/* fucking Windows... sigh. Since it might decide to translate \r\n into \n,
-		 * we need to query the real position rather than using simple math. */
-		long pos;
-		
-		if ((pos = ftell (fstream->fp)) == -1)
-			stream->position += nread;
-		else
-			stream->position = pos;
-#else
+	if ((nread = fread (buf, 1, len, fstream->fp)) > 0)
 		stream->position += nread;
-#endif
-	}
 	
 	return (ssize_t) nread;
 }
@@ -189,20 +181,8 @@ stream_write (GMimeStream *stream, const char *buf, size_t len)
 	/* make sure we are at the right position */
 	fseek (fstream->fp, (long) stream->position, SEEK_SET);
 
-	if ((nwritten = fwrite (buf, 1, len, fstream->fp)) > 0) {
-#ifdef G_OS_WIN32
-		/* fucking Windows... sigh. Since it might decide to translate \n into \r\n,
-		 * we need to query the real position rather than using simple math. */
-		long pos;
-		
-		if ((pos = ftell (fstream->fp)) == -1)
-			stream->position += nwritten;
-		else
-			stream->position = pos;
-#else
+	if ((nwritten = fwrite (buf, 1, len, fstream->fp)) > 0)
 		stream->position += nwritten;
-#endif
-	}
 	
 	return (ssize_t) nwritten;
 }
@@ -388,6 +368,10 @@ g_mime_stream_file_new (FILE *fp)
 	GMimeStreamFile *fstream;
 	gint64 start;
 	
+#ifdef G_OS_WIN32
+	_setmode (fileno (fp), O_BINARY);
+#endif
+	
 	if ((start = ftell (fp)) == -1)
 		start = 0;
 	
@@ -419,6 +403,10 @@ g_mime_stream_file_new_with_bounds (FILE *fp, gint64 start, gint64 end)
 {
 	GMimeStreamFile *fstream;
 	
+#ifdef G_OS_WIN32
+	_setmode (fileno (fp), O_BINARY);
+#endif
+	
 	fstream = g_object_newv (GMIME_TYPE_STREAM_FILE, 0, NULL);
 	g_mime_stream_construct (GMIME_STREAM (fstream), start, end);
 	fstream->owner = TRUE;
diff --git a/gmime/gmime-stream-fs.c b/gmime/gmime-stream-fs.c
index 5cc2e8b..5bcdd16 100644
--- a/gmime/gmime-stream-fs.c
+++ b/gmime/gmime-stream-fs.c
@@ -172,18 +172,7 @@ stream_read (GMimeStream *stream, char *buf, size_t len)
 	} while (nread == -1 && errno == EINTR);
 	
 	if (nread > 0) {
-#ifdef G_OS_WIN32
-		/* fucking Windows... sigh. Since it might decide to translate \r\n into \n,
-		 * we need to query the real position rather than using simple math. */
-		off_t pos;
-		
-		if ((pos = lseek (fs->fd, 0, SEEK_CUR)) == -1)
-			stream->position += nread;
-		else
-			stream->position = pos;
-#else
 		stream->position += nread;
-#endif
 	} else if (nread == 0) {
 		fs->eos = TRUE;
 	}
@@ -227,18 +216,7 @@ stream_write (GMimeStream *stream, const char *buf, size_t len)
 		fs->eos = TRUE;
 	
 	if (nwritten > 0) {
-#ifdef G_OS_WIN32
-		/* fucking Windows... sigh. Since it might decide to translate \n into \r\n,
-		 * we need to query the real position rather than using simple math. */
-		off_t pos;
-		
-		if ((pos = lseek (fs->fd, 0, SEEK_CUR)) == -1)
-			stream->position += nwritten;
-		else
-			stream->position = pos;
-#else
 		stream->position += nwritten;
-#endif
 	} else if (n == -1) {
 		/* error and nothing written */
 		return -1;
@@ -443,6 +421,10 @@ g_mime_stream_fs_new (int fd)
 	GMimeStreamFs *fs;
 	gint64 start;
 	
+#ifdef G_OS_WIN32
+	_setmode (fd, O_BINARY);
+#endif
+	
 	if ((start = lseek (fd, (off_t) 0, SEEK_CUR)) == -1)
 		start = 0;
 	
@@ -472,6 +454,10 @@ g_mime_stream_fs_new_with_bounds (int fd, gint64 start, gint64 end)
 {
 	GMimeStreamFs *fs;
 	
+#ifdef G_OS_WIN32
+	_setmode (fd, O_BINARY);
+#endif
+	
 	fs = g_object_newv (GMIME_TYPE_STREAM_FS, 0, NULL);
 	g_mime_stream_construct (GMIME_STREAM (fs), start, end);
 	fs->owner = TRUE;



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