From 96f1de72066316792fa239d20c9b78be623625a8 Mon Sep 17 00:00:00 2001 From: ksherlock Date: Mon, 31 May 2010 15:26:03 +0000 Subject: [PATCH] fix compression, add setFileSize() git-svn-id: https://profuse.googlecode.com/svn/branches/v2@311 aa027e90-d47c-11dd-86d7-074df07e0730 --- Pascal/FileEntry.cpp | 45 ++++++++++++++++++++++++++++++-------------- Pascal/FileEntry.h | 4 ++-- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/Pascal/FileEntry.cpp b/Pascal/FileEntry.cpp index 546bb5b..351214a 100644 --- a/Pascal/FileEntry.cpp +++ b/Pascal/FileEntry.cpp @@ -254,9 +254,7 @@ int FileEntry::truncateCommon(unsigned newSize) } - _lastBlock = 1 + _firstBlock + newSize / 512; - _lastByte = newSize % 512; - if (_lastByte == 0) _lastByte = 512; + setFileSize(newSize); return 0; } @@ -293,8 +291,8 @@ int FileEntry::write(TextWriter &text) } _modification = Date::Today(); - _lastBlock = 1 + firstBlock() + blocks; - _lastByte = 512; + + setFileSize(blocks * 512); parent()->writeEntry(this); parent()->sync(); @@ -302,7 +300,7 @@ int FileEntry::write(TextWriter &text) return blocks * 512; } -int FileEntry::write(uint8_t *buffer, unsigned size, unsigned offset) +int FileEntry::write(const uint8_t *buffer, unsigned size, unsigned offset) { #undef __METHOD__ #define __METHOD__ "FileEntry::write" @@ -368,13 +366,9 @@ int FileEntry::write(uint8_t *buffer, unsigned size, unsigned offset) block++; } - if (newSize > currentSize) - { - _lastBlock = 1 + _firstBlock + currentSize / 512; - _lastByte = currentSize % 512; - if (_lastByte == 0) _lastByte = 512; - - } + + if (newSize > currentSize) setFileSize(newSize); + _modification = Date::Today(); parent()->writeEntry(this); @@ -383,6 +377,26 @@ int FileEntry::write(uint8_t *buffer, unsigned size, unsigned offset) } +/* + * private + * set the file size. Does not check if > _maxFileSize. + * + */ +void FileEntry::setFileSize(unsigned size) +{ + if (size == 0) + { + // TODO -- verify how 0 byte files are handled. + _lastBlock = _firstBlock + 1; + _lastByte = 0; + return; + } + + _lastBlock = 1 + _firstBlock + size / 512; + _lastByte = size % 512; + if (_lastByte == 0) _lastByte = 512; +} + unsigned FileEntry::dataFileSize() { return blocks() * 512 - 512 + _lastByte; @@ -633,12 +647,14 @@ bool FileEntry::Compress(std::string& text) if (count < 3) return false; - count = std::max((int)count, 255 - 32); + count = std::min((int)count, 255 - 32); out.push_back(kDLE); out.push_back(32 + count); out.append(text.begin() + count, text.end()); + text.swap(out); + return true; } @@ -662,6 +678,7 @@ bool FileEntry::Uncompress(std::string& text) out.append(c - 32, ' '); out.append(text.begin() + 2, text.end()); + text.swap(out); return true; } diff --git a/Pascal/FileEntry.h b/Pascal/FileEntry.h index d5b0eab..721e80e 100644 --- a/Pascal/FileEntry.h +++ b/Pascal/FileEntry.h @@ -38,7 +38,7 @@ namespace Pascal { void setFileKind(unsigned kind); int read(uint8_t *buffer, unsigned size, unsigned offset); - int write(uint8_t *buffer, unsigned size, unsigned offset); + int write(const uint8_t *buffer, unsigned size, unsigned offset); int write(TextWriter& text); @@ -58,7 +58,7 @@ namespace Pascal { int truncateCommon(unsigned newSize); - + void setFileSize(unsigned size); unsigned _status;