NAME

Libutils is a library of useful routines

SYNOPSIS


#include <util.h>
#include <dstring.h>
#include <lists.h>

cc ... -lutils

INTRODUCTION

Libutils contains a set of commonly-used routines which fall into the following classes: *

util.c

This file contains various routines for file handling, string tokenizing, memory management etc

Declarations in util.h

Macro CATCH

#define CATCH() (setjmp(*get_error_buf()))
a macro to set a checkpoint where control is transferred to for a registered error handler.
register_error_handler Standard restrictions on use of setjmp(3) do apply.

Macro ALLOC, FREE

#define ALLOC(type) (alloc_chunk(sizeof(type)))
#define FREE(p) (free_chunk((p), sizeof(*(p))))
Wrapper for
alloc_chunk, free_chunk

Structure mapping

typedef struct mapping
{
        void *data;
        long len;
        int hfile;
        int really_mapped;
} mapping;
The structure for memory-mapped files. You can safely use its fields, but don't ever try to modify them directly. See map_file

Type ff_ioproc_t

typedef void *(*ff_ioproc_t)(const char *name);
The type of a function to open/create files found by find_filename Returns opaque data passed to the caller of find_filename

Structure table_t

typedef struct table_t
{
        char *key;
        void *data;
} table_t;
Generic sequential-access table type for lookup_table. Fields have obvious meaning.

Macro NUMOFITEMS

#define NUMOFITEMS(array) (sizeof(array) / sizeof(*(array)))
Returns the number of items in a given array

Macro ENDOF

#define ENDOF(array) ((array) + NUMOFITEMS(array))
Returns a pointer to the place right behind the next item of an array

Miscellaneous functions

Function libutils_version

const char *libutils_version(void) ;
Returns the version of libutils e. g. 0.7.15a

Function libutils_srcdate

const char *libutils_srcdate(void) ;
Returns the modification date of original source tree as YYYY/MM/DD

Function set_line_no

int set_line_no(int newnl) ;
Sets the current line number for parsing routines to newl, if it is non-negative Returns the previous line number

Function strupr

void strupr(char *str);
Converts str to upper case in-place.
Note:Incompatible with common DOS strupr – the latter returns char *

Function setenv

int setenv(const char *name, const char *value, int overwrite);
Emulates setenv(3) if it is not found in the standard library. Not present otherwise.

Function filelength

off_t filelength(int fd);
Returns the length of the file addressed by fh handle; -1 in case of an error

Function isnamesym

int isnamesym(int ch);
Returns 1 if ch is a valid XML name character (letter, digit, hyphen, underscore or colon); 0 otherwise

Function isplainnamesym

int isplainnamesym(int ch);
Returns 1 if isnamesym(ch) != 0 and ch is not a colon

Function versioncmp

