[med-svn] [libgtextutils] 32/83: Added File-Descriptor-Based Output Stream class.

Charles Plessy plessy at moszumanska.debian.org
Wed Jan 8 13:37:27 UTC 2014


This is an automated email from the git hooks/post-receive script.

plessy pushed a commit to branch debian/unstable
in repository libgtextutils.

commit 1bec9f5fa813e4ec4c9b10bf37ba8db762287e50
Author: A. Gordon <gordon at cshl.edu>
Date:   Wed Mar 25 21:24:02 2009 -0400

    Added File-Descriptor-Based Output Stream class.
---
 src/gtextutils/Makefile.am |   6 +-
 src/gtextutils/outbuf3.hpp | 150 +++++++++++++++++++++++++++++++++++++++++++++
 tests/Makefile.am          |   4 +-
 tests/test_fd_outbuf.cpp   |  89 +++++++++++++++++++++++++++
 4 files changed, 246 insertions(+), 3 deletions(-)

diff --git a/src/gtextutils/Makefile.am b/src/gtextutils/Makefile.am
index c7ac53b..034ead1 100644
--- a/src/gtextutils/Makefile.am
+++ b/src/gtextutils/Makefile.am
@@ -15,7 +15,8 @@ libgtextutils_0_2_a_SOURCES = stream_wrapper.cpp stream_wrapper.h \
 		          text_line_reader.cpp text_line_reader.h \
 			  container_join.h \
 			  natsort.h \
-			  strnatcmp.c strnatcmp.h
+			  strnatcmp.c strnatcmp.h \
+			  outbuf3.hpp
 
 libgtextutils_0_2_a_includedir = $(includedir)/gtextutils-0.2/gtextutils
 
@@ -23,4 +24,5 @@ libgtextutils_0_2_a_include_HEADERS = container_join.h \
 		  text_line_reader.h \
 		  stream_wrapper.h \
 		  natsort.h \
-		  strnatcmp.h 
+		  strnatcmp.h \
+		  outbuf3.hpp
diff --git a/src/gtextutils/outbuf3.hpp b/src/gtextutils/outbuf3.hpp
new file mode 100644
index 0000000..f0eea83
--- /dev/null
+++ b/src/gtextutils/outbuf3.hpp
@@ -0,0 +1,150 @@
+/*
+   Gordon's Text-Utilities Library
+   Copyright (C) 2009 Assaf Gordon (gordon at cshl.edu)
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Affero General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Affero General Public License for more details.
+
+   You should have received a copy of the GNU Affero General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>
+*/
+
+#ifndef __JOSUTTIS_FD_OUTBUF_H__
+#define __JOSUTTIS_FD_OUTBUF_H__
+
+/*
+ * The following code example is taken from the book
+ * "The C++ Standard Library - A Tutorial and Reference"
+ * by Nicolai M. Josuttis, Addison-Wesley, 1999
+ *
+ * (C) Copyright Nicolai M. Josuttis 1999.
+ * Permission to copy, use, modify, sell and distribute this software
+ * is granted provided this copyright notice appears in all copies.
+ * This software is provided "as is" without express or implied
+ * warranty, and with no claim as to its suitability for any purpose.
+ *
+ * Added by A. Gordon:
+ *  The file is available as "io/outbuf3.hpp" in the examples tarball at
+ *    http://www.josuttis.com/libbook/
+ *
+ *  And in the book at Chapter 13, Page 673.
+ *
+ * Modifications:
+ *    1. Larger buffer, with vector<char> 
+ *    2. Accepts output file descriptor in c'tor
+ */
+#include <cstdio>
+#include <streambuf>
+#include <vector>
+
+// for write():
+#ifdef _MSC_VER
+# include <io.h>
+#else
+# include <unistd.h>
+#endif
+
+class josuttis_fd_outbuf : public std::streambuf {
+  protected:
+    static const int bufferSize = 32768;   // size of data buffer
+
+    /*
+     * Note: The vector is used simply because it will manage the memory 
+     * resource for us. The buffer is used as a regular C buffer. 
+     * see "Effective STL" By Scott Meyer, Page 77,
+     * Item 17: "Know how to pass vector and string to legacy API"
+    */
+    std::vector<char> buffer_vector ;
+    char* buffer ;
+    int output_fd ;
+
+  public:
+    /* constructor
+     * - initialize data buffer
+     * - one character less to let the bufferSizeth character
+     *    cause a call of overflow()
+     */
+    josuttis_fd_outbuf ( int _output_fd ) :
+	buffer_vector(bufferSize),
+	buffer ( &buffer_vector[0] ),
+	output_fd ( _output_fd )
+    {
+        setp (buffer, buffer+(bufferSize-1));
+    }
+
+    /* destructor
+     * - flush data buffer
+     */
+    virtual ~josuttis_fd_outbuf () {
+        sync();
+    }
+
+  protected:
+    // flush the characters in the buffer
+    int flushBuffer () {
+        int num = pptr()-pbase();
+        if (write (output_fd, buffer, num) != num) {
+            return EOF;
+        }
+        pbump (-num);    // reset put pointer accordingly
+        return num;
+    }
+
+    /* buffer full
+     * - write c and all previous characters
+     */
+    virtual int_type overflow (int_type c) {
+        if (c != EOF) {
+            // insert character into the buffer
+            *pptr() = c;
+            pbump(1);
+        }
+        // flush the buffer
+        if (flushBuffer() == EOF) {
+            // ERROR
+            return EOF;
+        }
+        return c;
+    }
+
+    /* synchronize data with file/destination
+     * - flush the data in the buffer
+     */
+    virtual int sync () {
+        if (flushBuffer() == EOF) {
+            // ERROR
+            return -1;
+        }
+        return 0;
+    }
+};
+
+
+/*
+ * An output stream that uses the above outbuf
+ *
+ * Based on code example from page 673 (class fdostream)
+ */
+
+class josuttis_fdostream : public std::ostream 
+{
+private:
+	josuttis_fd_outbuf buf ;
+public:
+	josuttis_fdostream ( int fd ) :
+		std::ostream(0),
+		buf(fd)
+	{
+		rdbuf(&buf) ;
+	}
+};
+
+
+#endif
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 7e31520..dd19959 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -16,7 +16,8 @@ check_PROGRAMS = test_container_join \
 		 test_natural_sort \
 		 test_input_stream_wrapper \
 		 test_text_reader \
