All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Classes | Public Member Functions | Static Public Member Functions | Protected Attributes | Private Member Functions | Private Attributes | Friends | List of all members
PLMD::OFile Class Reference

Class for output files. More...

#include <OFile.h>

Inheritance diagram for PLMD::OFile:
Inheritance graph
[legend]

Classes

class  Field
 Class identifying a single field for fielded output. More...
 

Public Member Functions

 OFile ()
 Constructor. More...
 
 ~OFile ()
 Destructor. More...
 
OFilelink (OFile &)
 Allows linking this OFile to another one. More...
 
void setBackupString (const std::string &)
 Set the string name to be used for automatic backup. More...
 
void backupFile (const std::string &bstring, const std::string &fname)
 Backup a file by giving it a different name. More...
 
void backupAllFiles (const std::string &str)
 This backs up all the files that would have been created with the name str. More...
 
OFileopen (const std::string &name)
 Opens the file using automatic append/backup. More...
 
OFilesetLinePrefix (const std::string &)
 Set the prefix for output. More...
 
OFilefmtField (const std::string &)
 Set the format for writing double precision fields. More...
 
OFilefmtField ()
 Reset the format for writing double precision fields to its default. More...
 
OFileprintField (const std::string &, double)
 Set the value of a double precision field. More...
 
OFileprintField (const std::string &, int)
 Set the value of a int field. More...
 
OFileprintField (const std::string &, const std::string &)
 Set the value of a string field. More...
 
OFileaddConstantField (const std::string &)
 
OFilesetupPrintValue (Value *val)
 Used to setup printing of values. More...
 
OFileprintField (Value *val, const double &v)
 Print a value. More...
 
OFileprintField ()
 Close a line. More...
 
OFileclearFields ()
 Resets the list of fields. More...
 
int printf (const char *fmt,...)
 Formatted output with explicit format - a la printf. More...
 
OFilerewind ()
 Rewind a file. More...
 
FileBaselink (FILE *)
 Link to an already open filed. More...
 
FileBaselink (PlumedMain &)
 Link to a PlumedMain object Automatically links also the corresponding Communicator. More...
 
FileBaselink (Communicator &)
 Link to a Communicator object. More...
 
FileBaselink (Action &)
 Link to an Action object. More...
 
FileBaseflush ()
 Flushes the file to disk. More...
 
void close ()
 Closes the file Should be used only for explicitely opened files. More...
 
 operator bool () const
 Check for error/eof. More...
 
void setHeavyFlush ()
 Set heavyFlush flag. More...
 
FileBaseopen (const std::string &name, const std::string &mode)
 Opens the file (without auto-backup) More...
 
bool FileExist (const std::string &path)
 Check if the file exists. More...
 
bool isOpen ()
 Check if a file is open. More...
 

Static Public Member Functions

static void test ()
 Runs a small testcase. More...
 

Protected Attributes

FILE * fp
 file pointer More...
 
Communicatorcomm
 communicator. NULL if not set More...
 
PlumedMainplumed
 pointer to main plumed object. NULL if not linked More...
 
Actionaction
 pointer to corresponding action. NULL if not linked More...
 
bool cloned
 Control closing on destructor. More...
 
bool eof
 Set to true when end of file is encountered. More...
 
bool err
 Set to true when error is encountered. More...
 
std::string path
 path of the opened file More...
 
bool heavyFlush
 Set to true if you want flush to be heavy (close/reopen) More...
 

Private Member Functions

size_t llwrite (const char *, size_t)
 Low-level write. More...
 
unsigned findField (const std::string &name) const
 Find field index given name. More...
 

Private Attributes

OFilelinked
 Pointer to a linked OFile. More...
 
char * buffer_string
 Internal buffer for printf. More...
 
char * buffer
 Internal buffer (generic use) More...
 
int buflen
 Internal buffer length. More...
 
unsigned actual_buffer_length
 This variables stores the actual buffer length. More...
 
bool fieldChanged
 True if fields has changed. More...
 
std::string fieldFmt
 Format for fields writing. More...
 
std::vector< Fieldprevious_fields
 All the previously defined variable fields. More...
 
std::vector< Fieldfields
 All the defined variable fields. More...
 
std::vector< Fieldconst_fields
 All the defined constant fields. More...
 
std::string linePrefix
 Prefix for line (e.g. "PLUMED: ") More...
 
std::ostringstream oss
 Temporary ostringstream for << output. More...
 
std::string backstring
 The string used for backing up files. More...
 

Friends

template<class T >
OFileoperator<< (OFile &, const T &)
 Formatted output with << operator. More...
 

Detailed Description

Class for output files.

This class provides features similar to those in the standard C "FILE*" type, but only for sequential output. See IFile for sequential input.

See the example here for a possible use:

#include "File.h"

