Added widechar version of stlfgets
authorfluffymormegil <mpread@chiark.greenend.org.uk>
Tue, 26 Apr 2011 21:54:48 +0000 (22:54 +0100)
committerfluffymormegil <mpread@chiark.greenend.org.uk>
Tue, 26 Apr 2011 21:54:48 +0000 (22:54 +0100)
Makefile
src/stlwfgets.cc [new file with mode: 0644]

index 90df601..cfe2fff 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -22,7 +22,7 @@ LIBMORMEGIL_SONAME:=libmormegil.so.$(MAJOR_VER)
 LIBMORMEGIL_SHORTNAME=libmormegil.so
 LIBMORMEGIL_STATIC_BUILT:=$(LIB_BUILDDIR)/$(LIBMORMEGIL_STATIC)
 LIBMORMEGIL_SHARED_BUILT:=$(LIB_BUILDDIR)/$(LIBMORMEGIL_REALNAME)
-LIBOBJS:=$(OBJ_BUILDDIR)/stlprintf.o $(OBJ_BUILDDIR)/stlwprintf.o $(OBJ_BUILDDIR)/stlfgets.o $(OBJ_BUILDDIR)/points.o $(OBJ_BUILDDIR)/Appcfg.o $(OBJ_BUILDDIR)/ordinaldate.o $(OBJ_BUILDDIR)/dice.o
+LIBOBJS:=$(OBJ_BUILDDIR)/stlprintf.o $(OBJ_BUILDDIR)/stlwprintf.o $(OBJ_BUILDDIR)/stlfgets.o $(OBJ_BUILDDIR)/stlwfgets.o $(OBJ_BUILDDIR)/points.o $(OBJ_BUILDDIR)/Appcfg.o $(OBJ_BUILDDIR)/ordinaldate.o $(OBJ_BUILDDIR)/dice.o
 
 LIBS:=$(LIBMORMEGIL_SHARED_BUILT) $(LIBMORMEGIL_STATIC_BUILT)
 ALL_LIBOBJS:=$(LIBOBJS) $(LIBOBJS_C)
@@ -32,8 +32,8 @@ MANPAGES=man/dice.3 man/libmormegil.3 man/libmormegil::Appcfg.3 man/libmormegil:
 SRCARCH_NAME:=libmormegil-$(MAJOR_VER).$(MINOR_VER).$(COMPAT_DEPTH)
 # The list of what goes in the deliverable source archive. Only subsystems
 # which I deem "ready" get put into the deliverable archive.
-SRCARCH_SRC:=src/Appcfg.cc src/dice.cc src/points.cc src/stlfgets.cc src/ordinaldate.cc src/stlprintf.cc src/stlwprintf.cc
-SRCARCH_INC:=$(INC_DIR)/dice.h $(INC_DIR)/isotime.h $(INC_DIR)/Appcfg.hh $(INC_DIR)/Coord.hh $(INC_DIR)/Points.hh $(INC_DIR)/S20prng.hh $(INC_DIR)/abs.hh $(INC_DIR)/divup.hh $(INC_DIR)/serial.hh $(INC_DIR)/sign.hh $(INC_DIR)/stlfgets.hh $(INC_DIR)/stlprintf.hh $(INC_DIR)/stlwprintf.hh
+SRCARCH_SRC:=src/Appcfg.cc src/dice.cc src/points.cc src/stlfgets.cc src/stlwfgets.cc src/ordinaldate.cc src/stlprintf.cc src/stlwprintf.cc
+SRCARCH_INC:=$(INC_DIR)/dice.h $(INC_DIR)/isotime.h $(INC_DIR)/Appcfg.hh $(INC_DIR)/Coord.hh $(INC_DIR)/Points.hh $(INC_DIR)/S20prng.hh $(INC_DIR)/abs.hh $(INC_DIR)/divup.hh $(INC_DIR)/serial.hh $(INC_DIR)/sign.hh $(INC_DIR)/stlfgets.hh $(INC_DIR)/stlwfgets.hh $(INC_DIR)/stlprintf.hh $(INC_DIR)/stlwprintf.hh
 SRCARCH_MAN=man/libmormegil.3 man/dice.3 man/libmormegil::Appcfg.3 man/libmormegil::Coord.3 man/libmormegil::Points.3 man/libmormegil::S20prng.3 man/libmormegil::abs.3 man/libmormegil::divup.3 man/libmormegil::isotime.3 man/libmormegil::serialize.3 man/libmormegil::sign.3 man/libmormegil::stlfgets.3 man/libmormegil::stlprintf.3
 SRCARCH_LIC=CC0 BSD-lite COPYING Copyright
 SRCARCH_EXA=examples/dice-test.c examples/coord-test.cc
diff --git a/src/stlwfgets.cc b/src/stlwfgets.cc
new file mode 100644 (file)
index 0000000..f7222ec
--- /dev/null
@@ -0,0 +1,71 @@
+// stlwfgets.cc
+//
+// Copyright 2011 Martin Read. All rights reserved.
+// 
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions
+// are met:
+// 1. Redistributions of source code must retain the above copyright
+//    notice, this list of conditions and the following disclaimer.
+// 2. Redistributions in binary form must reproduce the above copyright
+//    notice, this list of conditions and the following disclaimer in the
+//    documentation and/or other materials provided with the distribution.
+// 3. Neither the name of the author nor the names of any other contributors
+//    may be used to endorse or promote products derived from this software
+//    without their specific prior written permission.
+// 
+// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+// ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+// SUCH DAMAGE.
+
+#include <string.h>
+#include <stdio.h>
+#include <wchar.h>
+#include <string>
+#include <libmormegil/stlwfgets.hh>
+
+int libmormegil::stlwfgets(std::wstring& str, FILE *fp, size_t size_suggest, size_t hard_size)
+{
+    int ch;
+    int ret = 0;
+    str.clear();
+    str.reserve(size_suggest);
+    do
+    {
+        ch = fgetc(fp);
+        if (ch == WEOF)
+        {
+            if (ferror(fp))
+            {
+                ret = -1;
+            }
+            break;
+        }
+        else if (ch == '\n')
+        {
+            str += (wchar_t) ch;
+            break;
+        }
+        else
+        {
+            str += (wchar_t) ch;
+        }
+        if (str.size() >= hard_size)
+        {
+            // we've reached the user's requested maximum size
+            break;
+        }
+    } while (1);
+    return ret;
+}
+
+// vim:ts=8:sw=4:expandtab:fo=croq
+