Transfert enumeration from file to enums

Former-commit-id: 0d9e191373affda50d0eb6c2376c7a23720e591b
This commit is contained in:
Gawaboumga 2015-08-21 11:22:35 +02:00
parent 8cda289b82
commit 3dbcb25384
16 changed files with 85 additions and 77 deletions

View File

@ -100,7 +100,7 @@ int main()
std::cout << oss.str() << std::endl; std::cout << oss.str() << std::endl;
NzFile reportFile("RapportHardwareInfo.txt"); NzFile reportFile("RapportHardwareInfo.txt");
if (reportFile.Open(NzFile::Text | NzFile::Truncate | NzFile::WriteOnly)) if (reportFile.Open(nzOpenMode_Text | nzOpenMode_Truncate | nzOpenMode_WriteOnly))
{ {
reportFile.Write(oss.str()); // Conversion implicite en NzString reportFile.Write(oss.str()); // Conversion implicite en NzString
reportFile.Close(); reportFile.Close();

View File

@ -15,6 +15,15 @@ enum nzCoordSys
nzCoordSys_Max = nzCoordSys_Local nzCoordSys_Max = nzCoordSys_Local
}; };
enum nzCursorPosition
{
nzCursorPosition_AtBegin, // Début du fichier
nzCursorPosition_AtCurrent, // Position du pointeur
nzCursorPosition_AtEnd, // Fin du fichier
nzCursorPosition_Max = nzCursorPosition_AtEnd
};
enum nzEndianness enum nzEndianness
{ {
nzEndianness_Unknown = -1, nzEndianness_Unknown = -1,
@ -57,7 +66,24 @@ enum nzHash
nzHash_SHA256, nzHash_SHA256,
nzHash_SHA384, nzHash_SHA384,
nzHash_SHA512, nzHash_SHA512,
nzHash_Whirlpool nzHash_Whirlpool,
nzHash_Max = nzHash_Whirlpool
};
enum nzOpenModeFlags
{
nzOpenMode_Current = 0x00, // Utilise le mode d'ouverture actuel
nzOpenMode_Append = 0x01, // Empêche l'écriture sur la partie déjà existante et met le curseur à la fin
nzOpenMode_Lock = 0x02, // Empêche le fichier d'être modifié tant qu'il est ouvert
nzOpenMode_ReadOnly = 0x04, // Ouvre uniquement en lecture
nzOpenMode_ReadWrite = 0x08, // Ouvre en lecture/écriture
nzOpenMode_Text = 0x10, // Ouvre en mode texte
nzOpenMode_Truncate = 0x20, // Créé le fichier s'il n'existe pas et le vide s'il existe
nzOpenMode_WriteOnly = 0x40, // Ouvre uniquement en écriture, créé le fichier s'il n'existe pas
nzOpenMode_Max = nzOpenMode_WriteOnly
}; };
enum nzParameterType enum nzParameterType

View File

