瀏覽代碼

More robust error checking...

master
父節點
當前提交
d3587d6351
共有 2 個文件被更改,包括 41 次插入23 次删除
  1. +36
    -20
      xtandard.c
  2. +5
    -3
      xtandard.h

+ 36
- 20
xtandard.c 查看文件

@@ -54,30 +54,36 @@ void fatal_failure (int condition, char * message) {
void * allocate (int size) {
char * data = NULL;

fatal_failure (size == 0, "allocate: Failed to allocate memory, size is zero.");

data = calloc ((unsigned long int) size, sizeof (* data));

fatal_failure (data == NULL, "allocate: Oh no...");
fatal_failure (data == NULL, "allocate: Failed to allocate memory, function calloc returned null pointer.");

return ((void *) data);
}

void * reallocate (void * data, int size) {
fatal_failure (size == 0, "reallocate: Failed to reallocate memory, size is zero.");

data = realloc (data, (unsigned long int) size);

fatal_failure (data == NULL, "reallocate: Oh no...");
fatal_failure (data == NULL, "reallocate: Failed to reallocate memory, function recalloc returned null pointer.");

/* Set new data to 0. */

return (data);
}

void * deallocate (void * data) {
fatal_failure (data == NULL, "deallocate: Oh no...");
fatal_failure (data == NULL, "deallocate: Failed to deallocate memory, data is null pointer.");

free (data);

return (NULL);
}

void * memorize (int size) {
void * memorize (int size) { /* I broke this for testing something out... */
static char * buffer = NULL;
char * points = NULL;
static int length = 0;
@@ -140,6 +146,10 @@ void * record (void) {
}

void argument_define (char * nick, char * name, void (* function) (void)) {
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.");

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));
@@ -158,6 +168,10 @@ void argument_define (char * nick, char * name, void (* function) (void)) {
void argument_select (int count, char * * array) {
int index_a, index_b;

if ((count == 1) || (array == NULL)) {
return;
}

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)
@@ -182,7 +196,7 @@ void argument_delete (void) {
}

void file_list_import (char * name) {
fatal_failure (name == NULL, "file_list_import: Failed to open file, name is null pointer.");
fatal_failure (name == NULL, "file_list_import: Failed to import file, name is null pointer.");

++file_list_count;

@@ -204,7 +218,7 @@ void file_list_import (char * name) {

file_list_mark [file_list_count - 1] = open (name, O_RDONLY);

fatal_failure (file_list_mark [file_list_count - 1] == -1, "file_list_import: Failed to open file, standard library failure.");
fatal_failure (file_list_mark [file_list_count - 1] == -1, "file_list_import: Failed to open file, function open returned invalid descriptor.");

file_list_size [file_list_count - 1] = (int) lseek (file_list_mark [file_list_count - 1], 0, SEEK_END) + 1;

@@ -220,6 +234,8 @@ void file_list_import (char * name) {
}

void file_list_export (char * name) {
fatal_failure (name == NULL, "file_list_export: Failed to export file, name is null pointer.");

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]);
@@ -248,13 +264,20 @@ int file_open (char * name, int mode) {

descriptor = open (name, mode);

fatal_failure (descriptor == -1, "file_open: Failed to open file, standard library failure.");
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 file descriptor.");
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.");
fatal_failure (size == 0, "file_read: Failed to read from file, size is zero.");

@@ -262,7 +285,7 @@ void file_read (int file, void * data, 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 (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.");
fatal_failure (size == 0, "file_write: Failed to write to file, size is zero.");

@@ -270,7 +293,7 @@ void file_write (int file, void * data, int size) {
}

int file_seek (int file, int whence) {
fatal_failure (file == -1, "file_seek: Failed to seek in file, invalid file descriptor.");
fatal_failure (file == -1, "file_seek: Failed to seek in file, invalid descriptor.");

return ((int) lseek (file, 0, whence));
}
@@ -278,7 +301,7 @@ int file_seek (int file, int whence) {
int file_size (int file) {
int size = 0;

fatal_failure (file == -1, "file_size: Failed to get size of file, invalid file descriptor.");
fatal_failure (file == -1, "file_size: Failed to get size of file, invalid descriptor.");

size = lseek (file, 0, SEEK_END);
(void) lseek (file, 0, SEEK_SET);
@@ -288,13 +311,6 @@ int file_size (int file) {
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;
@@ -514,8 +530,8 @@ void terminal_style (int effect, int colour) {
if ((effect == -1) || (colour == -1)) {
out ("\033[0m", 4);
} else {
format [2] = (char) effect + '0';
format [5] = (char) colour + '0';
format [2] = (char) (effect % EFFECT_COUNT) + '0';
format [5] = (char) (colour % COLOUR_COUNT) + '0';

out (format, 7);
}


+ 5
- 3
xtandard.h 查看文件

@@ -21,7 +21,8 @@ enum {
EFFECT_UNDERLINE,
EFFECT_BLINK,
EFFECT_UNDEFINED,
EFFECT_REVERSE
EFFECT_REVERSE,
EFFECT_COUNT
};

enum {
@@ -32,7 +33,8 @@ enum {
COLOUR_BLUE,
COLOUR_PINK,
COLOUR_CYAN,
COLOUR_WHITE
COLOUR_WHITE,
COLOUR_COUNT
};

extern int argument_count;
@@ -66,11 +68,11 @@ extern void argument_select (int, char * *);
extern void argument_delete (void);

extern int file_open (char *, int);
extern int file_close (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 *);



Loading…
取消
儲存