Skip to content

Commit

Permalink
text: allow to specify how the file content should be loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
martanne committed May 30, 2018
1 parent cbe37e6 commit d9b8f7b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
8 changes: 5 additions & 3 deletions text.c
Original file line number Diff line number Diff line change
Expand Up @@ -1120,9 +1120,11 @@ ssize_t text_write_range(Text *txt, Filerange *range, int fd) {
return size - rem;
}

/* load the given file as starting point for further editing operations.
* to start with an empty document, pass NULL as filename. */
Text *text_load(const char *filename) {
return text_load_method(filename, TEXT_LOAD_AUTO);
}

Text *text_load_method(const char *filename, enum TextLoadMethod method) {
int fd = -1;
size_t size = 0;
Text *txt = calloc(1, sizeof *txt);
Expand All @@ -1144,7 +1146,7 @@ Text *text_load(const char *filename) {
// XXX: use lseek(fd, 0, SEEK_END); instead?
size = txt->info.st_size;
if (size > 0) {
if (size < BLOCK_MMAP_SIZE)
if (method == TEXT_LOAD_READ || (method == TEXT_LOAD_AUTO && size < BLOCK_MMAP_SIZE))
txt->block = block_read(txt, size, fd);
else
txt->block = block_mmap(txt, size, fd, 0);
Expand Down
40 changes: 39 additions & 1 deletion text.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,48 @@ typedef struct {
* @defgroup load
* @{
*/
/**
* Method used to load existing file content.
*/
enum TextLoadMethod {
/** Automatically chose best option. */
TEXT_LOAD_AUTO,
/**
* Read file content and copy it to an in-memory buffer.
* Subsequent changes to the underlying file will have no
* effect on this text instance.
*
* @rst
* .. note:: Load time is linear in the file size.
* @endrst
*/
TEXT_LOAD_READ,
/**
* Memory map the the file from disk. Use file system / virtual memory
* subsystem as a caching layer.
* @rst
* .. note:: Load time is (almost) independent of the file size.
* .. warning:: Inplace modifications of the underlying file
* will be reflected in the current text content.
* In particular, truncatenation will raise ``SIGBUS``
* and result in data loss.
* @endrst
*/
TEXT_LOAD_MMAP,
};
/**
* Create a text instance populated with the given file content.
*
* @rst
* .. note:: Equivalent to ``text_load_method(filename, TEXT_LOAD_AUTO)``.
* @endrst
*/
Text *text_load(const char *filename);
/**
* Create a text instance populated with the given file content.
*
* @param filename The name of the file to load, if ``NULL`` an empty text is created.
* @param method How the file content should be loaded.
* @return The new Text object or ``NULL`` in case of an error.
* @rst
* .. note:: When attempting to load a non-regular file, ``errno`` will be set to:
Expand All @@ -70,7 +108,7 @@ typedef struct {
* - ``ENOTSUP`` otherwise.
* @endrst
*/
Text *text_load(const char *filename);
Text *text_load_method(const char *filename, enum TextLoadMethod);
/** Release all ressources associated with this text instance. */
void text_free(Text*);
/**
Expand Down

0 comments on commit d9b8f7b

Please sign in to comment.