3

My first thought here is to use a dynamic array, but I am looking for something better.

Currently I have the text files open into "chunks". Every word or group of spaces makes up a "chunk". Then I have a line number in this structure and a chunk number for ordering on a line.

typedef struct TextData {
    char* chunk;
    int   index, line;
}TDATA;

I am not sure if this is a good idea as this could lead to allocating to much dynamic memory and is prone to memory leaks. At least to my knowledge.

What I am looking for is a better way to implement this.

This is being used for a text editor/console combination program that is going to be made into a dynamic lib for reuse in other programs/games. A small finger print is a big part of this implementation.

Text is placed into a load buffer than a undo buffer tracks only changed chunks the last buffer is a display buffer that is used by an application or interface to display the unsaved edits of the complete file.

Needs to be thread safe as well.

9

1 Answer 1

4

Use the hardware - most OSes have a mechanism to map a large file into a 'window' of memory and the OS (and MMU) manages the mapping for you. As long as ytou have enough virtual address space, you can handle a file of any size as if it were all loaded into memory.

You'll want to look up Memory Mapped Files.

As for an undo feature, you can happily store changes to a list, and apply them to the file on save if you don't want to save them immediately and provide an undo feature to restore the original data.

5
  • 1
    How would memory mapped file and editing go together?
    – Murphy
    Mar 3, 2016 at 10:37
  • 1
    @Murphy you read the entire file and save the edited bits elsewhere, or you update the file directly and save the undo buffer elsewhere. Its no different to having it loaded into memory - you still need an undo or redo buffer to restore the right bit of edited file to its original state.
    – gbjbaanb
    Mar 3, 2016 at 10:57
  • is there a way to do this in a portable way? Like a lib that takes care of this between windows/linux/mac?
    – Joe
    Mar 3, 2016 at 11:40
  • 1
    @Joe Try boost (always try boost :-) ), but if it has to be plain C, you'll have to search.
    – gbjbaanb
    Mar 3, 2016 at 11:44
  • @Joe try this GPL wrapper for Windows so it looks like Linux's mmap. Mac possibly uses mmap too.
    – gbjbaanb
    Mar 3, 2016 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.