/** * Read a data object from a file. * @param filePath Full path of file to read. * @param inPlaceholder Placeholder handle for data object. * @return true on success, false on error. */ bool FileUtil::readDataFromFile( const MAUtil::String& filePath, MAHandle inPlaceholder) { MAHandle file = openFileForReading(filePath); if (file < 0) { return false; } int size = maFileSize(file); if (size < 1) { return false; } int result = maCreateData(inPlaceholder, size); if (RES_OK != result) { return false; } result = maFileReadToData(file, inPlaceholder, 0, size); maFileClose(file); return result == 0; }
/** * Loads an image from a file and returns the handle to it. * * @param imagePath relative path to the image file. */ MAHandle ResourceMessageHandler::loadImageResource(const char* imagePath) { if (!getFileUtil()) { return 0; } // Get the current apllication directory path. String appPath = getFileUtil()->getAppPath(); // Construct image path. char completePath[2048]; sprintf(completePath, "%s%s", appPath.c_str(), imagePath); // Load the image and create a data handle from it. MAHandle imageFile = maFileOpen(completePath, MA_ACCESS_READ); if (imageFile < 0) { return 0; } int fileSize = maFileSize(imageFile); if (fileSize < 1) { return 0; } // Create buffer to hold file data. MAHandle fileData = maCreatePlaceholder(); int result = maCreateData(fileData, fileSize); if (RES_OK != result) { maDestroyPlaceholder(fileData); return 0; } // Read data from file. result = maFileReadToData(imageFile, fileData, 0, fileSize); maFileClose(imageFile); if (result < 0) { maDestroyPlaceholder(fileData); return 0; } // Create image. MAHandle imageHandle = maCreatePlaceholder(); result = maCreateImageFromData( imageHandle, fileData, 0, maGetDataSize(fileData)); maDestroyPlaceholder(fileData); if (RES_OK != result) { maDestroyPlaceholder(imageHandle); return 0; } // Return the handle to the loaded image. return imageHandle; }
/** * Copy a file. Overwrites the destination file. * @return 0 on success <0 on error. */ static int FileCopyFile( const String& sourcePath, const String& destinationPath) { // Open source file. MAHandle sourceFile = maFileOpen(sourcePath.c_str(), MA_ACCESS_READ_WRITE); if (sourceFile < 0) { return -1; } // Check that source file exists. int exists = maFileExists(sourceFile); if (1 != exists) { maFileClose(sourceFile); return -1; } // Get and check source size. int fileSize = maFileSize(sourceFile); if (fileSize < 0) { maFileClose(sourceFile); return -1; } // Create data object for source data to copy. MAHandle data = maCreatePlaceholder(); int createDataResult = maCreateData(data, fileSize); if (RES_OK != createDataResult) { maFileClose(sourceFile); maDestroyPlaceholder(data); return -1; } int readResult = maFileReadToData(sourceFile, data, 0, fileSize); if (readResult < 0) { maFileClose(sourceFile); maDestroyPlaceholder(data); return -1; } // This deletes the destination file if it already exists. FileDeleteFile(destinationPath); // Create destination file. bool createSuccess = FileCreatePath(destinationPath); if (!createSuccess) { maFileClose(sourceFile); maDestroyPlaceholder(data); return -1; } // Open destination file. MAHandle destinationFile = maFileOpen(destinationPath.c_str(), MA_ACCESS_READ_WRITE); if (destinationFile < 0) { maFileClose(sourceFile); maDestroyPlaceholder(data); return -1; } // Write data to destination file. int writeResult = maFileWriteFromData(destinationFile, data, 0, fileSize); if (writeResult < 0) { maFileClose(sourceFile); maFileClose(destinationFile); maDestroyPlaceholder(data); return -1; } // Close files and free data object. maFileClose(sourceFile); maFileClose(destinationFile); maDestroyPlaceholder(data); // Success. return 0; }