|
- /*
- * Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
- *
- * Xtandard 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 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;
- int loling = 1024;
-
- buffer = reallocate (buffer, loling);
-
- do {
- if ((offset + 1) % loling == 0) {
- buffer = reallocate (buffer, ((offset + 1) / loling + 1) * loling);
- }
-
- 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 = 0;
-
- 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 = 0;
- int length = 0;
-
- 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 = 0;
-
- 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);
- }
-
- void string_copy (char * string_0, char * string_1) {
- int i = 0;
- int length = 0;
-
- 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) {
- int i = 0;
- int length_0 = 0;
- int length_1 = 0;
-
- fatal_failure (string_0 == NULL, "string_concatenate: Destination string is null.");
- fatal_failure (string_1 == NULL, "string_concatenate: Source string is null.");
-
- length_0 = string_length (string_0);
- length_1 = string_length (string_1);
-
- for (i = 0; i != length_1; ++i) {
- string_0 [length_0 + i] = string_1 [i];
- }
- }
-
- int string_compare_limit (char * string_0, char * string_1, int limit) {
- int i = 0;
-
- fatal_failure (string_0 == NULL, "string_compare_limit: Destination string is null.");
- fatal_failure (string_1 == NULL, "string_compare_limit: 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_limit (char * string_0, char * string_1, int limit) {
- int i = 0;
- int length = 0;
-
- fatal_failure (string_0 == NULL, "string_copy_limit: Destination string is null.");
- fatal_failure (string_1 == NULL, "string_copy_limit: Source string is null.");
-
- if (limit <= 0) {
- return;
- }
-
- length = string_length (string_1);
-
- for (i = 0; (i != length) && (i != limit); ++i) {
- string_0 [i] = string_1 [i];
- }
- }
-
- void string_concatenate_limit (char * string_0, char * string_1, int limit) {
- int i = 0;
- int length_0 = 0;
- int length_1 = 0;
-
- fatal_failure (string_0 == NULL, "string_concatenate_limit: Destination string is null.");
- fatal_failure (string_1 == NULL, "string_concatenate_limit: Source string is null.");
-
- if (limit <= 0) {
- return;
- }
-
- length_0 = string_length (string_0);
- length_1 = string_length (string_1);
-
- for (i = 0; (i != length_1) && (i != limit); ++i) {
- string_0 [length_0 + i] = string_1 [i];
- }
- }
-
- #endif
|