Skip to main content
GameDev.net gamedev.net
🔒 Locked

[C++] patching files

Started by Griwes Sep 14, 2010 at 8:53 AM 3 replies 850+ views
Original Post
Griwes
Griwes
I am currently working on diff/patching utility. I finished diff generator and now it's time to apply that diff...

I was thinking about rewriting to another file with deleted elements to delete and adding elements to add... but that requires additional disk space and for applying small patches for big files it is not efficient (good word? :>) method. Second though is to do sth like this:

for every element to be deleted in diff in reversed order (from highest indexes)
{
go to index of element
for every few bytes after element
{
rewrite them at prievous index
set prievous index to end of rewrited element
}
}

and everything is deleted. Later similar loop to insert new data. But I have a question:

How to shorten file? I can rewrite everything back, and there won't be any problem when lenght of added strings is greater or equal to lenght of deleted, but if it's lesser, then there will be some "extra" bytes. Example:

base: This is file. Thing to be partially deleted.
delete: index of partially and 9 next bytes (entire word)
add: index of file, "some ".

So, after using that patching algorithm, it will be:

"This is some file. Thing to be deleted.ted."

How to cut of that "ted." and decrease size of file?
frob
frob
Out of curiosity, why are you re-inventing the wheel?

The GNU tools Diff and Patch can handle this for you.

They are already written, tested, debugged, portable, and used by millions.


As for shortening the file, you want to include as little information as possible and then compress the data (which GNU diff can do). You want your patching program to work with the compressed form (which GNU patch can do) so your user doesn't need to do it.
Griwes
Griwes
Shortening as cutting off last x bytes ;>

And about reinventing the wheel, let's say that I have to implement that by myself.
ApochPiQ
ApochPiQ
On Windows, just call SetEndOfFile after you finish the last write operation. (You might need to open the file first and seek to the desired offset if you don't use the Windows API directly for your file IO).

On Unix-like platforms, there's the truncate function in unistd.h which should do the job.
Zahlman
Zahlman
Quote:
Original post by Griwes
And about reinventing the wheel, let's say that I have to implement that by myself.


Because you have a school assignment, or... ?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.