int versioncmp(const char *ver1, const char *ver2);
Compares two strings as version designators. They should have the following structure: xx[.yy]*(-?suffix)?. Suffix may be a alphanumeric modifier or a special suffix. Special suffixes are the following (in the order they're compared):

Function translate_cvs_date

const char *translate_cvs_date(const char *date);
A very peculiar function used to translate CVS-supplied date ($Date: 2003/02/16 10:29:45 $ into YYYY/MM/DD. You're unlikely to use this function.
Note:this function may cause buffer overflow

Error handling functions

Function register_error_handler

void register_error_handler(void *arg);
Registers a new error handler associated with data pointed by arg

Function unwind_error

void unwind_error(void);
Deletes the most recently registered error handler. Makes the previous handler (if present) active.

Function get_error_buf

jmp_buf *get_error_buf(void);
Returns the point to pass control to of the current error handler
Error:No error handlers registered

Function get_eh_data

void *get_eh_data(void);
Returns the associated data of the current error handler
Error:No error handlers registered

Function get_error_desc

char *get_error_desc(void);
Returns a pointer to static area holding the error description produced by recover_error.

Function set_current_filename

char *set_current_filename(char *fname);
If fname is not NULL, sets the current filename for error messages to fname Returns the previous filename

Function print_error_msg

void print_error_msg(const char *fmt, ...);
formats an error message a la printf(3) and prints it to stderr followed by a new line. If the current line number and filename are defined they are printed in front of a message.
Note:If vfprintf(3) is not present in the standard library, the format string itself is printed.

Function recover_error

void recover_error(const char *fmt, ...);
If there are registered error handlers, passes control where the active handler tells us. An error message is formatted according to fmt and put into a static area accessed by get_error_desc.

Otherwise error message is printed as if by print_error_msg, and the program terminates

Note:If the system has not vsnprintf(3) but has vsprintf(3), buffer overflow may occur. If none are present, the format string itself is copied.

Function fatal_error

void fatal_error(const char *fmt, ...);
Prints an error message as if by print_error_msg and terminates the program

Function warning

void warning(const char *fmt, ...);
Like print_error_msg but issues warning: between line number and the message itself

Safe memory allocation functions

The functions below are equivalent to corresponding standard functions, but they call
recover_error in case of memory lack and never return NULL

Function xmalloc

void *xmalloc(size_t size);

Function xcalloc

void *xcalloc(size_t n, size_t size);

Function xrealloc

void *xrealloc(void *addr, size_t news);

Function xstrdup

char *xstrdup(const char *src);
If strdup(3) is not in the standard library, implements it.

Chunk-based memory allocation

These functions maintain lists of chunks of a given size. On request, a chunk from a list is reused. If there is no chunks left, new one is allocated and zeroed. When freed, a chunk is returned to a list. The maximal size of a chunk is 16385 * sizeof(void *) - 1 which is 65539 on 32-bit machines. Sizes of chunks less than 257 * sizeof(void *) (1028) are rounded up to sizeof(void *) boundary, others are rounded to 1K boundary

Function alloc_chunk

void *alloc_chunk(int size);

Function free_chunk

void free_chunk(void *chunk, int size);
Note:explicit size, since the function has no other means to detect it

Function reset_free_chunks

void reset_free_chunks(void);
Reclaims all the memory kept in chunks. You will rarely need to call this function

Safe file management functions

They are equvalents of corresponding standard functions, but call
recover_error and never return invalid values.

Function xfopen

FILE *xfopen(const char *name, const char *mode);

Function xopen

int xopen(const char *name, int mode, int rights);

File loading functions

Function find_filename

void *find_filename(const char *name, const char *path, const char *defext, 
                                                        void *(*ioproc)(const char *name),                                                      void *(crproc)(const char *name)                                        );
Tries to find name.

Function load_file

char *load_file(const char *name, const char *path, const char *defext);
Finds a file via find_filename and loads it into memory.
Error:can't locate name

Returns a pointer to allocated buffer

Function imap_file

void *imap_file(const char *name);
Tries to map a file into memory for reading. If mapping fails or is not implemented on the system, loads the file Returns pointer to mapping
Note:In general, you'd better call map_file

Function map_file

mapping *map_file(const char *name, const char *path, const char *defext);
Like load_file, but first tries to map it, loads if mapping fails

Function delete_mapping

void delete_mapping(mapping *map);
Unmaps a file and frees all the resources allocated by imap_file

Function is_map_eof

int is_map_eof(mapping *map, const char *pos);
Returns 1 if pos points to the end of mapped area

Common parsing functions

Function skip_spaces

void skip_spaces(const char *start, char **end);
Skips all the space characters and comments in an input string start. A comment starts with #, ends with a newline. A pointer to the first non-space character is stored into end. Line number is incremented as necessary.
Note:Comment-introducing character may be changed by assigning to util_comment_character variable

Function skip_spaces_nc

void skip_spaces_nc(const char *start, char **end);
Like skip_spaces, but doesn't recognize comments

Function parse_single_char

int parse_single_char(const char *start, char **end);
Gets the first character of start recognizing C-style escape sequences. In addition, \e denotes ESC (code 27). A pointer to the next character is stored into end Returns a character code
Note:end may be NULL

Function parse_char

int parse_char(const char *start, char **end);
Parses a character constant like in C. The character pointed at by start is taken as a delimiter. Multicharacter constants are treated in a bigendian manner, i. e. the first character will occupy the most significant byte. A pointer to the character following the constant will be stored into end Returns the value of a constant
Note:end may be NULL

Function parse_string

char *parse_string(const char *start, char **end);
Parses a C-like string. The character pointed at by start is taken as a delimiter. A pointer to the character following the string is be stored into end Returns a parsed string
Note:The returned value is overwritten in a sequential call to parse_string and you shouldn't try to free it
Note:end may be NULL

Function parse_id

char *parse_id(const char *start, char **end);
Parses an identifier – a token consisting of characters for which isnamesym is true. Starting point is at start, the first non-id character is stored into end. Returns a parsed identifier
Note:See the note of parse_string
Note:end may be NULL

Function parse_plain_id

char *parse_plain_id(const char *start, char **end);
Like parse_id, but parses a plain identifer, i. e. which doesn't contain colons

Function parse_num_token

int parse_num_token(const char *start, char **end);
Parses a numerical token which is either a number in a form acceptable by strtol(3) or a character constant acceptable by parse_char delimited by an apostrophe. Parsing starts at start, a pointer to the following character is stored into end Returns numerical value of a token
Note:end may be NULL

Function skip_ifs

void skip_ifs(const char *start, char **end, int else_flag);
Used to skip a block of text starting with start until a matching .endif. A pointer to the text following .endif is stored into end. Nested .if* are properly handled.