Legofit
infers population history from nucleotide site patterns.
Macros | Functions
misc.c File Reference

Functions that didn't seem to belong anywhere else. More...

#include "misc.h"
#include "binary.h"
#include "lblndx.h"
#include "error.h"
#include <assert.h>
#include <ctype.h>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_rng.h>
#include <limits.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <execinfo.h>

Macros

#define CALLSTACK_SIZE   128
 

Functions

void dostacktrace (const char *file, int line, FILE *ofp)
 Print call stack. More...
 
void tellopt (const char *opt, const char *description)
 Describe an option. More...
 
int getNumCores (void)
 An almost platform-independent function that returns the number of CPU cores on the current machine. More...
 
void eprintf (const char *fmt,...)
 eprintf: print error message and exit. More...
 
FILE * efopen (const char *restrict name, const char *restrict mode)
 Open file. More...
 
double perturb_ratio_w (double x, double w, gsl_rng *rng)
 Uniform perturbation on log scale. More...
 
double perturb_ratio (double x, gsl_rng *rng)
 Uniform perturbation on log scale. More...
 
long double perturb_interval (long double x, long double lo, long double hi, gsl_rng *rng)
 Uniform perturbation within interval. More...
 
unsigned Dbl_first_geq (double val, unsigned len, double v[len])
 Vector v must be sorted in ascending order before this function is called. More...
 
long long_first_geq (long val, long *v, long len)
 Vector v must be sorted in ascending order before this function is called. More...
 
long long_last_leq (long val, long *v, long len)
 Vector v must be sorted in ascending order before this function is called. More...
 
void unitTstResult (const char *facility, const char *result)
 Print result of a unit test. More...
 
int strCountSetChunks (const char *str, const char *sep)
 In string str, count the number of contiguous chunks of characters belonging to set.
 
void * memdup (const void *p, size_t n)
 duplicate memory block
 
int compareLongs (const void *void_x, const void *void_y)
 Compare two long ints. More...
 
int compareInts (const void *void_x, const void *void_y)
 Compare two ints. More...
 
int compareDoubles (const void *void_x, const void *void_y)
 Compare two doubles. More...
 
double KLdiverg (int n, const double o[n], const double e[n])
 Return Kullback-Leibler divergence of o relative to e.
 
double sumDbl (int n, const double x[n])
 Sum an array of doubles.
 
double reflect (double x, double lo, double hi)
 Fold x back and forth across the boundaries "lo" and "hi" to obtain a value y such that lo <= y <= hi.
 
char * strlowercase (char *s)
 Convert NULL-terminated string to lower case.
 
unsigned long strhash (const char *ss)
 Hash a character string using algorithm of Daniel J. Boorstein.
 
int stripchr (char *s, int c)
 Remove character c from string s. Return length of string.
 
char * stripWhiteSpace (char *buff)
 Remove white space from beginning and end of a string. More...
 
char * stripInternalWhiteSpace (char *buff)
 
char * nextWhitesepToken (char **str)
 Return tokens separated by 1 or more spaces and/or tabs. More...
 
int tokenize (int dim, char *token[dim], char *s, const char *delim)
 Separate string s into tokens, separated by any character in string delim. More...
 
void strReplaceChr (char *s, int a, int b)
 In string s, replace instances of character a with character b.
 
double parseDbl (char *token)
 Parse token as a double. More...
 
char * strltrunc (char *s, int n)
 Truncate string to n characters (plus terminating '\0') by removing characters from the left.
 
void hdr (const char *msg)
 
char * strcenter (const char *text, unsigned width, char *buff, size_t buffsize)
 Center string "text" in a field of width "width". More...
 
int removeZeroes (int dim, unsigned x[dim])
 Remove zeroes from an array of unsigned ints by sliding positive entries to the left. More...
 
int readline (int dim, char buff[dim], FILE *fp)
 Read a line of input into buff. More...
 
const char * mybasename (const char *pathname)
 Strip leading pathname components; return a pointer to the filename. More...
 
int legalName (const char *name)
 Return nonzero if name is legal; zero otherwise. More...
 
int strnncopy (size_t n, char dst[n], size_t m, const char src[m])
 
int strchrcnt (const char *s, int c)
 
void collapse_whitespace (char *buff)
 
int LDbl_near (long double x, long double y)
 Return 1 if the relative difference between x and y is less than or equal to 8*DBL_EPSILON.
 
void stutter (char c, int n, FILE *fp)
 Print character c n times on output fp.
 

Detailed Description

Functions that didn't seem to belong anywhere else.

Author
Alan R. Rogers

Function Documentation

◆ compareDoubles()

int compareDoubles ( const void *  void_x,
const void *  void_y 
)

Compare two doubles.

Function interprets its arguments as pointers to doubles.

Parameters
void_x,void_yPointers to the two doubles, cast as pointers to voids.
Returns
-1, 0, or 1 depending on whether the first arg is <, ==, or > the second.

◆ compareInts()

int compareInts ( const void *  void_x,
const void *  void_y 
)

Compare two ints.

Function interprets its arguments as pointers to ints.

Parameters
void_x,void_yPointers to the two integers, cast as pointers to voids.
Returns
-1, 0, or 1 depending on whether the first arg is <, ==, or > the second.

◆ compareLongs()

int compareLongs ( const void *  void_x,
const void *  void_y 
)

Compare two long ints.

Function interprets its arguments as pointers to long ints.

Parameters
void_x,void_yPointers to the two integers, cast as pointers to voids.
Returns
-1, 0, or 1 depending on whether the first arg is <, ==, or > the second.

◆ Dbl_first_geq()

