/*---------------------------------------------------------------------------* Project: Horizon File: FsSampleFile.cpp Copyright (C)2009-2012 Nintendo Co., Ltd. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Rev: 47596 $ *---------------------------------------------------------------------------*/ #include "FsSampleFile.h" #include #include namespace { // File path const std::wstring pathNameRomW = L"rom:/test.txt"; const std::wstring pathNameSaveDataW = L"data:/test.txt"; const std::wstring pathNameExtSaveDataW = L"extdata:/test.txt"; const std::wstring pathNameSdmcWriteOnlyW = L"sdmcwo:/test.txt"; const std::wstring pathNameIcnFileW = L"rom:/ExtSaveData.icn"; /* Please see man pages for details */ nn::Result FsSampleReadRomFileImple() { // Open the file nn::fs::FileInputStream f(pathNameRomW.c_str()); // Prepare buffer size_t size = f.GetSize(); void* buf = FsSampleAllocateMemory(size); if(!buf) { NN_LOG("Cannot allocate memory : size=%d\n", size); return ResultAppOutOfMemory(); } // Load file f.Read(buf, size); // Close the file f.Finalize(); // Can be called even in the destructor FsSampleFreeMemory(buf); return nn::ResultSuccess(); } /* Please see man pages for details */ nn::Result FsSampleCreateSaveDataFileImpl() { const s64 size = 1024; // Creates files and opens them. nn::fs::FileOutputStream f; nn::Result result = f.TryInitialize(pathNameSaveDataW.c_str(), true); if(result.IsFailure()) { NN_LOG("TryInitialize() failed!\n"); return result; } // Expands the file size (0 when created) // When a file is created with nn::fs::TryCreateFile, this process is unnecessary since processing includes up to size expansion result = f.TrySetSize(size); if(result.IsFailure()) { NN_LOG("TrySetSize() failed!\n"); return result; } // Prepares data. s32 out; bit8* pData = static_cast(FsSampleAllocateMemory(size)); if(!pData) { NN_LOG("Cannot allocate memory : size=%d\n", size); return ResultAppOutOfMemory(); } for(s32 i = 0; i < size; ++i) { pData[i] = i; } // Write to file result = f.TryWrite(&out, pData, size, true); if(result.IsFailure()) { NN_LOG("TryWrite() failed!\n"); FsSampleFreeMemory(pData); return result; } /* // Flush the file cache // When flush=false is specified in the TryWrite argument, TryFlush must be called before closing the file result = f.TryFlush(); if(result.IsFailure()) { NN_LOG("TryFlush() failed!\n"); FsSampleFreeMemory(pData); return result; } */ // Close the file f.Finalize(); // Can be called even in the destructor FsSampleFreeMemory(pData); return nn::ResultSuccess(); } /* Please see man pages for details */ nn::Result FsSampleReadSaveDataFileImpl() { // Open file nn::fs::FileInputStream f; nn::Result result = f.TryInitialize(pathNameSaveDataW.c_str()); if(result.IsFailure()) { NN_LOG("TryInitialize() failed!\n"); return result; } // Prepare buffer s64 size; result = f.TryGetSize(&size); if(result.IsFailure()) { NN_LOG("TryGetSize() failed!\n"); return result; } void* buf = FsSampleAllocateMemory(size); if(!buf) { NN_LOG("Cannot allocate memory!\n"); return ResultAppOutOfMemory(); } // Load file s32 out; result = f.TryRead(&out, buf, size); if(result.IsFailure()) { NN_LOG("TryRead() failed!\n"); FsSampleFreeMemory(buf); return result; } // Close the file f.Finalize(); // Can be called even in the destructor FsSampleFreeMemory(buf); return nn::ResultSuccess(); } /* Please see man pages for details */ nn::Result FsSampleCreateExtSaveDataFileImpl() { const s64 size = 1024; // Create a file // Use nn::fs::TryCreateFile to create a file to the expanded save data nn::Result result = nn::fs::TryCreateFile(pathNameExtSaveDataW.c_str(), size); if(result.IsFailure() && !nn::fs::ResultAlreadyExists::Includes(result)) { // For TryCreateFile, an error is returned if the file already exists NN_LOG("TryCreateFile() failed!\n"); return result; } nn::fs::FileOutputStream f; result = f.TryInitialize(pathNameExtSaveDataW.c_str(), false); if(result.IsFailure()) { NN_LOG("TryInitialize() failed!\n"); return result; } // Prepares data. s32 out; bit8* pData = static_cast(FsSampleAllocateMemory(size)); if(!pData) { NN_LOG("Cannot allocate memory!\n"); return result; } for(s32 i = 0; i < size; ++i) { pData[i] = i; } // Write to file result = f.TryWrite(&out, pData, size, true); if(result.IsFailure()) { NN_LOG("TryWrite() failed!\n"); FsSampleFreeMemory(pData); return result; } /* // Flush the file cache // When flush=false is specified in the TryWrite argument, TryFlush must be called before closing the file result = f.TryFlush(); if(result.IsFailure()) { NN_LOG("TryFlush() failed!\n"); FsSampleFreeMemory(pData); return result; } */ // Close the file f.Finalize(); // Can be called even in the destructor FsSampleFreeMemory(pData); return nn::ResultSuccess(); } /* Please see man pages for details */ nn::Result FsSampleReadExtSaveDataFileImpl() { // Open file nn::fs::FileInputStream f; nn::Result result = f.TryInitialize(pathNameExtSaveDataW.c_str()); if(result.IsFailure()) { NN_LOG("TryInitialize() failed!\n"); return result; } // Prepare buffer s64 size; result = f.TryGetSize(&size); if(result.IsFailure()) { NN_LOG("TryGetSize() failed!\n"); return result; } void* buf = FsSampleAllocateMemory(size); if(!buf) { NN_LOG("Cannot allocate memory!\n"); return ResultAppOutOfMemory(); } // Load file s32 out; result = f.TryRead(&out, buf, size); if(result.IsFailure()) { NN_LOG("TryRead() failed!\n"); FsSampleFreeMemory(buf); return result; } // Close the file f.Finalize(); // Can be called even in the destructor FsSampleFreeMemory(buf); return nn::ResultSuccess(); } /* Please see man pages for details */ nn::Result FsSampleCreateSdmcWriteOnlyFileImpl() { const s64 size = 1024; // Creates files and opens them. nn::fs::FileOutputStream f; nn::Result result = f.TryInitialize(pathNameSdmcWriteOnlyW.c_str(), true); if(result.IsFailure()) { NN_LOG("TryInitialize() failed!\n"); return result; } // Expands the file size (0 when created) // When a file is created with nn::fs::TryCreateFile, this process is unnecessary since processing includes up to size expansion result = f.TrySetSize(size); if(result.IsFailure()) { NN_LOG("TrySetSize() failed!\n"); return result; } // Prepares data. s32 out; bit8* pData = static_cast(FsSampleAllocateMemory(size)); if(!pData) { NN_LOG("Cannot allocate memory!\n"); return result; } for(s32 i = 0; i < size; ++i) { pData[i] = i; } // Write to file result = f.TryWrite(&out, pData, size, true); if(result.IsFailure()) { NN_LOG("TryWrite() failed!\n"); FsSampleFreeMemory(pData); return result; } // Close the file f.Finalize(); // Can be called even in the destructor FsSampleFreeMemory(pData); return nn::ResultSuccess(); } } bool FsSampleReadRomFile() { NN_LOG("FsSampleRomFile() start.\n"); if(FsSampleReadRomFileImple().IsFailure()) { return false; } NN_LOG("FsSampleRomFile() succeed.\n"); return true; } bool FsSampleCreateSaveDataFile() { NN_LOG("FsSampleCreateSaveDataFile() start.\n"); nn::Result result = FsSampleCreateSaveDataFileImpl(); if(result.IsFailure()) { NN_DBG_PRINT_RESULT(result); if(result <= nn::fs::ResultNotFound()) { // The archive could not be found (not mounted) // This error should not occur in the production version return false; } else if(result <= nn::fs::ResultVerificationFailed()) { // The file system or directory entry is either corrupted or has been altered // It is necessary to reformat return false; } else if(result <= nn::fs::ResultOperationDenied()) { // It may be possible that a directory with the same name exists or the directory is already opened // This error should not occur in the production version return false; } else if(result <= nn::fs::ResultNotEnoughSpace()) { // Not enough free space // This error should not occur in the production version return false; } else if(result == ResultAppOutOfMemory()) { // Insufficient memory (application-specific defined error) // This error should not occur in the production version return false; } else { // Unexpected errors NN_ERR_THROW_FATAL_ALL(result); } } NN_LOG("FsSampleCreateSaveDataFile() succeed.\n"); return true; } bool FsSampleReadSaveDataFile() { NN_LOG("FsSampleReadSaveDataFile() start.\n"); nn::Result result = FsSampleReadSaveDataFileImpl(); if(result.IsFailure()) { NN_DBG_PRINT_RESULT(result); if(result <= nn::fs::ResultNotFound()) { // File or archive cannot be found return false; } else if(result <= nn::fs::ResultVerificationFailed()) { // File is either destroyed or altered // ・Requires reformatting when automatic redundancy is enabled. // ・If automatic redundancy is disabled, the file must be deleted and created again // The file could be corrupted due to causes like the media being removed when saving return false; } else if(result <= nn::fs::ResultOperationDenied()) { // It may be possible that a directory with the same name exists or the directory is already opened in write mode // This error should not occur in the production version return false; } else if(result == ResultAppOutOfMemory()) { // Insufficient memory (application-specific defined error) // This error should not occur in the production version return false; } else { // Unexpected errors NN_ERR_THROW_FATAL_ALL(result); } } NN_LOG("FsSampleReadSaveDataFile() succeed.\n"); return true; } bool FsSampleCreateExtSaveDataFile() { NN_LOG("FsSampleCreateExtSaveDataFile() start.\n"); nn::Result result = FsSampleCreateExtSaveDataFileImpl(); if(result.IsFailure()) { NN_DBG_PRINT_RESULT(result); if(result <= nn::fs::ResultNotFound()) { if(result <= nn::fs::ResultMediaNotFound()) { // The SD Card may have been removed } else { // The archive could not be found (not mounted) // This error should not occur in the production version } return false; } else if(result <= nn::fs::ResultVerificationFailed()) { // The file system or directory entry is either corrupted or has been altered // It is necessary to reformat return false; } else if(result <= nn::fs::ResultArchiveInvalidated()) { // The SD Card may have been removed return false; } else if(result <= nn::fs::ResultOperationDenied()) { if(result <= nn::fs::ResultWriteProtected()) { // SD Card has been write protected } else { // It may be possible that a directory with the same name exists or the directory is already opened // Because the data on the SD Card can be manipulated on the computer, this may also occur in the product } return false; } else if(result <= nn::fs::ResultNotEnoughSpace()) { // Not enough free space // Because the data on the SD Card can be manipulated on the computer, this may also occur in the product return false; } else if(result == ResultAppOutOfMemory()) { // Insufficient memory (application-specific defined error) // This error should not occur in the production version return false; } else { // Unexpected errors return false; } } NN_LOG("FsSampleCreateExtSaveDataFile() succeed.\n"); return true; } bool FsSampleReadExtSaveDataFile() { NN_LOG("FsSampleReadExtSaveDataFile() start.\n"); nn::Result result = FsSampleReadExtSaveDataFileImpl(); if(result.IsFailure()) { NN_DBG_PRINT_RESULT(result); if(result <= nn::fs::ResultNotFound()) { if(result <= nn::fs::ResultMediaNotFound()) { // The SD Card may have been removed } else { // File or archive cannot be found // Because the data on the SD Card can be manipulated on the computer, this may also occur in the product } return false; } else if(result <= nn::fs::ResultArchiveInvalidated()) { // The SD Card may have been removed return false; } else if(result <= nn::fs::ResultVerificationFailed()) { // File is either destroyed or altered // The file must be deleted and created again // The file could be corrupted due to causes like the media being removed when saving return false; } else if(result <= nn::fs::ResultOperationDenied()) { if(result <= nn::fs::ResultWriteProtected()) { // SD Card has been write protected } else { // It may be possible that a directory with the same name exists or the directory is already opened in write mode // Because the data on the SD Card can be manipulated on the computer, this may also occur in the product } return false; } else if(result == ResultAppOutOfMemory()) { // Insufficient memory (application-specific defined error) // This error should not occur in the production version return false; } else { // Unexpected errors return false; } } NN_LOG("FsSampleReadExtSaveDataFile() succeed.\n"); return true; } bool FsSampleCreateSdmcWriteOnlyFile() { NN_LOG("FsSampleCreateSdmcWriteOnlyFile() start.\n"); nn::Result result = FsSampleCreateSdmcWriteOnlyFileImpl(); if(result.IsFailure()) { NN_DBG_PRINT_RESULT(result); if(result <= nn::fs::ResultNotFound()) { if(result <= nn::fs::ResultMediaNotFound()) { // The SD Card may have been removed } else { // The archive could not be found (not mounted) // This error should not occur in the production version } return false; } else if(result <= nn::fs::ResultArchiveInvalidated()) { // The SD Card may have been removed return false; } else if(result <= nn::fs::ResultOperationDenied()) { if(result <= nn::fs::ResultWriteProtected()) { // SD Card has been write protected } else { // It may be possible that a directory with the same name exists or the directory is already opened // Because the data on the SD Card can be manipulated on the computer, this may also occur in the product } return false; } else if(result <= nn::fs::ResultNotEnoughSpace()) { // Not enough free space // Because the data on the SD Card can be manipulated on the computer, this may also occur in the product return false; } else if(result == ResultAppOutOfMemory()) { // Insufficient memory (application-specific defined error) // This error should not occur in the production version return false; } else { // Unexpected errors return false; } } NN_LOG("FsSampleCreateSdmcWriteOnlyFile() succeed.\n"); return true; } /* Please see man pages for details */ bool FsSampleReadIcnData(void** ppData, size_t* pSize) { // Open the *.icn file nn::fs::FileInputStream f(pathNameIcnFileW.c_str()); // Allocate buffer size_t size = f.GetSize(); void* p = FsSampleAllocateMemory(size); if(!p) { return false; } // Load data f.Read(p, size); *pSize = size; *ppData = p; return true; } /* Please see man pages for details */ void FsSampleFreeIcnData(void* p) { FsSampleFreeMemory(p); }