forked from xolatile/xhartae
91 lines
2.3 KiB
C
91 lines
2.3 KiB
C
/*
|
|
Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
|
|
|
|
Xhartae is free software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation.
|
|
And when you do redistribute it or modify it, it will use either version 3 of the License, or (at yours truly opinion) any later version.
|
|
It is distributed in the hope that it will be useful or harmful, it really depends... But no warranty what so ever, seriously. See GNU/GPLv3.
|
|
*/
|
|
|
|
#ifndef CHAPTER_0_SOURCE
|
|
#define CHAPTER_0_SOURCE
|
|
|
|
#include "chapter_0.h"
|
|
|
|
void in (void * data, int size) {
|
|
fatal_failure (data == NULL, "in: Failed to read from standard input, data is null pointer.");
|
|
fatal_failure (size == 0, "in: Failed to read from standard input, size is zero.");
|
|
|
|
(void) read (STDIN_FILENO, data, (unsigned long int) size);
|
|
}
|
|
|
|
void out (void * data, int size) {
|
|
fatal_failure (data == NULL, "out: Failed to write to standard output, data is null pointer.");
|
|
fatal_failure (size == 0, "out: Failed to write to standard output, size is zero.");
|
|
|
|
(void) write (STDOUT_FILENO, data, (unsigned long int) size);
|
|
}
|
|
|
|
void echo (char * string) {
|
|
out (string, string_length (string));
|
|
}
|
|
|
|
void fatal_failure (int condition, char * message) {
|
|
if (condition != 0) {
|
|
echo ("[\033[1;31mExiting\033[0m] ");
|
|
echo (message);
|
|
echo ("\n");
|
|
|
|
exit (EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
void limit (int * value, int minimum, int maximum) {
|
|
if (value == NULL) { /* We shouldn't dereference null pointer, but also don't want to abort the program for small mistake. */
|
|
return;
|
|
}
|
|
|
|
if (* value <= minimum) {
|
|
* value = minimum;
|
|
}
|
|
|
|
if (* value >= maximum) {
|
|
* value = maximum;
|
|
}
|
|
}
|
|
|
|
void * allocate (int size) {
|
|
char * data = NULL;
|
|
|
|
if (size <= 0) {
|
|
return (NULL);
|
|
}
|
|
|
|
data = calloc ((unsigned long int) size, sizeof (* data));
|
|
|
|
fatal_failure (data == NULL, "standard : allocate : Failed to allocate memory, internal function 'calloc' returned null pointer.");
|
|
|
|
return ((void *) data);
|
|
}
|
|
|
|
void * reallocate (void * data, int size) {
|
|
if (size <= 0) {
|
|
return (data);
|
|
}
|
|
|
|
data = realloc (data, (unsigned long int) size);
|
|
|
|
fatal_failure (data == NULL, "standard : reallocate: Failed to reallocate memory, internal function 'realloc' returned null pointer.");
|
|
|
|
return (data);
|
|
}
|
|
|
|
void * deallocate (void * data) {
|
|
if (data != NULL) {
|
|
free (data);
|
|
}
|
|
|
|
return (NULL);
|
|
}
|
|
|
|
#endif
|