Angband
Functions | Variables
z-util.c File Reference

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

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

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 the first n characters of two strings. More...
 
char * my_stristr (const char *string, const char *pattern)
 An ANSI version of strstr() with case insensitivity. More...
 
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. More...
 
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. More...
 
void my_strcap (char *buf)
 Capitalise the first letter of string 'str'. More...
 
bool streq (const char *a, const char *b)
 Determine if string "a" is equal to string "b". More...
 
bool suffix (const char *s, const char *t)
 Determine if string "t" is a suffix of string "s". 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...
 
void strskip (char *s, const char c, const char e)
 Rewrite string s in-place "skipping" every occurrence of character c except those preceded by character e. 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)
 returns true if string only contains spaces More...
 
bool is_a_vowel (int ch)
 Check a char for "vowel-hood". 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 (ala "exit()"). More...
 
int mean (int *nums, int size)
 Arithmetic mean of the first 'size' entries of the array 'nums'. More...
 
int variance (int *nums, int size)
 Variance of the first 'size' entries of the array 'nums'. More...
 
void sort (void *base, size_t nmemb, size_t smemb, int(*comp)(const void *, const void *))
 
u32b djb2_hash (const char *str)
 Create a hash for a string. More...
 

Variables

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

Function Documentation

bool contains_only_spaces ( const char *  s)

returns true if string only contains spaces

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)

Check a char for "vowel-hood".

Check if a char is a vowel.

Referenced by context_menu_cave(), describe_origin(), effect_handler_DAMAGE(), 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 Rand_sample(), store_base_power(), and variance().

void my_strcap ( char *  buf)
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 curses_conflict(), death_file(), describe_artifact(), 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_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 
)

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_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_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 
)

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 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 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 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(), 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 (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_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 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.

Referenced by show_file().

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

Rewrite string s in-place "skipping" every occurrence of character c except those preceded by character e.

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(), make_attack_normal(), 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 = 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.

Aux functions.

Referenced by text_mbstowcs().