-		 test_text_reader_unget
+		 test_text_reader_unget \
+		 test_fd_outbuf
 
 TESTS = $(check_PROGRAMS)
 
@@ -28,4 +29,5 @@ test_natural_sort_SOURCES = test_natural_sort.cpp
 test_input_stream_wrapper_SOURCES = test_input_stream_wrapper.cpp
 test_text_reader_SOURCES = test_text_reader.cpp
 test_text_reader_unget_SOURCES = test_text_reader_unget.cpp
+test_fd_outbuf_SOURCES = test_fd_outbuf.cpp
 
diff --git a/tests/test_fd_outbuf.cpp b/tests/test_fd_outbuf.cpp
new file mode 100644
index 0000000..853ed55
--- /dev/null
+++ b/tests/test_fd_outbuf.cpp
@@ -0,0 +1,89 @@
+/*
+   Gordon's Text-Utilities Library
+   Copyright (C) 2009 Assaf Gordon (gordon at cshl.edu)
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Affero General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Affero General Public License for more details.
+
+   You should have received a copy of the GNU Affero General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>
+*/
+#include <vector>
+#include <map>
+#include <string>
+#include <iostream>
+#include <fstream>
+
+#include <err.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdio.h>
+
+#include <gtextutils/outbuf3.hpp>
+#include "tests_assertion.h"
+
+/*
+ * Test for File-Descriptor-based output buffer.
+ */
+
+using namespace std;
+
+const char* filename = "fdout.tmp" ;
+
+void create_file()
+{
+	//Create a File (using C API),
+	//then connect an output stream to it
+	int fd = open("fdout.tmp", O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR|S_IWUSR ) ;
+	if ( fd == -1 )
+		perror("Failed to create file 'fdout.tmp'");
+
+	josuttis_fdostream out2(fd);
+	out2 << "first line" << endl ;
+	out2 << "second line" << endl ;
+	out2 << "third line" << endl;
+
+	close(fd);
+}
+
+void verify_file()
+{
+	//To verify - use only STL classes,
+	//no home made libgtextutils classes.
+	ifstream ifs(filename);
+	string s;
+	ifs >> s ;
+	ASSERT ( s == "first" ) ;
+	ifs >> s ;
+	ASSERT ( s == "line" ) ;
+	ifs >> s ;
+	ASSERT ( s == "second" ) ;
+	ifs >> s ;
+	ASSERT ( s == "line" ) ;
+	ifs >> s ;
+	ASSERT ( s == "third" ) ;
+	ifs >> s ;
+	ASSERT ( s == "line" ) ;
+}
+
+int main()
+{
+	//Use file-descriptor 1 = STDOUT
+	josuttis_fdostream out1(1);
+	out1 << "Hello World from File-Descriptor Output Stream (josuttifs_fdostream)!" << endl;
+
+	create_file();
+
+	verify_file();
+
+	unlink(filename);
+}

-- 
Alioth's /git/debian-med/git-commit-notice on /srv/git.debian.org/git/debian-med/libgtextutils.git



More information about the debian-med-commit mailing list