Page Menu
Home
HEPForge
Search
Configure Global Search
Log In
Files
F11222437
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Subscribers
None
View Options
Index: trunk/npstat/nm/vectorAsText.hh
===================================================================
--- trunk/npstat/nm/vectorAsText.hh (revision 878)
+++ trunk/npstat/nm/vectorAsText.hh (revision 879)
@@ -1,123 +1,122 @@
#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 <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <climits>
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 <typename T>
bool dumpVectorAsText(const std::vector<T>& 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<sz && ndumped<nElementsToDump; ++i, ++ndumped)
asciiStream << v[i] << '\n';
if (asciiStream.fail())
return false;
}
}
return true;
}
/**
// Function for filling vectors from text files, one element per line.
// Will only work with T objects that have default constructors.
// "true" is returned on success, "false" on failure.
//
// Empty lines, lines which consist of pure white space, and lines
// which start with an arbitrary amount of white space (including
// none) followed by '#' are ignored (considered comments).
*/
template <typename T>
bool fillVectorFromText(std::istream& asciiStream,
std::vector<T>* 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<maxElementsToFill)
{
std::getline(asciiStream, linebuf);
const unsigned long len = linebuf.size();
if (len == 0UL)
continue;
// Ignore lines which are pure white space
// or which start with an arbitrary number
// of white space characters followed by #.
bool isComment = false;
bool allSpace = true;
char* line = &linebuf[0];
- for (unsigned long i=0; i<len; ++i)
+ for (unsigned long i=0; i<len && allSpace; ++i)
{
if (isspace(line[i]))
continue;
if (allSpace && line[i] == '#')
{
isComment = true;
break;
}
allSpace = false;
- break;
}
if (isComment || allSpace)
continue;
is.str(linebuf);
is.clear();
is >> 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_
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Wed, May 14, 11:53 AM (2 h, 10 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5111552
Default Alt Text
(4 KB)
Attached To
rNPSTATSVN npstatsvn
Event Timeline
Log In to Comment