InChI
 
Loading...
Searching...
No Matches
util.c File Reference
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "mode.h"
#include "util.h"
#include "extr_ct.h"
#include "bcf_s.h"
+ Include dependency graph for util.c:

Data Structures

struct  tagElData
 

Macros

#define is_ascii(c)   ((unsigned)(c) < 0x80)
 
#define MIN_ATOM_CHARGE   (-2)
 
#define MAX_ATOM_CHARGE   2
 
#define NEUTRAL_STATE   (-MIN_ATOM_CHARGE)
 
#define NUM_ATOM_CHARGES   (MAX_ATOM_CHARGE - MIN_ATOM_CHARGE + 1)
 
#define MAX_NUM_VALENCES   5 /* max. number + 1 to provide zero termination */
 
#define MAXQ   16
 
#define __MYTOLOWER(c)   ( ((c) >= 'A') && ((c) <= 'Z') ? ((c) - 'A' + 'a') : (c) )
 

Typedefs

typedef struct tagElData ELDATA
 

Functions

int get_element_chemical_symbol (int nAtNum, char *szElement)
 Finds chemical symbol for element of given number.
 
int get_element_or_pseudoelement_symbol (int nAtNum, char *szElement)
 Finds symbol for element of given number. Accounts for (translates)pseudoelements.
 
int el_number_in_internal_ref_table (const char *elname)
 
int get_periodic_table_number (const char *elname)
 Get the periodic table number object.
 
int if_skip_add_H (int nPeriodicNum)
 Check if no H addition allowed.
 
int get_el_valence (int nPeriodicNum, int charge, int val_num)
 Get reference value of atom valence at given charge.
 
int get_unusual_el_valence (int nPeriodicNum, int charge, int radical, int bonds_valence, int num_H, int num_bonds)
 Output valence needed to unambiguosly reconstruct bonds.
 
int needed_unusual_el_valence (int nPeriodicNum, int charge, int radical, int bonds_valence, int actual_bonds_valence, int num_H, int num_bonds)
 Output valence needed to unambiguosly reconstruct number of H.
 
int detect_unusual_el_valence (int nPeriodicNum, int charge, int radical, int bonds_valence, int num_H, int num_bonds)
 Output valence that does not fit any known valences.
 
int get_el_type (int nPeriodicNum)
 Get the el type object.
 
int is_el_a_metal (int nPeriodicNum)
 Check if an element is a metal.
 
int extract_charges_and_radicals (char *elname, int *pnRadical, int *pnCharge)
 Extract charges and radicals from element name.
 
int extract_H_atoms (char *elname, S_CHAR num_iso_H[])
 Extract H atoms from element name.
 
int get_num_H (const char *elname, int inp_num_H, S_CHAR inp_num_iso_H[], int charge, int radical, int chem_bonds_valence, int atom_input_valence, int bAliased, int bDoNotAddH, int bHasMetalNeighbor)
 Get the number of attached hydrogens.
 
int get_atomic_mass_from_elnum (int nAtNum)
 Get the atomic mass from elnum object.
 
int get_atomic_mass (const char *elname)
 Get the atomic mass object.
 
AT_NUMBis_in_the_list (AT_NUMB *pathAtom, AT_NUMB nNextAtom, int nPathLen)
 Checks if an atom is in the list/path.
 
int * is_in_the_ilist (int *pathAtom, int nNextAtom, int nPathLen)
 Checks if an integer is in the list/path.
 
int is_ilist_inside (int *ilist, int nlist, int *ilist2, int nlist2)
 Checks if one list of integers is inside another list (ilist in ilist2)
 
int nBondsValToMetal (inp_ATOM *at, int iat)
 Gets the number of bond valences to a metal atom.
 
int num_of_H (inp_ATOM *at, int iat)
 Get the number of hydrogens.
 
U_CHAR ion_el_group (int el)
 Get the element group of an element. The base element rather than the periodic group is used to aid readability.
 
