LibRCG  3.1.1
util.c File Reference

Implementation of some utility functions. More...

Go to the source code of this file.

Macros

#define BUFSIZE   32
 Size of the initial buffer to be used in functions rgets and rgetsEOF. More...
 

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 merge (void *vals[], int begin, int middle, int end, int(*comp)(void *, void *))
 Merges two ordered parts of an array of pointers, based on a given comparison function. 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.

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

Definition in file util.c.

Macro Definition Documentation

#define BUFSIZE   32

Size of the initial buffer to be used in functions rgets and rgetsEOF.

Definition at line 19 of file util.c.

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 merge ( void *  vals[],
int  begin,
int  middle,
int  end,
int(*)(void *, void *)  comp 
)

Merges two ordered parts of an array of pointers, based on a given comparison function.

Parameters
valsthe array
beginbeginning of the first part
middlebeginning of the second part
endend of the second part
compcomparison function
Returns
1 if an error occurred
0 otherwise

Definition at line 155 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