int main(){
  PLMD::OFile pof;
  pof.open("ciao","w");
  pof.printf("%s\n","test1");
  pof.setLinePrefix("plumed: ");
  pof.printf("%s\n","test2");
  pof.setLinePrefix("");
  pof.addConstantField("x2").printField("x2",67.0);
  pof.printField("x1",10.0).printField("x3",20.12345678901234567890).printField();
  pof.printField("x1",10.0).printField("x3",-1e70*20.12345678901234567890).printField();
  pof.printField("x3",10.0).printField("x2",777.0).printField("x1",-1e70*20.12345678901234567890).printField();
  pof.printField("x3",67.0).printField("x1",18.0).printField();
  pof.close();
  return 0;
}

This program is expected to produce a file "ciao" which reads

test1
plumed: test2
#! FIELDS x1 x3
#! SET x2                      67
                     10      20.12345678901234
                     10 -2.012345678901235e+71
#! FIELDS x1 x3
#! SET x2                     777
 -2.012345678901235e+71                     10
                     18                     67

Notes

Definition at line 84 of file OFile.h.

Constructor & Destructor Documentation

PLMD::OFile::OFile ( )

Constructor.

Definition at line 50 of file OFile.cpp.

PLMD::OFile::~OFile ( )

Destructor.

Definition at line 66 of file OFile.cpp.

Member Function Documentation

OFile & PLMD::OFile::addConstantField ( const std::string &  name)

Definition at line 120 of file OFile.cpp.

void PLMD::OFile::backupAllFiles ( const std::string &  str)

This backs up all the files that would have been created with the name str.

It is used in analysis when you are not restarting. Analysis output files at different times, which are names analysis.0.<filename>, analysis.1.<filename> and <filename>, are backed up to bck.0.analysis.0.<filename>, bck.0.analysis.1.<filename> and bck.0.<filename>

Definition at line 222 of file OFile.cpp.

void PLMD::OFile::backupFile ( const std::string &  bstring,
const std::string &  fname 
)

Backup a file by giving it a different name.

Definition at line 237 of file OFile.cpp.

OFile & PLMD::OFile::clearFields ( )

Resets the list of fields.

As it is only possible to add new constant fields (addConstantField()), this method can be used to clean the field list.

Definition at line 128 of file OFile.cpp.

void PLMD::FileBase::close ( )
inherited

Closes the file Should be used only for explicitely opened files.

Definition at line 140 of file FileBase.cpp.

bool PLMD::FileBase::FileExist ( const std::string &  path)
inherited

Check if the file exists.

Definition at line 118 of file FileBase.cpp.

unsigned PLMD::OFile::findField ( const std::string &  name) const
private

Find field index given name.

FileBase & PLMD::FileBase::flush ( )
inherited

Flushes the file to disk.

Definition at line 70 of file FileBase.cpp.

OFile & PLMD::OFile::fmtField ( const std::string &  fmt)

Set the format for writing double precision fields.

Definition at line 135 of file OFile.cpp.

OFile & PLMD::OFile::fmtField ( )

Reset the format for writing double precision fields to its default.

Definition at line 140 of file OFile.cpp.

bool PLMD::FileBase::isOpen ( )
inherited

Check if a file is open.

Definition at line 134 of file FileBase.cpp.

FileBase & PLMD::FileBase::link ( FILE *  fp)
inherited

Link to an already open filed.

Definition at line 64 of file FileBase.cpp.

FileBase & PLMD::FileBase::link ( PlumedMain plumed)
inherited

Link to a PlumedMain object Automatically links also the corresponding Communicator.

Definition at line 85 of file FileBase.cpp.

FileBase & PLMD::FileBase::link ( Communicator comm)
inherited

Link to a Communicator object.

Definition at line 79 of file FileBase.cpp.

FileBase & PLMD::FileBase::link ( Action action)
inherited

Link to an Action object.

Automatically links also the corresponding PlumedMain and Communicator.

Definition at line 92 of file FileBase.cpp.

OFile & PLMD::OFile::link ( OFile l)

Allows linking this OFile to another one.

In this way, everything written to this OFile will be immediately written on the linked OFile. Notice that a OFile should be either opened explicitly, linked to a FILE or linked to a OFile

Definition at line 71 of file OFile.cpp.

size_t PLMD::OFile::llwrite ( const char *  ptr,
size_t  s 
)
private

Low-level write.

Definition at line 39 of file OFile.cpp.

FileBase & PLMD::FileBase::open ( const std::string &  name,
const std::string &  mode 
)
inherited

Opens the file (without auto-backup)

Definition at line 99 of file FileBase.cpp.

OFile & PLMD::OFile::open ( const std::string &  name)

Opens the file using automatic append/backup.

Definition at line 264 of file OFile.cpp.

PLMD::FileBase::operator bool ( ) const
inherited

Check for error/eof.

Definition at line 166 of file FileBase.cpp.

int PLMD::OFile::printf ( const char *  fmt,
  ... 
)

Formatted output with explicit format - a la printf.

Definition at line 82 of file OFile.cpp.

