Angband
Functions | Variables
z-util.c File Reference

Low-level string handling and other utilities. More...

#include <stdlib.h>
#include "z-util.h"

Functions

int my_stricmp (const char *s1, const char *s2)
 Case insensitive comparison between two strings.
int my_strnicmp (const char *a, const char *b, int n)
 Case insensitive comparison between the first n characters of two strings.
char * my_stristr (const char *string, const char *pattern)
 An ANSI version of strstr() with case insensitivity.
size_t my_strcpy (char *buf, const char *src, size_t bufsize)
 The my_strcpy() function copies up to 'bufsize'-1 characters from 'src' to 'buf' and NUL-terminates the result.
size_t my_strcat (char *buf, const char *src, size_t bufsize)
 The my_strcat() tries to append a string to an existing NUL-terminated string.
void my_strcap (char *buf)
 Capitalise the first letter of string 'str'.
bool streq (const char *a, const char *b)
 Determine if string "a" is equal to string "b".
bool suffix (const char *s, const char *t)
 Determine if string "t" is a suffix of string "s".
bool prefix (const char *s, const char *t)
 Determine if string "t" is a prefix of string "s".
bool prefix_i (const char *s, const char *t)
 Determine if string "t" is a prefix of string "s" - case insensitive.
void strskip (char *s, const char c)
 Rewrite string s in-place "skipping" every occurrence of character c.
void strescape (char *s, const char c)
 Rewrite string s in-place removing escape character c note that pairs of c will leave one instance of c in out.
bool contains_only_spaces (const char *s)
 returns TRUE if string only contains spaces
bool is_a_vowel (int ch)
 Check a char for "vowel-hood".
size_t text_mbstowcs (wchar_t *dest, const char *src, int n)
 Allow override of the multi-byte to wide char conversion.
void plog (const char *str)
 Print an error message.
void quit (const char *str)
 Exit (ala "exit()").
int mean (int *nums, int size)
 Arithmetic mean of the first 'size' entries of the array 'nums'.
int variance (int *nums, int size)
 Variance of the first 'size' entries of the array 'nums'.
void sort (void *base, size_t nmemb, size_t smemb, int(*comp)(const void *, const void *))

Variables

char * argv0 = NULL
 Convenient storage of the program name.
size_t(* text_mbcs_hook )(wchar_t *dest, const char *src, int n) = NULL
 Hook for platform-specific wide character handling.
void(* plog_aux )(const char *) = NULL
 Redefinable "plog" action.
void(* quit_aux )(const char *) = NULL
 Redefinable "quit" action.

Detailed Description

Low-level string handling and other utilities.

Copyright (c) 1997-2005 Ben Harrison, Robert Ruehlmann.

This work is free software; you can redistribute it and/or modify it under the terms of either:

a) the GNU General Public License as published by the Free Software Foundation, version 2, or

b) the "Angband licence": This software may be copied and distributed for educational, research, and not for profit purposes provided that this copyright and statement are included in all such copies. Other copyrights may also apply.

Function Documentation

bool contains_only_spaces ( const char *  s)

returns TRUE if string only contains spaces

Determines if a string is "empty".

References FALSE, and TRUE.

Referenced by show_file().

bool is_a_vowel ( int  ch)

Check a char for "vowel-hood".

Check if a char is a vowel.

References FALSE, and TRUE.

Referenced by context_menu_cave(), describe_origin(), monster_desc(), obj_desc_name_prefix(), randname_make(), and target_set_interactive_aux().

int mean ( int nums,
int  size 
)

Arithmetic mean of the first 'size' entries of the array 'nums'.

Mathematical functions.

References i, and size.

Referenced by store_base_power(), and variance().

void my_strcap ( char *  buf)

Capitalise the first letter of string 'str'.

Capitalise string 'buf'.

Referenced by artifact_gen_name(), do_ident_item(), get_name_keypress(), mon_take_hit(), monster_desc(), show_equip(), spell_menu_select(), textui_get_spell(), and user_name().

size_t my_strcat ( char *  buf,
const char *  src,
size_t  bufsize 
)

The my_strcat() tries to append a string to an existing NUL-terminated string.

Try to append a string to an existing NULL-terminated string, never writing more characters into the buffer than indicated by 'bufsize', and NULL-terminating the buffer.

It never writes more characters into the buffer than indicated by 'bufsize' and NUL-terminates the buffer. The 'buf' and 'src' strings may not overlap.

my_strcat() returns strlen(buf) + strlen(src). This makes checking for truncation easy. Example: if (my_strcat(buf, src, sizeof(buf)) >= sizeof(buf)) ...;

This function should be equivalent to the strlcat() function in BSD.

References my_strcpy().

Referenced by describe_origin(), display_object(), display_scores_aux(), dump_history(), ego_item_name(), flush_monster_messages(), get_mon_name(), grab_index_and_int(), history_display(), ignore_drop(), init_names(), init_stuff(), int_to_roman(), look_mon_desc(), lore_append_exp(), monster_desc(), monster_list_format_section(), object_list_format_name(), player_pickup_gold(), plural_aux(), set_file_paths(), show_equip(), textui_get_item(), and write_flags().

size_t my_strcpy ( char *  buf,
const char *  src,
size_t  bufsize 
)

The my_strcpy() function copies up to 'bufsize'-1 characters from 'src' to 'buf' and NUL-terminates the result.

Copy up to 'bufsize'-1 characters from 'src' to 'buf' and NULL-terminate the result.

The 'buf' and 'src' strings may not overlap.

my_strcpy() returns strlen(src). This makes checking for truncation easy. Example: if (my_strcpy(buf, src, sizeof(buf)) >= sizeof(buf)) ...;

This function should be equivalent to the strlcpy() function in BSD.

References void().

Referenced by build_score(), cmd_get_string(), cmd_set_arg(), de_armour(), death_knowledge(), describe_origin(), dir_create(), display_message(), do_cmd_change_name(), do_cmd_choose_name(), do_cmd_note(), do_cmd_pref(), do_cmd_query_symbol(), do_cmd_suicide(), do_cmd_wiz_effect(), ego_item_name(), flavor_init(), flush_monster_messages(), get_chamber_monsters(), get_character_name(), get_file_backup(), get_file_text(), get_history_command(), get_mon_name(), grab_base_and_int(), grab_index_and_int(), grab_int_value(), grab_rand_value(), handle_signal_abort(), handle_signal_simple(), history_add_full(), ignore_drop(), improve_attack_modifier(), init_stuff(), look_mon_desc(), main(), make_ranged_shot(), make_ranged_throw(), monster_desc(), my_dread(), my_strcat(), next_blockheader(), object_kind_name(), parse_random(), parser_parse(), parser_setstate(), path_parse(), player_outfit(), player_pickup_gold(), player_safe_name(), process_character_pref_files(), prt_depth(), prt_state(), py_attack_real(), ranged_helper(), rd_player(), save_game(), savefile_get_description(), set_file_paths(), show_equip(), show_file(), show_inven(), show_obj_list(), show_quiver(), string_append(), string_make(), take_hit(), text_out_to_file(), try_save(), user_name(), vstrnfmt(), wiz_cheat_death(), write_dummy_object_record(), and write_flags().

int my_stricmp ( const char *  s1,
const char *  s2 
)
char* my_stristr ( const char *  string,
const char *  pattern 
)

An ANSI version of strstr() with case insensitivity.

Case-insensitive strstr.

In the public domain; found at: http://c.snippets.org/code/stristr.c

References start.

Referenced by do_cmd_messages(), lookup_artifact_name(), lookup_monster(), and lookup_trap().

int my_strnicmp ( const char *  a,
const char *  b,
int  n 
)

Case insensitive comparison between the first n characters of two strings.

Case insensitive comparison between two strings, up to n characters long.

References s1, s2, z1, and z2.

void plog ( const char *  str)

Print an error message.

References argv0, and plog_aux.

Referenced by plog_fmt(), quit(), and textui_init().

bool prefix ( const char *  s,
const char *  t 
)

Determine if string "t" is a prefix of string "s".

References FALSE, and TRUE.

Referenced by ego_item_name(), grab_element_flag(), init_names(), object_desc(), path_build(), show_file(), and strip_ego_name().

bool prefix_i ( const char *  s,
const char *  t 
)

Determine if string "t" is a prefix of string "s" - case insensitive.

References FALSE, and TRUE.

void quit ( const char *  str)

Exit (ala "exit()").

Exit, with optional message.

If 'str' is NULL, do "exit(EXIT_SUCCESS)". Otherwise, plog() 'str' and exit with an error code of -1. But always use 'quit_aux', if set, before anything else.

References plog(), and quit_aux.

Referenced by choose_profile(), get_confirm_command(), get_history_command(), handle_signal_abort(), handle_signal_simple(), init_race_allocs(), main(), mem_alloc(), mem_realloc(), menu_question(), parse_file(), point_based_command(), quit_fmt(), roller_command(), run_template_parser(), safe_setuid_drop(), safe_setuid_grab(), start_game(), store_init(), textui_birth_quickstart(), and textui_init().

void sort ( void base,
size_t  nmemb,
size_t  smemb,
int(*)(const void *, const void *)  comp 
)
bool streq ( const char *  a,
const char *  b 
)

Determine if string "a" is equal to string "b".

Test equality, prefix, suffix.

void strescape ( char *  s,
const char  c 
)

Rewrite string s in-place removing escape character c note that pairs of c will leave one instance of c in out.

References FALSE, and TRUE.

Referenced by show_file().

void strskip ( char *  s,
const char  c 
)

Rewrite string s in-place "skipping" every occurrence of character c.

Skip occurrences of a characters.

Referenced by show_file().

bool suffix ( const char *  s,
const char *  t 
)

Determine if string "t" is a suffix of string "s".

References FALSE.

Referenced by grab_element_flag(), init_stuff(), player_safe_name(), and set_file_paths().

size_t text_mbstowcs ( wchar_t *  dest,
const char *  src,
int  n 
)
int variance ( int nums,
int  size 
)

Variance of the first 'size' entries of the array 'nums'.

References i, mean(), and size.

Referenced by store_base_power().

Variable Documentation

char* argv0 = NULL

Convenient storage of the program name.


Available variables

Referenced by main(), and plog().

void(* plog_aux)(const char *) = NULL

Redefinable "plog" action.

Referenced by main(), plog(), and setup_tests().

void(* quit_aux)(const char *) = NULL

Redefinable "quit" action.

Referenced by main(), and quit().

size_t(* text_mbcs_hook)(wchar_t *dest, const char *src, int n) = NULL

Hook for platform-specific wide character handling.

Referenced by text_mbstowcs().