-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major Features + Implemented a set of helper functions for POSIX compatible IO and accompanying set of tests. Minor Features + New pressio_data_num_elements to get the number of elements in a pressio_data buffer Bug Fixes + previous pressio_data_has_data returned the opposite value of what it should have
- Loading branch information
Showing
7 changed files
with
373 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include <stdio.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
struct pressio_data; | ||
|
||
/** \file | ||
* \brief IO functions for POSIX compatible systems | ||
* | ||
* NOTE attempting to read files written by different machines has undefined behavior!! | ||
*/ | ||
|
||
#ifndef PRESSIO_POSIX_IO | ||
#define PRESSIO_POSIX_IO | ||
|
||
/** read in a file from a POSIX FILE pointer | ||
* | ||
* NOTE attempting to read files written by different machines has undefined behavior!! | ||
* | ||
* \param[in,out] dims description of the dimension of the data if it known or NULL. | ||
* If dims is NULL, the remainder of the file will be read and the size of the resulting pointer will be a 1d pressio_byte_dtype of appropriate length | ||
* If dims is not null, The user SHOULD assume that the memory pointed to by this pointer has been "moved" in a C++11 sense and the user MUST not rely on its contents. | ||
* The implementation MAY return this pointer and reuse the underlying space if pressio_data_has_data(dims) returns true. | ||
* \param[in,out] in_file an file open for reading seeked to the beginning of the data to read in. | ||
* \returns a pointer to a (possibly new) pressio data structure. | ||
* | ||
*/ | ||
struct pressio_data* pressio_io_data_fread(struct pressio_data* dims, FILE* in_file); | ||
|
||
|
||
/** read in a file from a POSIX file descriptor | ||
* | ||
* NOTE attempting to read files written by different machines has undefined behavior!! | ||
* | ||
* \param[in,out] dims description of the dimension of the data if it known or NULL. | ||
* If dims is NULL, the remainder of the file will be read and the size of the resulting pointer will be a 1d pressio_byte_dtype of appropriate length | ||
* If dims is not null, The user SHOULD assume that the memory pointed to by this pointer has been "moved" in a C++11 sense and the user MUST not rely on its contents. | ||
* The implementation MAY return this pointer and reuse the underlying space if pressio_data_has_data(ptr) returns true. | ||
* \param[in,out] in_file an file open for reading seeked to the beginning of the data to read in. If dims is not null, only pressio_data_get_bytes(dims) bytes are read. | ||
* \returns a pointer to a (possibly new) pressio data structure. | ||
* | ||
*/ | ||
struct pressio_data* pressio_io_data_read(struct pressio_data* dims, int in_filedes); | ||
|
||
/** read in a file at a specifed location on the file-system | ||
* | ||
* NOTE attempting to read files written by different machines has undefined behavior!! | ||
* | ||
* \param[in,out] dims description of the dimension of the data if it known or NULL. | ||
* If dims is NULL, the remainder of the file will be read and the size of the resulting pointer will be a 1d pressio_byte_dtype of appropriate length | ||
* If dims is not null, The user SHOULD assume that the memory pointed to by this pointer has been "moved" in a C++11 sense and the user MUST not rely on its contents. | ||
* The implementation MAY return this pointer and reuse the underlying space if pressio_data_has_data(dims) returns true. | ||
* \param[in,out] in_file an file open for reading seeked to the beginning of the data to read in. | ||
* \returns a pointer to a (possibly new) pressio data structure. | ||
* | ||
*/ | ||
struct pressio_data* pressio_io_data_path_read(struct pressio_data* dims, const char* out_file); | ||
|
||
/** write in a file to the specified POSIX FILE pointer | ||
* | ||
* \param[in] data the data to be written. | ||
* \param[in,out] out_file | ||
* \returns the number of bytes written | ||
* | ||
*/ | ||
size_t pressio_io_data_fwrite(struct pressio_data* data, FILE* out_file); | ||
|
||
/** write in a file to the specified POSIX file descriptor | ||
* | ||
* \param[in] data the data to be written. | ||
* \param[in,out] out_filedes the file descriptor to write to | ||
* \returns the number of bytes written | ||
* | ||
*/ | ||
size_t pressio_io_data_write(struct pressio_data* data, int out_filedes); | ||
|
||
/** write in a file to the specified path on the file-system | ||
* | ||
* \param[in] data the data to be written. | ||
* \param[in,out] path the path to write to | ||
* \returns the number of bytes written | ||
* | ||
*/ | ||
size_t pressio_io_data_path_write(struct pressio_data* data, const char* path); | ||
|
||
|
||
#endif /*PRESSIO_POSIX_IO*/ | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#include <sys/stat.h> | ||
#include <unistd.h> | ||
#include <vector> | ||
#include "pressio_data.h" | ||
#include "libpressio_ext/io/posix.h" | ||
|
||
namespace { | ||
std::vector<size_t> get_all_dimentions(struct pressio_data const* data) { | ||
std::vector<size_t> dims; | ||
if(data) { | ||
for (size_t i = 0; i < pressio_data_num_dimentions(data); ++i) { | ||
dims.emplace_back(pressio_data_get_dimention(data, i)); | ||
} | ||
} | ||
return dims; | ||
} | ||
} | ||
|
||
extern "C" { | ||
|
||
struct pressio_data* pressio_io_data_read(struct pressio_data* dims, int in_filedes) { | ||
pressio_data* ret; | ||
if(dims != nullptr) { | ||
if(pressio_data_has_data(dims)) { | ||
//re-use the buffer provided by dims | ||
ret = dims; | ||
} else { | ||
//create a new buffer of the appropriate size | ||
auto dtype = pressio_data_dtype(dims); | ||
auto dims_v = get_all_dimentions(dims); | ||
pressio_data_free(dims); | ||
ret = pressio_data_new_owning(dtype, dims_v.size(), dims_v.data()); | ||
} | ||
} else { | ||
struct stat statbuf; | ||
if(fstat(in_filedes, &statbuf)) { | ||
return nullptr; | ||
} | ||
size_t size = static_cast<size_t>(statbuf.st_size); | ||
ret = pressio_data_new_owning(pressio_byte_dtype, 1, &size); | ||
} | ||
read(in_filedes, pressio_data_ptr(ret, nullptr), pressio_data_get_bytes(ret)); | ||
return ret; | ||
} | ||
|
||
struct pressio_data* pressio_io_data_fread(struct pressio_data* dims, FILE* in_file) { | ||
return pressio_io_data_read(dims, fileno(in_file)); | ||
} | ||
|
||
|
||
struct pressio_data* pressio_io_data_path_read(struct pressio_data* dims, const char* path) { | ||
FILE* in_file = fopen(path, "r"); | ||
if(in_file != nullptr) { | ||
auto ret = pressio_io_data_fread(dims, in_file); | ||
fclose(in_file); | ||
return ret; | ||
} else { | ||
return nullptr; | ||
} | ||
} | ||
|
||
|
||
size_t pressio_io_data_fwrite(struct pressio_data* data, FILE* out_file) { | ||
|
||
return fwrite(pressio_data_ptr(data, nullptr), | ||
pressio_dtype_size(pressio_data_dtype(data)), | ||
pressio_data_num_elements(data), | ||
out_file | ||
); | ||
} | ||
|
||
size_t pressio_io_data_write(struct pressio_data* data, int out_filedes) { | ||
return write(out_filedes, | ||
pressio_data_ptr(data, nullptr), | ||
pressio_data_get_bytes(data) | ||
); | ||
} | ||
|
||
size_t pressio_io_path_path_write(struct pressio_data* data, const char* path) { | ||
FILE* out_file = fopen(path, "w"); | ||
if(out_file != nullptr) { | ||
auto ret = pressio_io_data_fwrite(data, out_file); | ||
fclose(out_file); | ||
return ret; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.