\hypertarget{_2mnt_2_vers1_2kblib_2kblib_2io_8h-example}{}\section{/mnt/\+Vers1/kblib/kblib/io.\+h}
Read in spaces until the end of the line is found.\+nl may be used to consume whitespace left over after a formatted input operation before doing an unformatted input operation (such as std\+::getline).


\begin{DoxyCode}
\textcolor{keywordtype}{int} x\{\};
std::cout << \textcolor{stringliteral}{"Enter a number: "};
std::cin >> x; \textcolor{comment}{// potentially leaves a new line in the stream}
std::cout << \textcolor{stringliteral}{"Got "} << x << \textcolor{charliteral}{'\(\backslash\)n'};
std::string str;
std::cout << \textcolor{stringliteral}{"Enter a line of text: "};
\hyperlink{namespacekblib_a969c539efe527b12066408c0fd21f85e}{std::getline}(std::cin >> \hyperlink{namespacekblib_a1c69e2ede07e021b4a480b78496912e2}{kblib::nl}, str);
std::cout << \textcolor{stringliteral}{"Got "} << \hyperlink{namespacekblib_a70d68a88d3dd6d9b317c76d51a6863f8}{std::quoted}(str) << \textcolor{charliteral}{'\(\backslash\)n'};
\end{DoxyCode}



\begin{DoxyParams}{Parameters}
{\em is} & The stream to read from. \\
\hline
\end{DoxyParams}
\begin{DoxyReturn}{Returns}
std\+::istream\& is.
\end{DoxyReturn}

\begin{DoxyCodeInclude}
\textcolor{preprocessor}{#ifndef KBLIB\_IO\_H}
\textcolor{preprocessor}{#define KBLIB\_IO\_H}

\textcolor{preprocessor}{#include "\hyperlink{fakestd_8h}{fakestd.h}"}
\textcolor{preprocessor}{#include "\hyperlink{traits_8h}{traits.h}"}

\textcolor{preprocessor}{#include <fstream>}
\textcolor{preprocessor}{#include <functional>}
\textcolor{preprocessor}{#include <string>}

\textcolor{preprocessor}{#if KBLIB\_USE\_CXX17}
\textcolor{preprocessor}{#include <optional>}
\textcolor{preprocessor}{#endif}

\textcolor{preprocessor}{#include <iostream>}

\textcolor{keyword}{namespace }\hyperlink{namespacekblib}{kblib} \{

\textcolor{preprocessor}{#if KBLIB\_USE\_CXX17}

\textcolor{keyword}{template} <\textcolor{keyword}{typename} D = std::string, \textcolor{keyword}{typename} string,
          \textcolor{keyword}{typename} std::enable\_if\_t<is\_contiguous\_v<D>, \textcolor{keywordtype}{int}> = 0>
std::optional<D> \hyperlink{namespacekblib_aec38e0ea33d6fa9f741465683388f1b5}{get\_file\_contents}(\textcolor{keyword}{const} \textcolor{keywordtype}{string}& filename) \{
   static\_assert(std::is\_trivially\_copyable\_v<typename D::value\_type>,
                 \textcolor{stringliteral}{"D must be a sequence of trivial types"});
   static\_assert(\textcolor{keyword}{sizeof}(\textcolor{keyword}{typename} D::value\_type) == 1,
                 \textcolor{stringliteral}{"D must be a sequence of char-sized objects."});
   std::ifstream in(filename, std::ios::in | std::ios::binary);
   \textcolor{keywordflow}{if} (in) \{
      D contents;
      in.seekg(0, std::ios::end);
      \textcolor{keyword}{auto} \hyperlink{namespacekblib_a0e74aee94f446396c8f8b7bf34b16acb}{size} = in.tellg();
      contents.resize(\hyperlink{namespacekblib_a0e74aee94f446396c8f8b7bf34b16acb}{size});
      in.seekg(0, std::ios::beg);
      in.read(reinterpret\_cast<char*>(contents.data()), \hyperlink{namespacekblib_a0e74aee94f446396c8f8b7bf34b16acb}{size});
      in.close();
      \textcolor{keywordflow}{return} contents;
   \} \textcolor{keywordflow}{else} \{
      \textcolor{keywordflow}{return} std::nullopt;
   \}
\}

\textcolor{keyword}{template} <\textcolor{keyword}{typename} D = std::string, \textcolor{keyword}{typename} string,
          \textcolor{keyword}{typename} std::enable\_if\_t<!is\_contiguous\_v<D>, \textcolor{keywordtype}{int}> = 0>
std::optional<D> \hyperlink{namespacekblib_aec38e0ea33d6fa9f741465683388f1b5}{get\_file\_contents}(\textcolor{keyword}{const} \textcolor{keywordtype}{string}& filename) \{
   static\_assert(std::is\_trivially\_copyable\_v<typename D::value\_type>,
                 \textcolor{stringliteral}{"D must be a sequence of trivial types"});
   static\_assert(\textcolor{keyword}{sizeof}(\textcolor{keyword}{typename} D::value\_type) == 1,
                 \textcolor{stringliteral}{"D must be a sequence of char-sized objects."});
   std::ifstream in(filename, std::ios::in | std::ios::binary);
   \textcolor{keywordflow}{if} (in) \{
      D contents;
      in.seekg(0, std::ios::end);
      \hyperlink{namespacekblib_a6c72ec5b660d5a2c6c3e2a9de0794532}{try\_reserve}(contents, in.tellg());
      in.seekg(0, std::ios::beg);
      std::copy((std::istreambuf\_iterator<char>(in)),
                std::istreambuf\_iterator<char>(), std::back\_inserter(contents));
      in.close();
      \textcolor{keywordflow}{return} contents;
   \} \textcolor{keywordflow}{else} \{
      \textcolor{keywordflow}{return} std::nullopt;
   \}
\}
\textcolor{preprocessor}{#endif}

\textcolor{keyword}{inline} std::string \hyperlink{namespacekblib_a969c539efe527b12066408c0fd21f85e}{getline}(std::istream& is) \{
   std::string ret;
   \hyperlink{namespacekblib_a969c539efe527b12066408c0fd21f85e}{std::getline}(is, ret);
   \textcolor{keywordflow}{return} ret;
\}

\textcolor{keyword}{inline} std::istream& \hyperlink{namespacekblib_a71f23bcc21156290aefeec26cdeec07e}{eatWord}(std::istream& is) \{
   \textcolor{keywordflow}{do} \{
      is.get();
   \} \textcolor{keywordflow}{while} (is && !isspace(is.peek()));
   \textcolor{keywordflow}{return} is;
\}

[[deprecated(\textcolor{stringliteral}{"Use std::ws instead"})]] \textcolor{keyword}{inline} std::istream&
\hyperlink{namespacekblib_aff1643ceb294aebb8169542df4de244f}{eatSpace}(std::istream& is) \{
   \textcolor{keywordflow}{while} (is && isspace(is.peek())) \{
      is.get();
   \}
   \textcolor{keywordflow}{return} is;
\}

\textcolor{keyword}{template} <\textcolor{keyword}{typename} CharT, \textcolor{keyword}{typename} Traits>
\textcolor{keyword}{auto} \hyperlink{namespacekblib_a1c69e2ede07e021b4a480b78496912e2}{nl}(std::basic\_istream<CharT, Traits>& is)
    -> std::basic\_istream<CharT, Traits>& \{
   \textcolor{keywordflow}{for} (\textcolor{keyword}{typename} Traits::int\_type c = is.peek();
        is && c != kblib::eof<CharT> &&
        std::isspace(static\_cast<CharT>(c), is.getloc()) && c != is.widen(\textcolor{charliteral}{'\(\backslash\)n'});
        c = is.peek()) \{
      is.ignore();
   \}
   \textcolor{keywordflow}{if} (is.peek() == is.widen(\textcolor{charliteral}{'\(\backslash\)n'})) \{
      is.ignore();
   \}
   \textcolor{keywordflow}{return} is;
\}

\textcolor{keyword}{template} <\textcolor{keyword}{typename} F>
\textcolor{keyword}{struct }get\_manip \{
   F \hyperlink{structkblib_1_1get__manip_ad6c96adbf37dba3102defbb2ac1816f0}{\_f};
\};

\textcolor{keyword}{template} <\textcolor{keyword}{typename} CharT, \textcolor{keyword}{typename}... O,
          \textcolor{keyword}{template} <\textcolor{keyword}{typename}, \textcolor{keyword}{typename}...> \textcolor{keyword}{class }\textcolor{keywordtype}{string}>
\textcolor{keyword}{inline} \textcolor{keyword}{auto} \hyperlink{namespacekblib_ae8f8bebfafd521cb8073f469d94b130f}{get\_line}(string<CharT, O...>& str) \{
   \textcolor{keyword}{auto} \hyperlink{structkblib_1_1get__manip_ad6c96adbf37dba3102defbb2ac1816f0}{\_f} = [&](\textcolor{keyword}{auto}& istream) -> decltype(istream) \{
      \hyperlink{namespacekblib_a969c539efe527b12066408c0fd21f85e}{std::getline}(istream, str);
      \textcolor{keywordflow}{return} istream;
   \};
   \textcolor{keywordflow}{return} get\_manip<decltype(\_f)>\{\hyperlink{structkblib_1_1get__manip_ad6c96adbf37dba3102defbb2ac1816f0}{\_f}\};
\}

\textcolor{keyword}{template} <\textcolor{keyword}{typename} CharT, \textcolor{keyword}{typename}... O,
          \textcolor{keyword}{template} <\textcolor{keyword}{typename}, \textcolor{keyword}{typename}...> \textcolor{keyword}{class }\textcolor{keywordtype}{string}>
\textcolor{keyword}{inline} \textcolor{keyword}{auto} \hyperlink{namespacekblib_ae8f8bebfafd521cb8073f469d94b130f}{get\_line}(string<CharT, O...>& str, CharT delim) \{
   \textcolor{keyword}{auto} \hyperlink{structkblib_1_1get__manip_ad6c96adbf37dba3102defbb2ac1816f0}{\_f} = [&](\textcolor{keyword}{auto}& istream) -> decltype(istream) \{
      \hyperlink{namespacekblib_a969c539efe527b12066408c0fd21f85e}{std::getline}(istream, str, delim);
      \textcolor{keywordflow}{return} istream;
   \};
   \textcolor{keywordflow}{return} get\_manip<decltype(\_f)>\{\hyperlink{structkblib_1_1get__manip_ad6c96adbf37dba3102defbb2ac1816f0}{\_f}\};
\}

\textcolor{keyword}{template} <\textcolor{keyword}{typename} F, \textcolor{keyword}{typename} CharT, \textcolor{keyword}{typename} Tr>
std::basic\_istream<CharT, Tr>& \hyperlink{namespacekblib_a1b4db8b92e28dc79ffb9ba402b43fe75}{operator>>}(std::basic\_istream<CharT, Tr>& is,
                                          get\_manip<F> func) \{
   \textcolor{keywordflow}{return} func.\_f(is);
\}

\textcolor{keyword}{template} <\textcolor{keyword}{typename} F, \textcolor{keyword}{typename} CharT, \textcolor{keyword}{typename} Tr>
std::basic\_ostream<CharT, Tr>& operator<<(std::basic\_ostream<CharT, Tr>& is,
                                          get\_manip<F> func) \{
   \textcolor{keywordflow}{return} func.\_f(is);
\}

\} \textcolor{comment}{// namespace kblib}

\textcolor{preprocessor}{#endif // KBLIB\_IO\_H}
\end{DoxyCodeInclude}
 