xtandard/xtandard.c

213 lines
4.4 KiB
C
Raw Normal View History

2023-08-25 16:57:12 -04:00
/*
* Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
*
* Xtandard is deallocate 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 XTANDARD_SOURCE
#define XTANDARD_SOURCE
#include "xtandard.h"
void in (void * data, int size) {
(void) read (STDIN_FILENO, data, (unsigned long int) size);
}
void out (void * data, int size) {
(void) write (STDOUT_FILENO, data, (unsigned long int) size);
}
void fatal_failure (int condition, char * message) {
if (condition != 0) {
out ("\033[1;31m[!]\033[0m ", 15);
out (message, string_length (message));
out ("\n", 1);
exit (EXIT_FAILURE);
}
}
void * allocate (int size) {
char * data = NULL;
data = calloc ((unsigned long int) size, sizeof (* data));
fatal_failure (data == NULL, "allocate: Oh no...");
return ((void *) data);
}
void * reallocate (void * data, int size) {
data = realloc (data, (unsigned long int) size);
fatal_failure (data == NULL, "reallocate: Oh no...");
return (data);
}
void * deallocate (void * data) {
fatal_failure (data == NULL, "deallocate: Oh no...");
free (data);
return (NULL);
}
void * memorize (int size) {
static char * buffer = NULL;
char * points = NULL;
static int length = 0;
static int chunks = 0;
static int loling = 1024;
if (size == 0) {
free (buffer);
buffer = NULL;
length = 0;
chunks = 0;
return (NULL);
}
for (; length + size > chunks * loling; ) {
int i;
++chunks;
buffer = realloc (buffer, (unsigned long int) (chunks * loling));
fatal_failure (buffer == NULL, "memorize: Oh no...");
for (i = (chunks - 1) * loling; i != chunks * loling; ++i) {
buffer [i] = '\0';
}
}
points = & buffer [length];
length += size;
return ((void *) points);
}
void * record (void) {
char * buffer = NULL;
int offset = 0;
buffer = reallocate (buffer, 1024);
do {
if ((offset + 1) % 1024 == 0) {
buffer = reallocate (buffer, ((offset + 1) / 1024 + 1) * 1024);
}
buffer [offset] = '\0';
in (& buffer [offset], sizeof (* buffer));
++offset;
} while (buffer [offset - 1] != '\0');
buffer [offset - 1] = '\0';
return (buffer);
}
int string_length (char * string) {
int length;
fatal_failure (string == NULL, "string_length: String is null.");
for (length = 0; string [length] != '\0'; ++length);
return (length);
}
void string_delete (char * string) {
string [0] = '\0';
}
void string_reverse (char * string) {
int i, length;
fatal_failure (string == NULL, "string_reverse: String is null.");
length = string_length (string);
for (i = 0; string [i] != '\0'; ++i) {
string [i] = string [length - i];
}
}
int string_compare (char * string_0, char * string_1) {
int i;
fatal_failure (string_0 == NULL, "string_compare: Destination string is null.");
fatal_failure (string_1 == NULL, "string_compare: Source string is null.");
for (i = 0; (string_0 [i] != '\0') && (string_1 [i] != '\0'); ++i) {
if (string_0 [i] != string_1 [i]) {
return (0);
}
}
return (1);
}
int string_compare_limit (char * string_0, char * string_1, int limit) {
int i;
fatal_failure (string_0 == NULL, "string_compare: Destination string is null.");
fatal_failure (string_1 == NULL, "string_compare: Source string is null.");
for (i = 0; (string_0 [i] != '\0') && (string_1 [i] != '\0') && (i != limit); ++i) {
if (string_0 [i] != string_1 [i]) {
return (0);
}
}
return (1);
}
void string_copy (char * string_0, char * string_1) {
int i, length;
fatal_failure (string_0 == NULL, "string_copy: Destination string is null.");
fatal_failure (string_1 == NULL, "string_copy: Source string is null.");
length = string_length (string_1);
for (i = 0; i != length; ++i) {
string_0 [i] = string_1 [i];
}
}
/*
void string_concatenate (char * string_0, char * string_1) {
}
void string_remove (char * string_0, char * string_1) {
}
int string_string (char * string_0, char * string_1) {
}
int string_character (char * string_0, char * string_1) {
}
void string_offset (char * string, char character, int count) {
}
void string_replace (char * string, char character, int count) {
}
*/
#endif