int has_other_ion_neigh (inp_ATOM *at, int iat, int iat_ion_neigh)
 Check whether an atom has ion neighbors.
 
int has_other_ion_in_sphere_2 (inp_ATOM *at, int iat, int iat_ion_neigh)
 Check whether an atom has ion neighbors within sphere (breath first search (BFS) up to r=2)
 
int nNoMetalNumBonds (inp_ATOM *at, int at_no)
 Returns the number of non-metal bonds.
 
int nNoMetalBondsValence (inp_ATOM *at, int at_no)
 Returns the number of non-metal bond valences.
 
int nNoMetalNeighIndex (inp_ATOM *at, int at_no)
 Get the index of the first element that is not a metal.
 
int nNoMetalOtherNeighIndex (inp_ATOM *at, int at_no, int cur_neigh)
 Get the index of an element that is not a metal excluding a given index.
 
int nNoMetalOtherNeighIndex2 (inp_ATOM *at, int at_no, int cur_neigh, int cur_neigh2)
 Get the index of an element that is not a metal excluding a 2 given indexes.
 
int get_endpoint_valence (U_CHAR el_number)
 Get the endpoint valence object.
 
int get_endpoint_valence_KET (U_CHAR el_number)
 Get the endpoint valence KET object.
 
int normalize_string (char *name)
 Normalize string (remove leading & trailing spaces, replace consecutive spaces with a single space, remove tabs.)
 
int dotify_non_printable_chars (char *line)
 Replace non-ASCII characters with '.',.
 
int read_upto_delim (char **pstring, char *field, int maxlen, char *delims)
 Read up to any delimiter from the string.
 
int is_matching_any_delim (char c, char *delims)
 Check if a character is in the list of possible delimiters.
 
void remove_trailing_spaces (char *p)
 Remove trailing spaces from a string.
 
void remove_one_lf (char *p)
 Remove one line feed character from the end of a string.
 
int mystrncpy (char *target, const char *source, unsigned maxlen)
 Copies up to maxlen characters INCLUDING end null from source to target. Fills out the rest of the target with null bytes. Protected from non-zero-terminated source and overlapped target/source.
 
char * lrtrim (char *p, int *nLen)
 Trim leading and trailing spaces from a string.
 
void extract_inchi_substring (char **buf, const char *str, size_t slen)
 Extract InChI substring embedded into a longer string.
 
void extract_auxinfo_substring (char **buf, const char *str, size_t slen)
 Extract AuxInfo substring embedded into a longer string.
 
int inchi_memicmp (const void *p1, const void *p2, size_t length)
 Compare two memory blocks in a case-insensitive manner.
 
int inchi_stricmp (const char *s1, const char *s2)
 Case-insensitive string comparison.
 
char * inchi__strnset (char *s, int val, size_t length)
 Set a string to a specified value for a given length.
 
char * inchi__strdup (const char *string)
 Duplicate a string.
 

Variables

const ELDATA ElData []
 
const int ERR_ELEM = 255
 
const int nElDataLen = sizeof( ElData ) / sizeof( ElData[0] ) - 1
 

Macro Definition Documentation

◆ __MYTOLOWER

#define __MYTOLOWER (   c)    ( ((c) >= 'A') && ((c) <= 'Z') ? ((c) - 'A' + 'a') : (c) )

◆ is_ascii

#define is_ascii (   c)    ((unsigned)(c) < 0x80)

◆ MAX_ATOM_CHARGE

#define MAX_ATOM_CHARGE   2

◆ MAX_NUM_VALENCES

#define MAX_NUM_VALENCES   5 /* max. number + 1 to provide zero termination */

◆ MAXQ

#define MAXQ   16

◆ MIN_ATOM_CHARGE

#define MIN_ATOM_CHARGE   (-2)

◆ NEUTRAL_STATE

#define NEUTRAL_STATE   (-MIN_ATOM_CHARGE)

