FoofyFox Sandstorm 2
General-purpose Discord bot in C++
ffss::SSUtility Class Reference

Static Public Member Functions

static std::vector< std::string > get_args (const std::string &raw_args)
 Parse arguments. More...
 
static std::vector< std::string::size_type > calc_mindiff (std::vector< std::string > &v)
 Calculate the minimum distinguishable length for each string in v. More...
 
static std::string load_file (const std::string &path)
 Load a file into a string with path path. More...
 
static std::vector< std::string > load_files (const std::string &path_root)
 Load a files from directory path_root. More...
 
static std::vector< std::string > load_file_lines (const std::string &path)
 Load lines of a file into a vector of strings from path path. More...
 
static std::string load_pcm (const std::string &mp3_data)
 Converts a MP3 file into PCM data. More...
 

Static Public Attributes

static size_t read_buf_size = DEFAULT_READ_BUFFER_SIZE
 Buffer size for read functions, specifically load_file()
 
static std::string help_file = DEFAULT_HELP_FILENAME
 Path of the help file for the 'help' command.
 
static std::string fortune_file = DEFAULT_FORTUNE_FILENAME
 Path for the different messages read by the 'fortuneteller' command.
 
static size_t laugh_max = DEFAULT_LAUGH_MAX
 Maximum length of 'HA's for 'A[HA]+' in the 'laugh' command.
 
static std::string default_img = DEFAULT_IMG_FILENAME
 Image to display if the user only enters the cmd_switch and no other characters.
 
static std::string fenn_dir = DEFAULT_FENN_PATH
 Directory to look into for fennec fox images for the 'fennec' command.
 
static size_t audio_queue_max = DEFAULT_AUDIO_QUEUE_MAX
 Maximum size of the audio queue for the 'vplay' command.
 
static size_t args_max = DEFAULT_ARGS_MAX
 Maximum number of arguments that can be processed from get_args.
 
static char cmd_switch = DEFAULT_SWITCH
 The command switch, say for '-vplay', the switch is '-'.
 

Member Function Documentation

◆ calc_mindiff()

std::vector< std::string::size_type > ffss::SSUtility::calc_mindiff ( std::vector< std::string > &  v)
static

Calculate the minimum distinguishable length for each string in v.

Parameters
vA vector of strings.

The minimum distinguishable length for a string s in some non-empty set of strings is the length that s can be abbreviated down to without being ambiguous. For example, for strings "fennec" and "fortuneteller", the length is 2 for each, since we need at least two characters to distinguish both strings.

Documented preconditions are as follows:

  • All strings in v must be unique.
  • There cannot be an empty string in v.
Returns
A vector of minimum distinguishable lengths for each corresponding string (same index).
Exceptions
std::logic_errorif a string is empty, or not unique.

◆ get_args()

std::vector< std::string > ffss::SSUtility::get_args ( const std::string &  raw_args)
static

Parse arguments.

Parameters
raw_argsRaw arguments DEFAULT_ARGS_MAX

Parses arguments, delimited by a space. If an argument is within single '\'' or double '"' quotations, spaces are ignored.

Documented preconditions are as follows:

  • All quotes in raw_args must be closed, hence strings like hello "hello are not accepted.
Returns
A vector which is of size 0 if there are no arguments.
Exceptions
std::runtime_errorif a quote is not terminated.

◆ load_file()

std::string ffss::SSUtility::load_file ( const std::string &  path)
static

Load a file into a string with path path.

Parameters
patha reference to a valid file path.

Documented preconditions are as follows:

  • path is a valid path string on the system.
  • path must be a regular file on the system.
  • path must be accessible by the user that the program executes on.
    Returns
    A string containing the contents of the file at path.
    Exceptions
    std::runtime_errorif the file in path is not found.

◆ load_file_lines()

std::vector< std::string > ffss::SSUtility::load_file_lines ( const std::string &  path)
static

Load lines of a file into a vector of strings from path path.

Parameters
patha reference to a valid file path.

Documented preconditions are as follows:

  • path is a valid path string on the system.
  • path must be a regular file on the system.
  • path must be accessible by the user that the program executes on.
    Returns
    A vector of strings string containing the contents of the file at path.
    Exceptions
    std::runtime_errorif the file in path is not found.

◆ load_files()

std::vector< std::string > ffss::SSUtility::load_files ( const std::string &  path_root)
static

Load a files from directory path_root.

Parameters
path_roota reference to a valid directory.

This function loads all regular files from path_root but does not recurse into further directories. Hence the function only loads regular files at level 1 relative to path_root. This function then uses load_file() to read the contents of each file in path_root into a vector of strings. Documented preconditions are as follows:

  • path_root is a valid path string on the system.
  • path_root must be a directory on the system.
  • path_root must be accessible by the user that the program executes on.
    Returns
    A vector of minimum distinguishable lengths for each corresponding string (same index).
    Exceptions
    std::runtime_errorif there are any errors processing directories.
    See also
    load_file()

◆ load_pcm()

std::string ffss::SSUtility::load_pcm ( const std::string &  mp3_data)
static

Converts a MP3 file into PCM data.

Parameters
mp3_databinary data of the MP3 file.

Documented preconditions are as follows:

  • mp3_file is a valid MP3 file with a valid audio bitrate (48kHz only) and valid audio encoding (16-bit signed little-endian only).
    Returns
    A string containing the contents of the PCM data.
    Exceptions
    std::runtime_errorif any errors parsing the MP3 data are encountered.

The documentation for this class was generated from the following files: