Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize is_open value in ReadFileBuffer. #254

Merged
merged 2 commits into from
Jun 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions src/QMCHamiltonians/ECPComponentBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,35 @@ bool ECPComponentBuilder::parse(const std::string& fname, xmlNodePtr cur)
return read_pp_file(fname);
}

int get_file_length(std::ifstream *fin)
int ReadFileBuffer::get_file_length(std::ifstream *f) const
{
fin->seekg (0, std::ios::end);
int length = fin->tellg();
fin->seekg (0, std::ios::beg);
return length;
f->seekg (0, std::ios::end);
int len = f->tellg();
f->seekg (0, std::ios::beg);
return len;
}

void ReadFileBuffer::reset()
{
if (is_open)
{
if(myComm == NULL || myComm->rank() == 0)
{
delete fin;
fin = NULL;
}
delete[] cbuffer;
cbuffer = NULL;
is_open = false;
length = 0;
}
}

bool ReadFileBuffer::open_file(const std::string &fname)
{

reset();

if (myComm == NULL || myComm->rank() == 0)
{
fin = new std::ifstream(fname.c_str());
Expand Down Expand Up @@ -104,21 +123,21 @@ bool ECPComponentBuilder::read_pp_file(const std::string &fname)
bool okay = buf.open_file(fname);
if(!okay)
{
APP_ABORT("ECPComponentBuilder::parse Missing PP file " + fname +"\n");
APP_ABORT("ECPComponentBuilder::read_pp_file Missing PP file " + fname +"\n");
}

okay = buf.read_contents();
if(!okay)
{
APP_ABORT("ECPComponentBuilder::parse Unable to read PP file " + fname +"\n");
APP_ABORT("ECPComponentBuilder::read_pp_file Unable to read PP file " + fname +"\n");
}

xmlDocPtr m_doc = xmlReadMemory(buf.contents(),buf.length,NULL,NULL,0);

if (m_doc == NULL)
{
xmlFreeDoc(m_doc);
APP_ABORT("ECPComponentBuilder::parse xml file "+fname+" is invalid");
APP_ABORT("ECPComponentBuilder::read_pp_file xml file "+fname+" is invalid");
}
// Check the document is of the right kind
xmlNodePtr cur = xmlDocGetRootElement(m_doc);
Expand Down
5 changes: 4 additions & 1 deletion src/QMCHamiltonians/ECPComponentBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,16 @@ class ReadFileBuffer
char *cbuffer;
std::ifstream *fin;
Communicate *myComm;
int get_file_length(std::ifstream *f) const;

public:
bool is_open;
int length;
ReadFileBuffer(Communicate *c) : cbuffer(NULL), fin(NULL), myComm(c), length(0) {}
ReadFileBuffer(Communicate *c) : cbuffer(NULL), fin(NULL), myComm(c), is_open(false), length(0) {}
bool open_file(const std::string &fname);
bool read_contents();
char *contents() { return cbuffer; }
void reset();

~ReadFileBuffer() {
delete[] cbuffer;
Expand Down
21 changes: 21 additions & 0 deletions src/QMCHamiltonians/tests/test_ecp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,26 @@ TEST_CASE("ReadFileBuffer_ecp","[hamiltonian]")
// TODO: add more checks that pseudopotential file was read correctly
}

TEST_CASE("ReadFileBuffer_reopen","[hamiltonian]")
{
// Initializing with no Communicate pointer under MPI,
// this will read the file on every node. Should be okay
// for testing purposes.
ReadFileBuffer buf(NULL);
bool open_okay = buf.open_file("simple.txt");
REQUIRE(open_okay == true);

bool read_okay = buf.read_contents();
REQUIRE(read_okay);
REQUIRE(buf.length == 14);

open_okay = buf.open_file("C.BFD.xml");
REQUIRE(open_okay == true);

read_okay = buf.read_contents();
REQUIRE(read_okay);
REQUIRE(buf.length > 14);
}

}