| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478 |
- // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #include <AnKi/Util/File.h>
- #include <AnKi/Util/Filesystem.h>
- #include <AnKi/Util/Logger.h>
- #include <AnKi/Util/Assert.h>
- #include <cstring>
- #include <cstdarg>
- #if ANKI_OS_ANDROID
- # include <android_native_app_glue.h>
- # include <android/asset_manager.h>
- #endif
- namespace anki
- {
- #if ANKI_OS_ANDROID
- extern android_app* g_androidApp;
- # define ANKI_AFILE static_cast<AAsset*>(m_file)
- #endif
- #define ANKI_CFILE reinterpret_cast<FILE*>(m_file)
- File& File::operator=(File&& b)
- {
- close();
- if(b.m_file != nullptr)
- {
- m_file = b.m_file;
- m_flags = b.m_flags;
- m_size = b.m_size;
- }
- b.zero();
- return *this;
- }
- File::~File()
- {
- close();
- }
- Error File::open(const CString& filename, FileOpenFlag flags)
- {
- ANKI_ASSERT(m_file == nullptr && m_flags == FileOpenFlag::NONE);
- // Only these flags are accepted
- ANKI_ASSERT((flags
- & (FileOpenFlag::READ | FileOpenFlag::WRITE | FileOpenFlag::APPEND | FileOpenFlag::BINARY
- | FileOpenFlag::ENDIAN_LITTLE | FileOpenFlag::ENDIAN_BIG | FileOpenFlag::SPECIAL))
- != FileOpenFlag::NONE);
- // Cannot be both
- ANKI_ASSERT((flags & FileOpenFlag::READ) != (flags & FileOpenFlag::WRITE));
- // Open it
- #if ANKI_OS_ANDROID
- if(!!(flags & FileOpenFlag::SPECIAL))
- {
- ANKI_CHECK(openAndroidFile(filename, flags));
- }
- else
- #endif
- {
- ANKI_CHECK(openCFile(filename, flags));
- }
- // Set endianess
- if((flags & (FileOpenFlag::ENDIAN_BIG | FileOpenFlag::ENDIAN_LITTLE)) == FileOpenFlag::NONE)
- {
- // If the open() DIDN'T provided us the file endianess Set the machine's endianness
- m_flags = m_flags | getMachineEndianness();
- }
- else
- {
- // Else just make sure that only one of the flags is set
- ANKI_ASSERT((flags & FileOpenFlag::ENDIAN_BIG) != (flags & FileOpenFlag::ENDIAN_LITTLE));
- }
- return Error::NONE;
- }
- Error File::openCFile(const CString& filename, FileOpenFlag flags)
- {
- Error err = Error::NONE;
- const char* openMode;
- if((flags & FileOpenFlag::READ) != FileOpenFlag::NONE)
- {
- openMode = "rb";
- }
- else if((flags & FileOpenFlag::APPEND) == FileOpenFlag::APPEND)
- {
- openMode = "a+b";
- }
- else if((flags & FileOpenFlag::WRITE) != FileOpenFlag::NONE)
- {
- openMode = "wb";
- }
- else
- {
- ANKI_ASSERT(0);
- openMode = nullptr;
- }
- // Open
- m_file = fopen(filename.cstr(), openMode);
- if(m_file == nullptr)
- {
- ANKI_UTIL_LOGE("Failed to open file \"%s\", open mode \"%s\"", &filename[0], openMode);
- err = Error::FILE_ACCESS;
- }
- else
- {
- m_flags = flags;
- }
- // Get file size
- if(((flags & FileOpenFlag::READ) != FileOpenFlag::NONE) && !err)
- {
- fseek(ANKI_CFILE, 0, SEEK_END);
- I64 size = ftell(ANKI_CFILE);
- if(size < 1)
- {
- ANKI_UTIL_LOGE("ftell() failed");
- err = Error::FUNCTION_FAILED;
- }
- else
- {
- m_size = U32(size);
- rewind(ANKI_CFILE);
- }
- }
- return err;
- }
- #if ANKI_OS_ANDROID
- Error File::openAndroidFile(const CString& filename, FileOpenFlag flags)
- {
- ANKI_ASSERT(!!(flags & FileOpenFlag::SPECIAL));
- if(!!(flags & FileOpenFlag::WRITE))
- {
- ANKI_UTIL_LOGE("Cannot write inside archives");
- return Error::FILE_ACCESS;
- }
- if(!(flags & FileOpenFlag::READ))
- {
- ANKI_UTIL_LOGE("Missing FileOpenFlag::READ flag");
- return Error::FILE_ACCESS;
- }
- // Open file
- ANKI_ASSERT(g_androidApp != nullptr && g_androidApp->activity && g_androidApp->activity->assetManager);
- m_file = AAssetManager_open(g_androidApp->activity->assetManager, filename.cstr(), AASSET_MODE_STREAMING);
- if(m_file == nullptr)
- {
- ANKI_UTIL_LOGE("AAssetManager_open() failed");
- return Error::FILE_ACCESS;
- }
- m_flags = flags;
- return Error::NONE;
- }
- #endif
- void File::close()
- {
- if(m_file)
- {
- #if ANKI_OS_ANDROID
- if(!!(m_flags & FileOpenFlag::SPECIAL))
- {
- AAsset_close(ANKI_AFILE);
- }
- else
- #endif
- {
- fclose(ANKI_CFILE);
- }
- }
- zero();
- }
- Error File::flush()
- {
- ANKI_ASSERT(m_file);
- Error err = Error::NONE;
- if((m_flags & FileOpenFlag::WRITE) != FileOpenFlag::NONE)
- {
- #if ANKI_OS_ANDROID
- if(!!(m_flags & FileOpenFlag::SPECIAL))
- {
- ANKI_ASSERT(0 && "Cannot have write these file types");
- }
- else
- #endif
- {
- const I ierr = fflush(ANKI_CFILE);
- if(ierr)
- {
- ANKI_UTIL_LOGE("fflush() failed");
- err = Error::FUNCTION_FAILED;
- }
- }
- }
- return err;
- }
- Error File::read(void* buff, PtrSize size)
- {
- ANKI_ASSERT(buff);
- ANKI_ASSERT(size > 0);
- ANKI_ASSERT(m_file);
- ANKI_ASSERT((m_flags & FileOpenFlag::READ) != FileOpenFlag::NONE);
- I64 readSize = 0;
- #if ANKI_OS_ANDROID
- if(!!(m_flags & FileOpenFlag::SPECIAL))
- {
- readSize = AAsset_read(ANKI_AFILE, buff, size);
- }
- else
- #endif
- {
- readSize = fread(buff, 1, size, ANKI_CFILE);
- }
- Error err = Error::NONE;
- if(static_cast<I64>(size) != readSize)
- {
- ANKI_UTIL_LOGE("File read failed");
- err = Error::FILE_ACCESS;
- }
- return err;
- }
- PtrSize File::getSize() const
- {
- ANKI_ASSERT(m_file);
- ANKI_ASSERT(m_flags != FileOpenFlag::NONE);
- PtrSize out = 0;
- #if ANKI_OS_ANDROID
- if(!!(m_flags & FileOpenFlag::SPECIAL))
- {
- out = AAsset_getLength(ANKI_AFILE);
- }
- else
- #endif
- {
- ANKI_ASSERT(m_size != 0);
- out = m_size;
- }
- return out;
- }
- Error File::readU32(U32& out)
- {
- ANKI_ASSERT(m_file);
- ANKI_ASSERT(m_flags != FileOpenFlag::NONE);
- ANKI_ASSERT((m_flags & FileOpenFlag::READ) != FileOpenFlag::NONE);
- ANKI_ASSERT((m_flags & FileOpenFlag::BINARY) != FileOpenFlag::NONE && "Should be binary file");
- ANKI_ASSERT((m_flags & FileOpenFlag::ENDIAN_BIG) != (m_flags & FileOpenFlag::ENDIAN_LITTLE)
- && "One of those 2 should be active");
- Error err = read(&out, sizeof(out));
- if(!err)
- {
- // Copy it
- FileOpenFlag machineEndianness = getMachineEndianness();
- FileOpenFlag fileEndianness = ((m_flags & FileOpenFlag::ENDIAN_BIG) != FileOpenFlag::NONE)
- ? FileOpenFlag::ENDIAN_BIG
- : FileOpenFlag::ENDIAN_LITTLE;
- if(machineEndianness == fileEndianness)
- {
- // Same endianness between the file and the machine. Do nothing
- }
- else if(machineEndianness == FileOpenFlag::ENDIAN_BIG && fileEndianness == FileOpenFlag::ENDIAN_LITTLE)
- {
- U8* c = reinterpret_cast<U8*>(&out);
- out = (c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24));
- }
- else
- {
- U8* c = reinterpret_cast<U8*>(&out);
- out = ((c[0] << 24) | (c[1] << 16) | (c[2] << 8) | c[3]);
- }
- }
- return err;
- }
- Error File::readF32(F32& out)
- {
- U32 integer = MAX_U32;
- Error err = readU32(integer);
- if(!err)
- {
- memcpy(&out, &integer, sizeof(F32));
- }
- return err;
- }
- Error File::write(const void* buff, PtrSize size)
- {
- ANKI_ASSERT(buff);
- ANKI_ASSERT(size > 0);
- ANKI_ASSERT(m_file);
- ANKI_ASSERT((m_flags & FileOpenFlag::WRITE) != FileOpenFlag::NONE);
- Error err = Error::NONE;
- #if ANKI_OS_ANDROID
- if(!!(m_flags & FileOpenFlag::SPECIAL))
- {
- ANKI_UTIL_LOGE("Writting to special files is not supported");
- err = Error::FILE_ACCESS;
- }
- else
- #endif
- {
- PtrSize writeSize = 0;
- writeSize = std::fwrite(buff, 1, size, ANKI_CFILE);
- if(writeSize != size)
- {
- ANKI_UTIL_LOGE("std::fwrite() failed");
- err = Error::FILE_ACCESS;
- }
- }
- return err;
- }
- Error File::writeText(CString format, ...)
- {
- ANKI_ASSERT(m_file);
- ANKI_ASSERT(m_flags != FileOpenFlag::NONE);
- ANKI_ASSERT((m_flags & FileOpenFlag::WRITE) != FileOpenFlag::NONE);
- ANKI_ASSERT((m_flags & FileOpenFlag::BINARY) == FileOpenFlag::NONE);
- Error err = Error::NONE;
- va_list args;
- va_start(args, format);
- #if ANKI_OS_ANDROID
- if(!!(m_flags & FileOpenFlag::SPECIAL))
- {
- ANKI_UTIL_LOGE("Writting to special files is not supported");
- err = Error::FILE_ACCESS;
- }
- else
- #endif
- {
- std::vfprintf(ANKI_CFILE, &format[0], args);
- }
- va_end(args);
- return err;
- }
- Error File::seek(PtrSize offset, FileSeekOrigin origin)
- {
- ANKI_ASSERT(m_file);
- ANKI_ASSERT(m_flags != FileOpenFlag::NONE);
- Error err = Error::NONE;
- #if ANKI_OS_ANDROID
- if(!!(m_flags & FileOpenFlag::SPECIAL))
- {
- if(AAsset_seek(ANKI_AFILE, offset, I32(origin)) == off_t(-1))
- {
- ANKI_UTIL_LOGE("AAsset_seek() failed");
- err = Error::FUNCTION_FAILED;
- }
- }
- else
- #endif
- {
- if(fseek(ANKI_CFILE, offset, I32(origin)) != 0)
- {
- ANKI_UTIL_LOGE("fseek() failed");
- err = Error::FUNCTION_FAILED;
- }
- }
- return err;
- }
- PtrSize File::tell()
- {
- ANKI_ASSERT(m_file);
- ANKI_ASSERT(m_flags != FileOpenFlag::NONE);
- #if ANKI_OS_ANDROID
- if(!!(m_flags & FileOpenFlag::SPECIAL))
- {
- ANKI_ASSERT(0);
- }
- else
- #endif
- {
- return ftell(ANKI_CFILE);
- }
- return 0;
- }
- FileOpenFlag File::getMachineEndianness()
- {
- I32 num = 1;
- if(*(char*)&num == 1)
- {
- return FileOpenFlag::ENDIAN_LITTLE;
- }
- else
- {
- return FileOpenFlag::ENDIAN_BIG;
- }
- }
- Error File::readAllText(GenericMemoryPoolAllocator<U8> alloc, String& out)
- {
- Error err = Error::NONE;
- PtrSize size = getSize();
- if(size != 0)
- {
- out.create(alloc, '?', size);
- err = read(&out[0], size);
- }
- else
- {
- err = Error::FUNCTION_FAILED;
- }
- return err;
- }
- Error File::readAllText(StringAuto& out)
- {
- Error err = Error::NONE;
- PtrSize size = getSize();
- if(size != 0)
- {
- out.create('?', size);
- err = read(&out[0], size);
- }
- else
- {
- err = Error::FUNCTION_FAILED;
- }
- return err;
- }
- } // end namespace anki
|