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.

Joe
  • 339
  • 4
  • 14
  • 1
    Do you mean "footprint"? – Kilian Foth Mar 03 '16 at 10:10
  • 1
    avoiding leaks is just a matter of knowing when you stop using the resource so it can be returned for reuse. – ratchet freak Mar 03 '16 at 10:11
  • If this is for a text editor, don't you need only the lines displayed on the screen? – Florian Margaine Mar 03 '16 at 10:11
  • foot print finger print same concept, but refers to memory – Joe Mar 03 '16 at 10:11
  • No I need 3 buffers so far, undo buffer, save buffer, load buffer. I think what makes this project interesting is that is functions like a normal text editor but threw an api – Joe Mar 03 '16 at 10:14
  • added a little more info on the buffers and there purpose – Joe Mar 03 '16 at 10:18
  • 2
    @Joe: https://en.wikipedia.org/wiki/Memory_footprint // https://en.wikipedia.org/wiki/Fingerprint_(computing) - the terms are not interchangeable at all. – Mat Mar 03 '16 at 11:06
  • I stand corrected – Joe Mar 03 '16 at 11:30
  • You might be interested in my blog articles about text file viewers: http://blog.mischel.com/2011/12/01/large-text-file-viewers/, and http://blog.mischel.com/2011/12/02/designing-a-better-text-file-viewer/ – Jim Mischel Mar 22 '16 at 13:30

1 Answers1

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.

gbjbaanb
  • 48,354
  • 6
  • 102
  • 172
  • 1
    How would memory mapped file and editing go together? – Murphy Mar 03 '16 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 03 '16 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 03 '16 at 11:40
  • 1
    @Joe Try [boost](http://www.boost.org/doc/libs/1_50_0/libs/iostreams/doc/classes/mapped_file.html) (always try boost :-) ), but if it has to be plain C, you'll have to search. – gbjbaanb Mar 03 '16 at 11:44
  • @Joe [try this GPL wrapper](http://courses.washington.edu/hypertxt/cwb/cl/windows-mmap.c) for Windows so it looks like Linux's mmap. Mac possibly uses mmap too. – gbjbaanb Mar 03 '16 at 14:56