LibRCG  3.1.1
util.h File Reference

Implementation of some utility functions. More...

Go to the source code of this file.

Functions

int rgets (char **str)
 Reads a line from the stdin. More...
 
int rgetsEOF (char **str)
 Reads a text from the stdin. More...
 
int rngets (char *str, int dim)
 Reads a line from the stdin. More...
 
int getRandom (int min, int max)
 Generates a random number in a given interval. More...
 
int mergeSort (void *vals[], int begin, int end, int(*comp)(void *, void *))
 Orders an array into ascending order, based on a given comparison function. More...
 

Detailed Description

Implementation of some utility functions.

Provides functions to read text from de stdin, to generate random numbers, and to sort arrays.

Author
Rui Carlos Gonçalves
Version
3.1
Date
01/2014

Definition in file util.h.

Function Documentation

int getRandom ( int  min,
int  max 
)

Generates a random number in a given interval.

Attention
The value of max must be greater than min, otherwise the function will return 0.
Parameters
minminimum allowed value
maxmaximum allowed value
Returns
0 if max<min
a random number in the given interval otherwise

Definition at line 122 of file util.c.

int mergeSort ( void *  vals[],
int  begin,
int  end,
int(*)(void *, void *)  comp 
)

Orders an array into ascending order, based on a given comparison function.

Uses the algorithm merge sort.

Parameters
valsarray of pointer to the elements to sort
beginstarting position
endending position
compcomparison function
Returns
number of errors detected

Definition at line 188 of file util.c.

int rgets ( char **  str)

Reads a line from the stdin.

Reads all characters until a \n.

Attention
The memory space where the line read will be stored is allocated by this function. Therefore, it shall not be previously allocated.
Parameters
strpointer where a string containing the line read shall be put
Returns
-2 if the str is invalid
-1 if an error occur
the size of the string read otherwise

Definition at line 21 of file util.c.

int rgetsEOF ( char **  str)

Reads a text from the stdin.

Reads all characters until an EndOfFile.

Attention
The memory space where the text read will be stored is allocated by this function. Therefore, it shall not be previously allocated.
Parameters
strpointer where a string containing the text read shall be put
Returns
-2 if the str is invalid
-1 if an error occur
the size of the string read otherwise

Definition at line 61 of file util.c.

int rngets ( char *  str,
int  dim 
)

Reads a line from the stdin.

Reads all characters until a \n. The line shall not exceed the size dim-2 (the additional characters will be lost).

Attention
The memory location where the line read will be stored must be previously allocated.
Parameters
strpointer where the text read shall be put
dimmaximum size for the line to be read (the line shall not exceed the size dim-2)
Returns
-1 if the line exceeded the maximum size allowed
the size of the line read otherwise

Definition at line 101 of file util.c.

LibRCG © 2004-2015   Rui Carlos Gonçalves