Browse Source

New method: FileOutputStream::truncate()

tags/2021-05-28
jules 14 years ago
parent
commit
15252c7227
5 changed files with 51 additions and 8 deletions
  1. +13
    -0
      modules/juce_core/files/juce_File.cpp
  2. +6
    -0
      modules/juce_core/files/juce_FileOutputStream.h
  3. +21
    -7
      modules/juce_core/native/juce_posix_SharedCode.h
  4. +10
    -0
      modules/juce_core/native/juce_win32_Files.cpp
  5. +1
    -1
      modules/juce_gui_basics/layout/juce_Viewport.cpp

+ 13
- 0
modules/juce_core/files/juce_File.cpp View File

@@ -1009,6 +1009,19 @@ public:
expect (mb[0] == '0');
}
{
expect (tempFile.getSize() == 10);
FileOutputStream fo (tempFile);
expect (fo.openedOk());
expect (fo.setPosition (7));
expect (fo.truncate().wasOk());
expect (tempFile.getSize() == 7);
fo.write ("789", 3);
fo.flush();
expect (tempFile.getSize() == 10);
}
beginTest ("Memory-mapped files");
{


+ 6
- 0
modules/juce_core/files/juce_FileOutputStream.h View File

@@ -80,6 +80,12 @@ public:
*/
bool openedOk() const noexcept { return status.wasOk(); }
/** Attempts to truncate the file to the current write position.
To truncate a file to a specific size, first use setPosition() to seek to the
appropriate location, and then call this method.
*/
Result truncate();
//==============================================================================
void flush();
int64 getPosition();


+ 21
- 7
modules/juce_core/native/juce_posix_SharedCode.h View File

@@ -280,6 +280,11 @@ namespace
{
return value == -1 ? getResultForErrno() : Result::ok();
}
int getFD (void* handle) noexcept
{
return (int) (pointer_sized_int) handle;
}
}
bool File::isDirectory() const
@@ -399,10 +404,10 @@ Result File::createDirectoryInternal (const String& fileName) const
return getResultForReturnValue (mkdir (fileName.toUTF8(), 0777));
}
//==============================================================================
//=====================================================================
int64 juce_fileSetPosition (void* handle, int64 pos)
{
if (handle != 0 && lseek ((int) (pointer_sized_int) handle, pos, SEEK_SET) == pos)
if (handle != 0 && lseek (getFD (handle), pos, SEEK_SET) == pos)
return pos;
return -1;
@@ -424,7 +429,7 @@ void FileInputStream::closeHandle()
{
if (fileHandle != 0)
{
close ((int) (pointer_sized_int) fileHandle);
close (getFD (fileHandle));
fileHandle = 0;
}
}
@@ -435,7 +440,7 @@ size_t FileInputStream::readInternal (void* const buffer, const size_t numBytes)
if (fileHandle != 0)
{
result = ::read ((int) (pointer_sized_int) fileHandle, buffer, numBytes);
result = ::read (getFD (fileHandle), buffer, numBytes);
if (result < 0)
{
@@ -488,7 +493,7 @@ void FileOutputStream::closeHandle()
{
if (fileHandle != 0)
{
close ((int) (pointer_sized_int) fileHandle);
close (getFD (fileHandle));
fileHandle = 0;
}
}
@@ -499,7 +504,7 @@ int FileOutputStream::writeInternal (const void* const data, const int numBytes)
if (fileHandle != 0)
{
result = ::write ((int) (pointer_sized_int) fileHandle, data, numBytes);
result = ::write (getFD (fileHandle), data, numBytes);
if (result == -1)
status = getResultForErrno();
@@ -511,10 +516,19 @@ int FileOutputStream::writeInternal (const void* const data, const int numBytes)
void FileOutputStream::flushInternal()
{
if (fileHandle != 0)
if (fsync ((int) (pointer_sized_int) fileHandle) == -1)
if (fsync (getFD (fileHandle)) == -1)
status = getResultForErrno();
}
Result FileOutputStream::truncate()
{
if (fileHandle == 0)
return status;
flush();
return getResultForReturnValue (ftruncate (getFD (fileHandle), (off_t) currentPosition));
}
//==============================================================================
MemoryMappedFile::MemoryMappedFile (const File& file, MemoryMappedFile::AccessMode mode)
: address (nullptr),


+ 10
- 0
modules/juce_core/native/juce_win32_Files.cpp View File

@@ -289,6 +289,16 @@ void FileOutputStream::flushInternal()
status = WindowsFileHelpers::getResultForLastError();
}
Result FileOutputStream::truncate()
{
if (fileHandle == nullptr)
return status;
flush();
return SetEndOfFile ((HANDLE) fileHandle) ? Result::ok()
: WindowsFileHelpers::getResultForLastError();
}
//==============================================================================
MemoryMappedFile::MemoryMappedFile (const File& file, MemoryMappedFile::AccessMode mode)
: address (nullptr),


+ 1
- 1
modules/juce_gui_basics/layout/juce_Viewport.cpp View File

@@ -185,7 +185,7 @@ void Viewport::updateVisibleArea()
const bool canShowHBar = showHScrollbar && canShowAnyBars;
const bool canShowVBar = showVScrollbar && canShowAnyBars;
bool hBarVisible, vBarVisible;
bool hBarVisible = false, vBarVisible = false;
Rectangle<int> contentArea;
for (int i = 3; --i >= 0;)


Loading…
Cancel
Save