unsigned Dbl_first_geq ( double  val,
unsigned  len,
double  v[len] 
)

Vector v must be sorted in ascending order before this function is called.

Function returns index of first element in sorted array that is >= val. The function assumes without checking that the input is sorted. If val > vec[len-1], the function returns len.

◆ dostacktrace()

void dostacktrace ( const char *  file,
int  line,
FILE *  ofp 
)

Print call stack.

This function is not useful when compiler optimizations are turned on, because then the executable doesn't know the names of functions. The stack trace shows addresses instead. Call like this: "dostacktrace(__FILE__, __LINE__)"

◆ efopen()

FILE * efopen ( const char *restrict  name,
const char *restrict  mode 
)

Open file.

On success, return pointer to opened file. On failure abort with an error message. The error message includes the current working directory, whether or not "name" is an absolute pathname.

◆ eprintf()

void eprintf ( const char *  fmt,
  ... 
)

eprintf: print error message and exit.

This function is dangerous, because unlike ordinary printf, it doesn't detect mismatches in the type and number of arguments.

◆ getNumCores()

int getNumCores ( void  )

An almost platform-independent function that returns the number of CPU cores on the current machine.

Source: http://stackoverflow.com/questions/150355/ programmatically-find-the-number-of-cores-on-a-machine. If I understand the webpage correctly, this code was written by Dirk-Jan Kroon.

◆ legalName()

int legalName ( const char *  name)

Return nonzero if name is legal; zero otherwise.

Legal names begin with an alphabetic character and then continue with any number of letters, digits, underscores, colons, or periods".

◆ long_first_geq()

long long_first_geq ( long  val,
long *  v,
long  len 
)

Vector v must be sorted in ascending order before this function is called.

Function returns index of first element in sorted array that is >= val. The function assumes without checking that the input is sorted. If val > vec[len-1], the function returns len.

Referenced by Boot_multiplicity().

◆ long_last_leq()

long long_last_leq ( long  val,
long *  v,
long  len 
)

Vector v must be sorted in ascending order before this function is called.

Function returns index of last element in sorted array that is <= val. The function assumes without checking that the input is sorted. If val < vec[0], the function returns -1.

◆ mybasename()

const char * mybasename ( const char *  pathname)

Strip leading pathname components; return a pointer to the filename.

There is a standard library function for this, but it can't be used with const strings. This one can. Current code assumes that '/' is the path separator. If pathname ends with '/', this will return an empty string.

◆ nextWhitesepToken()

char * nextWhitesepToken ( char **  str)

Return tokens separated by 1 or more spaces and/or tabs.

On entry, str should be the address of a pointer into a character string. For example:

char buff[100];
char *token, *ptr = buff;

token = nextWhitesepToken(&ptr);

On return, token is NULL if no tokens are found. Otherwise, it points to the next token in the string, and *str is either NULL (if the end of the string has been reached) or points to the character immediately following the token.

Referenced by countSegments(), parseDerive(), parseMix(), parseParam(), and parseSegment().

◆ parseDbl()

double parseDbl ( char *  token)

Parse token as a double.

If strtod sets errno, then leave errno alone. If the token isn't a float or if there are extraneous characters at the end of the token, then set errno=EINVAL. If parseDbl sets errno, it returns 0.0.

◆ perturb_interval()

long double perturb_interval ( long double  x,
long double  lo,
long double  hi,
gsl_rng *  rng 
)

Uniform perturbation within interval.

New value in range (lo, hi).

◆ perturb_ratio()

double perturb_ratio ( double  x,
gsl_rng *  rng 
)

Uniform perturbation on log scale.

New value in range (0.1*x, 10*x).

References perturb_ratio_w().

◆ perturb_ratio_w()

double perturb_ratio_w ( double  x,
double  w,
gsl_rng *  rng 
)

Uniform perturbation on log scale.

Log10 of new value in range (log10(x)-w, log10(x)+w)

Referenced by perturb_ratio().

◆ readline()

int readline ( int  dim,
char  buff[dim],
FILE *  fp 
)

Read a line of input into buff.

Return EOF or BUFFER_OVERFLOW on failure; 0 on success.

Referenced by readuntil().

◆ removeZeroes()

int removeZeroes ( int  dim,
unsigned  x[dim] 
)

Remove zeroes from an array of unsigned ints by sliding positive entries to the left.

Return number of non-zero entries. This function doesn't re-allocate the array. It just moves zeroes to the end.

◆ strcenter()

char * strcenter ( const char *  text,
unsigned  width,
char *  buff,
size_t  buffsize 
)

Center string "text" in a field of width "width".

The centered string is written into the character string "buff", whose size is "buffsize".

◆ stripWhiteSpace()

char * stripWhiteSpace ( char *  buff)

Remove white space from beginning and end of a string.

Return pointer to stripped string, which lies within the memory buffer passed as input.

Referenced by parseDerive(), parseMix(), and parseSegment().

◆ tellopt()

void tellopt ( const char *  opt,
const char *  description 
)

Describe an option.

For use in "usage" functions.

Parameters
[in]optName of option.
[in]descriptionDescription of option.

◆ tokenize()

int tokenize ( int  dim,
char *  token[dim],
char *  s,
const char *  delim 
)

Separate string s into tokens, separated by any character in string delim.

Set ptr[i] equal to the address of the i'th token. Terminate each substring with '\0' within s. Return the number of tokens. Abort if the number of substrings exceeds n.

◆ unitTstResult()

void unitTstResult ( const char *  facility,
const char *  result 
)

Print result of a unit test.

Parameters
[in]facilitythe name of the facility being tested
[in]resultsomething like "OK" or "FAIL".