NAMD
Public Member Functions | List of all members
IndexFile Class Reference

#include <IndexFile.h>

Public Member Functions

 IndexFile (std::string _filename)
 
 ~IndexFile ()
 
unsigned int size ()
 
unsigned int getNumElements ()
 
std::vector< unsigned int > getAllElements ()
 
void seek (off_t pos)
 
std::vector< unsigned int > getNElements (off_t offset, size_t numElementsRange)
 
std::vector< unsigned int > getNElements (size_t numElements)
 
std::vector< unsigned int > getElements ()
 
bool open ()
 
void close ()
 

Detailed Description

Copyright (c) 2025 by The Board of Trustees of the University of Illinois. All rights reserved.

Definition at line 46 of file IndexFile.h.

Constructor & Destructor Documentation

◆ IndexFile()

IndexFile::IndexFile ( std::string  _filename)
inline

Definition at line 54 of file IndexFile.h.

References NAMD_die(), and open().

54  : filename(_filename){
55  version=0;
56  open();
57  if(fileStream.fail())
58  NAMD_die("error in IndexFile::IndexFile");
59  }
void NAMD_die(const char *err_msg)
Definition: common.C:147
bool open()
Definition: IndexFile.h:104

◆ ~IndexFile()

IndexFile::~IndexFile ( )
inline

Definition at line 61 of file IndexFile.h.

References close().

61 {close();}
void close()
Definition: IndexFile.h:146

Member Function Documentation

◆ close()

void IndexFile::close ( )
inline

Definition at line 146 of file IndexFile.h.

Referenced by ~IndexFile().

146 {fileStream.close();}

◆ getAllElements()

std::vector<unsigned int> IndexFile::getAllElements ( )
inline

Definition at line 64 of file IndexFile.h.

References NAMD_die().

Referenced by getElements(), and ParallelIOMgr::readInfoForParOutDcdSelection().

65  {
66  std::vector<unsigned int> index;
67  index.resize(numElements);
68  fileStream.read(reinterpret_cast<char*>(&index[0]), index.size()* sizeof(unsigned int));
69  // auto print = [] (const int &n){ std::cout << n << "\n";};
70  // std::for_each(index.begin(), index.end(), print);
71  if(networkEndian)
72  std::for_each(index.begin(), index.end(), [] (unsigned int &n) { n = ntohl(n);});
73  if(fileStream.good())
74  return index;
75  else
76  NAMD_die("error in IndexFile::getAllElements");
77  }
void NAMD_die(const char *err_msg)
Definition: common.C:147

◆ getElements()

std::vector<unsigned int> IndexFile::getElements ( )
inline

Definition at line 99 of file IndexFile.h.

References getAllElements().

100  {
101  return getAllElements();
102  }
std::vector< unsigned int > getAllElements()
Definition: IndexFile.h:64

◆ getNElements() [1/2]

std::vector<unsigned int> IndexFile::getNElements ( off_t  offset,
size_t  numElementsRange 
)
inline

Definition at line 81 of file IndexFile.h.

References NAMD_die(), and seek().

Referenced by getNElements().

82  {
83  std::vector<unsigned int> index;
84  index.resize(numElementsRange);
85  seek(offset);
86  fileStream.read(reinterpret_cast<char*>(&index[0]), index.size()* sizeof(unsigned int));
87  if(networkEndian)
88  std::for_each(index.begin(), index.end(), [] (unsigned int &n) { n = ntohl(n);});
89  if(fileStream.good())
90  return index;
91  else
92  NAMD_die("error in IndexFile::getNElements");
93  }
void NAMD_die(const char *err_msg)
Definition: common.C:147
void seek(off_t pos)
Definition: IndexFile.h:79

◆ getNElements() [2/2]

std::vector<unsigned int> IndexFile::getNElements ( size_t  numElements)
inline

Definition at line 95 of file IndexFile.h.

References getNElements().

95  {
96  return getNElements(0, numElements);
97  }
std::vector< unsigned int > getNElements(off_t offset, size_t numElementsRange)
Definition: IndexFile.h:81

◆ getNumElements()

unsigned int IndexFile::getNumElements ( )
inline

Definition at line 63 of file IndexFile.h.

63 { return numElements;}

◆ open()

bool IndexFile::open ( void  )
inline

Definition at line 104 of file IndexFile.h.

References NAMD_die().

Referenced by IndexFile().

105  {
106  fileStream.open(filename.c_str(), std::ios::in | std::ifstream::binary);
107  // ifstream portable way to get file size
108  fileStream.read(reinterpret_cast<char*>(&version), sizeof(unsigned short int));
109  fileStream.seekg(0, fileStream.end);
110  unsigned int lengthCheck = fileStream.tellg();
111  unsigned int countedLength =0;
112  fileStream.seekg(0, fileStream.beg);
113  unsigned short beVersion=ntohs(version);
114  if(version>=256)
115  {
116  if(ntohs(version)==1)
117  {
118  networkEndian = true;
119  version = beVersion;
120 
121  }
122  }
123  switch (version) {
124  case 0:
125  numElements = lengthCheck/sizeof(unsigned int);
126  break;
127  case 1:
128  fileStream.seekg(2, fileStream.beg); // need to seek past the version
129  fileStream.read(reinterpret_cast<char*>(&numElements), sizeof(unsigned int));
130  numElements = (networkEndian) ? ntohl(numElements) : numElements;
131  countedLength = (lengthCheck-sizeof(char)-sizeof(unsigned int))/sizeof(unsigned int);
132  if(numElements!= countedLength)
133  {
134  std::string e="error in IndexFile::open mismatch in element count";
135  e+=" file reports "+std::to_string(numElements)+ " counted "+std::to_string(countedLength);
136  NAMD_die(e.c_str());
137  }
138  break;
139  default:
140  NAMD_die("error in IndexFile::open unsupported version");
141  break;
142  }
143  return(fileStream.is_open());
144  }
void NAMD_die(const char *err_msg)
Definition: common.C:147

◆ seek()

void IndexFile::seek ( off_t  pos)
inline

Definition at line 79 of file IndexFile.h.

Referenced by getNElements().

79 {fileStream.seekg(pos);}

◆ size()

unsigned int IndexFile::size ( )
inline

Definition at line 62 of file IndexFile.h.

62 { return numElements;}

The documentation for this class was generated from the following file: