Implementation of some utility functions.
More...
Go to the source code of this file.
|
#define | BUFSIZE 32 |
| Size of the initial buffer to be used in functions rgets and rgetsEOF . More...
|
|
|
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...
|
|
Implementation of some utility functions.
- Author
- Rui Carlos Gonçalves
- Version
- 3.1
- Date
- 01/2014
Definition in file util.c.
Size of the initial buffer to be used in functions rgets
and rgetsEOF
.
Definition at line 19 of file util.c.
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
-
min | minimum allowed value |
max | maximum 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
-
vals | the array |
begin | beginning of the first part |
middle | beginning of the second part |
end | end of the second part |
comp | comparison 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
-
vals | array of pointer to the elements to sort |
begin | starting position |
end | ending position |
comp | comparison function |
- Returns
- number of errors detected
Definition at line 188 of file util.c.
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
-
str | pointer 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
-
str | pointer 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
-
str | pointer where the text read shall be put |
dim | maximum 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.