Updated List read

This commit is contained in:
Hrvoje Jasak 2020-03-20 14:00:19 +00:00
parent cb6aa365c3
commit 24584aa90c

View file

@ -41,57 +41,66 @@ Foam::List<T>::List(Istream& is)
template<class T>
Foam::Istream& Foam::operator>>(Istream& is, List<T>& L)
Foam::Istream& Foam::operator>>(Istream& is, List<T>& list)
{
// Anull list
L.setSize(0);
list.resize(0);
is.fatalCheck("operator>>(Istream&, List<T>&)");
is.fatalCheck(FUNCTION_NAME);
token firstToken(is);
is.fatalCheck("operator>>(Istream&, List<T>&) : reading first token");
is.fatalCheck(FUNCTION_NAME);
// Compound: simply transfer contents
if (firstToken.isCompound())
{
L.transfer
list.transfer
(
dynamicCast<token::Compound<List<T> > >
(
firstToken.transferCompoundToken(is)
)
);
}
else if (firstToken.isLabel())
{
label s = firstToken.labelToken();
// Set list length to that read
L.setSize(s);
return is;
}
// Label: could be int(..), int{...} or just a plain '0'
if (firstToken.isLabel())
{
const label len = firstToken.labelToken();
// Resize to length read
list.resize(len);
// Read list contents depending on data format
if (is.format() == IOstream::ASCII || !contiguous<T>())
{
// Read beginning of contents
char delimiter = is.readBeginList("List");
const char delimiter = is.readBeginList("List");
if (s)
if (len)
{
if (delimiter == token::BEGIN_LIST)
{
for (label i=0; i<s; i++)
for (label i=0; i<len; ++i)
{
is >> L[i];
is >> list[i];
is.fatalCheck
(
"operator>>(Istream&, List<T>&) : reading entry"
"operator>>(Istream&, List<T>&) : "
"reading entry"
);
}
}
else
{
// Uniform content (delimiter == token::BEGIN_BLOCK)
T element;
is >> element;
@ -101,9 +110,9 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& L)
"reading the single entry"
);
for (label i=0; i<s; i++)
for (label i=0; i<len; ++i)
{
L[i] = element;
list[i] = element; // Copy the value
}
}
}
@ -111,20 +120,25 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& L)
// Read end of contents
is.readEndList("List");
}
else
else if (len)
{
if (s)
{
is.read(reinterpret_cast<char*>(L.data()), s*sizeof(T));
// Non-empty, binary, contiguous
is.fatalCheck
(
"operator>>(Istream&, List<T>&) : reading the binary block"
);
}
is.read(reinterpret_cast<char*>(list.data()), len*sizeof(T));
is.fatalCheck
(
"operator>>(Istream&, List<T>&) : "
"reading the binary block"
);
}
return is;
}
else if (firstToken.isPunctuation())
// "(...)" : read as SLList and transfer contents
if (firstToken.isPunctuation())
{
if (firstToken.pToken() != token::BEGIN_LIST)
{
@ -134,23 +148,22 @@ Foam::Istream& Foam::operator>>(Istream& is, List<T>& L)
<< exit(FatalIOError);
}
// Putback the opening bracket
is.putBack(firstToken);
is.putBack(firstToken); // Putback the opening bracket
// Now read as a singly-linked list
SLList<T> sll(is);
SLList<T> sll(is); // Read as singly-linked list
// Convert the singly-linked list to this list
L = sll;
}
else
{
FatalIOErrorInFunction(is)
<< "incorrect first token, expected <int> or '(', found "
<< firstToken.info()
<< exit(FatalIOError);
// Reallocate and move assign list elements
list = std::move(sll);
return is;
}
FatalIOErrorInFunction(is)
<< "incorrect first token, expected <int> or '(', found "
<< firstToken.info()
<< exit(FatalIOError);
return is;
}