Work in progress

Added test for temp files, getter for filename, and wrappers for fwrite, fprintf, and fgets
This commit is contained in:
Michael Tryby
2019-04-01 18:05:02 -04:00
parent a0b08921fc
commit 2c6d4ca74d
4 changed files with 75 additions and 8 deletions

View File

@@ -21,6 +21,7 @@
#include <stdlib.h>
#endif
#include <stdarg.h>
#include <string.h>
#include "filemanager.h"
@@ -50,6 +51,15 @@ void delete_file_manager(file_handle_t *file_handle) {
}
void get_filename(file_handle_t *file_handle, char **filename)
{
char *temp = (char*) malloc((FILE_MAXNAME)*sizeof(char));
strncpy(temp, file_handle->filename, FILE_MAXNAME);
*filename = temp;
}
int open_file(file_handle_t *file_handle, const char *filename, const char *file_mode) {
int error = 0;
@@ -89,11 +99,37 @@ F_OFF tell_file(file_handle_t *file_handle)
return FTELL64(file_handle->file);
}
// Read and write to a binary file
size_t read_file(void *ptr, size_t size, size_t nmemb, file_handle_t *file_handle)
{
return fread(ptr, size, nmemb, file_handle->file);
}
size_t write_file(const void * ptr, size_t size, size_t count, file_handle_t *file_handle)
{
return fwrite(ptr, size, count, file_handle->file);
}
// print and get from a text file
int printf_file(file_handle_t *file_handle, const char *format, ... )
{
int error = 0;
va_list args;
va_start(args, format);
error = vfprintf(file_handle->file, format, args);
va_end(args);
return error;
}
int gets_file(char *str, int num, file_handle_t *file_handle)
{
fgets(str, num, file_handle->file);
return 0;
}
int close_file(file_handle_t *file_handle) {
int error = 0;

View File

@@ -16,6 +16,7 @@
#include <stdio.h>
#include <stdarg.h>
// F_OFF Must be a 8 byte / 64 bit integer for large file support
@@ -41,14 +42,27 @@ file_handle_t *create_file_manager();
void delete_file_manager(file_handle_t *file_handle);
void get_filename(file_handle_t *file_handle, char **filename);
int open_file(file_handle_t *file_handle, const char *filename, const char *file_mode);
int seek_file(file_handle_t *file_handle, F_OFF offset, int whence);
F_OFF tell_file(file_handle_t *file_handle);
// Functions for working with binary files
size_t read_file(void *ptr, size_t size, size_t nmemb, file_handle_t *file_handle);
size_t write_file(const void *ptr, size_t size, size_t count, file_handle_t *file_handle);
// Functions for working with text files
int printf_file(file_handle_t *file_handle, const char *format, ... );
int gets_file(char *str, int num, file_handle_t *file_handle);
int close_file(file_handle_t *file_handle);