Work in progress
Added test for temp files, getter for filename, and wrappers for fwrite, fprintf, and fgets
This commit is contained in:
@@ -21,6 +21,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <stdarg.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "filemanager.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 open_file(file_handle_t *file_handle, const char *filename, const char *file_mode) {
|
||||||
int error = 0;
|
int error = 0;
|
||||||
|
|
||||||
@@ -89,11 +99,37 @@ F_OFF tell_file(file_handle_t *file_handle)
|
|||||||
return FTELL64(file_handle->file);
|
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)
|
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);
|
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 close_file(file_handle_t *file_handle) {
|
||||||
int error = 0;
|
int error = 0;
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
|
||||||
// F_OFF Must be a 8 byte / 64 bit integer for large file support
|
// 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 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 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);
|
int seek_file(file_handle_t *file_handle, F_OFF offset, int whence);
|
||||||
|
|
||||||
F_OFF tell_file(file_handle_t *file_handle);
|
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 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);
|
int close_file(file_handle_t *file_handle);
|
||||||
|
|
||||||
|
|||||||
@@ -7,17 +7,11 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
|||||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||||
|
|
||||||
|
|
||||||
set (test_source
|
add_executable(test_errormanager ./test_errormanager.cpp ../../src/util/errormanager.c)
|
||||||
./test_errormanager.cpp
|
|
||||||
../../src/util/errormanager.c
|
|
||||||
)
|
|
||||||
|
|
||||||
add_executable(test_errormanager ${test_source})
|
|
||||||
target_include_directories(test_errormanager PUBLIC ../../src/)
|
target_include_directories(test_errormanager PUBLIC ../../src/)
|
||||||
target_link_libraries(test_errormanager ${Boost_LIBRARIES})
|
target_link_libraries(test_errormanager ${Boost_LIBRARIES})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
add_executable(test_filemanager ./test_filemanager.cpp ../../src/util/filemanager.c)
|
add_executable(test_filemanager ./test_filemanager.cpp ../../src/util/filemanager.c)
|
||||||
target_include_directories(test_filemanager PUBLIC ../../src/)
|
target_include_directories(test_filemanager PUBLIC ../../src/)
|
||||||
target_link_libraries(test_filemanager ${Boost_LIBRARIES})
|
target_link_libraries(test_filemanager ${Boost_LIBRARIES})
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
#define BOOST_TEST_MODULE filemanager
|
#define BOOST_TEST_MODULE filemanager
|
||||||
|
|
||||||
#include <boost/test/unit_test.hpp>
|
#include <boost/test/unit_test.hpp>
|
||||||
|
#include <boost/filesystem.hpp>
|
||||||
|
|
||||||
#include "util/filemanager.h"
|
#include "util/filemanager.h"
|
||||||
|
|
||||||
@@ -21,6 +22,15 @@
|
|||||||
#define DATA_PATH_OUTPUT "./example1.out"
|
#define DATA_PATH_OUTPUT "./example1.out"
|
||||||
|
|
||||||
|
|
||||||
|
boost::test_tools::predicate_result check_string(std::string test, std::string ref)
|
||||||
|
{
|
||||||
|
if (ref.compare(test) == 0)
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE(test_filemanager)
|
BOOST_AUTO_TEST_SUITE(test_filemanager)
|
||||||
|
|
||||||
|
|
||||||
@@ -57,7 +67,7 @@ struct Fixture{
|
|||||||
file_handle = NULL;
|
file_handle = NULL;
|
||||||
|
|
||||||
file_handle = create_file_manager();
|
file_handle = create_file_manager();
|
||||||
open_file(file_handle, DATA_PATH_OUTPUT, "rb");
|
open_file(file_handle, NULL, "wt");
|
||||||
}
|
}
|
||||||
~Fixture() {
|
~Fixture() {
|
||||||
close_file(file_handle);
|
close_file(file_handle);
|
||||||
@@ -67,5 +77,18 @@ struct Fixture{
|
|||||||
file_handle_t *file_handle;
|
file_handle_t *file_handle;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
BOOST_FIXTURE_TEST_CASE(test_temp_file, Fixture)
|
||||||
|
{
|
||||||
|
char *filename;
|
||||||
|
|
||||||
|
printf_file(file_handle, "%s", "This is a test.");
|
||||||
|
|
||||||
|
get_filename(file_handle, &filename);
|
||||||
|
//BOOST_CHECK(check_string(filename, "./test_file.txt"));
|
||||||
|
|
||||||
|
BOOST_CHECK(boost::filesystem::exists(filename) == true);
|
||||||
|
|
||||||
|
free(filename);
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|||||||
Reference in New Issue
Block a user