Skip to content

Instantly share code, notes, and snippets.

@arrieta
Created June 28, 2016 04:33
Show Gist options
  • Save arrieta/2d3c0972511c79d4ed6e0d9ebf78266e to your computer and use it in GitHub Desktop.
Save arrieta/2d3c0972511c79d4ed6e0d9ebf78266e to your computer and use it in GitHub Desktop.
Slurps a file into a C++ std::string.
// -*- coding:utf-8; mode:c++; mode:auto-fill; fill-column:80; -*-
/// @file slurp.hpp
/// @brief Slurps a file into an std::string.
/// @author J. Arrieta <Juan.Arrieta@nablazerolabs.com>
/// @copyright Copyright (c) 2016 Nabla Zero Labs. All rights reserved.
/// @license This software is released under the MIT License.
///
/// The MIT License (MIT)
/// Copyright (c) 2016 Nabla Zero Labs
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy
/// of this software and associated documentation files (the "Software"), to
/// deal in the Software without restriction, including without limitation the
/// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
/// sell copies of the Software, and to permit persons to whom the Software is
/// furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in
/// all copies or substantial portions of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
/// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
/// IN THE SOFTWARE.
#pragma once
#ifndef SLURP_HPP_HEADER_GUARDS
#define SLURP_HPP_HEADER_GUARDS
// C++ Standard Library
#include <cstring>
#include <sstream>
#include <fstream>
#include <string>
#include <stdexcept>
/// @brief Slurp a file into an `std::string`.
/// @param [in] filename Path to the file.
/// @return An `std::string` with the file contents.
/// @throws std::runtime_error If the file cannot be open or read.
/// @throws std::length_error If the file size is larger than `std::string.max_size()`,
/// @throws std::ios_base::failure If an error occurred while reading the file.
/// @note The implementation of this function is based on the answers to my
/// stackoverflow.com question `Read whole ASCII file into C++ std::string`
/// posted on April 08, 2010.
///
/// Example:
///
/// @code
/// #include <iostream>
/// #include "slurp.hpp"
///
/// int
/// main()
/// {
/// auto contents = slurp(__FILE__);
/// std::cout << "read " __FILE__ " (size: " << contents.size() << ")\n";
/// }
///
/// @endcode
///
/// The example can be compiled with g++ or clang++ using either the
/// `-std=c++11` or `-std=c++14` flags (and it should compile on any compiler
/// supporting at least C++11).
///
/// Output:
///
/// @code
/// read test-slurp.cpp (size: 164)
/// @endcode
///
inline std::string
slurp(const std::string& filename)
{
std::ifstream fp(filename, std::ios::in | std::ios::binary);
if (fp) {
std::string contents;
fp.seekg(0, std::ios::end);
contents.resize(fp.tellg());
fp.seekg(0, std::ios::beg);
fp.read(&contents[0], contents.size());
fp.close();
return contents;
} else {
std::ostringstream msg;
msg << "cannot slurp \""
<< filename
<< "\": " << std::strerror(errno);
throw std::runtime_error(msg.str());
}
}
#endif // SLURP_HPP_HEADER_GUARDS
@arrieta
Copy link
Author

arrieta commented Jun 28, 2016

Anyone interested should read the answers to my stackoverflow.com question; they are very good, and provide numerous alternatives.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment