Angband
Macros | Functions | Variables
z-util.h File Reference

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

#include "h-basic.h"

Go to the source code of this file.

Macros

#define PLURAL(n)   ((n) == 1 ? "" : "s")
 

Available Functions

More...
 
#define VERB_AGREEMENT(count, singular, plural)   (((count) == 1) ? (singular) : (plural))
 Return the verb form matching the given count. More...
 
#define streq(s, t)   (!strcmp(s, t))
 

Functions

size_t utf8_strlen (char *s)
 Count the number of characters in a UTF-8 encoded string. More...
 
void utf8_clipto (char *s, size_t n)
 Clip a null-terminated UTF-8 string 's' to 'n' unicode characters. More...
 
int my_stricmp (const char *s1, const char *s2)
 Case insensitive comparison between two strings. More...
 
int my_strnicmp (const char *a, const char *b, int n)
 Case insensitive comparison between two strings, up to n characters long. More...
 
char * my_stristr (const char *string, const char *pattern)
 Case-insensitive strstr. More...
 
size_t my_strcpy (char *buf, const char *src, size_t bufsize)
 Copy up to 'bufsize'-1 characters from 'src' to 'buf' and NULL-terminate the result. More...
 
size_t my_strcat (char *buf, const char *src, size_t bufsize)
 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. More...
 
void my_strcap (char *buf)
 Capitalise string 'buf'. More...
 
bool streq (const char *s, const char *t)
 Test equality, prefix, suffix. More...
 
bool prefix (const char *s, const char *t)
 Determine if string "t" is a prefix of string "s". More...
 
bool prefix_i (const char *s, const char *t)
 Determine if string "t" is a prefix of string "s" - case insensitive. More...
 
bool suffix (const char *s, const char *t)
 Determine if string "t" is a suffix of string "s". More...
 
void strskip (char *s, const char c, const char e)
 Skip occurrences of a characters. More...
 
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. More...
 
bool contains_only_spaces (const char *s)
 Determines if a string is "empty". More...
 
bool is_a_vowel (int ch)
 Check if a char is a vowel. More...
 
size_t text_mbstowcs (wchar_t *dest, const char *src, int n)
 Allow override of the multi-byte to wide char conversion. More...
 
void plog (const char *str)
 Print an error message. More...
 
void quit (const char *str)
 Exit, with optional message. More...
 
void sort (void *array, size_t nmemb, size_t smemb, int(*comp)(const void *a, const void *b))
 Sorting functions. More...
 
u32b djb2_hash (const char *str)
 Create a hash for a string. More...
 
int mean (int *nums, int size)
 Mathematical functions. More...
 
int variance (int *nums, int size)
 Variance of the first 'size' entries of the array 'nums'. More...
 

Variables

char * argv0
 

Available variables

More...
 
size_t(* text_mbcs_hook )(wchar_t *dest, const char *src, int n)
 Aux functions. More...
 
void(* plog_aux )(const char *)
 Redefinable "plog" action. More...
 
void(* quit_aux )(const char *)
 Redefinable "quit" action. More...
 

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.

Macro Definition Documentation

#define PLURAL (   n)    ((n) == 1 ? "" : "s")


Available Functions

Return "s" (or not) depending on whether n is singular.

Referenced by inven_item_charges(), lore_append_drop(), lore_append_exp(), monster_list_format_section(), obj_desc_charges(), object_list_format_section(), and spell_learn().

#define streq (   s,
 
)    (!strcmp(s, t))

Referenced by add_brand(), append_random_brand(), append_random_slay(), apply_autoinscription(), blow_color(), brand_object(), build_greater_vault(), build_lesser_vault(), build_medium_vault(), cmd_get_arg(), cmd_set_arg(), code_index_in_array(), debug_opt(), effect_lookup(), effect_param(), enter_score(), find_loader(), findact(), findeff(), findmeth(), finish_parse_projection(), flag_index_by_name(), flavor_init(), grab_element_flag(), grab_index_and_int(), grab_int_value(), grab_name(), grab_rand_value(), helper_char1(), init_rune(), init_sound(), lookup_artifact_name(), lookup_curse(), lookup_ego_item(), lookup_feat(), lookup_flag(), lookup_monster_base(), lookup_pit_profile(), lookup_symbol(), lookup_trap(), lore_append_abilities(), main(), make_attack_normal(), mon_restrict(), mon_timed_name_to_idx(), money_kind(), monster_blow_method_action(), object_has_rune(), object_learn_brand(), object_power(), option_set(), parse_artifact_brand(), parse_artifact_curse(), parse_artifact_slay(), parse_constants_carry_cap(), parse_constants_dun_gen(), parse_constants_level_max(), parse_constants_mon_gen(), parse_constants_mon_play(), parse_constants_obj_make(), parse_constants_player(), parse_constants_store(), parse_constants_world(), parse_eff_resist(), parse_ego_brand(), parse_ego_curse(), parse_ego_slay(), parse_object_base_defaults(), parse_object_brand(), parse_object_curse(), parse_object_power_iterate(), parse_object_power_operation(), parse_object_property_id_type(), parse_object_property_subtype(), parse_object_property_type(), parse_object_slay(), parse_player_timed_fail(), parse_prefs_expr(), parse_prefs_feat(), parse_prefs_gf(), parse_prefs_trap(), parse_profile_name(), parse_profile_room(), parse_projection_code(), parse_room_height(), parse_room_width(), parse_vault_columns(), parse_vault_rows(), player_learn_rune(), player_pickup_gold(), process_pref_file_expr(), random_vault(), rd_monster_memory(), rd_player(), react_to_specific_slay(), same_monsters_slain(), savefile_get_description(), show_file(), slot_by_name(), summon_name_to_idx(), test_base0(), test_d0(), test_desc0(), test_flavor0(), test_mimic0(), test_msg0(), test_name0(), test_owner0(), test_phrase0(), test_store0(), test_title0(), and test_typ0().

