FEDEM Solver  R8.0
Source code of the dynamics solver
Macros | Functions | Variables
binaryDB.c File Reference

Global functions binary file IO. More...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include <unistd.h>
#include "FFaLib/FFaOS/FFaTag_C.h"
#include "FFaLib/FFaOS/FFaFortran.H"
Include dependency graph for binaryDB.c:

Macros

#define _MAX_DBFIL   50
 

Functions

 SUBROUTINE (openbinarydb, OPENBINARYDB) const
 Opens a binary direct access file for read or write. More...
 
 SUBROUTINE (setbufdb, SETBUFDB) const
 Allocates an in-core buffer for the specified file. More...
 
static int flushBinaryDB (int file, int doFlush)
 Flushes the in-core buffer of the specified file to disk. More...
 
 SUBROUTINE (flushbinarydb, FLUSHBINARYDB) const
 Flushes the in-core buffer of the specified file to disk. More...
 
 SUBROUTINE (closebinary_db, CLOSEBINARY_DB) const
 Closes the specified file. More...
 
 SUBROUTINE (deletedb, DELETEDB) const
 Deletes the named file. More...
 
void closeAllBinaryDB ()
 Closes all opened binary files. More...
 
static int writeBinaryDB (int ifile, const void *p, size_t nSize, size_t nData)
 Internal generic function to actually write binary data to file. More...
 
static int writeDoubleDB (int ifile, const double *data, size_t ndat)
 Internal function to write double precision data to file. More...
 
 SUBROUTINE (writeintdb, WRITEINTDB) const
 Writes an integer array to the specified file. More...
 
 SUBROUTINE (writefloatdb, WRITEFLOATDB) const
 Writes a single precision array to the specified file. More...
 
 SUBROUTINE (writedoubled4, WRITEDOUBLED4) const
 Writes a double precision array to the specified file. More...
 
 SUBROUTINE (writedoubled8, WRITEDOUBLED8) const
 Writes a double precision array to the specified file. More...
 
 SUBROUTINE (writechardb, WRITECHARDB) const
 Writes a character string to the specified file. More...
 
static int swapBytes (char *p, size_t m, size_t n)
 Performs byte swapping of an array in case of endian discrepancies. More...
 
static int readBinaryDB (int ifile, void *p, size_t nSize, size_t nData, char silence)
 Internal generic function to actually read binary data from file. More...
 
 SUBROUTINE (readintdb, READINTDB) const
 Reads an integer array from the specified file. More...
 
 SUBROUTINE (readfloatdb, READFLOATDB) const
 Reads a single precision array from the specified file. More...
 
 SUBROUTINE (readdoubled4, READDOUBLED4) const
 Reads a double precision array from the specified file. More...
 
 SUBROUTINE (readdoubled8, READDOUBLED8) const
 Reads a double precision array from the specified file. More...
 
 SUBROUTINE (readchardb, READCHARDB) const
 Reads a character string from the specified file. More...
 
 SUBROUTINE (writetagdb, WRITETAGDB) const
 Writes the file tag and checksum to the specified file. More...
 
 SUBROUTINE (readtagdb, READTAGDB) const
 Reads the file tag and checksum from the specified file. More...
 
 SUBROUTINE (setpositiondb, SETPOSITIONDB) const
 Sets the file pointer for next read operation for the specified file. More...
 
 SUBROUTINE (getpositiondb, GETPOSITIONDB) const
 Returns the current file pointer for the specified file. More...
 
 SUBROUTINE (putchardb, PUTCHARDB) const
 Writes a character string at specified location in the specified file. More...
 
 SUBROUTINE (copybinarydb, COPYBINARYDB) const
 Copies data from one binary file to another. More...
 

Variables

static char * fname [_MAX_DBFIL]
 
static char * fbuff [_MAX_DBFIL]
 
static size_t bptr [_MAX_DBFIL]
 
static size_t bsiz [_MAX_DBFIL]
 
static FT_FILE fdb [_MAX_DBFIL]
 
static short doSwap [_MAX_DBFIL]
 
static const float Version = 1.0f
 

Detailed Description

Global functions binary file IO.

This file contains some Fortran-callable global functions for performing binary file IO based on low-level fwrite/fread calls.

Macro Definition Documentation

◆ _MAX_DBFIL

#define _MAX_DBFIL   50

Max number of binary files that can be open at the same time

Function Documentation

◆ closeAllBinaryDB()

void closeAllBinaryDB ( )

Closes all opened binary files.

Used by signal handlers only.

◆ flushBinaryDB()

static int flushBinaryDB ( int  file,
int  doFlush 
)
static

Flushes the in-core buffer of the specified file to disk.

Parameters
[in]fileIndex of the file to flush
[in]doFlushIf true, perform a physical flush of the file

◆ readBinaryDB()

static int readBinaryDB ( int  ifile,
void *  p,
size_t  nSize,
size_t  nData,
char  silence 
)
static

Internal generic function to actually read binary data from file.

Parameters
[in]ifileFile number in range <-_MAX_DBFIL,_MAX_DBFIL> < 0 : rewind the file before reading it
[out]pPointer to memory segment in which to store the data read
[in]nSizeSize of each data item
[in]nDataNumber of data items to read
[in]silenceIf > 0 suppress error message on read failure
Returns
0 : Nothing is done (nData is zero)
> 0 : Number of bytes read
-1 : Illegal file number or file not opened
-2 : Error during read
-3 : Invalid record length
-99 : End-of-file detected (no data read)

◆ SUBROUTINE() [1/21]

SUBROUTINE ( closebinary_db  ,
CLOSEBINARY_DB   
) const

Closes the specified file.

◆ SUBROUTINE() [2/21]

SUBROUTINE ( copybinarydb  ,
COPYBINARYDB   
) const

Copies data from one binary file to another.

◆ SUBROUTINE() [3/21]

SUBROUTINE ( deletedb  ,
DELETEDB   
) const

Deletes the named file.

◆ SUBROUTINE() [4/21]

SUBROUTINE ( flushbinarydb  ,
FLUSHBINARYDB   
) const

Flushes the in-core buffer of the specified file to disk.

◆ SUBROUTINE() [5/21]

SUBROUTINE ( getpositiondb  ,
GETPOSITIONDB   
) const

Returns the current file pointer for the specified file.

◆ SUBROUTINE() [6/21]

SUBROUTINE ( openbinarydb  ,
OPENBINARYDB   
) const