◆ NUM_ATOM_CHARGES

#define NUM_ATOM_CHARGES   (MAX_ATOM_CHARGE - MIN_ATOM_CHARGE + 1)

Typedef Documentation

◆ ELDATA

typedef struct tagElData ELDATA

Function Documentation

◆ detect_unusual_el_valence()

int detect_unusual_el_valence ( int  nPeriodicNum,
int  charge,
int  radical,
int  bonds_valence,
int  num_H,
int  num_bonds 
)

Output valence that does not fit any known valences.

Parameters
nPeriodicNumPeriodic table number
chargeCharge
radicalRadical
bonds_valenceBonds valence
num_HNumber of hydrogens
num_bondsNumber of bonds
Returns
int Chemical valence if unusual, 0 otherwise
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ dotify_non_printable_chars()

int dotify_non_printable_chars ( char *  line)

Replace non-ASCII characters with '.',.

Parameters
linePointer to the input line
Returns
int Number of replacements
+ Here is the caller graph for this function:

◆ el_number_in_internal_ref_table()

int el_number_in_internal_ref_table ( const char *  elname)
+ Here is the caller graph for this function:

◆ extract_auxinfo_substring()

void extract_auxinfo_substring ( char **  buf,
const char *  str,
size_t  slen 
)

Extract AuxInfo substring embedded into a longer string.

Parameters
bufPointer to output buffer
strPointer to input string
slenLength of the input string
+ Here is the caller graph for this function:

◆ extract_charges_and_radicals()

int extract_charges_and_radicals ( char *  elname,
int *  pnRadical,
int *  pnCharge 
)

Extract charges and radicals from element name.

Parameters
elnameElement name
pnRadicalNumber of radicals
pnChargeNumber of charges
Returns
int Number of charges or radicals extracted
+ Here is the caller graph for this function:

◆ extract_H_atoms()

int extract_H_atoms ( char *  elname,
S_CHAR  num_iso_H[] 
)

Extract H atoms from element name.

Parameters
elnameElement name
num_iso_HNumber of isotopic H
Returns
int Number of H atoms extracted
+ Here is the caller graph for this function:

◆ extract_inchi_substring()

void extract_inchi_substring ( char **  buf,
const char *  str,
size_t  slen 
)

Extract InChI substring embedded into a longer string.

Parameters
bufPointer to output buffer
strPointer to input string
slenLength of the input string
+ Here is the caller graph for this function:

◆ get_atomic_mass()

int get_atomic_mass ( const char *  elname)

Get the atomic mass object.

Parameters
elnameElement name
Returns
Returns the atomic mass as integer
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_atomic_mass_from_elnum()

int get_atomic_mass_from_elnum ( int  nAtNum)

Get the atomic mass from elnum object.

Parameters
nAtNumElement number
Returns
Returns the atomic mass as integer
+ Here is the caller graph for this function:

◆ get_el_type()

int get_el_type ( int  nPeriodicNum)

Get the el type object.

Parameters
nPeriodicNumPeriodic table number
Returns
int Element type
+ Here is the caller graph for this function:

◆ get_el_valence()

int get_el_valence ( int  nPeriodicNum,
int  charge,
int  val_num 
)

Get reference value of atom valence at given charge.

Parameters
nPeriodicNumPeriodic table number
chargeCharge
val_numValence number
Returns
int Reference valence value
+ Here is the caller graph for this function:

◆ get_element_chemical_symbol()

int get_element_chemical_symbol ( int  nAtNum,
char *  szElement 
)

Finds chemical symbol for element of given number.

Parameters
nAtNumAtom number
szElementPointer to chemical symbol
Returns
int Returns 0 if OK and -1 if element was not found.
+ Here is the caller graph for this function:

◆ get_element_or_pseudoelement_symbol()

int get_element_or_pseudoelement_symbol ( int  nAtNum,
char *  szElement 
)