OFile & PLMD::OFile::printField ( const std::string &  name,
double  v 
)

Set the value of a double precision field.

Definition at line 145 of file OFile.cpp.

OFile & PLMD::OFile::printField ( const std::string &  name,
int  v 
)

Set the value of a int field.

Definition at line 151 of file OFile.cpp.

OFile & PLMD::OFile::printField ( const std::string &  name,
const std::string &  v 
)

Set the value of a string field.

Definition at line 157 of file OFile.cpp.

OFile & PLMD::OFile::printField ( Value val,
const double &  v 
)

Print a value.

Definition at line 180 of file OFile.cpp.

OFile & PLMD::OFile::printField ( )

Close a line.

Typically used as

  of.printField("a",a).printField("b",b).printField();

Definition at line 190 of file OFile.cpp.

OFile & PLMD::OFile::rewind ( )

Rewind a file.

Definition at line 284 of file OFile.cpp.

void PLMD::OFile::setBackupString ( const std::string &  str)

Set the string name to be used for automatic backup.

Definition at line 218 of file OFile.cpp.

void PLMD::FileBase::setHeavyFlush ( )
inlineinherited

Set heavyFlush flag.

Definition at line 101 of file FileBase.h.

OFile & PLMD::OFile::setLinePrefix ( const std::string &  l)

Set the prefix for output.

Typically "PLUMED: ". Notice that lines with a prefix cannot be parsed using fields in a IFile.

Definition at line 77 of file OFile.cpp.

OFile & PLMD::OFile::setupPrintValue ( Value val)

Used to setup printing of values.

Definition at line 172 of file OFile.cpp.

void PLMD::FileBase::test ( )
staticinherited

Runs a small testcase.

Definition at line 37 of file FileBase.cpp.

Friends And Related Function Documentation

template<class T >
OFile& operator<< ( OFile of,
const T &  t 
)
friend

Formatted output with << operator.

Definition at line 193 of file OFile.h.

Member Data Documentation

Action* PLMD::FileBase::action
protectedinherited

pointer to corresponding action. NULL if not linked

Definition at line 63 of file FileBase.h.

unsigned PLMD::OFile::actual_buffer_length
private

This variables stores the actual buffer length.

Definition at line 96 of file OFile.h.

std::string PLMD::OFile::backstring
private

The string used for backing up files.

Definition at line 120 of file OFile.h.

char* PLMD::OFile::buffer
private

Internal buffer (generic use)

Definition at line 92 of file OFile.h.

char* PLMD::OFile::buffer_string
private

Internal buffer for printf.

Definition at line 90 of file OFile.h.

int PLMD::OFile::buflen
private

Internal buffer length.

Definition at line 94 of file OFile.h.

bool PLMD::FileBase::cloned
protectedinherited

Control closing on destructor.

If true, file will not be closed in destructor

Definition at line 66 of file FileBase.h.

Communicator* PLMD::FileBase::comm
protectedinherited

communicator. NULL if not set

Definition at line 59 of file FileBase.h.

std::vector<Field> PLMD::OFile::const_fields
private

All the defined constant fields.

Definition at line 114 of file OFile.h.

bool PLMD::FileBase::eof
protectedinherited

Set to true when end of file is encountered.

Definition at line 71 of file FileBase.h.

bool PLMD::FileBase::err
protectedinherited

Set to true when error is encountered.

Definition at line 73 of file FileBase.h.

bool PLMD::OFile::fieldChanged
private

True if fields has changed.

This could be due to a change in the list of fields or a reset of a nominally constant field

Definition at line 106 of file OFile.h.

std::string PLMD::OFile::fieldFmt
private

Format for fields writing.

Definition at line 108 of file OFile.h.

std::vector<Field> PLMD::OFile::fields
private

All the defined variable fields.

Definition at line 112 of file OFile.h.

FILE* PLMD::FileBase::fp
protectedinherited

file pointer

Definition at line 57 of file FileBase.h.

bool PLMD::FileBase::heavyFlush
protectedinherited

Set to true if you want flush to be heavy (close/reopen)

Definition at line 77 of file FileBase.h.

std::string PLMD::OFile::linePrefix
private

Prefix for line (e.g. "PLUMED: ")

Definition at line 116 of file OFile.h.

OFile* PLMD::OFile::linked
private

Pointer to a linked OFile.

see link(OFile&)

Definition at line 88 of file OFile.h.

std::ostringstream PLMD::OFile::oss
private

Temporary ostringstream for << output.

Definition at line 118 of file OFile.h.

std::string PLMD::FileBase::path
protectedinherited

path of the opened file

Definition at line 75 of file FileBase.h.

PlumedMain* PLMD::FileBase::plumed
protectedinherited

pointer to main plumed object. NULL if not linked

Definition at line 61 of file FileBase.h.

std::vector<Field> PLMD::OFile::previous_fields
private

All the previously defined variable fields.

Definition at line 110 of file OFile.h.


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