#define VERB_AGREEMENT (   count,
  singular,
  plural 
)    (((count) == 1) ? (singular) : (plural))

Function Documentation

bool contains_only_spaces ( const char *  s)

Determines if a string is "empty".

Determines if a string is "empty".

References NULL.

Referenced by show_file().

u32b djb2_hash ( const char *  str)

Create a hash for a string.

Referenced by message_sound_define().

bool is_a_vowel ( int  ch)
int mean ( int nums,
int  size 
)

Mathematical functions.

Mathematical functions.

References i, and size.

Referenced by store_base_power(), and variance().

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

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.

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.

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 curses_conflict(), death_file(), describe_effect(), describe_origin(), display_object(), display_scores_aux(), do_cmd_change_name(), do_cmd_pref_file_hack(), dump_history(), ego_item_name(), get_mon_name(), get_pref_path(), get_subject(), grab_index_and_int(), history_display(), ignore_drop(), improve_attack_modifier(), init_names(), init_stuff(), int_to_roman(), item_menu(), look_mon_desc(), lore_append_exp(), menu_header(), monster_desc(), monster_list_format_section(), object_list_format_name(), player_pickup_gold(), plural_aux(), process_character_pref_files(), rune_add_autoinscription(), set_file_paths(), and write_flags().

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

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

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

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.

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_obj_list(), build_score(), cmd_get_string(), cmd_set_arg(), curse_menu_browser(), 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_version(), do_cmd_wiz_effect(), effect_handler_DAMAGE(), ego_item_name(), file_archive(), flavor_init(), get_chamber_monsters(), get_character_name(), get_file_backup(), get_file_text(), get_history_command(), get_name_command(), get_subject(), get_vault_monsters(), 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_file_paths(), init_stuff(), load_roller_data(), load_sound(), look_mon_desc(), lore_append_abilities(), main(), make_ranged_shot(), make_ranged_throw(), mon_restrict(), monster_desc(), my_dread(), my_strcat(), next_blockheader(), object_kind_name(), parse_random(), parser_parse(), parser_setstate(), path_parse(), player_embody(), player_pickup_gold(), player_safe_name(), process_character_pref_files(), prt_depth(), prt_level_feeling(), prt_state(), py_attack_real(), ranged_helper(), rd_player(), rune_add_autoinscription(), save_game(), save_roller_data(), savefile_get_description(), savefile_set_name(), set_file_paths(), show_file(), show_obj(), string_append(), string_make(), take_hit(), text_out_to_file(), try_save(), user_name(), vstrnfmt(), wipe_obj_list(), wiz_cheat_death(), wiz_create_item_display(), 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 
)

Case-insensitive strstr.

Case-insensitive strstr.

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

References NULL, and 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 two strings, up to n characters long.

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 load_sample_sdl(), plog_fmt(), quit(), and textui_init().

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

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

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.

void quit ( const char *  str)

Exit, with optional message.

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_debug_command(), get_history_command(), handle_signal_abort(), handle_signal_simple(), init_race_allocs(), main(), mem_alloc(), mem_realloc(), menu_question(), parse_file_quit_not_found(), point_based_command(), quit_fmt(), roller_command(), run_template_parser(), safe_setuid_drop(), safe_setuid_grab(), sf_get(), start_game(), store_init(), textui_birth_quickstart(), and textui_init().

void sort ( void array,
size_t  nmemb,
size_t  smemb,
int(*)(const void *a, const void *b)  comp 
)

Sorting functions.

bool streq ( const char *  a,
const char *  b 
)

Test equality, prefix, suffix.

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.

Referenced by show_file().

void strskip ( char *  s,
const char  c,
const char  e 
)

Skip occurrences of a characters.

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".

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

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

Allow override of the multi-byte to wide char conversion.

References point_set::n, and text_mbcs_hook.

Referenced by char_matches_key(), parser_parse(), Term_addstr(), text_out_to_screen(), and textblock_vappend_c().

void utf8_clipto ( char *  s,
size_t  n 
)

Clip a null-terminated UTF-8 string 's' to 'n' unicode characters.

e.g. utf8_clipto("example", 4) will clip after 'm', resulting in 'exam'.

References i.

Referenced by monster_list_format_section(), object_list_format_section(), and test_alloc().

size_t utf8_strlen ( char *  s)

Count the number of characters in a UTF-8 encoded string.

Taken from http://canonical.org/~kragen/strlen-utf8.html

References i.

Referenced by monster_list_format_section(), and object_list_format_section().

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


Available variables

The name of the program.


Available variables

Referenced by main(), and plog().

void(* plog_aux)(const char *)

Redefinable "plog" action.

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

void(* quit_aux)(const char *)

Redefinable "quit" action.

Referenced by main(), and quit().

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

Aux functions.

Aux functions.

Referenced by text_mbstowcs().