Antiprism 0.23
Classes | Functions

base/utils.h File Reference

utility routines for maths operations. More...

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include "vec3d.h"
#include "vec4d.h"
#include "mat3d.h"
#include "geom.h"
#include "utils_ultragetopt.h"
Include dependency graph for utils.h:
This graph shows which files directly or indirectly include this file:

Classes

class  prog_opts
 Command line processing. More...

Functions

const char * basename2 (const char *path)
 Get the Basename.
string dots2underscores (string str)
 Convert dots to underscores.
bool read_double (const char *str, double *f, char *errmsg=0)
 Read a floating point number, which may be a mathematical expression, from a string.
bool read_double_noparse (const char *str, double *f, char *errmsg=0)
 Read a floating point number from a string.
bool read_int (const char *str, int *i, char *errmsg=0)
 Read an integer from a string.
bool read_double_list (vector< char * > &vals, vector< double > &nums, char *errmsg=0)
 Read floating point numbers, which may be maththematical expressions, from a list of strings.
bool read_double_list_noparse (vector< char * > &vals, vector< double > &nums, char *errmsg=0)
 Read floating point numbers from a list of strings.
bool read_double_list (char *str, vector< double > &nums, char *errmsg=0, int len=0, const char *sep=",")
 Read floating point numbers, which may be mathematical expressions, listed in a single string.
bool read_double_list_noparse (char *str, vector< double > &nums, char *errmsg=0, int len=0, const char *sep=",")
 Read floating point numbers listed in a single string.
bool read_int_list (vector< char * > &vals, vector< int > &nums, char *errmsg=0, bool is_index=false)
 Read integers from a list of strings.
bool read_int_list (char *str, vector< int > &nums, char *errmsg=0, bool is_index=false, int len=0, const char *sep=",")
 Read integers listed in a single string.
bool read_idx_list (char *str, vector< int > &nums, int num_idxs, bool allow_extra=false, char *errmsg=0)
 Read index numbers listed in a single string.
int read_line (FILE *file, char **line)
 Read a line of arbitrary length.
int split_line (char *line, vector< char * > &parts, const char *delims=0, bool strict=false)
 Split a line into delimited parts.
char * clear_extra_whitespace (char *str)
 Remove leading and trailing space, convert any whitespace to a single space.
char * to_resource_name (char *to, const char *from)
 Convert to a normalised resource name.
FILE * open_sup_file (const char *fname, const char *subdir, string *alt_name=0, int *where=0, string *fpath=0)
 Open a support file.
string msg_str (const char *fmt,...)
char * copy_str (const char *str)
string itostr (int i)
 Convert an integer to a string.
char * itostr (char *buf, int i)
 Convert an integer to a string.
string dtostr (double f, int sig_dgts=17)
 Convert a floating point number to a string.
char * dtostr (char *buf, double f, int sig_dgts=17)
 Convert a floating point number to a string.
string vtostr (vec3d v, const char *sep=", ", int sig_dgts=17)
 Convert a coordinate vector to a string.
char * vtostr (char *buf, vec3d v, const char *sep=", ", int sig_dgts=17)
 Convert a coordinate vector to a string.
char * vtostr (char *buf, vec4d v, const char *sep=", ", int sig_dgts=17)
 Convert a coordinate vector to a string.

Detailed Description

utility routines for maths operations.


Function Documentation

const char* basename2 ( const char *  path)

Get the Basename.

Get the name of the file from a path. This is taken as the last part of the path after any '%\' or '/', otherwise the path is the file name.

Parameters:
paththe path to the file.
Returns:
A pointer to the start of the file name.
char* clear_extra_whitespace ( char *  str)

Remove leading and trailing space, convert any whitespace to a single space.

Parameters:
strthe string to convert.
Returns:
A pointer to the string.
char* copy_str ( const char *  str)

The copy is dynamically allocated and must be freed with free()

Parameters:
strthe string to copy
Returns:
A pointer to the newly allocated string, or 0
string dots2underscores ( string  str)

Convert dots to underscores.

Parameters:
strthe string to convert.
Returns:
The converted string.
string dtostr ( double  f,
int  sig_dgts = 17 
) [inline]

Convert a floating point number to a string.

Parameters:
fthe floating point number.
sig_dgtsthe number of significant digits in the conversion, or if negative then the number of digits after the decimal point.
Returns:
The string.
char * dtostr ( char *  buf,
double  f,
int  sig_dgts = 17 
) [inline]

Convert a floating point number to a string.

Parameters:
bufa buffer to return the string.
fthe floating point number.
sig_dgtsthe number of significant digits in the conversion, or if negative then the number of digits after the decimal point.
Returns:
A pointer to buf, which holds the string.
char * itostr ( char *  buf,
int  i 
) [inline]