Opens a binary direct access file for read or write.

  • filnam Name of file to open
  • filtyp Type of file to open
    • 0 : Temporary file with no name, automatically deleted when closed
    • 1 : Existing file, opened for read-only
    • 2 : New file, opened for write
    • 3 : Existing file, opened for append
    • 4 : New file, opened for read and write
    • 5 : Existing file, opened for read and write
  • ifile Assigned file number in range [0,_MAX_DBFIL>
  • status Exit status
    • 0 : Everything is OK
    • -1 : Could not open the specified file
    • -2 : Too many simultaneously opened files

◆ SUBROUTINE() [7/21]

SUBROUTINE ( putchardb  ,
PUTCHARDB   
) const

Writes a character string at specified location in the specified file.

The actual location is identified by the character string tag. Any existing data at the specified location will be overwritten such that the total file size will not change (unless at the end of the file). If the tag is not found, the file is not touched.

◆ SUBROUTINE() [8/21]

SUBROUTINE ( readchardb  ,
READCHARDB   
) const

Reads a character string from the specified file.

◆ SUBROUTINE() [9/21]

SUBROUTINE ( readdoubled4  ,
READDOUBLED4   
) const

Reads a double precision array from the specified file.

◆ SUBROUTINE() [10/21]

SUBROUTINE ( readdoubled8  ,
READDOUBLED8   
) const

Reads a double precision array from the specified file.

◆ SUBROUTINE() [11/21]

SUBROUTINE ( readfloatdb  ,
READFLOATDB   
) const

Reads a single precision array from the specified file.

◆ SUBROUTINE() [12/21]

SUBROUTINE ( readintdb  ,
READINTDB   
) const

Reads an integer array from the specified file.

◆ SUBROUTINE() [13/21]

SUBROUTINE ( readtagdb  ,
READTAGDB   
) const

Reads the file tag and checksum from the specified file.

◆ SUBROUTINE() [14/21]

SUBROUTINE ( setbufdb  ,
SETBUFDB   
) const

Allocates an in-core buffer for the specified file.

◆ SUBROUTINE() [15/21]

SUBROUTINE ( setpositiondb  ,
SETPOSITIONDB   
) const

Sets the file pointer for next read operation for the specified file.

◆ SUBROUTINE() [16/21]

SUBROUTINE ( writechardb  ,
WRITECHARDB   
) const

Writes a character string to the specified file.

◆ SUBROUTINE() [17/21]

SUBROUTINE ( writedoubled4  ,
WRITEDOUBLED4   
) const

Writes a double precision array to the specified file.

◆ SUBROUTINE() [18/21]

SUBROUTINE ( writedoubled8  ,
WRITEDOUBLED8   
) const

Writes a double precision array to the specified file.

◆ SUBROUTINE() [19/21]

SUBROUTINE ( writefloatdb  ,
WRITEFLOATDB   
) const

Writes a single precision array to the specified file.

◆ SUBROUTINE() [20/21]

SUBROUTINE ( writeintdb  ,
WRITEINTDB   
) const

Writes an integer array to the specified file.

◆ SUBROUTINE() [21/21]

SUBROUTINE ( writetagdb  ,
WRITETAGDB   
) const

Writes the file tag and checksum to the specified file.

◆ swapBytes()

static int swapBytes ( char *  p,
size_t  m,
size_t  n 
)
static

Performs byte swapping of an array in case of endian discrepancies.

◆ writeBinaryDB()

static int writeBinaryDB ( int  ifile,
const void *  p,
size_t  nSize,
size_t  nData 
)
static

Internal generic function to actually write binary data to file.

Parameters
[in]ifileFile number in range [0,_MAX_DBFIL>
[in]pPointer to the data to write
[in]nSizeSize of each data item
[in]nDataNumber of data items to write
Returns
0 : Nothing is done (nData is zero)
1 : The number of bytes written is larger than INT_MAX
> 1 : Number of bytes written (less than or equal to INT_MAX)
-1 : Illegal file number or file not opened
-2 : Error during write

◆ writeDoubleDB()

static int writeDoubleDB ( int  ifile,
const double *  data,
size_t  ndat 
)
static

Internal function to write double precision data to file.

Parameters
[in]ifileFile number in range [0,_MAX_DBFIL>
[in]dataPointer to the data to write
[in]ndatNumber of doubles to write

This function is a wrapper for writeBinaryDB() used for large double arrays. The data is written in 2GB chunks on Windows to avoid run-time failure.

Variable Documentation

◆ bptr

size_t bptr[_MAX_DBFIL]
static

Indices to next available buffer location

◆ bsiz

size_t bsiz[_MAX_DBFIL]
static

The size of each in-core buffer

◆ doSwap

short doSwap[_MAX_DBFIL]
static

Byte-swapping flags for each file

◆ fbuff

char* fbuff[_MAX_DBFIL]
static

In-core buffers for the binary files

◆ fdb

FT_FILE fdb[_MAX_DBFIL]
static

File pointers for each file

◆ fname

char* fname[_MAX_DBFIL]
static

Names of all opened binary files

◆ Version

const float Version = 1.0f
static

Internal file version tag