1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00
JUCE/extras/Introjucer/Source/Utility/jucer_FileHelpers.cpp
2012-04-10 10:29:14 +01:00

148 lines
4.7 KiB
C++

/*
==============================================================================
This file is part of the JUCE library - "Jules' Utility Class Extensions"
Copyright 2004-11 by Raw Material Software Ltd.
------------------------------------------------------------------------------
JUCE can be redistributed and/or modified under the terms of the GNU General
Public License (Version 2), as published by the Free Software Foundation.
A copy of the license is included in the JUCE distribution, or can be found
online at www.gnu.org/licenses.
JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.rawmaterialsoftware.com/juce for more information.
==============================================================================
*/
#include "../jucer_Headers.h"
#include "jucer_CodeHelpers.h"
//==============================================================================
namespace FileHelpers
{
static int64 calculateMemoryHashCode (const void* data, const int numBytes)
{
int64 t = 0;
for (int i = 0; i < numBytes; ++i)
t = t * 65599 + static_cast <const uint8*> (data)[i];
return t;
}
int64 calculateStreamHashCode (InputStream& in)
{
int64 t = 0;
const int bufferSize = 4096;
HeapBlock <uint8> buffer;
buffer.malloc (bufferSize);
for (;;)
{
const int num = in.read (buffer, bufferSize);
if (num <= 0)
break;
for (int i = 0; i < num; ++i)
t = t * 65599 + buffer[i];
}
return t;
}
int64 calculateFileHashCode (const File& file)
{
ScopedPointer <FileInputStream> stream (file.createInputStream());
return stream != nullptr ? calculateStreamHashCode (*stream) : 0;
}
bool overwriteFileWithNewDataIfDifferent (const File& file, const void* data, int numBytes)
{
if (file.getSize() == numBytes
&& calculateMemoryHashCode (data, numBytes) == calculateFileHashCode (file))
return true;
if (file.exists())
return file.replaceWithData (data, numBytes);
else
return file.appendData (data, numBytes);
}
bool overwriteFileWithNewDataIfDifferent (const File& file, const MemoryOutputStream& newData)
{
return overwriteFileWithNewDataIfDifferent (file, newData.getData(), newData.getDataSize());
}
bool overwriteFileWithNewDataIfDifferent (const File& file, const String& newData)
{
const char* const utf8 = newData.toUTF8();
return overwriteFileWithNewDataIfDifferent (file, utf8, strlen (utf8));
}
bool containsAnyNonHiddenFiles (const File& folder)
{
DirectoryIterator di (folder, false);
while (di.next())
if (! di.getFile().isHidden())
return true;
return false;
}
String unixStylePath (const String& path)
{
return path.replaceCharacter ('\\', '/');
}
String windowsStylePath (const String& path)
{
return path.replaceCharacter ('/', '\\');
}
static String appendPath (const String& path, const String& subpath)
{
if (File::isAbsolutePath (subpath)
|| subpath.startsWithChar ('$')
|| subpath.startsWithChar ('~')
|| (CharacterFunctions::isLetter (subpath[0]) && subpath[1] == ':'))
return subpath.replaceCharacter ('\\', '/');
String path1 (path.replaceCharacter ('\\', '/'));
if (! path1.endsWithChar ('/'))
path1 << '/';
return path1 + subpath.replaceCharacter ('\\', '/');
}
bool shouldPathsBeRelative (String path1, String path2)
{
path1 = unixStylePath (path1);
path2 = unixStylePath (path2);
const int len = jmin (path1.length(), path2.length());
int commonBitLength = 0;
for (int i = 0; i < len; ++i)
{
if (CharacterFunctions::toLowerCase (path1[i]) != CharacterFunctions::toLowerCase (path2[i]))
break;
++commonBitLength;
}
return path1.substring (0, commonBitLength).removeCharacters ("/:").isNotEmpty();
}
}