Updated few functions, lots untested...

This commit is contained in:
Ognjen Milan Robovic 2023-10-28 13:50:40 -04:00
parent 1b114e9820
commit 9d8d83d38f
2 changed files with 55 additions and 18 deletions

View File

@ -338,16 +338,18 @@ int file_seek (int file, int whence) {
return ((int) lseek (file, 0, whence)); return ((int) lseek (file, 0, whence));
} }
int file_size (int file) { int file_size (char * name) {
int size = 0; int size = -1;
int file = -1;
fatal_failure (file == -1, "file_size: Failed to get size of file, invalid descriptor."); file = file_open (name, O_RDONLY);
size = lseek (file, 0, SEEK_END); 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."); fatal_failure (size == -1, "file_size: Failed to get size of file, invalid file size.");
file = file_close (file);
return (size); return (size);
} }
@ -372,16 +374,33 @@ int file_type (char * name) {
return (-1); 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);
}
char * file_import (char * name) { char * file_import (char * name) {
int file = -1; int file = -1;
int size = -1; int size = -1;
char * data = NULL; char * data = NULL;
fatal_failure (name == NULL, "file_import: Failed to import file, name is null pointer."); fatal_failure (name == NULL, "file_import: Failed to import file, name is null pointer.");
fatal_failure (data != NULL, "file_import: Failed to import file, data is not null pointer.");
file = file_open (name, O_RDONLY); file = file_open (name, O_RDONLY);
size = file_size (file) + 1; size = file_size (name) + 1;
data = allocate (size); data = allocate (size);
file_read (file, data, size - 1); file_read (file, data, size - 1);
@ -537,18 +556,17 @@ char * string_reverse_limit (char * string, int limit) {
fatal_failure (string == NULL, "string_reverse: String is null pointer."); fatal_failure (string == NULL, "string_reverse: String is null pointer.");
for (i = 0; i != limit / 2; ++i) { for (i = 0; i < limit / 2; ++i) {
char reverse_0 = string [limit - i]; char temporary = string [i];
char reverse_1 = string [i]; string [i] = string [limit - 1 - i];
string [i] = reverse_0; string [limit - 1 - i] = temporary;
string [limit - i] = reverse_1;
} }
return (string); return (string);
} }
char * string_reverse (char * string) { char * string_reverse (char * string) {
return (string_reverse_limit (string, string_length (string) - 1)); return (string_reverse_limit (string, string_length (string)));
} }
char * string_delete (char * string, int length) { char * string_delete (char * string, int length) {
@ -783,13 +801,24 @@ char * decode_byte (int byte) {
return (decode); return (decode);
} }
void number_to_string (int number, char * string) { char * number_to_string (int number) {
int i; int i, sign;
static char string [32];
string_delete (string, 32);
if (number == 0) {
string [0] = '0';
string [1] = '\0';
return (string);
}
if (number < 0) { if (number < 0) {
string [0] = '-';
++i;
number *= -1; number *= -1;
sign = 1;
} else {
sign = 0;
} }
for (i = (string [0] == '-'); number != 0; ++i) { for (i = (string [0] == '-'); number != 0; ++i) {
@ -797,9 +826,16 @@ void number_to_string (int number, char * string) {
number /= 10; number /= 10;
} }
if (sign != 0) {
string [i] = '-';
++i;
}
string [i] = '\0'; string [i] = '\0';
string_reverse (string); string_reverse (string);
return (string);
} }
#endif #endif

View File

@ -80,8 +80,9 @@ extern int file_close (int file);
extern void file_read (int file, void * data, int size); extern void file_read (int file, void * data, int size);
extern void file_write (int file, void * data, int size); extern void file_write (int file, void * data, int size);
extern int file_seek (int file, int whence); extern int file_seek (int file, int whence);
extern int file_size (int file); extern int file_size (char * name);
extern int file_type (char * name); extern int file_type (char * name);
extern void * file_record (char * name);
extern char * file_import (char * name); extern char * file_import (char * name);
extern void file_export (char * name, void * data); extern void file_export (char * name, void * data);
@ -133,6 +134,6 @@ extern void terminal_show_cursor (int show);
extern int encode_byte (char * byte); extern int encode_byte (char * byte);
extern char * decode_byte (int byte); extern char * decode_byte (int byte);
extern void number_to_string (int number, char * string); extern char * number_to_string (int number);
#endif #endif