Finds symbol for element of given number. Accounts for (translates)pseudoelements.

Parameters
nAtNumAtom number
szElementPointer to chemical symbol
Returns
int Returns 0 if OK and -1 if element was not found.
+ Here is the caller graph for this function:

◆ get_endpoint_valence()

int get_endpoint_valence ( U_CHAR  el_number)

Get the endpoint valence object.

Parameters
el_numberElement number
Returns
int 3 if No, 2 if O, S, SE, TE or 0 otherwise
+ Here is the caller graph for this function:

◆ get_endpoint_valence_KET()

int get_endpoint_valence_KET ( U_CHAR  el_number)

Get the endpoint valence KET object.

Parameters
el_numberElement number
Returns
int 4 if C, 2 if O or 0 otherwise
+ Here is the caller graph for this function:

◆ get_num_H()

int get_num_H ( const char *  elname,
int  inp_num_H,
S_CHAR  num_iso_H[],
int  charge,
int  radical,
int  chem_bonds_valence,
int  atom_input_valence,
int  bAliased,
int  bDoNotAddH,
int  bHasMetalNeighbor 
)

Get the number of attached hydrogens.

Parameters
elnameElement name
inp_num_HInput number of H
num_iso_HNumber of isotopic H
chargeCharge
radicalRadical
chem_bonds_valenceNumber of chemical valence bonds
atom_input_valenceNumber of atom valence
bAliasedFlag indicating whether the atom is aliased??
bDoNotAddHFlag indicating whether to add hydrogens
bHasMetalNeighborFlag indicating whether there is a metal neighbor
Returns
int Number of attached hydrogens
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_periodic_table_number()

int get_periodic_table_number ( const char *  elname)

Get the periodic table number object.

Parameters
elnamePointer to element name
Returns
int Periodic table number
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_unusual_el_valence()

int get_unusual_el_valence ( int  nPeriodicNum,
int  charge,
int  radical,
int  bonds_valence,
int  num_H,
int  num_bonds 
)

Output valence needed to unambiguosly reconstruct bonds.

Parameters
nPeriodicNumPeriodic table number
chargeCharge
radicalRadical
bonds_valenceBonds valence
num_HNumber of hydrogens
num_bondsNumber of bonds
Returns
int Valence needed to unambiguously reconstruct bonds
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ has_other_ion_in_sphere_2()

int has_other_ion_in_sphere_2 ( inp_ATOM at,
int  iat,
int  iat_ion_neigh 
)

Check whether an atom has ion neighbors within sphere (breath first search (BFS) up to r=2)

Parameters
atPointer to atom array
iatAtom id
iat_ion_neighAtom id of ion neighbor
Returns
int Number of ions in sphere
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ has_other_ion_neigh()

int has_other_ion_neigh ( inp_ATOM at,
int  iat,
int  iat_ion_neigh 
)

Check whether an atom has ion neighbors.

Parameters
atPointer to atom array
iatAtom id
iat_ion_neighAtom id of ion neighbor
Returns
int Returns 1 for having an ion neighbor, 0 for none
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ if_skip_add_H()

int if_skip_add_H ( int  nPeriodicNum)

Check if no H addition allowed.

Parameters
nPeriodicNumPeriodic table number
Returns
int Non-zero if no H addition is allowed, zero otherwise
+ Here is the caller graph for this function:

◆ inchi__strdup()

char * inchi__strdup ( const char *  string)

Duplicate a string.

Parameters
stringPointer to the input string
Returns
char* Pointer to the duplicated string
+ Here is the caller graph for this function:

◆ inchi__strnset()

char * inchi__strnset ( char *  s,
int  val,
size_t  length 
)

Set a string to a specified value for a given length.

Parameters
sPointer to the string
valValue to set
lengthLength of the string to set
Returns
char* Pointer to the modified string

◆ inchi_memicmp()

int inchi_memicmp ( const void *  p1,
const void *  p2,
size_t  length 
)

Compare two memory blocks in a case-insensitive manner.

Parameters
p1Pointer to the first memory block.
p2Pointer to the second memory block.
lengthNumber of bytes to compare.
Returns
int < 0 if p1 < p2, 0 if p1 == p2, > 0 if p1 > p2
+ Here is the caller graph for this function:

◆ inchi_stricmp()

int inchi_stricmp ( const char *  s1,
const char *  s2 
)

Case-insensitive string comparison.

Parameters
s1Pointer to first string
s2Pointer to second string
Returns
int < 0 if p1 < p2, 0 if p1 == p2, > 0 if p1 > p2
+ Here is the caller graph for this function:

◆ ion_el_group()

U_CHAR ion_el_group ( int  el)

Get the element group of an element. The base element rather than the periodic group is used to aid readability.

Parameters
elElement number
Returns
U_CHAR Returns element group (NitrogenGroup = 7 (EL_NUMBER_N), OxygenGroup = 8 (EL_NUMBER_O), Carbon = 6 (EL_NUMBER_C))
+ Here is the caller graph for this function:

◆ is_el_a_metal()

int is_el_a_metal ( int  nPeriodicNum)

Check if an element is a metal.

Parameters
nPeriodicNumPeriodic table number
Returns
int 1 if the element is a metal, 0 otherwise
+ Here is the caller graph for this function:

◆ is_ilist_inside()

int is_ilist_inside ( int *  ilist,
int  nlist,
int *  ilist2,
int  nlist2 
)

Checks if one list of integers is inside another list (ilist in ilist2)

Parameters
ilistPointer to the first list of integers
nlistLength of ilist
ilist2Pointer to the second list of integers
nlist2Length of ilist2
Returns
int Returns 1 if ilist is inside ilist2, 0 otherwise
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ is_in_the_ilist()

int * is_in_the_ilist ( int *  pathAtom,
int  nNextAtom,
int  nPathLen 
)

Checks if an integer is in the list/path.

Parameters
pathAtomPointer to the list/path of integers
nNextAtomInteger to find
nPathLenLength of the list/path
Returns
int* Pointer to the found integer in the list/path, NULL if not found
+ Here is the caller graph for this function:

◆ is_in_the_list()

AT_NUMB * is_in_the_list ( AT_NUMB pathAtom,
AT_NUMB  nNextAtom,
int  nPathLen 
)

Checks if an atom is in the list/path.

Parameters
pathAtomPointer to the list/path of atoms
nNextAtomAtom to find
nPathLenList length
Returns
AT_NUMB* Pointer to the found atom in the list/path, NULL if not found
+ Here is the caller graph for this function:

◆ is_matching_any_delim()

int is_matching_any_delim ( char  c,
char *  delims 
)

Check if a character is in the list of possible delimiters.

Note
same as isspace if delims is " \t\n\v\f\r" (0x20 and 0x09-0x0D)
Parameters
cCharacter to check
delimsString of delimiter characters
Returns
int 1 if c is a delimiter, 0 otherwise
+ Here is the caller graph for this function:

◆ lrtrim()

char * lrtrim ( char *  p,
int *  nLen 
)

Trim leading and trailing spaces from a string.

Parameters
pPointer to the input string
nLenLength of the trimmed string
Returns
char* Pointer to the trimmed string
+ Here is the caller graph for this function:

◆ mystrncpy()

int mystrncpy ( char *  target,
const char *  source,
unsigned  maxlen 
)

Copies up to maxlen characters INCLUDING end null from source to target. Fills out the rest of the target with null bytes. Protected from non-zero-terminated source and overlapped target/source.

Note
If source is NULL or maxlen is 0, nothing is copied and 0 is returned.
Parameters
targetTarget string
sourceSource string
maxlenMaximum length to copy
Returns
1 on success, 0 if target is NULL or maxlen is 0 or source is NULL
+ Here is the caller graph for this function:

◆ nBondsValToMetal()

int nBondsValToMetal ( inp_ATOM at,
int  iat 
)

Gets the number of bond valences to a metal atom.

Parameters
atPointer to atom list
iatAtom number
Returns
int Number of bond valences, -1 if bond to metal order is not well defined
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ needed_unusual_el_valence()

int needed_unusual_el_valence ( int  nPeriodicNum,
int  charge,
int  radical,
int  bonds_valence,
int  actual_bonds_val,
int  num_H,
int  num_bonds 
)

Output valence needed to unambiguosly reconstruct number of H.

Parameters
nPeriodicNumPeriodic table number
chargeCharge
radicalRadical
bonds_valenceBonds valence
actual_bonds_valActual bonds valence
num_HNumber of hydrogens
num_bondsNumber of bonds
Returns
int Chemical valence needed to unambiguously reconstruct number of H
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nNoMetalBondsValence()

int nNoMetalBondsValence ( inp_ATOM at,
int  at_no 
)

Returns the number of non-metal bond valences.

Parameters
atPointer to atom list
at_noAtom number
Returns
int Number of non-metal bond valences
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nNoMetalNeighIndex()

int nNoMetalNeighIndex ( inp_ATOM at,
int  at_no 
)

Get the index of the first element that is not a metal.

Parameters
atPointer to atom list
at_noAtom number
Returns
int Atom index, -1 if nothing found
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nNoMetalNumBonds()

int nNoMetalNumBonds ( inp_ATOM at,
int  at_no 
)

Returns the number of non-metal bonds.

Parameters
atPointer to atom list
at_noAtom number
Returns
int Number of number of no metal bonds
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nNoMetalOtherNeighIndex()

int nNoMetalOtherNeighIndex ( inp_ATOM at,
int  at_no,
int  cur_neigh 
)

Get the index of an element that is not a metal excluding a given index.

Parameters
atPointer to atom list
at_noAtom number
cur_neighExcluding atom number
Returns
int Atom index, -1 if nothing found
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nNoMetalOtherNeighIndex2()

int nNoMetalOtherNeighIndex2 ( inp_ATOM at,
int  at_no,
int  cur_neigh,
int  cur_neigh2 
)

Get the index of an element that is not a metal excluding a 2 given indexes.

Parameters
atPointer to atom list
at_noAtom number
cur_neighExcluding atom number 1
cur_neigh2Excluding atom number 2
Returns
int Atom index, -1 if nothing found
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ normalize_string()

int normalize_string ( char *  name)

Normalize string (remove leading & trailing spaces, replace consecutive spaces with a single space, remove tabs.)

Parameters
nameInput string to normalize
Returns
int Number of characters in normalized string
+ Here is the caller graph for this function:

◆ num_of_H()

int num_of_H ( inp_ATOM at,
int  iat 
)

Get the number of hydrogens.

Parameters
atPointer to atom list
iatInput atom id
Returns
int Number of total hydrogens atoms
+ Here is the caller graph for this function:

◆ read_upto_delim()

int read_upto_delim ( char **  pstring,
char *  field,
int  maxlen,
char *  delims 
)

Read up to any delimiter from the string.

Parameters
pstringPointer to input string
fieldPointer to output field
maxlenMaximum length of the field
delimsDelimiters
Returns
int
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ remove_one_lf()

void remove_one_lf ( char *  p)

Remove one line feed character from the end of a string.

Parameters
pInput string
+ Here is the caller graph for this function:

◆ remove_trailing_spaces()

void remove_trailing_spaces ( char *  p)

Remove trailing spaces from a string.

Parameters
pInput string
+ Here is the caller graph for this function:

Variable Documentation

◆ ElData

const ELDATA ElData[]

◆ ERR_ELEM

const int ERR_ELEM = 255

◆ nElDataLen

const int nElDataLen = sizeof( ElData ) / sizeof( ElData[0] ) - 1