Antiprism 0.23

- Main Page
- Classes
- Files

- File List
- File Members

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:

  path | the 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:

  str | the 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:

  str | the string to copy

Returns:
  A pointer to the newly allocated string, or 0

string dots2underscores | ( | string | str | ) |

Convert dots to underscores.

Parameters:

  str | the 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:

  f | the floating point number.
  sig_dgts | the 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:

  buf | a buffer to return the string.
  f | the floating point number.
  sig_dgts | the 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:

  buf | a buffer to return the string.
  i | the integer.

Returns:
  The string.

string itostr | ( | int | i | ) | `[inline]`

Convert an integer to a string.

Parameters:

  i | the 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:

  fmt | the 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:

  fname | the name of the file to open.
  subdir | the data directory subdirectory to search in.
  alt_name | a name that is found in an alt_names.txt file before a file with the name is found.
  where | used to return the place that the file was found `0` - locally `1` - in the ANTIPRISM_DATA directory `2` - in the installation data directory
  fpath | used 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:

  str | the string holding the floating point number or expressions.
  f | used to return the floating point number.
  errmsg | an 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:

  str | the string holding the comma-separated floating point numbers.
  nums | used to return the floating point numbers.
  errmsg | an array at least `MSG_SZ` chars long to return any error message.
  len | the maximum total of numbers that should be in str, or if it is `0` then there is no maximum.
  sep | the 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:

  vals | the strings holding the floating point numbers.
  nums | used to return the floating point numbers.
  errmsg | an 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:

  str | the string holding the comma-separated floating point numbers.
  nums | used to return the floating point numbers.
  errmsg | an array at least `MSG_SZ` chars long to return any error message.
  len | the maximum total of numbers that should be in str, or if it is `0` then there is no maximum.
  sep | the 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:

  vals | the strings holding the floating point numbers.
  nums | used to return the floating point numbers.
  errmsg | an 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:

  str | the string holding the floating point number.
  f | used to return the floating point number.
  errmsg | an 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:

  str | the string holding the comma-separated integers.
  nums | used to return the integers.
  num_idxs | index numbers with this value or higher are out of range
  allow_extra | allow out-of-range index numbers, prefixed by an x or X, which are indexed relative to num_idxs, i.e. X0 = num_idxs.
  errmsg | an 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:

  str | the string holding the integer.
  i | used to return the integer.
  errmsg | an 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:

  vals | the strings holding the integers.
  nums | used to return the integers.
  errmsg | an array at least `MSG_SZ` chars long to return any error message.
  is_index | if 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:

  str | the string holding the comma-separated integers.
  nums | used to return the integers.
  errmsg | an array at least `MSG_SZ` chars long to return any error message.
  is_index | if true then the integers cannot be negative.
  len | the maximum total of numbers that should be in str, or if it is `0` then there is no maximum.
  sep | the 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:

  file | the file stream to read from.
  line | where 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:

  line | the line to split (this will be modified).
  parts | the parts of the split line.
  delims | the characters to use as delimiters, if `0` then use whitespace characters.
  strict | if 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:

  to | the string to convert (up to `MSG_SZ-1` characters used.)
  from | the 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:

  buf | a buffer to return the string.
  v | the vector.
  sep | the separator between the numbers.
  sig_dgts | the 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:

  v | the vector.
  sep | the separator between the numbers.
  sig_dgts | the 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:

  buf | a buffer to return the string.
  v | the vector.
  sep | the separator between the numbers.
  sig_dgts | the 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.