imagecache.c
来自「This is a resource based on j2me embedde」· C语言 代码 · 共 577 行 · 第 1/2 页
C
577 行
/* Get the amount of space available at this point */ remainingSpace = storage_get_free_space(storageId); errorCode = midp_suite_get_class_path(suiteId, storageId, KNI_TRUE, &jarFileName); if (errorCode != MIDP_ERROR_NONE) { return; } result = loadAndCacheJarFileEntries(&jarFileName, (jboolean (*)(const pcsl_string *))&png_filter, (jboolean (*)(const pcsl_string *))&png_cache_action); /* If something went wrong then clean up anything that was created */ if (result != 1) { REPORT_WARN1(LC_LOWUI, "Warning: image cache could not be created; Error: %d\n", result); deleteImageCache(suiteId, storageId); if (pOutDataSize != NULL) { *pOutDataSize = 0; } } else { if (pOutDataSize != NULL) { *pOutDataSize = (jint)cachedDataSize; } } pcsl_string_free(&jarFileName);}/** * Moves cached native images from ome storage to another. * For security reasons we allow to move cache only to the * internal storage. * * @param suiteId The suite ID * @param storageIdFrom ID of the storage where images are cached * @param storageIdTo ID of the storage where to move the cache */void moveImageCache(SuiteIdType suiteId, StorageIdType storageIdFrom, StorageIdType storageIdTo) { pcsl_string root; pcsl_string filePath; char* pszError; void* handle = NULL; jint errorCode; jsize oldRootLength; jsize newRootLength; const pcsl_string* newRoot; if (suiteId == UNUSED_SUITE_ID) { return; } /* * IMPL_NOTE: for security reasons we allow to move cache * only to the internal storage. */ if (storageIdTo != INTERNAL_STORAGE_ID) { return; } errorCode = midp_suite_get_cached_resource_filename(suiteId, storageIdFrom, &PCSL_STRING_EMPTY, &root); if (errorCode != MIDP_ERROR_NONE) { return; } handle = storage_open_file_iterator(&root); if (handle == NULL) { pcsl_string_free(&root); return; } newRoot = storage_get_root(storageIdTo); newRootLength = pcsl_string_length(newRoot); oldRootLength = pcsl_string_length(storage_get_root(storageIdFrom)); /* * Move all files that start with suite Id and end with * TMP_EXT to new storage. */ for (;;) { pcsl_string fileName; pcsl_string newFilePath = PCSL_STRING_NULL; jsize filePathLength; if (0 != storage_get_next_file_in_iterator(&root, handle, &filePath)) { break; } if (pcsl_string_ends_with(&filePath, &TMP_EXT)) { /* construct new file name. */ filePathLength = pcsl_string_length(&filePath); pcsl_string_predict_size(&fileName, filePathLength - oldRootLength); if (PCSL_STRING_OK != pcsl_string_substring(&filePath, oldRootLength, filePathLength, &fileName)) { break; } pcsl_string_predict_size(&newFilePath, newRootLength + pcsl_string_length(&fileName)); if (PCSL_STRING_OK != pcsl_string_append(&newFilePath, newRoot)) { pcsl_string_free(&fileName); break; } if (PCSL_STRING_OK != pcsl_string_append(&newFilePath, (const pcsl_string*)&fileName)) { pcsl_string_free(&fileName); pcsl_string_free(&newFilePath); break; } /* rename file. */ storage_rename_file(&pszError, &filePath, &newFilePath); pcsl_string_free(&fileName); pcsl_string_free(&newFilePath); if (pszError != NULL) { storageFreeError(pszError); } } pcsl_string_free(&filePath); } storageCloseFileIterator(handle); pcsl_string_free(&root);}/** * Store a native image to cache. * * @param suiteId The suite id * @param storageId ID of the storage where to create the cache * @param resName The image resource name * @param bufPtr The buffer with the image data * @param len The length of the buffer * @return 1 if successful, 0 if not */static int storeImageToCache(SuiteIdType suiteId, StorageIdType storageId, const pcsl_string * resName, unsigned char *bufPtr, int len) { int handle = -1; char* errmsg = NULL; pcsl_string path; int status = 1; int errorCode; if (suiteId == UNUSED_SUITE_ID || pcsl_string_is_null(resName) || (bufPtr == NULL)) { return 0; } /* Build the complete path */ errorCode = midp_suite_get_cached_resource_filename(suiteId, storageId, resName, &path); if (errorCode != MIDP_ERROR_NONE) { return 0; } /* Open the file */ handle = storage_open(&errmsg, &path, OPEN_READ_WRITE_TRUNCATE); pcsl_string_free(&path); if (errmsg != NULL) { REPORT_WARN1(LC_LOWUI,"Warning: could not save cached image; %s\n", errmsg); storageFreeError(errmsg); return 0; } /* Finally write the file */ storageWrite(&errmsg, handle, (char*)bufPtr, len); if (errmsg != NULL) { status = 0; storageFreeError(errmsg); } storageClose(&errmsg, handle); return status;}/** * Loads a native image from cache, if present. * * @param suiteId The suite id * @param resName The image resource name * @param **bufPtr Pointer where a buffer will be allocated and data stored * @return -1 if failed, else length of buffer */int loadImageFromCache(SuiteIdType suiteId, const pcsl_string * resName, unsigned char **bufPtr) { int len = -1; int handle; char* errmsg = NULL; int bytesRead; pcsl_string path; pcsl_string resNameFix; MIDPError errorCode; StorageIdType storageId; pcsl_string_status res; if (suiteId == UNUSED_SUITE_ID || pcsl_string_is_null(resName)) { return len; } /* If resource starts with slash, remove it */ if (pcsl_string_index_of(resName, (jint)'/') == 0) { jsize resNameLen = pcsl_string_length(resName); res = pcsl_string_substring(resName, 1, resNameLen, &resNameFix); } else { res = pcsl_string_dup(resName, &resNameFix); } if (PCSL_STRING_OK != res) { return len; } /* * IMPL_NOTE: here is assumed that the image cache is located in * the same storage as the midlet suite. This may not be true. */ /* Build path */ errorCode = midp_suite_get_suite_storage(suiteId, &storageId); if (errorCode != ALL_OK) { return len; } errorCode = midp_suite_get_cached_resource_filename(suiteId, storageId, &resNameFix, &path); pcsl_string_free(&resNameFix); if (errorCode != ALL_OK) { return len; } /* Open file */ handle = storage_open(&errmsg, &path, OPEN_READ); pcsl_string_free(&path); if (errmsg != NULL) { REPORT_WARN1(LC_LOWUI,"Warning: could not load cached image; %s\n", errmsg); storageFreeError(errmsg); return len; } do { /* Get size of file and allocate buffer */ len = storageSizeOf(&errmsg, handle); *bufPtr = midpMalloc(len); if (*bufPtr == NULL) { len = -1; break; } /* Read data */ bytesRead = storageRead(&errmsg, handle, (char*)*bufPtr, len); if (errmsg != NULL) { len = -1; midpFree(*bufPtr); storageFreeError(errmsg); break; } /* Sanity check */ if (bytesRead != len) { len = -1; midpFree(*bufPtr); break; } } while (0); storageClose(&errmsg, handle); return len;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?