@ -30,29 +30,9 @@ class NzFileImpl;
class NAZARA_API NzFile : public NzHashable, public NzInputStream, NzNonCopyable class NAZARA_API NzFile : public NzHashable, public NzInputStream, NzNonCopyable
{ {
public: public:
enum CursorPosition
{
AtBegin, // Début du fichier
AtCurrent, // Position du pointeur
AtEnd // Fin du fichier
};
enum OpenMode
{
Current = 0x00, // Utilise le mode d'ouverture actuel
Append = 0x01, // Empêche l'écriture sur la partie déjà existante et met le curseur à la fin
Lock = 0x02, // Empêche le fichier d'être modifié tant qu'il est ouvert
ReadOnly = 0x04, // Ouvre uniquement en lecture
ReadWrite = 0x08, // Ouvre en lecture/écriture
Text = 0x10, // Ouvre en mode texte
Truncate = 0x20, // Créé le fichier s'il n'existe pas et le vide s'il existe
WriteOnly = 0x40 // Ouvre uniquement en écriture, créé le fichier s'il n'existe pas
};
NzFile(); NzFile();
NzFile(const NzString& filePath); NzFile(const NzString& filePath);
NzFile(const NzString& filePath, unsigned long openMode); NzFile(const NzString& filePath, unsigned int openMode);
NzFile(NzFile&& file) noexcept; NzFile(NzFile&& file) noexcept;
~NzFile(); ~NzFile();
@ -79,14 +59,14 @@ class NAZARA_API NzFile : public NzHashable, public NzInputStream, NzNonCopyable
bool IsOpen() const; bool IsOpen() const;
bool Open(unsigned long openMode = Current); bool Open(unsigned int openMode = nzOpenMode_Current);
bool Open(const NzString& filePath, unsigned long openMode = Current); bool Open(const NzString& filePath, unsigned int openMode = nzOpenMode_Current);
std::size_t Read(void* buffer, std::size_t size); std::size_t Read(void* buffer, std::size_t size);
std::size_t Read(void* buffer, std::size_t typeSize, unsigned int count); std::size_t Read(void* buffer, std::size_t typeSize, unsigned int count);
bool Rename(const NzString& newFilePath); bool Rename(const NzString& newFilePath);
bool SetCursorPos(CursorPosition pos, nzInt64 offset = 0); bool SetCursorPos(nzCursorPosition pos, nzInt64 offset = 0);
bool SetCursorPos(nzUInt64 offset); bool SetCursorPos(nzUInt64 offset);
void SetEndianness(nzEndianness endianness); void SetEndianness(nzEndianness endianness);
bool SetFile(const NzString& filePath); bool SetFile(const NzString& filePath);

View File

@ -57,7 +57,7 @@ bool NzResourceLoader<Type, Parameters>::LoadFromFile(Type* resource, const NzSt
if (checkFunc && !file.IsOpen()) if (checkFunc && !file.IsOpen())
{ {
if (!file.Open(NzFile::ReadOnly)) if (!file.Open(nzOpenMode_ReadOnly))
{ {
NazaraError("Failed to load file: unable to open \"" + filePath + '"'); NazaraError("Failed to load file: unable to open \"" + filePath + '"');
return false; return false;

View File

@ -110,7 +110,7 @@ namespace
// Nous devons gérer nous-même le flux car il doit rester ouvert après le passage du loader // Nous devons gérer nous-même le flux car il doit rester ouvert après le passage du loader
// (les flux automatiquement ouverts par le ResourceLoader étant fermés après celui-ci) // (les flux automatiquement ouverts par le ResourceLoader étant fermés après celui-ci)
std::unique_ptr<NzFile> file(new NzFile); std::unique_ptr<NzFile> file(new NzFile);
if (!file->Open(filePath, NzFile::ReadOnly)) if (!file->Open(filePath, nzOpenMode_ReadOnly))
{ {
NazaraError("Failed to open stream from file: " + NzError::GetLastError()); NazaraError("Failed to open stream from file: " + NzError::GetLastError());
return false; return false;

View File

@ -32,22 +32,22 @@
NzFile::NzFile() : NzFile::NzFile() :
m_endianness(nzEndianness_Unknown), m_endianness(nzEndianness_Unknown),
m_impl(nullptr), m_impl(nullptr),
m_openMode(0) m_openMode(nzOpenMode_Current)
{ {
} }
NzFile::NzFile(const NzString& filePath) : NzFile::NzFile(const NzString& filePath) :
m_endianness(nzEndianness_Unknown), m_endianness(nzEndianness_Unknown),
m_impl(nullptr), m_impl(nullptr),
m_openMode(0) m_openMode(nzOpenMode_Current)
{ {
SetFile(filePath); SetFile(filePath);
} }
NzFile::NzFile(const NzString& filePath, unsigned long openMode) : NzFile::NzFile(const NzString& filePath, unsigned int openMode) :
m_endianness(nzEndianness_Unknown), m_endianness(nzEndianness_Unknown),
m_impl(nullptr), m_impl(nullptr),
m_openMode(0) m_openMode(openMode)
{ {
Open(filePath, openMode); Open(filePath, openMode);
} }
@ -133,7 +133,7 @@ void NzFile::Flush()
return; return;
} }
if ((m_openMode & ReadWrite) == 0 && (m_openMode & WriteOnly) == 0) if ((m_openMode & nzOpenMode_ReadWrite) == 0 && (m_openMode & nzOpenMode_WriteOnly) == 0)
{ {
NazaraError("Cannot flush file without write access"); NazaraError("Cannot flush file without write access");
return; return;
@ -225,7 +225,7 @@ std::size_t NzFile::Read(void* buffer, std::size_t size)
return 0; return 0;
} }
if ((m_openMode & ReadOnly) == 0 && (m_openMode & ReadWrite) == 0) if ((m_openMode & nzOpenMode_ReadOnly) == 0 && (m_openMode & nzOpenMode_ReadWrite) == 0)
{ {
NazaraError("File not opened with read access"); NazaraError("File not opened with read access");
return 0; return 0;
@ -242,7 +242,7 @@ std::size_t NzFile::Read(void* buffer, std::size_t size)
// Si nous ne devons rien lire, nous avançons simplement // Si nous ne devons rien lire, nous avançons simplement
nzUInt64 currentPos = m_impl->GetCursorPos(); nzUInt64 currentPos = m_impl->GetCursorPos();
m_impl->SetCursorPos(NzFile::AtCurrent, size); m_impl->SetCursorPos(nzCursorPosition_AtCurrent, size);
return static_cast<std::size_t>(m_impl->GetCursorPos()-currentPos); return static_cast<std::size_t>(m_impl->GetCursorPos()-currentPos);
} }
@ -281,7 +281,7 @@ bool NzFile::Rename(const NzString& newFilePath)
return success; return success;
} }
bool NzFile::Open(unsigned long openMode) bool NzFile::Open(unsigned int openMode)
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
@ -306,13 +306,13 @@ bool NzFile::Open(unsigned long openMode)
m_impl = impl.release(); m_impl = impl.release();
if (m_openMode & Text) if (m_openMode & nzOpenMode_Text)
m_streamOptions |= nzStreamOption_Text; m_streamOptions |= nzStreamOption_Text;
return true; return true;
} }
bool NzFile::Open(const NzString& filePath, unsigned long openMode) bool NzFile::Open(const NzString& filePath, unsigned int openMode)
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
@ -322,7 +322,7 @@ bool NzFile::Open(const NzString& filePath, unsigned long openMode)
return Open(openMode); return Open(openMode);
} }
bool NzFile::SetCursorPos(CursorPosition pos, nzInt64 offset) bool NzFile::SetCursorPos(nzCursorPosition pos, nzInt64 offset)
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
@ -349,7 +349,7 @@ bool NzFile::SetCursorPos(nzUInt64 offset)
} }
#endif #endif
return m_impl->SetCursorPos(AtBegin, offset); return m_impl->SetCursorPos(nzCursorPosition_AtBegin, offset);
} }
void NzFile::SetEndianness(nzEndianness endianness) void NzFile::SetEndianness(nzEndianness endianness)
@ -389,7 +389,7 @@ bool NzFile::SetOpenMode(unsigned int openMode)
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
if (openMode == 0 || openMode == m_openMode) if (openMode == nzOpenMode_Current || openMode == m_openMode)
return true; return true;
if (IsOpen()) if (IsOpen())
@ -406,7 +406,7 @@ bool NzFile::SetOpenMode(unsigned int openMode)
m_impl = impl.release(); m_impl = impl.release();
if (m_openMode & Text) if (m_openMode & nzOpenMode_Text)
m_streamOptions |= nzStreamOption_Text; m_streamOptions |= nzStreamOption_Text;
} }
@ -417,7 +417,7 @@ bool NzFile::SetOpenMode(unsigned int openMode)
bool NzFile::Write(const NzByteArray& byteArray) bool NzFile::Write(const NzByteArray& byteArray)
{ {
unsigned int size = byteArray.GetSize(); NzByteArray::size_type size = byteArray.GetSize();
return Write(byteArray.GetConstBuffer(), 1, size) == size; return Write(byteArray.GetConstBuffer(), 1, size) == size;
} }
@ -455,7 +455,7 @@ std::size_t NzFile::Write(const void* buffer, std::size_t typeSize, unsigned int
return 0; return 0;
} }
if ((m_openMode & ReadWrite) == 0 && (m_openMode & WriteOnly) == 0) if ((m_openMode & nzOpenMode_ReadWrite) == 0 && (m_openMode & nzOpenMode_WriteOnly) == 0)
{ {
NazaraError("File not opened with write access"); NazaraError("File not opened with write access");
return 0; return 0;
@ -721,7 +721,7 @@ bool NzFile::Rename(const NzString& sourcePath, const NzString& targetPath)
bool NzFile::FillHash(NzAbstractHash* hash) const bool NzFile::FillHash(NzAbstractHash* hash) const
{ {
NzFile file(m_filePath); NzFile file(m_filePath);
if (!file.Open(NzFile::ReadOnly)) if (!file.Open(nzOpenMode_ReadOnly))
{ {
NazaraError("Unable to open file"); NazaraError("Unable to open file");
return false; return false;

View File

@ -114,7 +114,7 @@ void NzLog::Write(const NzString& string)
if (m_enabled) if (m_enabled)
{ {
if (!m_file) if (!m_file)
m_file = new NzFile(m_filePath, NzFile::Text | NzFile::WriteOnly | ((m_append) ? NzFile::Append : NzFile::Truncate)); m_file = new NzFile(m_filePath, nzOpenMode_Text | nzOpenMode_WriteOnly | ((m_append) ? nzOpenMode_Append : nzOpenMode_Truncate));
NzString line; NzString line;

View File

@ -51,33 +51,33 @@ bool NzFileImpl::Open(const NzString& filePath, unsigned int mode)
int flags; int flags;
mode_t permissions = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; mode_t permissions = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
if (mode & NzFile::ReadOnly) if (mode & nzOpenMode_ReadOnly)
flags = O_RDONLY; flags = O_RDONLY;
else if (mode & NzFile::ReadWrite) else if (mode & nzOpenMode_ReadWrite)
{ {
flags = O_CREAT | O_RDWR; flags = O_CREAT | O_RDWR;
if (mode & NzFile::Append) if (mode & nzOpenMode_Append)
flags |= O_APPEND; flags |= O_APPEND;
if (mode & NzFile::Truncate) if (mode & nzOpenMode_Truncate)
flags |= O_TRUNC; flags |= O_TRUNC;
} }
else if (mode & NzFile::WriteOnly) else if (mode & nzOpenMode_WriteOnly)
{ {
flags = O_CREAT | O_WRONLY; flags = O_CREAT | O_WRONLY;
if (mode & NzFile::Append) if (mode & nzOpenMode_Append)
flags |= O_APPEND; flags |= O_APPEND;
if (mode & NzFile::Truncate) if (mode & nzOpenMode_Truncate)
flags |= O_TRUNC; flags |= O_TRUNC;
} }
else else
return false; return false;
///TODO: lock ///TODO: lock
// if ((mode & NzFile::Lock) == 0) // if ((mode & nzOpenMode_Lock) == 0)
// shareMode |= FILE_SHARE_WRITE; // shareMode |= FILE_SHARE_WRITE;
m_fileDescriptor = open64(filePath.GetConstBuffer(), flags, permissions); m_fileDescriptor = open64(filePath.GetConstBuffer(), flags, permissions);
@ -98,20 +98,20 @@ std::size_t NzFileImpl::Read(void* buffer, std::size_t size)
return 0; return 0;
} }
bool NzFileImpl::SetCursorPos(NzFile::CursorPosition pos, nzInt64 offset) bool NzFileImpl::SetCursorPos(nzCursorPosition pos, nzInt64 offset)
{ {
int moveMethod; int moveMethod;
switch (pos) switch (pos)
{ {
case NzFile::AtBegin: case nzCursorPosition_AtBegin:
moveMethod = SEEK_SET; moveMethod = SEEK_SET;
break; break;
case NzFile::AtCurrent: case nzCursorPosition_AtCurrent:
moveMethod = SEEK_CUR; moveMethod = SEEK_CUR;
break; break;
case NzFile::AtEnd: case nzCursorPosition_AtEnd:
moveMethod = SEEK_END; moveMethod = SEEK_END;
break; break;

View File

@ -35,7 +35,7 @@ class NzFileImpl : NzNonCopyable
nzUInt64 GetCursorPos() const; nzUInt64 GetCursorPos() const;
bool Open(const NzString& filePath, unsigned int mode); bool Open(const NzString& filePath, unsigned int mode);
std::size_t Read(void* buffer, std::size_t size); std::size_t Read(void* buffer, std::size_t size);
bool SetCursorPos(NzFile::CursorPosition pos, nzInt64 offset); bool SetCursorPos(nzCursorPosition pos, nzInt64 offset);
std::size_t Write(const void* buffer, std::size_t size); std::size_t Write(const void* buffer, std::size_t size);
static bool Copy(const NzString& sourcePath, const NzString& targetPath); static bool Copy(const NzString& sourcePath, const NzString& targetPath);

View File

@ -57,31 +57,31 @@ bool NzFileImpl::Open(const NzString& filePath, unsigned int mode)
DWORD access; DWORD access;
DWORD shareMode = FILE_SHARE_READ; DWORD shareMode = FILE_SHARE_READ;
DWORD openMode; DWORD openMode;
if (mode & NzFile::ReadOnly) if (mode & nzOpenMode_ReadOnly)
{ {
access = GENERIC_READ; access = GENERIC_READ;
openMode = OPEN_EXISTING; openMode = OPEN_EXISTING;
} }
else if (mode & NzFile::ReadWrite) else if (mode & nzOpenMode_ReadWrite)
{ {
if (mode & NzFile::Append) if (mode & nzOpenMode_Append)
access = FILE_APPEND_DATA; access = FILE_APPEND_DATA;
else else
access = GENERIC_READ | GENERIC_WRITE; access = GENERIC_READ | GENERIC_WRITE;
if (mode & NzFile::Truncate) if (mode & nzOpenMode_Truncate)
openMode = CREATE_ALWAYS; openMode = CREATE_ALWAYS;
else else
openMode = OPEN_ALWAYS; openMode = OPEN_ALWAYS;
} }
else if (mode & NzFile::WriteOnly) else if (mode & nzOpenMode_WriteOnly)
{ {
if (mode & NzFile::Append) if (mode & nzOpenMode_Append)
access = FILE_APPEND_DATA; access = FILE_APPEND_DATA;
else else
access = GENERIC_WRITE; access = GENERIC_WRITE;
if (mode & NzFile::Truncate) if (mode & nzOpenMode_Truncate)
openMode = CREATE_ALWAYS; openMode = CREATE_ALWAYS;
else else
openMode = OPEN_ALWAYS; openMode = OPEN_ALWAYS;
@ -89,7 +89,7 @@ bool NzFileImpl::Open(const NzString& filePath, unsigned int mode)
else else
return false; return false;
if ((mode & NzFile::Lock) == 0) if ((mode & nzOpenMode_Lock) == 0)
shareMode |= FILE_SHARE_WRITE; shareMode |= FILE_SHARE_WRITE;
m_handle = CreateFileW(filePath.GetWideString().data(), access, shareMode, nullptr, openMode, 0, nullptr); m_handle = CreateFileW(filePath.GetWideString().data(), access, shareMode, nullptr, openMode, 0, nullptr);
@ -134,20 +134,20 @@ std::size_t NzFileImpl::Read(void* buffer, std::size_t size)
return 0; return 0;
} }
bool NzFileImpl::SetCursorPos(NzFile::CursorPosition pos, nzInt64 offset) bool NzFileImpl::SetCursorPos(nzCursorPosition pos, nzInt64 offset)
{ {
DWORD moveMethod; DWORD moveMethod;
switch (pos) switch (pos)
{ {
case NzFile::AtBegin: case nzCursorPosition_AtBegin:
moveMethod = FILE_BEGIN; moveMethod = FILE_BEGIN;
break; break;
case NzFile::AtCurrent: case nzCursorPosition_AtCurrent:
moveMethod = FILE_CURRENT; moveMethod = FILE_CURRENT;
break; break;
case NzFile::AtEnd: case nzCursorPosition_AtEnd:
moveMethod = FILE_END; moveMethod = FILE_END;
break; break;

View File

@ -28,7 +28,7 @@ class NzFileImpl : NzNonCopyable
nzUInt64 GetCursorPos() const; nzUInt64 GetCursorPos() const;
bool Open(const NzString& filePath, unsigned int mode); bool Open(const NzString& filePath, unsigned int mode);
std::size_t Read(void* buffer, std::size_t size); std::size_t Read(void* buffer, std::size_t size);
bool SetCursorPos(NzFile::CursorPosition pos, nzInt64 offset); bool SetCursorPos(nzCursorPosition pos, nzInt64 offset);
std::size_t Write(const void* buffer, std::size_t size); std::size_t Write(const void* buffer, std::size_t size);
static bool Copy(const NzString& sourcePath, const NzString& targetPath); static bool Copy(const NzString& sourcePath, const NzString& targetPath);

View File

@ -38,7 +38,7 @@ namespace
bool LoadMaterials(NzModel* model, const NzString& filePath, const NzMaterialParams& parameters, const NzString* materials, const NzOBJParser::Mesh* meshes, unsigned int meshCount) bool LoadMaterials(NzModel* model, const NzString& filePath, const NzMaterialParams& parameters, const NzString* materials, const NzOBJParser::Mesh* meshes, unsigned int meshCount)
{ {
NzFile file(filePath); NzFile file(filePath);
if (!file.Open(NzFile::ReadOnly | NzFile::Text)) if (!file.Open(nzOpenMode_ReadOnly | nzOpenMode_Text))
{ {
NazaraError("Failed to open MTL file (" + file.GetPath() + ')'); NazaraError("Failed to open MTL file (" + file.GetPath() + ')');
return false; return false;
@ -121,6 +121,8 @@ namespace
model->SetMaterial(meshes[i].material, it->second); model->SetMaterial(meshes[i].material, it->second);
} }
return true;
} }
bool Load(NzModel* model, NzInputStream& stream, const NzModelParameters& parameters) bool Load(NzModel* model, NzInputStream& stream, const NzModelParameters& parameters)

View File

@ -393,7 +393,7 @@ bool NzLuaInstance::Execute(const NzString& code)
bool NzLuaInstance::ExecuteFromFile(const NzString& filePath) bool NzLuaInstance::ExecuteFromFile(const NzString& filePath)
{ {
NzFile file(filePath); NzFile file(filePath);
if (!file.Open(NzFile::ReadOnly | NzFile::Text)) if (!file.Open(nzOpenMode_ReadOnly | nzOpenMode_Text))
{ {
NazaraError("Failed to open file"); NazaraError("Failed to open file");
return false; return false;

View File

@ -174,7 +174,7 @@ bool NzShaderStage::SetSourceFromFile(const NzString& filePath)
#endif #endif
NzFile file(filePath); NzFile file(filePath);
if (!file.Open(NzFile::ReadOnly | NzFile::Text)) if (!file.Open(nzOpenMode_ReadOnly | nzOpenMode_Text))
{ {
NazaraError("Failed to open \"" + filePath + '"'); NazaraError("Failed to open \"" + filePath + '"');
return false; return false;

View File

@ -151,7 +151,7 @@ void NzUberShaderPreprocessor::SetShader(nzShaderStage stage, const NzString& so
bool NzUberShaderPreprocessor::SetShaderFromFile(nzShaderStage stage, const NzString& filePath, const NzString& shaderFlags, const NzString& requiredFlags) bool NzUberShaderPreprocessor::SetShaderFromFile(nzShaderStage stage, const NzString& filePath, const NzString& shaderFlags, const NzString& requiredFlags)
{ {
NzFile file(filePath); NzFile file(filePath);
if (!file.Open(NzFile::ReadOnly | NzFile::Text)) if (!file.Open(nzOpenMode_ReadOnly | nzOpenMode_Text))
{ {
NazaraError("Failed to open \"" + filePath + '"'); NazaraError("Failed to open \"" + filePath + '"');
return false; return false;

View File

@ -280,7 +280,7 @@ namespace
bool SetFile(const NzString& filePath) bool SetFile(const NzString& filePath)
{ {
std::unique_ptr<NzFile> file(new NzFile); std::unique_ptr<NzFile> file(new NzFile);
if (!file->Open(filePath, NzFile::ReadOnly)) if (!file->Open(filePath, nzOpenMode_ReadOnly))
{ {
NazaraError("Failed to open stream from file: " + NzError::GetLastError()); NazaraError("Failed to open stream from file: " + NzError::GetLastError());
return false; return false;