Atrinik Server 2.5
Functions
server/utils.c File Reference
#include <global.h>

Go to the source code of this file.

Functions

int rndm (int min, int max)
int rndm_chance (uint32 n)
int look_up_spell_name (const char *spname)
void replace (const char *src, const char *key, const char *replacement, char *result, size_t resultsize)
char * cleanup_string (char *ustring)
const char * get_word_from_string (const char *str, int *pos)
void adjust_player_name (char *name)
void replace_unprintable_chars (char *buf)
size_t split_string (char *str, char *array[], size_t array_size, char sep)
int get_random_dir ()
int get_randomized_dir (int dir)
int buf_overflow (const char *buf1, const char *buf2, size_t bufsize)
char * cleanup_chat_string (char *ustring)
char * format_number_comma (uint64 num)
void copy_file (const char *filename, FILE *fpout)
void convert_newline (char *str)
void string_remove_markup (char *str)

Detailed Description

General convenience functions for Atrinik.

Definition in file utils.c.


Function Documentation

void adjust_player_name ( char *  name)

Adjusts a player name like "xxXxx " to "Xxxxx".

Parameters:
namePlayer name to adjust.

Definition at line 186 of file utils.c.

int buf_overflow ( const char *  buf1,
const char *  buf2,
size_t  bufsize 
)

We don't want to exceed the buffer size of buf1 by adding on buf2!

Parameters:
buf1
buf2Buffers we plan on concatenating. Can be NULL.
bufsizeSize of buf1. Can be 0.
Returns:
1 if overflow will occur, 0 otherwise.

Definition at line 301 of file utils.c.

char* cleanup_chat_string ( char *  ustring)

This function does three things:

  1. Controls that we have a legal string; if not, return NULL
  2. Removes all left whitespace (if all whitespace return NULL)
  3. Change and/or process all control characters like '^', '~', etc.
    Parameters:
    ustringThe string to cleanup
    Returns:
    Cleaned up string, or NULL

Definition at line 330 of file utils.c.

char* cleanup_string ( char *  ustring)

Checks for a legal string by first trimming left whitespace and then checking if there is anything left.

Parameters:
ustringThe string to clean up.
Returns:
Cleaned up string, or NULL if the cleaned up string doesn't have any characters left.

Definition at line 131 of file utils.c.

void convert_newline ( char *  str)

Replaces "\n" by a newline char.

Since we are replacing 2 chars by 1, no overflow should happen.

Parameters:
lineText to replace into.

Definition at line 422 of file utils.c.

void copy_file ( const char *  filename,
FILE *  fpout 
)

Copy a file.

Parameters:
filenameSource file.
fpoutWhere to copy to.

Definition at line 396 of file utils.c.

char* format_number_comma ( uint64  num)

Adds thousand separators to a given number.

Parameters:
numNumber.
Returns:
Thousands-separated string.

Definition at line 367 of file utils.c.

int get_random_dir ( )

Returns a random direction (1..8).

Returns:
The random direction.

Definition at line 280 of file utils.c.

int get_randomized_dir ( int  dir)

Returns a random direction (1..8) similar to a given direction.

Parameters:
dirThe exact direction.
Returns:
The randomized direction.

Definition at line 289 of file utils.c.

const char* get_word_from_string ( const char *  str,
int *  pos 
)

Returns a single word from a string, free from left and right whitespace.

Parameters:
strThe string.
posPosition in string.
Returns:
The word, NULL if there is no word left in str.

Definition at line 154 of file utils.c.

int look_up_spell_name ( const char *  spname)

Return the number of the spell that whose name matches the passed string argument.

Parameters:
spnameName of the spell to look up.
Returns:
-1 if no such spell name match is found, the spell ID otherwise.

Definition at line 75 of file utils.c.

void replace ( const char *  src,
const char *  key,
const char *  replacement,
char *  result,
size_t  resultsize 
)

Replace in string src all occurrences of key by replacement. The resulting string is put into result; at most resultsize characters (including the terminating null character) will be written to result.

Definition at line 94 of file utils.c.

void replace_unprintable_chars ( char *  buf)

Replaces any unprintable character in the given buffer with a space.

Parameters:
bufThe buffer to modify.

Definition at line 212 of file utils.c.

int rndm ( int  min,
int  max 
)

Calculates a random number between min and max.

It is suggested one uses this function rather than RANDOM()%, as it would appear that a number of off-by-one-errors exist due to improper use of %.

This should also prevent SIGFPE.

Parameters:
minStarting range.
maxEnding range.
Returns:
The random number.

Definition at line 43 of file utils.c.

int rndm_chance ( uint32  n)

Calculates a chance of 1 in 'n'.

Parameters:
nNumber.
Returns:
1 if the chance of 1/n was successful, 0 otherwise.

Definition at line 58 of file utils.c.

size_t split_string ( char *  str,
char *  array[],
size_t  array_size,
char  sep 
)

Splits a string delimited by passed in sep value into characters into an array of strings.

Parameters:
strThe string to be split; will be modified.
arrayThe string array; will be filled with pointers into str.
array_sizeThe number of elements in array; if str contains more fields excess fields are not split but included into the last element.
sepSeparator to use.
Returns:
The number of elements found; always less or equal to array_size.

Definition at line 233 of file utils.c.

void string_remove_markup ( char *  str)

Strips markup from a string.

Replaces '<' characters with a space, effectively disabling any markup (note that entities such as < are still allowed).

Parameters:
strThe string.

Definition at line 441 of file utils.c.