Added "stlfgets" functionality.
authorMartin Read <mpread@chiark.greenend.org.uk>
Mon, 28 Feb 2011 10:50:48 +0000 (10:50 +0000)
committerMartin Read <mpread@chiark.greenend.org.uk>
Mon, 28 Feb 2011 10:50:48 +0000 (10:50 +0000)
include/libmormegil/stlfgets.hh [new file with mode: 0644]
man/libmormegil::stlfgets.3 [new file with mode: 0644]
src/stlfgets.cc [new file with mode: 0644]

diff --git a/include/libmormegil/stlfgets.hh b/include/libmormegil/stlfgets.hh
new file mode 100644 (file)
index 0000000..ce6d7ab
--- /dev/null
@@ -0,0 +1,37 @@
+// libmormegil/stlfgets.hh
+//
+// 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 <stdio.h>
+#include <string>
+
+namespace libmormegil
+{
+    int stlfgets(std::string& str, FILE *fp, size_t size_suggest = 128, size_t hard_size = 0x7fffffff);
+}
+
+// vim:ts=8:sw=4:expandtab:fo=cq
diff --git a/man/libmormegil::stlfgets.3 b/man/libmormegil::stlfgets.3
new file mode 100644 (file)
index 0000000..f593a8d
--- /dev/null
@@ -0,0 +1,31 @@
+.TH "LIBMORMEGIL::STLFGETS" 3 "February 25, 2011" "libmormegil Version 1.0" "libmormegil User Manual"
+.SH NAME
+libmormegil::stlfgets, libmormegil::vstlfgets \- fgets-alikes for std::string
+.SH SYNOPSIS
+#include <libmormegil/stlfgets.hh>
+
+int libmormegil::stlfgets(std::string& str, size_t size_suggest,
+                          FILE *fp, size_t hard_size);
+
+.SH DESCRIPTION
+The \fIlibmormegil::stlfgets\fP function clears the contents of \fIstr\fP,
+then reads a line from the FILE object pointed to by \fIfp\fP into \fIstr\fP
+using the same algorithm as described for the standard C library's \fIfgets\fP
+function, up to a maximum of \fIhard_size\fP bytes.
+
+The \fIsize_suggest\fP argument to \fIlibmormegil::stlfgets\fP is an allocation
+control hint, indicating an "expected" size for the line. Once \fIsize_suggest\fP
+bytes have been read from \fIfp\fP, the function will return even if no newline
+has been encountered.
+
+.SH RETURNS
+A return value of 0 indicates that no errors were encountered while reading
+from \fIfp\fP. A return value of -1 indicates that an error occurred while
+reading from \fIfp\fP. See the local documentation for the \fIfgetc\fP
+function for a list of possible \fIerrno\fP values in this case.
+
+.SH AUTHOR
+Martin Read
+
+.SH SEE ALSO
+libmormegil(3), libmormegil::stlprintf(3), fgets(3), fgetc(3)
diff --git a/src/stlfgets.cc b/src/stlfgets.cc
new file mode 100644 (file)
index 0000000..c057925
--- /dev/null
@@ -0,0 +1,70 @@
+// stlfgets.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 <string>
+#include <libmormegil/stlfgets.hh>
+
+int libmormegil::stlfgets(std::string& 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 < 0)
+        {
+            if (ferror(fp))
+            {
+                ret = -1;
+            }
+            break;
+        }
+        else if (ch == '\n')
+        {
+            str += (char) ch;
+            break;
+        }
+        else
+        {
+            str += (char) 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=cq
+