Index: trunk/npstat/nm/vectorAsText.hh =================================================================== --- trunk/npstat/nm/vectorAsText.hh (revision 879) +++ trunk/npstat/nm/vectorAsText.hh (revision 880) @@ -1,122 +1,119 @@ #ifndef NPSTAT_VECTORASTEXT_HH_ #define NPSTAT_VECTORASTEXT_HH_ /*! // \file vectorAsText.hh // // \brief Utilities for reading/writing std::vector objects from/to text files // // Author: I. Volobouev // // January 2023 */ #include #include #include #include #include namespace npstat { /** // Function for dumping vectors into text files, one element per line. // // Note that, while this function will work with T objects that do // not have default constructors, it will not be possible to read // such objects back. // // "true" is returned on success, "false" on failure. // Dumping less than "nElementsToDump" elements is considered // a success. */ template bool dumpVectorAsText(const std::vector& v, std::ostream& asciiStream, const unsigned long firstElementToDump=0, const unsigned long nElementsToDump=ULONG_MAX) { if (nElementsToDump) { const unsigned long sz = v.size(); if (firstElementToDump < sz) { unsigned long ndumped = 0; for (unsigned long i=firstElementToDump; i bool fillVectorFromText(std::istream& asciiStream, std::vector* v, const unsigned long maxElementsToFill=ULONG_MAX) { bool status = true; if (maxElementsToFill && asciiStream) { assert(v); std::string linebuf; std::istringstream is; unsigned long nfilled = 0; T buffer; while (asciiStream && status && nfilled> buffer; if (is.fail()) status = false; else v->push_back(buffer); if ((asciiStream.fail() && !asciiStream.eof()) || asciiStream.bad()) status = false; } } return status; } } #endif // NPSTAT_VECTORASTEXT_HH_