xtandard/xtandard.c

866 lines
21 KiB
C
Raw Normal View History

2023-08-25 16:57:12 -04:00
#ifndef XTANDARD_SOURCE
#define XTANDARD_SOURCE
2023-10-01 06:06:40 -04:00
#include <xolatile/xtandard.h>
2023-08-25 16:57:12 -04:00
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
2024-06-16 05:38:02 -04:00
#include <stdarg.h>
static char * log_notify = null;
static int file_list_active = 0;
static int file_list_count = 0;
static int * file_list_mark = null;
static int * file_list_size = null;
static char * * file_list_name = null;
static char * * file_list_data = null;
2023-09-05 14:22:23 -04:00
2023-08-25 16:57:12 -04:00
void in (void * data, int size) {
fatal_failure (data == null, "in: Failed to read from standard input, data is null pointer.");
2023-09-05 12:29:48 -04:00
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) {
fatal_failure (data == null, "out: Failed to write to standard output, data is null pointer.");
2023-09-05 12:29:48 -04:00
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-10-01 06:06:40 -04:00
void log_in (int type, int flag, char * data) {
2023-10-01 13:33:04 -04:00
int start;
char * type_mark [log_count] = {
2023-10-01 13:33:04 -04:00
"[\033[1;32mSuccess\033[0m] ",
"[\033[1;33mWarning\033[0m] ",
"[\033[1;31mFailure\033[0m] ",
"[\033[1;30mComment\033[0m] "
2023-10-01 06:06:40 -04:00
};
if ((flag == 0) || (type <= -1) || (type >= log_count)) {
2023-10-01 06:06:40 -04:00
return;
}
2023-10-01 13:33:04 -04:00
start = string_length (log_notify);
2023-10-01 06:06:40 -04:00
2023-10-01 13:33:04 -04:00
log_notify = reallocate (log_notify, start + string_length (type_mark [type]) + string_length (data) + 2);
log_notify [start] = '\0';
2023-10-01 06:06:40 -04:00
string_concatenate (log_notify, type_mark [type]);
string_concatenate (log_notify, data);
string_concatenate (log_notify, "\n");
}
2023-10-04 15:11:46 -04:00
void log_out (char * name) {
dump (name, log_notify);
2023-10-01 06:06:40 -04:00
log_notify = deallocate (log_notify);
}
2023-09-05 12:29:48 -04:00
void echo (char * data) {
if (data == null) {
2023-10-01 06:06:40 -04:00
return;
}
2023-09-05 12:29:48 -04:00
out (data, string_length (data));
}
2023-10-04 15:11:46 -04:00
void dump (char * name, char * data) {
int file = -1;
if (name == null) {
2023-10-04 15:11:46 -04:00
echo (data);
return;
}
if (data == null) {
2023-10-04 15:11:46 -04:00
return;
}
file = file_open (name, O_RDWR | O_CREAT | O_APPEND);
file_write (file, data, string_length (data));
file = file_close (file);
}
2023-09-18 15:36:50 -04:00
void echo_byte (int byte) {
out ("0123456789ABCDEF" + (byte % 256) / 16, 1);
out ("0123456789ABCDEF" + (byte % 256) % 16, 1);
out (" ", 1);
}
2023-08-25 16:57:12 -04:00
void fatal_failure (int condition, char * message) {
if (condition != 0) {
2023-10-01 13:33:04 -04:00
echo ("[\033[1;31mExiting\033[0m] ");
2023-09-05 12:29:48 -04:00
echo (message);
echo ("\n");
2023-08-25 16:57:12 -04:00
exit (EXIT_FAILURE);
}
}
2023-09-20 07:45:17 -04:00
void limit (int * value, int minimum, int maximum) {
if (value == null) {
2023-10-01 13:33:04 -04:00
return;
}
2023-09-20 07:45:17 -04:00
if (* value <= minimum) {
* value = minimum;
}
if (* value >= maximum) {
* value = maximum;
}
}
2023-08-25 16:57:12 -04:00
void * allocate (int size) {
char * data = null;
2023-08-25 16:57:12 -04:00
2023-10-01 06:06:40 -04:00
if (size <= 0) {
return (null);
2023-10-01 06:06:40 -04:00
}
2023-09-12 09:59:13 -04:00
2023-08-25 16:57:12 -04:00
data = calloc ((unsigned long int) size, sizeof (* data));
fatal_failure (data == null, "standard : allocate : Failed to allocate memory, internal function 'calloc' returned null pointer.");
2023-08-25 16:57:12 -04:00
return ((void *) data);
}
void * reallocate (void * data, int size) {
2023-10-01 06:06:40 -04:00
if (size <= 0) {
return (data);
}
2023-09-12 09:59:13 -04:00
2023-08-25 16:57:12 -04:00
data = realloc (data, (unsigned long int) size);
fatal_failure (data == null, "standard : reallocate: Failed to reallocate memory, internal function 'realloc' returned null pointer.");
2023-09-12 09:59:13 -04:00
/* Set new data to 0. */
2023-08-25 16:57:12 -04:00
return (data);
}
void * deallocate (void * data) {
if (data != null) {
2023-10-01 06:06:40 -04:00
free (data);
}
2023-08-25 16:57:12 -04:00
return (null);
2023-08-25 16:57:12 -04:00
}
void * record (void) {
char * buffer = null;
2023-08-25 16:57:12 -04:00
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';
2023-11-01 09:06:02 -04:00
in (& buffer [offset], (int) sizeof (* buffer));
2023-08-25 16:57:12 -04:00
++offset;
} while (buffer [offset - 1] != '\0');
buffer [offset - 1] = '\0';
return (buffer);
}
2023-09-17 14:28:03 -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.");
2023-09-17 14:28:03 -04:00
descriptor = open (name, mode);
fatal_failure (descriptor == -1, "file_open: Failed to open file, function open returned invalid descriptor.");
return (descriptor);
}
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);
}
void file_read (int file, void * data, int size) {
fatal_failure (file == -1, "file_read: Failed to read from file, invalid descriptor.");
fatal_failure (data == null, "file_read: Failed to read from file, data is null pointer.");
2023-09-17 14:28:03 -04:00
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) {
fatal_failure (file == -1, "file_write: Failed to write to file, invalid descriptor.");
fatal_failure (data == null, "file_write: Failed to write to file, data is null pointer.");
2023-09-17 14:28:03 -04:00
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) {
fatal_failure (file == -1, "file_seek: Failed to seek in file, invalid descriptor.");
return ((int) lseek (file, 0, whence));
}
int file_size (char * name) {
int size = -1;
int file = -1;
2023-09-17 14:28:03 -04:00
file = file_open (name, O_RDONLY);
2023-09-17 14:28:03 -04:00
size = lseek (file, 0, SEEK_END);
fatal_failure (size == -1, "file_size: Failed to get size of file, invalid file size.");
file = file_close (file);
2023-09-17 14:28:03 -04:00
return (size);
}
int file_type (char * name) {
2024-02-23 12:40:30 -05:00
char * extensions [file_type_count] = {
".txt", ".asm", ".fasm", ".gasm", ".nasm", ".yasm", ".c", ".h", ".adb", ".ads", ".cpp", ".hpp",
2024-02-23 13:57:38 -05:00
".f90", ".sh", ".cfg", ".x"
2023-09-17 14:28:03 -04:00
};
int type = 0;
2024-02-23 12:40:30 -05:00
for (; * name != '.'; ++name);
2023-09-17 14:28:03 -04:00
for (type = 0; type != file_type_count; ++type) {
2024-02-23 12:40:30 -05:00
if (string_compare_limit (name, extensions [type], string_length (extensions [type]))) {
2023-09-17 14:28:03 -04:00
return (type);
}
}
return (-1);
}
void * file_record (char * name) {
int file = -1;
int size = -1;
char * data = null;
fatal_failure (name == null, "file_import: Failed to import file, name is null pointer.");
file = file_open (name, O_RDONLY);
size = file_size (name);
data = allocate (size);
file_read (file, data, size);
file = file_close (file);
return (data);
}
2023-09-17 14:28:03 -04:00
char * file_import (char * name) {
int file = -1;
int size = -1;
char * data = null;
2023-09-17 14:28:03 -04:00
fatal_failure (name == null, "file_import: Failed to import file, name is null pointer.");
2023-09-17 14:28:03 -04:00
file = file_open (name, O_RDONLY);
size = file_size (name) + 1;
2023-09-17 14:28:03 -04:00
data = allocate (size);
file_read (file, data, size - 1);
data [size - 1] = '\0';
file = file_close (file);
return (data);
}
void file_export (char * name, void * data) {
(void) name;
(void) data;
}
2023-09-05 14:22:23 -04:00
void file_list_import (char * name) {
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_active] = -1;
file_list_size [file_list_active] = -1;
file_list_name [file_list_active] = null;
file_list_data [file_list_active] = null;
2023-09-05 14:22:23 -04:00
file_list_name [file_list_active] = allocate (string_length (name) + 1);
2023-09-05 14:22:23 -04:00
(void) string_copy_limit (file_list_name [file_list_active], name, string_length (name) + 1);
2023-09-05 14:22:23 -04:00
file_list_mark [file_list_active] = open (name, O_RDWR);
2023-09-05 14:22:23 -04:00
fatal_failure (file_list_mark [file_list_active] == -1, "file_list_import: Failed to open file, function open returned invalid descriptor.");
2023-09-10 14:42:06 -04:00
file_list_size [file_list_active] = (int) lseek (file_list_mark [file_list_active], 0, SEEK_END) + 1;
2023-09-05 14:22:23 -04:00
(void) lseek (file_list_mark [file_list_active], 0, SEEK_SET);
2023-09-05 14:22:23 -04:00
file_list_data [file_list_active] = allocate (file_list_size [file_list_active]);
2023-09-05 14:22:23 -04:00
(void) read (file_list_mark [file_list_active], file_list_data [file_list_active], (unsigned long int) (file_list_size [file_list_active] - 1));
2023-09-05 14:22:23 -04:00
close (file_list_mark [file_list_active]);
2023-09-05 14:22:23 -04:00
file_list_data [file_list_active] [file_list_size [file_list_active] - 1] = '\0';
2023-09-05 14:22:23 -04:00
}
void file_list_export (char * name) {
fatal_failure (name == null, "file_list_export: Failed to export file, name is null pointer.");
2023-09-12 09:59:13 -04:00
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]);
}
2023-10-31 07:06:25 -04:00
void file_list_insert_character (char character, int position) {
int offset;
2023-10-31 07:06:25 -04:00
++file_list_size [file_list_active];
2023-11-01 09:06:02 -04:00
if (file_list_size [file_list_active] < string_length (file_list_data [file_list_active])) {
file_list_data [file_list_active] = reallocate (file_list_data [file_list_active], file_list_size [file_list_active]);
}
2023-11-01 09:06:02 -04:00
2023-10-31 07:06:25 -04:00
file_list_data [file_list_active] = reallocate (file_list_data [file_list_active], file_list_size [file_list_active]);
for (offset = file_list_size [file_list_active] - 1; offset != position; --offset) {
file_list_data [file_list_active] [offset] = file_list_data [file_list_active] [offset - 1];
}
file_list_data [file_list_active] [position] = character;
}
void file_list_remove_character (int position) {
int offset;
2023-10-31 07:06:25 -04:00
if (position == 0) {
return;
}
--file_list_size [file_list_active];
for (offset = position - 1; offset != file_list_size [file_list_active] - 1; ++offset) {
file_list_data [file_list_active] [offset] = file_list_data [file_list_active] [offset + 1];
}
2023-11-01 09:06:02 -04:00
file_list_data [file_list_active] [offset] = '\0';
2023-10-31 07:06:25 -04:00
}
2023-09-05 14:22:23 -04:00
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);
}
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 = "~!@#$%^&*()+{}|:\"<>?`-=[]\\;',./";
2023-10-03 10:35:13 -04:00
return (character_compare_array (character, symbols));
}
int character_is_visible (char character) {
2023-09-20 06:00:01 -04:00
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 == '_'));
}
2023-09-18 15:36:50 -04:00
int character_is_hexadecimal (char character) {
char * hexadecimals = "0123456789ABCDEF";
2023-10-03 10:35:13 -04:00
return (character_compare_array (character, hexadecimals));
2023-09-18 15:36:50 -04:00
}
2023-10-03 10:35:13 -04:00
int character_compare_array (char character, char * character_array) {
int i = 0;
do {
if (character == character_array [i]) {
return (true);
}
2023-10-03 10:35:13 -04:00
} while (++i != string_length (character_array));
return (false);
}
2023-09-05 12:29:48 -04:00
2023-11-03 08:25:15 -04:00
int character_count (char * string, char this, int from, int to, char stop) {
2023-10-31 07:06:25 -04:00
int count;
2023-11-18 17:33:07 -05:00
for (count = 0; (from != to) && (string [from] != stop); from += ((to < from) ? -1 : 1)) {
2023-11-03 08:25:15 -04:00
count += (int) ((string [from] == this) || (this == '\0'));
}
2023-10-31 07:06:25 -04:00
return (count);
}
2023-08-25 16:57:12 -04:00
int string_length (char * string) {
2023-10-19 17:10:50 -04:00
int length;
2023-08-25 16:57:12 -04:00
if (string == null) {
2023-10-01 06:06:40 -04:00
return (0);
}
2023-08-25 16:57:12 -04:00
for (length = 0; string [length] != '\0'; ++length);
return (length);
}
2023-10-19 17:10:50 -04:00
char * string_reverse_limit (char * string, int limit) {
int i;
2023-08-25 16:57:12 -04:00
fatal_failure (string == null, "string_reverse: String is null pointer.");
2023-08-25 16:57:12 -04:00
for (i = 0; i < limit / 2; ++i) {
char temporary = string [i];
string [i] = string [limit - 1 - i];
string [limit - 1 - i] = temporary;
2023-08-25 16:57:12 -04:00
}
2023-10-19 17:10:50 -04:00
return (string);
}
char * string_reverse (char * string) {
return (string_reverse_limit (string, string_length (string)));
2023-08-25 16:57:12 -04:00
}
2023-10-19 17:10:50 -04:00
char * string_delete (char * string, int length) {
2023-09-05 14:22:23 -04:00
int i;
if ((string == null) || (length <= 0)) {
2023-10-19 17:10:50 -04:00
return (string);
2023-10-01 06:06:40 -04:00
}
2023-09-05 14:22:23 -04:00
for (i = 0; i != length; ++i) {
string [i] = '\0';
}
2023-10-19 17:10:50 -04:00
return (string);
2023-09-05 14:22:23 -04:00
}
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
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 (false);
2023-08-25 16:57:12 -04:00
}
}
return (true);
2023-08-25 16:57:12 -04:00
}
2023-10-19 17:10:50 -04:00
char * string_copy (char * string_0, char * string_1) {
2023-10-01 06:06:40 -04:00
int i = 0;
2023-08-25 16:57:12 -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
2023-10-01 06:06:40 -04:00
for (i = 0; i != string_length (string_1) + 1; ++i) {
2023-08-25 16:57:12 -04:00
string_0 [i] = string_1 [i];
}
2023-10-19 17:10:50 -04:00
return (string_0);
2023-08-25 16:57:12 -04:00
}
2023-08-28 09:02:20 -04:00
2023-10-19 17:10:50 -04:00
char * string_concatenate (char * string_0, char * string_1) {
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-10-01 06:06:40 -04:00
string_0 += string_length (string_0);
2023-08-25 16:57:12 -04:00
2023-10-01 06:06:40 -04:00
while (* string_1 != '\0') {
* string_0++ = * string_1++;
/*++string_0;
++string_1;*/
2023-08-28 09:02:20 -04:00
}
2023-10-01 06:06:40 -04:00
* string_0 = '\0';
2023-10-19 17:10:50 -04:00
return (string_0);
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
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 (false);
2023-08-28 09:02:20 -04:00
}
}
2023-08-25 16:57:12 -04:00
return (true);
2023-08-25 16:57:12 -04:00
}
2023-10-19 17:10:50 -04:00
char * 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
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) {
2023-10-19 17:10:50 -04:00
return (string_0);
2023-08-28 09:02:20 -04:00
}
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-10-19 17:10:50 -04:00
return (string_0);
2023-08-25 16:57:12 -04:00
}
2023-10-19 17:10:50 -04:00
char * string_concatenate_limit (char * string_0, char * string_1, int limit) {
2023-08-28 09:02:20 -04:00
int i = 0;
int length_0 = 0;
int length_1 = 0;
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) {
2023-10-19 17:10:50 -04:00
return (string_0);
2023-08-28 09:02:20 -04:00
}
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-10-19 17:10:50 -04:00
return (string_0);
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.");
2023-09-10 14:42:06 -04:00
for (i = 0; string [i] != '\0'; ++i) {
if ((string [i] == ' ') || (string [i] == '\t') || (string [i] == '\n')) {
string [i] = '\0';
++count;
}
}
return (++count);
}
2023-10-29 06:57:18 -04:00
char * string_realign (char * string, int amount, char character) {
int offset, length;
length = string_length (string);
for (offset = 0; offset != length; ++offset) {
string [amount - offset - 1] = string [length - offset - 1];
}
for (offset = 0; offset != amount - length; ++offset) {
string [offset] = character;
}
string [amount] = '\0';
return (string);
}
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.");
2023-09-12 10:15:13 -04:00
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.");
2023-09-12 10:15:13 -04:00
if (length <= 0) {
2023-10-01 06:06:40 -04:00
return (-1);
2023-09-12 10:15:13 -04:00
}
for (i = 0; (cast_0 [i] != '\0') && (cast_1 [i] != '\0'); ++i) {
if (cast_0 [i] != cast_1 [i]) {
return (false);
2023-09-12 10:15:13 -04:00
}
}
return (true);
2023-09-12 10:15:13 -04:00
}
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.");
2023-09-12 10:15:13 -04:00
if (length <= 0) {
return;
}
for (i = 0; i != length; ++i) {
cast_0 [i] = cast_1 [i];
}
}
void terminal_clear (void) {
2023-09-20 06:27:59 -04:00
echo ("\033[2J\033[H");
}
2023-09-20 06:27:59 -04:00
void terminal_colour (int colour, int effect) {
char format [8] = "\033[ ;3 m";
format [2] = (char) (effect % effect_count) + '0';
format [5] = (char) (colour % colour_count) + '0';
2023-09-20 06:27:59 -04:00
echo (format);
}
void terminal_cancel (void) {
echo ("\033[0m");
}
void terminal_show_cursor (int show) {
if (show != 0) {
2023-09-20 06:27:59 -04:00
echo ("\033[?25h");
} else {
2023-09-20 06:27:59 -04:00
echo ("\033[?25l");
}
}
2023-09-18 15:36:50 -04:00
int encode_byte (char * byte) {
int encode = 0;
fatal_failure (character_is_hexadecimal (byte [0]) == 0, "encode_byte: Upper byte character is not hexadecimal digit.");
fatal_failure (character_is_hexadecimal (byte [1]) == 0, "encode_byte: Lower byte character is not hexadecimal digit.");
encode = ((byte [0] - (character_is_digit (byte [0]) ? ('0') : ('A' - 10))) << 4) | (byte [1] - (character_is_digit (byte [1]) ? ('0') : ('A' - 10)));
return (encode);
}
char * decode_byte (int byte) {
2023-10-01 06:06:40 -04:00
char * decode = " ";
fatal_failure ((byte <= -1) || (byte >= 256), "decode_byte: Byte is out of range.");
decode [0] = byte / 16;
decode [1] = byte % 16;
2023-09-18 15:36:50 -04:00
return (decode);
}
char * number_to_string (int number) {
int i, sign;
static char string [32];
string_delete (string, 32);
if (number == 0) {
string [0] = '0';
string [1] = '\0';
return (string);
}
2023-10-19 17:10:50 -04:00
if (number < 0) {
number *= -1;
sign = 1;
} else {
sign = 0;
2023-10-19 17:10:50 -04:00
}
for (i = (string [0] == '-'); number != 0; ++i) {
string [i] = (char) (number % 10) + '0';
number /= 10;
}
if (sign != 0) {
string [i] = '-';
++i;
}
2023-10-19 17:10:50 -04:00
string [i] = '\0';
string_reverse (string);
return (string);
2023-10-19 17:10:50 -04:00
}
char * format_to_string (int number, int sign, int base, int amount, char character) {
int i;
static char string [32];
string_delete (string, 32);
if (number == 0) {
string [0] = '0';
string [1] = '\0';
string_realign (string, amount, character);
return (string);
}
if (number < 0) {
number *= -1;
}
for (i = (string [0] == '-'); number != 0; ++i) {
string [i] = "0123456789ABCDEF" [number % base];
number /= base;
}
if (sign != 0) {
string [i] = '-';
++i;
}
string [i] = '\0';
string_reverse (string);
string_realign (string, amount, character);
return (string);
}
2024-06-16 05:38:02 -04:00
void print (char * format, ...) {
va_list list;
va_start (list, format);
for (; * format != character_null; ++format) {
switch (* format) {
case '%': {
int integer;
char * string;
++format;
switch (* format) {
case '%': out ("%", 1); break;
case 'i': integer = va_arg (list, int); string = number_to_string (integer); out (string, string_length (string)); break;
case 's': string = va_arg (list, char *); out (string, string_length (string)); break;
default: out ("?", 1); break;
}
} break;
case '/': {
++format;
switch (* format) {
case '/': out ("/", 1); break;
case '0': terminal_colour (colour_grey, effect_bold); break;
case '1': terminal_colour (colour_red, effect_bold); break;
case '2': terminal_colour (colour_green, effect_bold); break;
case '3': terminal_colour (colour_yellow, effect_bold); break;
case '4': terminal_colour (colour_blue, effect_bold); break;
case '5': terminal_colour (colour_pink, effect_bold); break;
case '6': terminal_colour (colour_cyan, effect_bold); break;
case '7': terminal_colour (colour_white, effect_bold); break;
case '-': terminal_cancel (); break;
default: out ("?", 1); break;
}
} break;
default: {
out (format, 1);
} break;
}
}
va_end (list);
}
2023-08-25 16:57:12 -04:00
#endif