Convert an integer to a string.

Parameters:
bufa buffer to return the string.
ithe integer.
Returns:
The string.
string itostr ( int  i) [inline]

Convert an integer to a string.

Parameters:
ithe integer.
Returns:
A pointer to buf, which holds the string.
string msg_str ( const char *  fmt,
  ... 
)

Converts the first MSG_SZ-1 characters of the C format string

Parameters:
fmtthe formatted string
...the values for the format
Returns:
The converted string.
FILE* open_sup_file ( const char *  fname,
const char *  subdir,
string *  alt_name = 0,
int *  where = 0,
string *  fpath = 0 
)

Open a support file.

Tries to open a file by its name, then tries to open it in $ANTIPRISM_DATA/sub_dir, finally tries to open it in sub_dir in the installation data directory.

Parameters:
fnamethe name of the file to open.
subdirthe data directory subdirectory to search in.
alt_namea name that is found in an alt_names.txt file before a file with the name is found.
whereused to return the place that the file was found

  • 0 - locally
  • 1 - in the ANTIPRISM_DATA directory
  • 2 - in the installation data directory
fpathused to return the full path to the file that was found.
Returns:
A pointer to the opened file stream.
bool read_double ( const char *  str,
double *  f,
char *  errmsg = 0 
)

Read a floating point number, which may be a mathematical expression, from a string.

The string should only hold the floating point number or expression, but may have leading and trailing whitespace.

Parameters:
strthe string holding the floating point number or expressions.
fused to return the floating point number.
errmsgan array at least MSG_SZ chars long to return any error message.
Returns:
true if a valid floating point number was read, otherwise false and the error is detailed in errmsg.
bool read_double_list ( char *  str,
vector< double > &  nums,
char *  errmsg = 0,
int  len = 0,
const char *  sep = "," 
)

Read floating point numbers, which may be mathematical expressions, listed in a single string.

The numbers in the string should be comma separated, and may have leading and trailing whitespace.

Parameters:
strthe string holding the comma-separated floating point numbers.
numsused to return the floating point numbers.
errmsgan array at least MSG_SZ chars long to return any error message.
lenthe maximum total of numbers that should be in str, or if it is 0 then there is no maximum.
septhe characters that can separate the numbers.
Returns:
true if only valid floating point numbers were read, and no more than len (if len>0), otherwise false. and the error is detailed in errmsg.
bool read_double_list ( vector< char * > &  vals,
vector< double > &  nums,
char *  errmsg = 0 
)

Read floating point numbers, which may be maththematical expressions, from a list of strings.

The strings should only hold the floating point number, but may have leading and trailing whitespace.

Parameters:
valsthe strings holding the floating point numbers.
numsused to return the floating point numbers.
errmsgan array at least MSG_SZ chars long to return any error message.
Returns:
true if only valid floating point numbers were read, otherwise false and the error is detailed in errmsg.
bool read_double_list_noparse ( char *  str,
vector< double > &  nums,
char *  errmsg = 0,
int  len = 0,
const char *  sep = "," 
)

Read floating point numbers listed in a single string.

The numbers in the string should be comma separated, and may have leading and trailing whitespace.

Parameters:
strthe string holding the comma-separated floating point numbers.
numsused to return the floating point numbers.
errmsgan array at least MSG_SZ chars long to return any error message.
lenthe maximum total of numbers that should be in str, or if it is 0 then there is no maximum.
septhe characters that can separate the numbers.
Returns:
true if only valid floating point numbers were read, and no more than len (if len>0), otherwise false. and the error is detailed in errmsg.
bool read_double_list_noparse ( vector< char * > &  vals,
vector< double > &  nums,
char *  errmsg = 0 
)

Read floating point numbers from a list of strings.

The strings should only hold the floating point number, but may have leading and trailing whitespace.

Parameters:
valsthe strings holding the floating point numbers.
numsused to return the floating point numbers.
errmsgan array at least MSG_SZ chars long to return any error message.
Returns:
true if only valid floating point numbers were read, otherwise false and the error is detailed in errmsg.
bool read_double_noparse ( const char *  str,
double *  f,
char *  errmsg = 0 
)

Read a floating point number from a string.

The string should only hold the floating point number, but may have leading and trailing whitespace.

Parameters:
strthe string holding the floating point number.
fused to return the floating point number.
errmsgan array at least MSG_SZ chars long to return any error message.
Returns:
true if a valid floating point number was read, otherwise false and the error is detailed in errmsg.
bool read_idx_list ( char *  str,
vector< int > &  nums,
int  num_idxs,
bool  allow_extra = false,
char *  errmsg = 0 
)

Read index numbers listed in a single string.

The string consists of comma separated index number ranges, and may have leading and trailing whitespace. A number range is either a single number, or a sequential list indicated by two numbers seperated by '-'. If the numbers are not given they default to the first and last index number respectively.

Parameters:
strthe string holding the comma-separated integers.
numsused to return the integers.
num_idxsindex numbers with this value or higher are out of range
allow_extraallow out-of-range index numbers, prefixed by an x or X, which are indexed relative to num_idxs, i.e. X0 = num_idxs.
errmsgan array at least MSG_SZ chars long to return any error message.
Returns:
true if only valid index numbers were read, otherwise false. and the error is detailed in errmsg.
bool read_int ( const char *  str,
int *  i,
char *  errmsg = 0 
)

Read an integer from a string.

The string should only hold the integer, but may have leading and trailing whitespace.

Parameters:
strthe string holding the integer.
iused to return the integer.
errmsgan array at least MSG_SZ chars long to return any error message.
Returns:
true if a valid integer was read, otherwise false and the error is detailed in errmsg.
bool read_int_list ( vector< char * > &  vals,
vector< int > &  nums,
char *  errmsg = 0,
bool  is_index = false 
)

Read integers from a list of strings.

The strings should only hold the integers, but may have leading and trailing whitespace.

Parameters:
valsthe strings holding the integers.
numsused to return the integers.
errmsgan array at least MSG_SZ chars long to return any error message.
is_indexif true then the integers cannot be negative.
Returns:
true if only valid integers were read, otherwise false and the error is detailed in errmsg.
bool read_int_list ( char *  str,
vector< int > &  nums,
char *  errmsg = 0,
bool  is_index = false,
int  len = 0,
const char *  sep = "," 
)

Read integers listed in a single string.

The numbers in the string should be comma separated, and may have leading and trailing whitespace.

Parameters:
strthe string holding the comma-separated integers.
numsused to return the integers.
errmsgan array at least MSG_SZ chars long to return any error message.
is_indexif true then the integers cannot be negative.
lenthe maximum total of numbers that should be in str, or if it is 0 then there is no maximum.
septhe characters that can separate the numbers.
Returns:
true if only valid integers were read, and no more than len (if len>0), otherwise false. and the error is detailed in errmsg.
int read_line ( FILE *  file,
char **  line 
)

Read a line of arbitrary length.

The caller is responsible for freeing the memory allocated to line after each read.

Parameters:
filethe file stream to read from.
linewhere the line is returned
Returns:
  • 0 if the line was read correctly.
  • -1 if memory for line could not be allocated.
  • 1 if an final unterminated empty line was read.
int split_line ( char *  line,
vector< char * > &  parts,
const char *  delims = 0,
bool  strict = false 
)

Split a line into delimited parts.

Parameters:
linethe line to split (this will be modified).
partsthe parts of the split line.
delimsthe characters to use as delimiters, if 0 then use whitespace characters.
strictif true then treat every delimiter as a separator, returning null strings between adjacent delimiters, always returning at least one part.
Returns:
The number of parts.
char* to_resource_name ( char *  to,
const char *  from 
)

Convert to a normalised resource name.

Remove leading and trailing space, convert any whitespace to a single space, make lowercase

Parameters:
tothe string to convert (up to MSG_SZ-1 characters used.)
fromthe string to convert (length MSG_SZ.)
Returns:
A pointer to the to string.
char * vtostr ( char *  buf,
vec4d  v,
const char *  sep = ", ",
int  sig_dgts = 17 
) [inline]

Convert a coordinate vector to a string.

Parameters:
bufa buffer to return the string.
vthe vector.
septhe separator between the numbers.
sig_dgtsthe number of significant digits in the conversion, or if negative then the number of digits after the decimal point.
Returns:
A pointer to buf, which holds the string.
string vtostr ( vec3d  v,
const char *  sep = ", ",
int  sig_dgts = 17 
) [inline]

Convert a coordinate vector to a string.

Parameters:
vthe vector.
septhe separator between the numbers.
sig_dgtsthe number of significant digits in the conversion, or if negative then the number of digits after the decimal point.
Returns:
The string.
char * vtostr ( char *  buf,
vec3d  v,
const char *  sep = ", ",
int  sig_dgts = 17 
) [inline]

Convert a coordinate vector to a string.

Parameters:
bufa buffer to return the string.
vthe vector.
septhe separator between the numbers.
sig_dgtsthe number of significant digits in the conversion, or if negative then the number of digits after the decimal point.
Returns:
A pointer to buf, which holds the string.