Nearly finished day 3p1
This commit is contained in:
1 parent
0e305e4334
commit
f17cc04b49
13 files changed
+355
-39
No files matched your search
+25
-25
@@ -9,43 +9,43 @@
|
||||
#include <sys/stat.h>
|
||||
#include "fileLoader.h"
|
||||
|
||||
struct OpenFile
|
||||
struct OpenFile *load_file_to_mem(char *filename)
|
||||
{
|
||||
char *fileData;
|
||||
int fileSize;
|
||||
};
|
||||
|
||||
struct OpenFile load_file_to_mem(char *filename) {
|
||||
struct OpenFile file;
|
||||
struct OpenFile *file = NULL;
|
||||
file = (struct OpenFile *)
|
||||
malloc(sizeof(struct OpenFile));
|
||||
int fd = open(filename, O_RDONLY);
|
||||
|
||||
if (fd < 0) {
|
||||
printf("\n\"%s \" could not open", filename);
|
||||
exit(1);
|
||||
if (fd < 0)
|
||||
{
|
||||
printf("\n\"%s \" could not open", filename);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
struct stat statbuf;
|
||||
int err = fstat(fd, &statbuf);
|
||||
if (err < 0) {
|
||||
printf("\n\"%s \" could not open", filename);
|
||||
exit(2);
|
||||
if (err < 0)
|
||||
{
|
||||
printf("\n\"%s \" could not open", filename);
|
||||
exit(2);
|
||||
}
|
||||
|
||||
char *filecontents = mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
if (filecontents == MAP_FAILED) {
|
||||
printf("Mapping failed");
|
||||
exit(3);
|
||||
if (filecontents == MAP_FAILED)
|
||||
{
|
||||
printf("Mapping failed");
|
||||
exit(3);
|
||||
}
|
||||
file->fileSize = statbuf.st_size;
|
||||
file->fileData = malloc(statbuf.st_size);
|
||||
memcpy(file->fileData, filecontents, statbuf.st_size);
|
||||
|
||||
if (err != 0)
|
||||
{
|
||||
printf("Unmapping failed");
|
||||
exit(1);
|
||||
}
|
||||
file.fileSize = statbuf.st_size;
|
||||
file.fileData = malloc(file.fileSize);
|
||||
memcpy(file.fileData, filecontents, file.fileSize);
|
||||
|
||||
if (err != 0) {
|
||||
printf("Unmapping failed");
|
||||
exit(1);
|
||||
}
|
||||
close(fd);
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
+9
-4
@@ -1,6 +1,11 @@
|
||||
#ifndef HEADER_FILE
|
||||
#define HEADER_FILE
|
||||
struct OpenFile;
|
||||
char *load_file_to_mem(char *filename);
|
||||
#ifndef fileloader
|
||||
#define fileloader
|
||||
struct OpenFile
|
||||
{
|
||||
char *fileData;
|
||||
int fileSize;
|
||||
};
|
||||
|
||||
struct OpenFile *load_file_to_mem(char *filename);
|
||||
int unmap_file(char *ptr, char *filename);
|
||||
#endif
|
||||
@@ -0,0 +1,14 @@
|
||||
#include "strTools.h"
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
int char_to_int(char *str)
|
||||
{
|
||||
int result;
|
||||
char *tmp;
|
||||
result = strtol(str, &tmp, 10);
|
||||
printf("%i", errno);
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
#ifndef HEADER_FILE
|
||||
#define HEADER_FILE
|
||||
int char_to_int(char *str);
|
||||
#endif
|
||||
Reference in new issue
Block a user