2023-11-07 10:14:07 -05:00
|
|
|
/*
|
|
|
|
Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
|
|
|
|
|
|
|
|
Xhartae 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 CHAPTER_1_SOURCE
|
|
|
|
#define CHAPTER_1_SOURCE
|
|
|
|
|
2023-11-09 09:41:53 -05:00
|
|
|
#include "chapter_1.h"
|
|
|
|
|
2023-11-09 07:33:47 -05:00
|
|
|
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) {
|
2023-11-09 09:41:53 -05:00
|
|
|
return ((int) ((character == ' ') || (character == CHARACTER_TAB_HORIZONTAL) || (character == CHARACTER_CARRIAGE_RETURN) || (character == CHARACTER_LINE_FEED)));
|
|
|
|
// If you like smaller line length limit, you can align it like this:
|
|
|
|
// return ((character == ' ')
|
|
|
|
// || (character == CHARACTER_TAB_HORIZONTAL)
|
|
|
|
// || (character == CHARACTER_CARRIAGE_RETURN)
|
|
|
|
// || (character == CHARACTER_LINE_FEED));
|
|
|
|
// Or:
|
|
|
|
// return ((character == ' ') ||
|
|
|
|
// (character == CHARACTER_TAB_HORIZONTAL) ||
|
|
|
|
// (character == CHARACTER_CARRIAGE_RETURN) ||
|
|
|
|
// (character == CHARACTER_LINE_FEED));
|
|
|
|
// Or even use literal characters:
|
|
|
|
// return ((character == ' ') ||
|
|
|
|
// (character == '\t') ||
|
|
|
|
// (character == '\r') ||
|
|
|
|
// (character == '\n'));
|
2023-11-09 07:33:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int character_is_alpha (char character) {
|
|
|
|
return ((character_is_uppercase (character) != 0) || (character_is_lowercase (character) != 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
int character_is_symbol (char character) {
|
|
|
|
return (character_compare_array (character, "~!@#$%^&*()+{}|:\"<>?`-=[]\\;',./"));
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2023-11-09 13:22:59 -05:00
|
|
|
return ((int) (character == CHARACTER_ESCAPE));
|
2023-11-09 07:33:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int character_is_underscore (char character) {
|
|
|
|
return ((int) (character == '_'));
|
|
|
|
}
|
|
|
|
|
|
|
|
int character_is_hexadecimal (char character) {
|
|
|
|
return (character_compare_array (character, "0123456789ABCDEF"));
|
|
|
|
}
|
|
|
|
|
|
|
|
int character_compare_array (char character, char * character_array) {
|
|
|
|
int i;
|
|
|
|
|
2023-11-09 09:41:53 -05:00
|
|
|
for (i = 0; i != string_length (character_array); ++i) {
|
2023-11-09 07:33:47 -05:00
|
|
|
if (character == character_array [i]) {
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2023-11-09 13:22:59 -05: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);
|
|
|
|
|
|
|
|
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.");
|
|
|
|
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.");
|
|
|
|
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;
|
|
|
|
|
|
|
|
file = file_open (name, O_RDONLY);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
return (size);
|
|
|
|
}
|
|
|
|
|
|
|
|
int file_type (char * name) {
|
|
|
|
char * file_type_data [FILE_TYPE_COUNT] = {
|
|
|
|
".txt", ".s", ".fasm", ".gasm", ".nasm", ".yasm", ".c", ".h",
|
|
|
|
".adb", ".ads", ".cpp", ".hpp"
|
|
|
|
};
|
|
|
|
|
|
|
|
int type = 0;
|
|
|
|
|
|
|
|
while (* name != '.') {
|
|
|
|
++name;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (type = 0; type != FILE_TYPE_COUNT; ++type) {
|
|
|
|
if (string_compare (name, file_type_data [type]) != 0) {
|
|
|
|
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-11-07 10:14:07 -05:00
|
|
|
#endif
|