Updating things I hate...

This commit is contained in:
Ognjen Milan Robovic 2023-09-05 12:29:48 -04:00
parent bcf3b17e83
commit 6934f14936
2 changed files with 146 additions and 3 deletions

View File

@ -11,19 +11,34 @@
#include "xtandard.h"
int argument_count = 0;
char * * argument_nick = NULL;
char * * argument_name = NULL;
void (* * argument_function) (void) = NULL;
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 * data) {
out (data, string_length (data));
}
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);
echo ("\033[1;31m[!]\033[0m ");
echo (message);
echo ("\n");
exit (EXIT_FAILURE);
}
@ -117,6 +132,113 @@ void * record (void) {
return (buffer);
}
void argument_define (char * nick, char * name, void (* function) (void)) {
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;
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);
}
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);
fatal_failure (descriptor == -1, "file_open: Failed to open file, standard library failure.");
return (descriptor);
}
void file_read (int file, void * data, int size) {
fatal_failure (file == -1, "file_read: Failed to read from file, invalid file descriptor.");
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) {
fatal_failure (file == -1, "file_write: Failed to write to file, invalid file descriptor.");
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) {
fatal_failure (file == -1, "file_seek: Failed to seek in file, invalid file descriptor.");
return ((int) lseek (file, 0, whence));
}
int file_size (int file) {
int size = 0;
fatal_failure (file == -1, "file_size: Failed to get size of file, invalid file descriptor.");
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);
}
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, standard library failure.");
return (-1);
}
void file_import (char * name, void * data) {
(void) name;
(void) data;
}
void file_export (char * name, void * data) {
(void) name;
(void) data;
}
int string_length (char * string) {
int length = 0;

View File

@ -11,6 +11,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
enum {
EFFECT_NORMAL,
@ -34,9 +35,16 @@ enum {
COLOUR_WHITE
};
extern int argument_count;
extern char * * argument_nick;
extern char * * argument_name;
extern void (* * argument_function) (void);
extern void in (void *, int);
extern void out (void *, int);
extern void echo (char *);
extern void fatal_failure (int, char *);
extern void * allocate (int);
@ -46,6 +54,19 @@ extern void * memorize (int);
extern void * record (void);
extern void argument_define (char *, char *, void (*) (void));
extern void argument_select (int, char * *);
extern void argument_delete (void);
extern int file_open (char *, int);
extern void file_read (int, void *, int);
extern void file_write (int, void *, int);
extern int file_seek (int, int);
extern int file_size (int);
extern int file_close (int);
extern void file_import (char *, void *);
extern void file_export (char *, void *);
extern int string_length (char *);
extern void string_delete (char *);
extern void string_reverse (char *);