--- /dev/null
+// 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
--- /dev/null
+.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)
--- /dev/null
+// 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
+