xtandard/xtandard.c

605 lines
16 KiB
C
Raw Normal View History

2023-08-25 16:57:12 -04:00
/*
* Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
*
2023-08-28 09:02:20 -04:00
* Xtandard is free software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation.
2023-08-25 16:57:12 -04:00
* 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"
2023-09-05 12:29:48 -04:00
int argument_count = 0;
char * * argument_nick = NULL;
char * * argument_name = NULL;
void (* * argument_function) (void) = NULL;
2023-09-05 14:22:23 -04:00
int file_list_active = 0;
int file_list_count = 0;
int * file_list_mark = NULL;
int * file_list_size = NULL;
char * * file_list_name = NULL;
char * * file_list_data = NULL;
2023-08-25 16:57:12 -04:00
void in (void * data, int size) {
2023-09-05 12:29:48 -04:00
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.");
2023-08-25 16:57:12 -04:00
(void) read (STDIN_FILENO, data, (unsigned long int) size);
}
void out (void * data, int size) {
2023-09-05 12:29:48 -04:00
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.");
2023-08-25 16:57:12 -04:00
(void) write (STDOUT_FILENO, data, (unsigned long int) size);
}
2023-09-05 12:29:48 -04:00
void echo (char * data) {
out (data, string_length (data));
}
2023-08-25 16:57:12 -04:00
void fatal_failure (int condition, char * message) {
if (condition != 0) {
2023-09-05 12:29:48 -04:00
echo ("\033[1;31m[!]\033[0m ");
echo (message);
echo ("\n");
2023-08-25 16:57:12 -04:00
exit (EXIT_FAILURE);
}
}
void * allocate (int size) {
char * data = NULL;
2023-09-12 09:59:13 -04:00
fatal_failure (size == 0, "allocate: Failed to allocate memory, size is zero.");
2023-08-25 16:57:12 -04:00
data = calloc ((unsigned long int) size, sizeof (* data));
2023-09-12 09:59:13 -04:00
fatal_failure (data == NULL, "allocate: Failed to allocate memory, function calloc returned null pointer.");
2023-08-25 16:57:12 -04:00
return ((void *) data);
}
void * reallocate (void * data, int size) {
2023-09-12 09:59:13 -04:00
fatal_failure (size == 0, "reallocate: Failed to reallocate memory, size is zero.");
2023-08-25 16:57:12 -04:00
data = realloc (data, (unsigned long int) size);
2023-09-12 09:59:13 -04:00
fatal_failure (data == NULL, "reallocate: Failed to reallocate memory, function recalloc returned null pointer.");
/* Set new data to 0. */
2023-08-25 16:57:12 -04:00
return (data);
}
void * deallocate (void * data) {
2023-09-12 09:59:13 -04:00
fatal_failure (data == NULL, "deallocate: Failed to deallocate memory, data is null pointer.");
2023-08-25 16:57:12 -04:00
free (data);
return (NULL);
}
2023-09-12 09:59:13 -04:00
void * memorize (int size) { /* I broke this for testing something out... */
2023-08-25 16:57:12 -04:00
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;
2023-08-28 09:02:20 -04:00
int loling = 1024;
2023-08-25 16:57:12 -04:00
2023-08-28 09:02:20 -04:00
buffer = reallocate (buffer, loling);
2023-08-25 16:57:12 -04:00
do {
2023-08-28 09:02:20 -04:00
if ((offset + 1) % loling == 0) {
buffer = reallocate (buffer, ((offset + 1) / loling + 1) * loling);
2023-08-25 16:57:12 -04:00
}
buffer [offset] = '\0';
in (& buffer [offset], sizeof (* buffer));
++offset;
} while (buffer [offset - 1] != '\0');
buffer [offset - 1] = '\0';
return (buffer);
}
2023-09-05 12:29:48 -04:00
void argument_define (char * nick, char * name, void (* function) (void)) {
2023-09-12 09:59:13 -04:00
fatal_failure (nick == NULL, "argument_define: Failed to define an argument, nick is null pointer.");
fatal_failure (name == NULL, "argument_define: Failed to define an argument, name is null pointer.");
fatal_failure (function == NULL, "argument_define: Failed to define an argument, function is null pointer.");
2023-09-05 12:29:48 -04:00
argument_nick = reallocate (argument_nick, (argument_count + 1) * (int) sizeof (* argument_nick));
argument_name = reallocate (argument_name, (argument_count + 1) * (int) sizeof (* argument_name));
argument_function = reallocate (argument_function, (argument_count + 1) * (int) sizeof (* argument_function));
argument_nick [argument_count] = allocate (string_length (nick) + 1);
argument_name [argument_count] = allocate (string_length (name) + 1);
string_copy (argument_nick [argument_count], nick);
string_copy (argument_name [argument_count], name);
argument_function [argument_count] = function;
++argument_count;
}
void argument_select (int count, char * * array) {
int index_a, index_b;
2023-09-12 09:59:13 -04:00
if ((count == 1) || (array == NULL)) {
return;
}
2023-09-05 12:29:48 -04:00
for (index_a = 0; index_a != count; ++index_a) {
for (index_b = 0; index_b != argument_count; ++index_b) {
if ((string_compare (array [index_a], argument_nick [index_b]) != 0)
|| (string_compare (array [index_a], argument_name [index_b]) != 0)) {
argument_function [index_b] ();
}
}
}
}
void argument_delete (void) {
int index;
for (index = 0; index != argument_count; ++index) {
argument_nick [index] = deallocate (argument_nick [index]);
argument_name [index] = deallocate (argument_name [index]);
}
argument_nick = deallocate (argument_nick);
argument_name = deallocate (argument_name);
argument_function = deallocate (argument_function);
}
2023-09-05 14:22:23 -04:00
void file_list_import (char * name) {
2023-09-12 09:59:13 -04:00
fatal_failure (name == NULL, "file_list_import: Failed to import file, name is null pointer.");
2023-09-10 14:42:06 -04:00
2023-09-05 14:22:23 -04:00
++file_list_count;
file_list_active = file_list_count - 1;
file_list_mark = reallocate (file_list_mark, (int) sizeof (* file_list_mark) * file_list_count);
file_list_size = reallocate (file_list_size, (int) sizeof (* file_list_size) * file_list_count);
file_list_name = reallocate (file_list_name, (int) sizeof (* file_list_name) * file_list_count);
file_list_data = reallocate (file_list_data, (int) sizeof (* file_list_data) * file_list_count);
file_list_mark [file_list_count - 1] = -1;
file_list_size [file_list_count - 1] = -1;
file_list_name [file_list_count - 1] = NULL;
file_list_data [file_list_count - 1] = NULL;
file_list_name [file_list_count - 1] = allocate (string_length (name) + 1);
(void) string_copy_limit (file_list_name [file_list_count - 1], name, string_length (name) + 1);
file_list_mark [file_list_count - 1] = open (name, O_RDONLY);
2023-09-12 09:59:13 -04:00
fatal_failure (file_list_mark [file_list_count - 1] == -1, "file_list_import: Failed to open file, function open returned invalid descriptor.");
2023-09-10 14:42:06 -04:00
2023-09-05 14:22:23 -04:00
file_list_size [file_list_count - 1] = (int) lseek (file_list_mark [file_list_count - 1], 0, SEEK_END) + 1;
(void) lseek (file_list_mark [file_list_count - 1], 0, SEEK_SET);
file_list_data [file_list_count - 1] = allocate (file_list_size [file_list_count - 1]);
(void) read (file_list_mark [file_list_count - 1], file_list_data [file_list_count - 1], (unsigned long int) (file_list_size [file_list_count - 1] - 1));
close (file_list_mark [file_list_count - 1]);
file_list_data [file_list_count - 1] [file_list_size [file_list_count - 1] - 1] = '\0';
}
void file_list_export (char * name) {
2023-09-12 09:59:13 -04:00
fatal_failure (name == NULL, "file_list_export: Failed to export file, name is null pointer.");
2023-09-05 14:22:23 -04:00
file_list_mark [file_list_active] = open (name, O_WRONLY | O_CREAT | O_TRUNC);
(void) write (file_list_mark [file_list_active], file_list_data [file_list_active], (unsigned long int) file_list_size [file_list_active]);
close (file_list_mark [file_list_active]);
}
void file_list_delete (void) {
int i;
for (i = 0; i != file_list_count; ++i) {
file_list_name [i] = deallocate (file_list_name [i]);
file_list_data [i] = deallocate (file_list_data [i]);
}
file_list_mark = deallocate (file_list_mark);
file_list_size = deallocate (file_list_size);
file_list_name = deallocate (file_list_name);
file_list_data = deallocate (file_list_data);
}
2023-09-05 12:29:48 -04:00
int file_open (char * name, int mode) {
int descriptor = -1;
fatal_failure (name == NULL, "file_open: Failed to open file, name is null pointer.");
descriptor = open (name, mode);
2023-09-12 09:59:13 -04:00
fatal_failure (descriptor == -1, "file_open: Failed to open file, function open returned invalid descriptor.");
2023-09-05 12:29:48 -04:00
return (descriptor);
}
2023-09-12 09:59:13 -04:00
int file_close (int file) {
fatal_failure (file == -1, "file_close: Failed to close file, invalid file descriptor.");
fatal_failure (close (file) == -1, "file_close: Failed to close file, function close returned invalid code.");
return (-1);
}
2023-09-05 12:29:48 -04:00
void file_read (int file, void * data, int size) {
2023-09-12 09:59:13 -04:00
fatal_failure (file == -1, "file_read: Failed to read from file, invalid descriptor.");
2023-09-05 12:29:48 -04:00
fatal_failure (data == NULL, "file_read: Failed to read from file, data is null pointer.");
fatal_failure (size == 0, "file_read: Failed to read from file, size is zero.");
(void) read (file, data, (unsigned long int) size);
}
void file_write (int file, void * data, int size) {
2023-09-12 09:59:13 -04:00
fatal_failure (file == -1, "file_write: Failed to write to file, invalid descriptor.");
2023-09-05 12:29:48 -04:00
fatal_failure (data == NULL, "file_write: Failed to write to file, data is null pointer.");
fatal_failure (size == 0, "file_write: Failed to write to file, size is zero.");
(void) write (file, data, (unsigned long int) size);
}
int file_seek (int file, int whence) {
2023-09-12 09:59:13 -04:00
fatal_failure (file == -1, "file_seek: Failed to seek in file, invalid descriptor.");
2023-09-05 12:29:48 -04:00
return ((int) lseek (file, 0, whence));
}
int file_size (int file) {
int size = 0;
2023-09-12 09:59:13 -04:00
fatal_failure (file == -1, "file_size: Failed to get size of file, invalid descriptor.");
2023-09-05 12:29:48 -04:00
size = lseek (file, 0, SEEK_END);
(void) lseek (file, 0, SEEK_SET);
fatal_failure (size == -1, "file_size: Failed to get size of file, invalid file size.");
return (size);
}
void file_import (char * name, void * data) {
(void) name;
(void) data;
}
void file_export (char * name, void * data) {
(void) name;
(void) data;
}
int character_is_uppercase (char character) {
return ((int) ((character >= 'A') && (character <= 'Z')));
}
int character_is_lowercase (char character) {
return ((int) ((character >= 'a') && (character <= 'z')));
}
int character_is_digit (char character) {
return ((int) ((character >= '0') && (character <= '9')));
}
int character_is_blank (char character) {
return ((int) ((character == ' ') || (character == '\t') || (character == '\r') || (character == '\n')));
}
int character_is_alpha (char character) {
return ((character_is_uppercase (character) != 0) || (character_is_lowercase (character) != 0));
}
int character_is_symbol (char character) {
char * symbols = "~!@#$%^&*()+{}|:\"<>?`-=[]\\;',./";
return (character_compare_array (character, symbols, string_length (symbols)));
}
int character_is_visible (char character) {
return ((int) ((character >= '!') && (character <= '~')));
}
int character_is_invisible (char character) {
return (character_is_visible (character) == 0);
}
int character_is_escape (char character) {
return ((int) (character == '\033'));
}
int character_is_underscore (char character) {
return ((int) (character == '_'));
}
int character_compare_array (char character, char * character_array, int count) {
int i = 0;
do {
if (character == character_array [i]) {
return (1);
}
} while (++i != count);
return (0);
}
2023-09-05 12:29:48 -04:00
2023-08-25 16:57:12 -04:00
int string_length (char * string) {
2023-08-28 09:02:20 -04:00
int length = 0;
2023-08-25 16:57:12 -04:00
2023-09-10 14:42:06 -04:00
fatal_failure (string == NULL, "string_length: String is null pointer.");
2023-08-25 16:57:12 -04:00
for (length = 0; string [length] != '\0'; ++length);
return (length);
}
void string_reverse (char * string) {
2023-08-28 09:02:20 -04:00
int i = 0;
int length = 0;
2023-08-25 16:57:12 -04:00
2023-09-10 14:42:06 -04:00
fatal_failure (string == NULL, "string_reverse: String is null pointer.");
2023-08-25 16:57:12 -04:00
length = string_length (string);
for (i = 0; string [i] != '\0'; ++i) {
string [i] = string [length - i];
}
}
2023-09-05 14:22:23 -04:00
void string_delete (char * string, int length) {
int i;
2023-09-10 14:42:06 -04:00
fatal_failure (string == NULL, "string_delete: String is null pointer.");
2023-09-05 14:22:23 -04:00
fatal_failure (length == 0, "string_delete: String length is zero.");
for (i = 0; i != length; ++i) {
string [i] = '\0';
}
}
2023-08-25 16:57:12 -04:00
int string_compare (char * string_0, char * string_1) {
2023-08-28 09:02:20 -04:00
int i = 0;
2023-08-25 16:57:12 -04:00
2023-09-10 14:42:06 -04:00
fatal_failure (string_0 == NULL, "string_compare: Destination string is null pointer.");
fatal_failure (string_1 == NULL, "string_compare: Source string is null pointer.");
2023-08-25 16:57:12 -04:00
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) {
2023-08-28 09:02:20 -04:00
int i = 0;
int length = 0;
2023-08-25 16:57:12 -04:00
2023-09-10 14:42:06 -04:00
fatal_failure (string_0 == NULL, "string_copy: Destination string is null pointer.");
fatal_failure (string_1 == NULL, "string_copy: Source string is null pointer.");
2023-08-25 16:57:12 -04:00
length = string_length (string_1);
for (i = 0; i != length; ++i) {
string_0 [i] = string_1 [i];
}
}
2023-08-28 09:02:20 -04:00
2023-08-25 16:57:12 -04:00
void string_concatenate (char * string_0, char * string_1) {
2023-08-28 09:02:20 -04:00
int i = 0;
int length_0 = 0;
int length_1 = 0;
2023-08-25 16:57:12 -04:00
2023-09-10 14:42:06 -04:00
fatal_failure (string_0 == NULL, "string_concatenate: Destination string is null pointer.");
fatal_failure (string_1 == NULL, "string_concatenate: Source string is null pointer.");
2023-08-25 16:57:12 -04:00
2023-08-28 09:02:20 -04:00
length_0 = string_length (string_0);
length_1 = string_length (string_1);
2023-08-25 16:57:12 -04:00
2023-08-28 09:02:20 -04:00
for (i = 0; i != length_1; ++i) {
string_0 [length_0 + i] = string_1 [i];
}
2023-08-25 16:57:12 -04:00
}
2023-08-28 09:02:20 -04:00
int string_compare_limit (char * string_0, char * string_1, int limit) {
int i = 0;
2023-08-25 16:57:12 -04:00
2023-09-10 14:42:06 -04:00
fatal_failure (string_0 == NULL, "string_compare_limit: Destination string is null pointer.");
fatal_failure (string_1 == NULL, "string_compare_limit: Source string is null pointer.");
2023-08-25 16:57:12 -04:00
2023-09-05 14:22:23 -04:00
for (i = 0; i != limit; ++i) {
2023-08-28 09:02:20 -04:00
if (string_0 [i] != string_1 [i]) {
return (0);
}
}
2023-08-25 16:57:12 -04:00
2023-08-28 09:02:20 -04:00
return (1);
2023-08-25 16:57:12 -04:00
}
2023-08-28 09:02:20 -04:00
void string_copy_limit (char * string_0, char * string_1, int limit) {
2023-09-05 14:22:23 -04:00
int i = 0;
2023-08-28 09:02:20 -04:00
2023-09-10 14:42:06 -04:00
fatal_failure (string_0 == NULL, "string_copy_limit: Destination string is null pointer.");
fatal_failure (string_1 == NULL, "string_copy_limit: Source string is null pointer.");
2023-08-25 16:57:12 -04:00
2023-08-28 09:02:20 -04:00
if (limit <= 0) {
return;
}
2023-09-05 14:22:23 -04:00
for (i = 0; i != limit; ++i) {
2023-08-28 09:02:20 -04:00
string_0 [i] = string_1 [i];
}
2023-08-25 16:57:12 -04:00
}
2023-08-28 09:02:20 -04:00
void string_concatenate_limit (char * string_0, char * string_1, int limit) {
int i = 0;
int length_0 = 0;
int length_1 = 0;
2023-09-10 14:42:06 -04:00
fatal_failure (string_0 == NULL, "string_concatenate_limit: Destination string is null pointer.");
fatal_failure (string_1 == NULL, "string_concatenate_limit: Source string is null pointer.");
2023-08-25 16:57:12 -04:00
2023-08-28 09:02:20 -04:00
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];
}
2023-08-25 16:57:12 -04:00
}
2023-08-28 09:02:20 -04:00
2023-09-10 14:42:06 -04:00
int string_split_space (char * string) {
int i = 0;
int count = 0;
fatal_failure (string == NULL, "string_split_space: Source string is null pointer.");
for (i = 0; string [i] != '\0'; ++i) {
if ((string [i] == ' ') || (string [i] == '\t') || (string [i] == '\n')) {
string [i] = '\0';
++count;
}
}
return (++count);
}
2023-09-12 10:15:13 -04:00
void memory_delete (void * memory, int length) {
int i = 0;
char * cast = (char *) memory;
fatal_failure (memory == NULL, "memory_delete: Memory is null pointer.");
if (length <= 0) {
return;
}
for (i = 0; i != length; ++i) {
cast [i] = '\0';
}
}
int memory_compare (void * memory, void * source, int length) {
int i = 0;
char * cast_0 = (char *) memory;
char * cast_1 = (char *) source;
fatal_failure (memory == NULL, "memory_compare: Memory is null pointer.");
fatal_failure (source == NULL, "memory_compare: Source is null pointer.");
if (length <= 0) {
return;
}
for (i = 0; (cast_0 [i] != '\0') && (cast_1 [i] != '\0'); ++i) {
if (cast_0 [i] != cast_1 [i]) {
return (0);
}
}
return (1);
}
void memory_copy (void * memory, void * source, int length) {
int i = 0;
char * cast_0 = (char *) memory;
char * cast_1 = (char *) source;
fatal_failure (memory == NULL, "memory_copy: Memory is null pointer.");
fatal_failure (source == NULL, "memory_copy: Source is null pointer.");
if (length <= 0) {
return;
}
for (i = 0; i != length; ++i) {
cast_0 [i] = cast_1 [i];
}
}
void terminal_clear (void) {
out ("\033[2J\033[H", 7);
}
void terminal_style (int effect, int colour) {
char format [8] = "\033[ ;3 m";
if ((effect == -1) || (colour == -1)) {
out ("\033[0m", 4);
} else {
2023-09-12 09:59:13 -04:00
format [2] = (char) (effect % EFFECT_COUNT) + '0';
format [5] = (char) (colour % COLOUR_COUNT) + '0';
out (format, 7);
}
}
void terminal_show_cursor (int show) {
if (show != 0) {
out ("\033[?25h", 6);
} else {
out ("\033[?25l", 6);
}
}
2023-08-25 16:57:12 -04:00
#endif