/** * Get URL component paths. * Decompose a URL into its constituent components, returned strings should be freed with NCSFree(). * @param pURLPath A pointer to a C string. eg ecwp://www.earthetc.com/images/usa/1metercalif.ecw * @param ppProtocol The address of a pointer to the C string returned. eg ecwp:// * @param ppHost The address of a pointer to the C string returned. eg www.earthetc.com * @param ppFilename The address of a pointer to the C string returned. eg images/usa/1metercalif.ecw * @see NCSFree() * @return TRUE or FALSE if the path represented a valid URL */ BOOLEAN CNCSFile::BreakdownURL( char *pURLPath, char **ppProtocol, char **ppHost, char **ppFilename) { int nProtocolLength; int nHostLength; int nFilenameLength; char *pHost, *pProtocol, *pFilename; BOOLEAN bError = NCSecwNetBreakdownUrl(pURLPath, &pProtocol, &nProtocolLength, &pHost, &nHostLength, &pFilename, &nFilenameLength); if (bError == TRUE) { *ppProtocol = (char *)NCSMalloc(nProtocolLength + 1, TRUE); strncpy(*ppProtocol, pProtocol, nProtocolLength); pHost+=2; nHostLength -= 3; *ppHost = (char *)NCSMalloc(nHostLength + 1, TRUE); strncpy(*ppHost, pHost, nHostLength); *ppFilename = (char *)NCSMalloc(nFilenameLength + 1, TRUE); strncpy(*ppFilename, pFilename, nFilenameLength); } return bError; }
void CNCSJPCEcwpIOStream::PacketRecvCB(NCSPacket *pPacket, INT32 nLength, pNCSnetClient pClientNetID, CNCSJPCEcwpIOStream *pStream, NCSError eError) { // Check returned error code if( eError != NCS_SUCCESS ) { /**[11]**/ pStream->Lock(); pStream->m_bIsConnected = false; pStream->UnLock(); return; } pStream->Lock(false); ReceivedPacket *pRP = (ReceivedPacket*)NCSMalloc(sizeof(ReceivedPacket), FALSE); if(pRP) { pRP->pPacket = pPacket; pRP->nLength = nLength; pStream->m_ReceivedPackets.push_back(pRP); } pStream->UnLock(false); }
// // NCSWinHttpGetConnectionStatusText: gets the status text on the connection // BOOL NCSWinHttpGetConnectionStatusText(LPVOID hInstance, LPCSTR* ppwszStatus){ LPVOID lpBuffer = NULL; DWORD lpdwBufferLength; DWORD dwIndex; BOOL bValue = FALSE; DWORD dwLen = 0; // Get the length of the text buffer, by passing a null buffer NCSWinHttpQueryHeaders((HINTERNET)hInstance, WINHTTP_QUERY_STATUS_TEXT, WINHTTP_HEADER_NAME_BY_INDEX, NULL, &lpdwBufferLength, &dwIndex); lpBuffer = NCSMalloc(lpdwBufferLength, TRUE); // Get the actual buffer bValue = NCSWinHttpQueryHeaders((HINTERNET)hInstance, WINHTTP_QUERY_STATUS_TEXT, WINHTTP_HEADER_NAME_BY_INDEX, lpBuffer, &dwLen, &dwIndex); ppwszStatus = lpBuffer; return bValue; }
// Open the stream on the specified full path name. CNCSError CNCSJPCFileIOStream::Open(wchar_t*pFileName, bool bWrite) { *(CNCSError*)this = NCSFileOpen(pFileName, bWrite ? NCS_FILE_READ_WRITE : NCS_FILE_READ, &m_hFile); if(GetErrorNumber() == NCS_SUCCESS) { *(CNCSError*)this = CNCSJPCIOStream::Open(pFileName, bWrite); m_pIOCache = (UINT8*)NCSMalloc(m_nMaxIOCache, FALSE); m_nIOWriteCache = 0; m_iIOReadCache = m_nMaxIOCache; m_nFileSize = NCSFileSizeBytes(m_pName); } return(*(CNCSError*)this); }
// Open the stream on the specified full path name. CNCSError CNCSJPCFileIOStream::Open(char *pFileName, bool bWrite) { *(CNCSError*)this = NCSFileOpen(OS_STRING(pFileName), bWrite ? (NCS_FILE_READ_WRITE|NCS_FILE_CREATE) : NCS_FILE_READ, &m_hFile); if(GetErrorNumber() == NCS_SUCCESS) { *(CNCSError*)this = CNCSJPCIOStream::Open(pFileName, bWrite); m_pIOCache = (UINT8*)NCSMalloc(m_nMaxIOCache, FALSE); m_nIOWriteCache = 0; m_iIOReadCache = m_nMaxIOCache; if(bWrite) { m_nFileSize = 0; } else { m_nFileSize = NCSFileSizeBytes(m_pName); } } return(*(CNCSError*)this); }
NCSError NCSCreateJNIInfoStruct(JNIEnv *pEnv, jobject ECWFile, NCSFileView *pNCSFileView, NCSJNIInfo **pReturn) { NCSJNIInfo *pJNIInfo; char *pErrorString = NULL; pJNIInfo = (NCSJNIInfo *)NCSMalloc(sizeof(NCSJNIInfo), TRUE); if (pJNIInfo != NULL) { pJNIInfo->pFileView = pNCSFileView; //pJNIInfo->ECWFile = (void *)(*pEnv)->NewGlobalRef(pEnv, ECWFile); *pReturn = pJNIInfo; } else { return NCS_COULDNT_ALLOC_MEMORY; } return NCS_SUCCESS; }
/* * Class: com_ermapper_ecw_JNCSFile * Method: ECWSetView * Signature: (I[IIIIIDDDDII)I */ JNIEXPORT jint JNICALL Java_com_ermapper_ecw_JNCSFile_ECWSetView (JNIEnv *pEnv, jobject ECWFile, jint nBands, jintArray nBandList, jint nDatasetTLX, jint nDatasetTLY, jint nDatasetBRX, jint nDatasetBRY, jdouble dWorldTLX, jdouble dWorldTLY, jdouble dWorldBRX, jdouble dWorldBRY, jint nWidth, jint nHeight) { NCSError nError = NCS_SUCCESS; NCSJNIInfo *pJNIInfo = NULL; // Sanity check if ((*pEnv)->IsInstanceOf(pEnv, ECWFile, (*pEnv)->FindClass(pEnv, "com/ermapper/ecw/JNCSFile")) == JNI_FALSE) { #ifdef WIN32 MessageBox(NULL, OS_STRING("ECWSetView() error : object is not a JNCSFile instance"), NCS_T("JNCSClass Library (JNI)"), MB_OK); #else fprintf(stderr, "ECWSetView() error : object is not a JNCSFile instance"); #endif return NCS_JNI_ERROR; } pJNIInfo = (NCSJNIInfo *)(*pEnv)->GetLongField(pEnv, ECWFile, pGlobalJNCSFieldIDs->jIDNativeDataPointer); if (pJNIInfo) { jint *pBandBuffer = (jint *)NCSMalloc(sizeof(UINT32)*nBands+1, TRUE); (*pEnv)->GetIntArrayRegion(pEnv, nBandList, 0, nBands, pBandBuffer); nError = NCScbmSetFileViewEx( pJNIInfo->pFileView, nBands, (UINT32*)pBandBuffer, nDatasetTLX, nDatasetTLY, nDatasetBRX, nDatasetBRY, nWidth, nHeight, dWorldTLX, dWorldTLY, dWorldBRX, dWorldBRY); NCSFree(pBandBuffer); } else { NCSFormatErrorText(NCS_JNI_ERROR, "method SetView() could not get native data from JNCSFile object."); nError = NCS_JNI_ERROR; } return nError; }
extern "C" NCSError NCSEcwEditReadInfo(char *pFilename, NCSEcwEditInfo **ppInfo) { NCSError eError = NCS_SUCCESS; if(ppInfo) { NCSEcwEditInfo *pInfo = (NCSEcwEditInfo*)NULL; NCSFile *pFile = (NCSFile*)NULL; eError = NCSecwOpenFile(&pFile, pFilename, FALSE, FALSE); if(eError == NCS_SUCCESS) { pInfo = (NCSEcwEditInfo*)NCSMalloc(sizeof(NCSEcwEditInfo), TRUE); if(pInfo) { pInfo->nVersion = pFile->pTopQmf->p_file_qmf->version; pInfo->bCompressedOffsetTable = pFile->pTopQmf->bRawBlockTable ? FALSE : TRUE; pInfo->eCellSizeUnits = pFile->pFileInfo->eCellSizeUnits; pInfo->fCellIncrementX = pFile->pFileInfo->fCellIncrementX; pInfo->fCellIncrementY = pFile->pFileInfo->fCellIncrementY; pInfo->fOriginX = pFile->pFileInfo->fOriginX; pInfo->fOriginY = pFile->pFileInfo->fOriginY; pInfo->szDatum = NCSStrDup(pFile->pFileInfo->szDatum); pInfo->szProjection = NCSStrDup(pFile->pFileInfo->szProjection); } else { eError = NCS_COULDNT_ALLOC_MEMORY; } // Force it to close pFile->bValid = FALSE; NCSecwCloseFile(pFile); } *ppInfo = pInfo; } else { eError = NCS_INVALID_PARAMETER; } return(eError); }
/* Allocate a new CompressionClient structure and fill in defaults */ extern "C" NCSEcwCompressClient *NCSEcwCompressAllocClient(void) { NCSEcwCompressClient *pClient; if((pClient = (NCSEcwCompressClient*)NCSMalloc(sizeof(NCSEcwCompressClient), TRUE)) != NULL) { pClient->fTargetCompression = 10; pClient->eCompressFormat = COMPRESS_UINT8; pClient->eCompressHint = COMPRESS_HINT_INTERNET; pClient->nBlockSizeX = X_BLOCK_SIZE; pClient->nBlockSizeY = Y_BLOCK_SIZE; pClient->fCellIncrementX = 1.0; pClient->fCellIncrementY = 1.0; pClient->fOriginX = 0.0; pClient->fOriginY = 0.0; pClient->eCellSizeUnits = ECW_CELL_UNITS_METERS; strcpy(pClient->szDatum, "RAW"); strcpy(pClient->szProjection, "RAW"); } return(pClient); }
// // NCSWinHttpIsConnectionViaProxy: determines if this connection is via a proxy // BOOL NCSWinHttpIsConnectionViaProxy(LPVOID hInternet) { BOOLEAN bIsConnIsViaProxy = FALSE; DWORD nLen = sizeof(WINHTTP_PROXY_INFO); WINHTTP_PROXY_INFO *pProxyInfo = (WINHTTP_PROXY_INFO *)NULL; // Get the size of the structure it will return by passing in a null pointer. NCSWinHttpQueryOption((HINTERNET)hInternet, WINHTTP_OPTION_PROXY, (void *)NULL, &nLen); pProxyInfo = NCSMalloc(nLen, TRUE); // Now get the actual structure. if (NCSWinHttpQueryOption((HINTERNET)hInternet, WINHTTP_OPTION_PROXY, pProxyInfo, &nLen)) { if (pProxyInfo->dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY) { bIsConnIsViaProxy = TRUE; } else { bIsConnIsViaProxy = FALSE; } } if (pProxyInfo) NCSFree(pProxyInfo); return bIsConnIsViaProxy; }
/* ** Create a Timer. Actual timer period is tsPeriod + exec time of pFunc. */ NCSTimer *NCSTimerCreate(NCSTimeStampMs tsPeriod, void (*pFunc)(void*), void *pData) { NCSTimer *pTimer; if((pTimer = (NCSTimer *)NCSMalloc(sizeof(NCSTimer), TRUE)) != NULL) { pTimer->tsPeriod = tsPeriod; pTimer->pFunc = pFunc; pTimer->pData = pData; pTimer->bRun = TRUE; pTimer->bClear = FALSE; pTimer->bRunning = FALSE; if(NCSThreadSpawn(&(pTimer->tid), (void(*)(void*))NCSTimerFunc, pTimer, FALSE)) { return(pTimer); } NCSFree(pTimer); } return((NCSTimer*)NULL); }
// Open the stream on the specified memory. CNCSError CNCSJPCEcwpIOStream::Open(char *pEcwpURL) { Lock(); Close(); *(CNCSError*)this = NCScbmNetFileOpenInternal((UINT8**)&m_pMemory, &m_nMemoryLength, &m_ClientNetID, &m_ClientUID, (NCSnetPacketSentCB*)PacketSentCB, (NCSnetPacketRecvCB*)PacketRecvCB, (void*)this, pEcwpURL); if(GetErrorNumber() == NCS_SUCCESS) { *(CNCSError*)this = CNCSJPCIOStream::Open(pEcwpURL, false); m_bIsConnected = true; if(!m_pSendPacket) { m_pSendPacket = (NCSPacket*)NCSMalloc( NCSECW_MAX_SEND_PACKET_SIZE + 1 , FALSE); } } UnLock(); return(*(CNCSError*)this); }
CNCSError ECWDEMCompressor::Compress(ImageFormatECWDEM *NewIFECW, RasterBounds *RBounds, char *pDstFile, UINT32 ImgWidth, UINT32 ImgHeight, char Depth, UINT16 nRate) { //NCSFileViewFileInfoEx has basic info about the file - dimensions, bands, //georeferencing information etc. NCSFileViewFileInfoEx *pDstInfo = (NCSFileViewFileInfoEx *)NCSMalloc(sizeof(NCSFileViewFileInfoEx), true); NCSFileViewFileInfoEx DstInfo = *pDstInfo; CNCSError Error; IFECW = NewIFECW; DstInfo.nSizeX = ImgWidth; DstInfo.nSizeY = ImgHeight; switch (Depth) { case 8: DstInfo.eCellType = NCSCT_UINT8; break; case 16: DstInfo.eCellType = NCSCT_UINT16; break; //case 28: DstInfo.eCellType = NCSCT_IEEE4; break; case 28: DstInfo.eCellType = NCSCT_UINT32; break; } // switch DstInfo.nBands = 1; DstInfo.nCompressionRate = nRate; DstInfo.eColorSpace = NCSCS_GREYSCALE; #ifdef WCS_BUILD_VNS if (RBounds && RBounds->FetchCoordSys()) { IFECW->CreateECWGeoRefNewAPI(RBounds->FetchCoordSys(), &DstInfo, RBounds); } // if #endif // WCS_BUILD_VNS //DstInfo.szProjection = "RAW"; //DstInfo.szDatum = "RAW"; //DstInfo.eCellSizeUnits = ECW_CELL_UNITS_METERS; //DstInfo.fCellIncrementX = 1.0; //DstInfo.fCellIncrementY = 1.0; //DstInfo.fOriginX = 0.0; //DstInfo.fOriginY = 0.0; DstInfo.pBands = (NCSFileBandInfo *)(new NCSFileBandInfo[DstInfo.nBands]); DstInfo.pBands[0].nBits = Depth; DstInfo.pBands[0].bSigned = false; DstInfo.pBands[0].szDesc = "Elevation"; //Call SetFileInfo to establish the file information we are going to //use for compression. The parameters used are those from the NCSFileViewFileInfoEx //struct we have populated using metadata derived from our input raster. Error = SetFileInfo(DstInfo); Error = Open(pDstFile, false, true); if (Error == NCS_SUCCESS) { #ifdef WCS_BUILD_JP2BOX_SUPPORT JP2UUID3DNBox RangeEquivs(NewIFECW->HighElev, NewIFECW->LowElev, (unsigned long int)NewIFECW->MaxValue, 0); AddBox(&RangeEquivs); #endif // WCS_BUILD_JP2BOX_SUPPORT Error = Write(); if (Error == NCS_SUCCESS) fprintf(stdout,"Finished compression\n"); else if (Error == NCS_USER_CANCELLED_COMPRESSION) fprintf(stdout,"Compression cancelled\n"); else fprintf(stdout,"Error during compression: %s\n",Error.GetErrorMessage()); Error = Close(true); } // if return(Error); } // ECWDEMCompressor::Compress
//#ifdef NCSJPC_ECW_SUPPORT //void NCSecwInit_ECW() //#else void NCSecwInitInternal() //#endif { if( !pNCSEcwInfo ) { NCSecwStatistics *pStatistics = (NCSecwStatistics *)NULL;// &cNCSEcwSharedStatistics; NCSGlobalLockInfo *pLock; /**[10]**/ pLock = NCSGlobalLock(ECW_STATS_LOCK_NAME); /**[10]**/ pNCSEcwInfo = (NCSEcwInfo *) NCSMalloc(sizeof(NCSEcwInfo), TRUE); /**[17]**/ pNCSEcwInfo->pIDWT = (NCSidwt *) NCSMalloc(sizeof(NCSidwt), TRUE); /**[17]**/ pNCSEcwInfo->pNCSFileList = NULL; #ifdef WIN32 if(!pStatistics) { /**[13]**/ // No NCSecw.dll in system dir, so use shared memory for stats instead. pNCSEcwInfo->pStatisticsSHM = NCSMapSHM(sizeof(NCSecwStatistics), TRUE, NCS_STATUS_STRUCT_NAME_VERSION_2); if(pNCSEcwInfo->pStatisticsSHM == NULL) { // Fails if SHM already exists, which means it's V2 with a global lock pNCSEcwInfo->pStatisticsSHM = NCSMapSHM(sizeof(NCSecwStatistics), FALSE, NCS_STATUS_STRUCT_NAME_VERSION_2); pNCSEcwInfo->nStatsStructVersion = ECW_STATS_STRUCT_VERSION_2; pNCSEcwInfo->StatsLockKey = NCSThreadLSAlloc(); } else { // Worked, which means no V2 SHM exists currently, free it and create a V3 one NCSUnmapSHM(pNCSEcwInfo->pStatisticsSHM); pNCSEcwInfo->pStatisticsSHM = NCSMapSHM(sizeof(NCSecwStatistics), FALSE, NCS_STATUS_STRUCT_NAME_VERSION); pNCSEcwInfo->nStatsStructVersion = ECW_STATS_STRUCT_VERSION; } pStatistics = (NCSecwStatistics*)pNCSEcwInfo->pStatisticsSHM->pData; } #elif defined(MACINTOSH)||defined(POSIX) pStatistics = (NCSecwStatistics *)NCSMalloc(sizeof(NCSecwStatistics), TRUE); pNCSEcwInfo->nStatsStructVersion = ECW_STATS_STRUCT_VERSION; #endif pNCSEcwInfo->pStatistics = pStatistics; // shared statistics across applications NCSGlobalUnlock(pLock); /**[10]**/ pNCSEcwInfo->bEcwpReConnect = FALSE; //[21] pNCSEcwInfo->bJP2ICCManage = TRUE; //[22] pNCSEcwInfo->nMaxJP2FileIOCache = 1024; //[22] pNCSEcwInfo->nMaxProgressiveViewSize = NCSECW_MAX_VIEW_SIZE_TO_CACHE; /**[25]**/ pNCSEcwInfo->nForceFileReopen = FALSE; // normally want to merge file opens for optimization pNCSEcwInfo->bForceLowMemCompress = FALSE; pNCSEcwInfo->tLastCachePurge = NCSGetTimeStampMs(); pNCSEcwInfo->nAggressivePurge = 0; // larger number means be more aggressive next purge pNCSEcwInfo->pIDWT->eIDWTState = NCSECW_THREAD_DEAD; pNCSEcwInfo->nMaximumOpen = NCSECW_MAX_UNUSED_CACHED_FILES; /**[18]**/ #ifdef POSIX pNCSEcwInfo->nMaximumOpen = MIN(pNCSEcwInfo->nMaximumOpen, (UINT32)sysconf(_SC_OPEN_MAX) / 4); /**[18]**/ #endif // POSIX #ifdef WIN32 pNCSEcwInfo->m_hSuspendEvent = NULL; #endif // WIN32 pNCSEcwInfo->nPurgeDelay = NCSECW_PURGE_DELAY_MS; /**[19]**/ pNCSEcwInfo->nFilePurgeDelay = NCSECW_FILE_PURGE_TIME_MS; /**[19]**/ pNCSEcwInfo->nMinFilePurgeDelay = NCSECW_FILE_MIN_PURGE_TIME_MS; /**[19]**/ pNCSEcwInfo->nMaxOffsetCache = NCSECW_MAX_OFFSET_CACHE; /**[19]**/ if( pStatistics->nApplicationsOpen == 0 ) { #ifdef MACINTOSH // This causes problems on the Mac, so we'll disable caching until we get // the threading/IPS/Shared memory support in place pStatistics->nMaximumCacheSize = NCSPhysicalMemorySize() / 4; #else pStatistics->nMaximumCacheSize = NCSPhysicalMemorySize() / 4; #endif // FIXME!! Should remember these by storing them in the registry pStatistics->nBlockingTime = NCSECW_BLOCKING_TIME_MS; pStatistics->nRefreshTime = NCSECW_REFRESH_TIME_MS; } NCSEcwStatsLock(); NCSEcwStatsIncrement(&pStatistics->nApplicationsOpen, 1); NCSEcwStatsUnLock(); #if defined LIBECWJ2 || defined NCSECW_STATIC_LIBS NCSUtilInit(); NCScnetInit(); #endif NCSMutexInit(&pNCSEcwInfo->mutex); #ifdef NCSJPC_ECW_SUPPORT NCScbmInitThreadIDWT(pNCSEcwInfo->pIDWT); #endif // NCSJPC_ECW_SUPPORT pNCSEcwInfo->bShutdown = FALSE; } }
/* * Class: com_ermapper_ecw_JNCSFile * Method: NCSJNIInit * Signature: ()I */ JNIEXPORT jint JNICALL Java_com_ermapper_ecw_JNCSFile_NCSJNIInit (JNIEnv *pEnv, jclass ECWFileClass) { NCSJNIFieldIDs *pJNIInfo = NULL; char *pErrorString = NULL; if (!pGlobalJNCSFieldIDs) { pJNIInfo = (NCSJNIFieldIDs *)NCSMalloc(sizeof(NCSJNIFieldIDs), TRUE); // Get all the field ids of the ECWFile object pJNIInfo->jIDNativeDataPointer = (*pEnv)->GetFieldID(pEnv, ECWFileClass, "nativeDataPointer", "J" ); pJNIInfo->jIDWidth = (*pEnv)->GetFieldID(pEnv, ECWFileClass, "width", "I" ); pJNIInfo->jIDHeight = (*pEnv)->GetFieldID(pEnv, ECWFileClass, "height", "I" ); pJNIInfo->jIDNumberOfBands = (*pEnv)->GetFieldID(pEnv, ECWFileClass, "numBands", "I" ); pJNIInfo->jIDCompressionRate = (*pEnv)->GetFieldID(pEnv, ECWFileClass, "compressionRate", "D" ); pJNIInfo->jIDCellIncrementX = (*pEnv)->GetFieldID(pEnv, ECWFileClass, "cellIncrementX", "D" ); pJNIInfo->jIDCellIncrementY = (*pEnv)->GetFieldID(pEnv, ECWFileClass, "cellIncrementY", "D" ); pJNIInfo->jIDCellSizeUnits = (*pEnv)->GetFieldID(pEnv, ECWFileClass, "cellSizeUnits", "I" ); pJNIInfo->jIDOriginX = (*pEnv)->GetFieldID(pEnv, ECWFileClass, "originX", "D" ); pJNIInfo->jIDOriginY = (*pEnv)->GetFieldID(pEnv, ECWFileClass, "originY", "D" ); pJNIInfo->jIDDatum = (*pEnv)->GetFieldID(pEnv, ECWFileClass, "datum", "Ljava/lang/String;" ); pJNIInfo->jIDProjection = (*pEnv)->GetFieldID(pEnv, ECWFileClass, "projection", "Ljava/lang/String;" ); pJNIInfo->jIDFilename = (*pEnv)->GetFieldID(pEnv, ECWFileClass, "fileName", "Ljava/lang/String;" ); pJNIInfo->jIDIsOpen = (*pEnv)->GetFieldID(pEnv, ECWFileClass, "bIsOpen", "Z" ); pJNIInfo->jIDRefreshUpdateMethod= (*pEnv)->GetMethodID(pEnv, ECWFileClass, "refreshUpdate", "(IIDDDD)V"); pJNIInfo->jIDFileType = (*pEnv)->GetFieldID(pEnv, ECWFileClass, "fileType", "I" ); pJNIInfo->jIDFileMimeType = (*pEnv)->GetFieldID(pEnv, ECWFileClass, "mimeType", "Ljava/lang/String;" ); // Do some error checking if (!pJNIInfo->jIDNativeDataPointer) pErrorString = "Could not determine fieldID for 'nativeDataPointer' in ECWFile object."; if (!pJNIInfo->jIDWidth) pErrorString = "Could not determine fieldID for 'width' in ECWFile object."; if (!pJNIInfo->jIDHeight) pErrorString = "Could not determine fieldID for 'height' in ECWFile object."; if (!pJNIInfo->jIDNumberOfBands) pErrorString = "Could not determine fieldID for 'numBands' in ECWFile object."; if (!pJNIInfo->jIDCompressionRate) pErrorString = "Could not determine fieldID for 'compressionRate' in ECWFile object."; if (!pJNIInfo->jIDCellIncrementX) pErrorString = "Could not determine fieldID for 'cellIncrementX' in ECWFile object."; if (!pJNIInfo->jIDCellIncrementY) pErrorString = "Could not determine fieldID for 'cellIncrementY' in ECWFile object."; if (!pJNIInfo->jIDCellSizeUnits) pErrorString = "Could not determine fieldID for 'cellSizeUnits' in ECWFile object."; if (!pJNIInfo->jIDOriginX) pErrorString = "Could not determine fieldID for 'originX' in ECWFile object."; if (!pJNIInfo->jIDOriginY) pErrorString = "Could not determine fieldID for 'originY' in ECWFile object."; if (!pJNIInfo->jIDDatum) pErrorString = "Could not determine fieldID for 'datum' in ECWFile object."; if (!pJNIInfo->jIDProjection) pErrorString = "Could not determine fieldID for 'projection' in ECWFile object."; if (!pJNIInfo->jIDFilename) pErrorString = "Could not determine fieldID for 'fileName' in ECWFile object."; if (!pJNIInfo->jIDIsOpen) pErrorString = "Could not determine fieldID for 'bIsOpen' in ECWFile object."; if (!pJNIInfo->jIDFileType) pErrorString = "Could not determine fieldID for 'fileType' in ECWFile object."; if (!pJNIInfo->jIDFileMimeType) pErrorString = "Could not determine fieldID for 'mimeType' in ECWFile object."; if (pErrorString) { #ifdef WIN32 MessageBox(NULL, OS_STRING(pErrorString), NCS_T("JNCSClass Library (JNI)"), MB_OK); #else fprintf(stderr, "JNCSClass Library (JNI) : %s\n", pErrorString); #endif NCSFormatErrorText(NCS_JNI_ERROR, pErrorString); return NCS_JNI_ERROR; } else { pGlobalJNCSFieldIDs = pJNIInfo; return NCS_SUCCESS; } } else { return NCS_SUCCESS; } }
NCSError NCSecwOpenFile( NCSFile **ppNCSFile, char *szUrlPath, // input file name or network path BOOLEAN bReadOffsets, // TRUE if the client wants the block Offset Table BOOLEAN bReadMemImage) // TRUE if the client wants a Memory Image of the Header { NCSFile *pNCSFile = NULL; UINT32 nSemiUniqueId; UINT8 *pFileHeaderMemImage=NULL; UINT32 nFileHeaderMemImageLen=0; char *pProtocol, *pHost, *pFilename; int nProtocolLength, nHostLength, nFilenameLength; pProtocol = (char *)NULL; pHost = (char *)NULL; pFilename = (char *)NULL; if (!pNCSEcwInfo) { NCSecwInitInternal(); } if (!ppNCSFile) return NCS_INVALID_ARGUMENTS; *ppNCSFile = NULL; NCSMutexBegin(&pNCSEcwInfo->mutex); // Get URL info first off. This will also sort out / vs \ problems NCSecwNetBreakdownUrl(szUrlPath, &pProtocol, &nProtocolLength, &pHost, &nHostLength, &pFilename, &nFilenameLength); nSemiUniqueId = NCSecwSemiUniqueId(szUrlPath); // See if the file is already in the cache system // unless nForceFileReopen == TRUE, in which case we // are running server load tests, so want to open a new // file channel if( pNCSEcwInfo->nForceFileReopen == FALSE ) { pNCSFile = pNCSEcwInfo->pNCSFileList; while(pNCSFile) { if( pNCSFile->SemiUniqueId == nSemiUniqueId && pNCSFile->bValid) { if( stricmp(pNCSFile->szUrlPath, szUrlPath) == 0 ) { // See if the file has changed on disk. If so, mark it as invalid and keep looking // in cache (the file cache will be removed from cache once no opens are present for it) #ifdef NOTUSED // no longer required, file is locked on disk so can't change, this just sucks up CPU cycles... if( pNCSFile->bLocalFile ) { DATETIME tModifiedTime;/**[17]**/ if( NCSecwGetFileModifiedTime(pNCSFile, &tModifiedTime) || tModifiedTime > pNCSFile->tModifiedTime ) { NCSEcwStatsLock(); pNCSFile->bValid = FALSE; NCSEcwStatsIncrement(&pNCSEcwInfo->pStatistics->nFilesModified, 1); NCSEcwStatsUnlock(); } } #endif if((pProtocol && pHost) || /*[08]*/ ((!bReadOffsets || (bReadOffsets && pNCSFile->bReadOffsets)) && /*[08]*/ (!bReadMemImage || (bReadMemImage && pNCSFile->bReadMemImage)))) { /*[08]*/ if( pNCSFile->bValid ) break; // found a match, so use the cached file } } } pNCSFile = pNCSFile->pNextNCSFile; } } // If we have a match, just return the existing file if( pNCSFile ) { NCSEcwStatsLock(); NCSEcwStatsIncrement(&pNCSEcwInfo->pStatistics->nFilesCacheHits, 1); if( !pNCSFile->nUsageCount) { NCSEcwStatsIncrement(&pNCSEcwInfo->pStatistics->nFilesOpen, 1); NCSEcwStatsDecrement(&pNCSEcwInfo->pStatistics->nFilesCached, 1); } NCSEcwStatsUnLock(); pNCSFile->nUsageCount += 1; NCSMutexEnd(&pNCSEcwInfo->mutex); *ppNCSFile = pNCSFile; return(NCS_SUCCESS); } NCSEcwStatsLock(); // File was not in cache, so have to load it NCSEcwStatsIncrement(&pNCSEcwInfo->pStatistics->nFilesCacheMisses, 1); NCSEcwStatsUnLock(); pNCSFile = (NCSFile *) NCSMalloc(sizeof(NCSFile), FALSE); if( !pNCSFile ) { NCSMutexEnd(&pNCSEcwInfo->mutex); return(NCS_FILE_NO_MEMORY); } pNCSFile->szUrlPath = (char *)NCSMalloc( (UINT32)strlen(szUrlPath) + 1, FALSE); if( !pNCSFile->szUrlPath ) { NCSFree(pNCSFile); NCSMutexEnd(&pNCSEcwInfo->mutex); return(NCS_FILE_NO_MEMORY); } strcpy(pNCSFile->szUrlPath, szUrlPath); // Init client specific information pNCSFile->pBlockCachePool = NULL; pNCSFile->pFirstCachedBlock = NULL; pNCSFile->pWorkingCachedBlock = NULL; pNCSFile->pLastReceivedCachedBlock = NULL; pNCSFile->pSendPacket = NULL; pNCSFile->nClientUID = 0; pNCSFile->nServerSequence = 0; pNCSFile->nClientSequence = 1; pNCSFile->pLevel0ZeroBlock = pNCSFile->pLevelnZeroBlock = NULL; pNCSFile->pClientNetID = NULL; pNCSFile->bSendInProgress = FALSE; pNCSFile->nRequestsXmitPending = 0; pNCSFile->nCancelsXmitPending = 0; pNCSFile->tLastSetViewTime = NCSGetTimeStampMs(); // init list and file information pNCSFile->bValid = TRUE; // file is currently valid (not changed on disk yet) pNCSFile->pNextNCSFile = pNCSFile->pPrevNCSFile = NULL; pNCSFile->nUsageCount = 1; pNCSFile->SemiUniqueId = nSemiUniqueId; pNCSFile->bIsConnected = TRUE; pNCSFile->bIsCorrupt = FALSE; pNCSFile->bFileIOError = FALSE; NCSMutexInit( &pNCSFile->mFileAccess ); //[20] // decide if local or remote file { if( pProtocol && pHost ) { NCSError nResult = NCScbmNetFileOpen(&pFileHeaderMemImage, &nFileHeaderMemImageLen, pNCSFile, pProtocol); if( nResult != NCS_SUCCESS) { NCSFree(pNCSFile->szUrlPath); NCSFree(pNCSFile); NCSMutexEnd(&pNCSEcwInfo->mutex); return(nResult); } pNCSFile->bLocalFile = FALSE; } else { pFileHeaderMemImage = NULL; pNCSFile->bLocalFile = TRUE; } } if( pNCSFile->bLocalFile ) { // always read offsets and Memory Image for a local file, even if not asked for #ifdef NOTDEF /*[07]*/ bReadOffsets = TRUE; #endif bReadMemImage = TRUE; } else { // don't read offsets or memory image bReadOffsets = FALSE; bReadMemImage = FALSE; } pNCSFile->bReadOffsets = bReadOffsets; pNCSFile->bReadMemImage = bReadMemImage; pNCSFile->pNCSFileViewList = NULL; pNCSFile->pNCSCachedFileViewList = NULL; pNCSFile->pOffsetCache = NULL; pNCSFile->nOffsetCache = 0; pNCSFile->tsLastReconnectTry = 0; //[21] pNCSFile->nReconnectCount = 0; // Get file modified time // if( pNCSFile->bLocalFile ) // (void) NCSecwGetFileModifiedTime(pNCSFile, &pNCSFile->tModifiedTime); pNCSFile->pTopQmf = erw_decompress_open( szUrlPath, pFileHeaderMemImage, bReadOffsets, bReadMemImage ); //[21] if( !pNCSFile->pTopQmf ) { if( pFileHeaderMemImage ) NCSFree( pFileHeaderMemImage ); if( pNCSFile->pClientNetID ) NCScnetDestroy(pNCSFile->pClientNetID); NCSFree(pNCSFile->szUrlPath); NCSFree(pNCSFile); NCSMutexEnd(&pNCSEcwInfo->mutex); // _ASSERT( pNCSFile->pTopQmf ); return(NCS_ECW_ERROR); } if( !pNCSFile->bLocalFile && pFileHeaderMemImage && (nFileHeaderMemImageLen>0) ) { //[21] pNCSFile->pTopQmf->pHeaderMemImage = pFileHeaderMemImage; pNCSFile->pTopQmf->nHeaderMemImageLen = nFileHeaderMemImageLen; } pNCSFile->nUnpackedBlockBandLength = pNCSFile->pTopQmf->x_block_size * pNCSFile->pTopQmf->y_block_size * sizeof(INT16); pNCSFile->pNCSCachePurge = (NCSFileCachePurge *) NCSMalloc(sizeof(NCSFileCachePurge) * pNCSFile->pTopQmf->nr_levels, FALSE); if(!pNCSFile->pNCSCachePurge) { return NCS_COULDNT_ALLOC_MEMORY; } // Point to File Info in the QMF structure pNCSFile->pFileInfo = pNCSFile->pTopQmf->pFileInfo; // Note the file is now open NCSEcwStatsLock(); NCSEcwStatsIncrement(&pNCSEcwInfo->pStatistics->nFilesOpen, 1); NCSEcwStatsUnLock(); pNCSFile->pNextNCSFile = pNCSEcwInfo->pNCSFileList; if( pNCSEcwInfo->pNCSFileList ) pNCSEcwInfo->pNCSFileList->pPrevNCSFile = pNCSFile; pNCSEcwInfo->pNCSFileList = pNCSFile; NCSMutexEnd(&pNCSEcwInfo->mutex); *ppNCSFile = pNCSFile; return(NCS_SUCCESS); }
void CNCSJPCEcwpIOStream::ProcessReceivedPackets() { Lock(); while(m_ReceivedPackets.size()) { ReceivedPacket *pRP = *(m_ReceivedPackets.begin()); NCSPacket *pPacket = pRP->pPacket; INT32 nLength = pRP->nLength; m_ReceivedPackets.remove(pRP); NCSFree(pRP); if( pPacket ) { UINT32 iPacketSize; NCSPacketType ptType; NCSClientUID nClientUID; // have a valid file, so now unpack the blocks read NCS_PACKET_UNPACK_BEGIN(pPacket); NCS_PACKET_UNPACK_ELEMENT(iPacketSize); NCS_PACKET_UNPACK_ELEMENT(nClientUID); NCS_PACKET_UNPACK_ELEMENT(ptType); switch( ptType ) { case NCSPT_BLOCKS: { UINT16 nBlocks; UINT32 i; NCS_PACKET_UNPACK_ELEMENT(nBlocks); /* ** Unpack block data */ for(i = 0; i < nBlocks; i++) { NCSBlockId nBlock; UINT32 nBlockLength; UINT8 *pImage; NCS_PACKET_UNPACK_ELEMENT(nBlock); NCS_PACKET_UNPACK_ELEMENT(nBlockLength); pImage = (UINT8*)NCSMalloc(nBlockLength, FALSE); NCS_PACKET_UNPACK_ELEMENT_SIZE(pImage[0], nBlockLength); //char buf[1024]; //sprintf(buf, "Packet %ld size %ld (", nBlock, nBlockLength); //for(int v = 0; v < NCSMin(20, nBlockLength); v++) { // sprintf(buf + strlen(buf), "0x%lx,", pImage[v]); //} //sprintf(buf + strlen(buf), ")\r\n"); //OutputDebugStringA(buf); // m_pJPC->Lock(); if(GetPacketStatus(nBlock) == CNCSJPCPacketStatus::REQUESTED) { SetPacketStatus(nBlock, CNCSJPCPacketStatus::RECEIVED); CNCSJPCProgression p; p.m_nCurPacket = nBlock; CNCSJPCPacket *pHeader = m_pJPC->GetPacketHeader(nBlock); if(pHeader && m_pJPC->FindPacketRCPL(nBlock, p.m_nCurTile, p.m_nCurResolution, p.m_nCurComponent, p.m_nCurPrecinctX, p.m_nCurPrecinctY, p.m_nCurLayer)) { CNCSJPCEcwpIOStream Stream(m_pJPC, true); if(((CNCSJPCMemoryIOStream&)Stream).Open(pImage, nBlockLength) == NCS_SUCCESS) { pImage = NULL; pHeader->ParseHeader(*m_pJPC, Stream, &p); Stream.Close(); } } delete pHeader; } // m_pJPC->UnLock(); NCSFree(pImage); } } break; case NCSPT_FLOW_CONTROL: // NOTUSED break; case NCSPT_SYNCHRONISE: /* [03] */ { // m_pJPC->Lock(); if(m_bSendInProgress == false) { CNCSJPCPacketStatusIterator pCur = m_Packets.begin(); CNCSJPCPacketStatusIterator pEnd = m_Packets.end(); while(pCur != pEnd) { UINT32 nBlock = (*pCur).second.m_nPacket; if((*pCur).second.m_eStatus == CNCSJPCPacketStatus::REQUESTED) { SetPacketStatus(nBlock, CNCSJPCPacketStatus::NONE); pCur = m_Packets.begin(); CNCSJPCProgression p; p.m_nCurPacket = nBlock; if(m_pJPC->FindPacketRCPL(nBlock, p.m_nCurTile, p.m_nCurResolution, p.m_nCurComponent, p.m_nCurPrecinctX, p.m_nCurPrecinctY, p.m_nCurLayer)) { CNCSJPCTilePartHeader *pMainTP = m_pJPC->GetTile(p.m_nCurTile); RequestPrecinct((CNCSJPCPrecinct*)pMainTP->m_Components[p.m_nCurComponent]->m_Resolutions[p.m_nCurResolution]->m_Precincts.find(p.m_nCurPrecinctX, p.m_nCurPrecinctY)); } } pCur++; } } SendRequests(); // m_pJPC->UnLock(); } break; default: // ERROR unknown packet - ignore it break; } NCS_PACKET_UNPACK_END(pPacket); NCSFree(pPacket); } } UnLock(); }
/* ** Get a temporary file name ** ** Can pass in Dir to use instead of %TEMP% ** Can pass in prefix to use instead of NCS ** Can pass in file extension to use instead of .tmp */ char *NCSGetTempFileName(char *pDir, char *pPrefix, char *pExt) { char *pTmpName = NULL; #if defined PALM return(NULL); #else /* MACINTOSH */ char buf[MAX_PATH]; if(pDir == (char *)NULL || (pDir && strlen(pDir) == 0)) { pDir = NCSGetTempDirectory(); } else { pDir = NCSStrDup(pDir); } if(pExt == NULL) { pExt = ".tmp"; } if(pPrefix == NULL) { pPrefix = "NCS"; } #ifdef WIN32 { int i = 0; #ifndef _WIN32_WCE srand( (unsigned)time( NULL ) ); #endif while(i < 65535) { sprintf(buf, "%s\\%s%lx%lx%s", pDir, pPrefix, rand(), rand(), pExt); #if defined(_WIN32_WCE)||defined(NCS_MINDEP_BUILD) if(NCSFileSizeBytes(buf) < 0) { #else if(PathFileExistsA(buf) == FALSE) { #endif pTmpName = NCSStrDup(buf); break; } i++; } } NCSFree((void*)pDir); #elif defined( MACOSX ) sprintf(buf, "%sXXXXXX", pPrefix ? pPrefix : "NCS"); pTmpName = NCSMalloc(strlen(pDir) + strlen(buf) + strlen(pExt) + 3, FALSE); sprintf(pTmpName, "%s/%s", pDir, buf); mktemp(pTmpName); NCSFree((void*)pDir); strcat(pTmpName, pExt); // FIXME: Is this really going to be unique? #else sprintf(buf, "%sXXXXXX", pPrefix ? pPrefix : "NCS"); pTmpName = NCSMalloc(strlen(pDir) + strlen(buf) + strlen(pExt) + 3, FALSE); sprintf(pTmpName, "%s/%s", pDir, buf); mktemp(pTmpName); NCSFree((void*)pDir); strcat(pTmpName, pExt); // FIXME: Is this really going to be unique? #endif #endif return(pTmpName); } /* ** Get name of temp directory ** ** WIN32: ** Default: %TEMP% env variable ** 9x: C:\Windows\Temp ** NT: C:\Temp ** CE: \Temp */ char *NCSGetTempDirectory(void) { #ifdef _WIN32_WCE return(NCSStrDup("\\Temp")); #elif defined WIN32 NCSTChar winbuf[MAX_PATH]; if(GetTempPath((DWORD)MAX_PATH, winbuf) == 0) { if(GetSystemDirectory(winbuf, MAX_PATH)) { if(NCSGetPlatform() == NCS_WINDOWS_NT) { /* eg, c:\Temp */ winbuf[3] = '\0'; NCSTCat(winbuf, NCS_T("Temp")); } else { /* eg, c:\Windows\Temp */ NCSTCat(winbuf, NCS_T("Temp")); } } } if((winbuf[0] != '\0') && (winbuf[NCSTLen(winbuf) - 1] == '\\')) { winbuf[NCSTLen(winbuf) - 1] = '\0'; } return(NCSStrDup(CHAR_STRING(winbuf))); #elif defined PALM return(NCSStrDup("")); #elif defined MACOSX FSRef tempRef; UInt8 szPath[1024] = ""; if( FSFindFolder( kUserDomain, kTemporaryFolderType, kDontCreateFolder, &tempRef ) == noErr ) { if( FSRefMakePath( &tempRef, szPath, 1024 ) == noErr ) { } } return( NCSStrDup(szPath) ); #elif defined POSIX return(NCSStrDup("/tmp")); #else /* PALM */ char *szTmpDir = getenv("TMP"); if (szTmpDir != NULL) return NCSStrDup(szTmpDir); else return NCSStrDup("/tmp"); #endif } /* ** Get File Version information [01] ** */ BOOLEAN NCSFileGetVersion(char *pFileName, UINT16 *pMajor, UINT16 *pMinor, UINT16 *pRevision, UINT16 *pBuild) { BOOLEAN bRVal = FALSE; if(pFileName) { #if defined(_WIN32_WCE)||defined(NCS_MINDEP_BUILD) return(FALSE); #elif defined WIN32 DWORD dwVISize; DWORD dwZero; dwVISize = GetFileVersionInfoSize(OS_STRING(pFileName), &dwZero); if(dwVISize != 0) { LPVOID lpData = NCSMalloc(dwVISize, TRUE); if(lpData) { if(GetFileVersionInfo(OS_STRING(pFileName), 0, dwVISize, lpData)) { VS_FIXEDFILEINFO *pVI = (VS_FIXEDFILEINFO*)NULL; UINT dwSize; if(VerQueryValue(lpData, NCS_T("\\"), (LPVOID*)&pVI, &dwSize) && pVI) { if(pMajor) { *pMajor = (UINT16)(pVI->dwFileVersionMS >> 16); } if(pMinor) { *pMinor = (UINT16)(pVI->dwFileVersionMS & 0xffff); } if(pRevision) { *pRevision = (UINT16)(pVI->dwFileVersionLS >> 16); } if(pBuild) { *pBuild = (UINT16)(pVI->dwFileVersionLS & 0xffff); } bRVal = TRUE; } } NCSFree(lpData); } }
NCSError NCSFileOpen(const NCSTChar *szFilename, int iFlags, NCS_FILE_HANDLE *phFile) { #ifdef WIN32 DWORD dwMode = GENERIC_READ; DWORD dwCreate = OPEN_EXISTING; if(iFlags & NCS_FILE_READ) dwMode = GENERIC_READ; if(iFlags & NCS_FILE_READ_WRITE) dwMode = GENERIC_READ|GENERIC_WRITE; if(iFlags & NCS_FILE_CREATE) dwCreate = CREATE_ALWAYS; if(iFlags & NCS_FILE_CREATE_UNIQUE) dwCreate = CREATE_NEW; if(iFlags & NCS_FILE_APPEND) dwCreate = OPEN_ALWAYS; *phFile = CreateFile(szFilename, // file name dwMode, // Generic read mode FILE_SHARE_READ, // Let anyone access and share the file NULL, // No security info (so can't be inherited by child process) dwCreate, // File must exist to be opened FILE_FLAG_RANDOM_ACCESS, // Going to be doing lots of random access NULL); // And no template file for attributes if( *phFile == INVALID_HANDLE_VALUE ) { return( NCS_FILE_OPEN_FAILED ); } else { return( NCS_SUCCESS ); } #elif defined MACINTOSH #if __POWERPC__ int i,length, result; Str255 pascalString; FSSpec fileSpec; // We have a C string, we need a PASCAL string. length = strlen(szFilename) + 1; for(i = 1; i < length; ++i) pascalString[i] = szFilename[i - 1]; pascalString[0] = strlen(szFilename); // Create a File Specification Record, then create a File result = FSMakeFSSpec(0,0,pascalString,&fileSpec); // return is meaningless, since the only possible error doesn't effect processing in this case switch(result) { case noErr: // we could dRes pFile here, but we are the only user result =FSpOpenDF(&fileSpec, fsRdPerm, (short *)phFile); if(result) return NCS_FILE_OPEN_FAILED; else return NCS_SUCCESS; break; default: return NCS_SUCCESS; break; } #else /* __POWERPC__ */ int i,length, result; Str255 pascalString; // We have a C string, we need a PASCAL string. length = strlen(szFilename) + 1; for(i = 1; i < length; ++i) pascalString[i] = szFilename[i - 1]; pascalString[0] = strlen(szFilename); result =FSOpen(pascalString, 0, (short *)phFile); if(result) return TRUE; else return FALSE; #endif /* __POWERPC__ */ #elif defined PALM NCS_FILE_HANDLE hFile; Err eErr; UInt32 nMode = 0; if(hFile = (NCS_FILE_HANDLE)NCSMalloc(sizeof(NCS_FILE_HANDLE_STRUCT), TRUE)) { hFile->dbID = DmFindDatabase(0, szFilename); if(hFile->dbID) { Char nameP[dmDBNameLength]; UInt16 attributes; UInt16 version; UInt32 crDate; UInt32 modDate; UInt32 bckUpDate; UInt32 modNum; LocalID appInfoID; LocalID sortInfoID; UInt32 type; UInt32 creator; DmDatabaseInfo(0, hFile->dbID, nameP, &attributes, &version, &crDate, &modDate, &bckUpDate, &modNum, &appInfoID, &sortInfoID, &type, &creator); if(creator == NCS_PALM_CREATOR_ID) { if(hFile->dbRef = DmOpenDatabase(0, hFile->dbID, dmModeReadOnly|dmModeShowSecret)) { UInt32 nRecords; UInt32 nTotalBytes; UInt32 nDataBytes; eErr = DmDatabaseSize(0, hFile->dbID, &nRecords, &nTotalBytes, &nDataBytes); if(eErr == errNone) { MemHandle hRecord; hFile->nRecords = nRecords; hFile->nDBSize = nDataBytes; #ifdef NOTDEF if(hRecord = DmGetRecord(hFile->dbRef, 0)) { MemPtr pData; if(pData = MemHandleLock(hRecord)) { hFile->nRecordSize = ((UINT16*)pData)[0]; MemHandleUnlock(hRecord); } DmReleaseRecord(hFile->dbRef, 0, false); } #endif *phFile = hFile; return(NCS_SUCCESS); } DmCloseDatabase(hFile->dbRef); return(NCSPalmGetNCSError(eErr)); } } } } else { return(NCS_COULDNT_ALLOC_MEMORY); } /* if(iFlags & NCS_FILE_READ) nMode = fileModeReadOnly|fileModeAnyTypeCreator; if(iFlags & NCS_FILE_READ_WRITE) nMode = fileModeUpdate|fileModeAnyTypeCreator; if(iFlags & NCS_FILE_CREATE) nMode = fileModeReadWrite|fileModeAnyTypeCreator; if(iFlags & NCS_FILE_CREATE_UNIQUE) nMode = fileModeReadWrite|fileModeDontOverwrite|fileModeAnyTypeCreator; if(iFlags & NCS_FILE_APPEND) nMode = fileModeAppend|fileModeAnyTypeCreator; *phFile = FileOpen(0, (char*)szFilename, 0, 0, nMode, &eErr); return(NCSPalmGetNCSError(eErr)); */ #elif defined(POSIX) int flags = O_RDONLY; if(iFlags & NCS_FILE_READ) flags = O_RDONLY; if(iFlags & NCS_FILE_READ_WRITE) flags = O_RDWR; if(iFlags & NCS_FILE_CREATE) flags |= O_CREAT; if(iFlags & NCS_FILE_CREATE_UNIQUE) flags |= O_CREAT|O_EXCL; if(iFlags & NCS_FILE_APPEND) flags |= O_APPEND; #if defined SOLARIS || (defined(HPUX) && !defined(__LP64__)) // Enable 64bit! flags |= O_LARGEFILE; #endif #ifdef HPUX *phFile = open64((const char*)CHAR_STRING(szFilename), (int)flags); #ifdef NOTDEF if (*phFile < 0) { fprintf(stderr, "Error opening file : %ld\n", errno); if (errno == EOVERFLOW) { fprintf(stderr, "The named file is a regular file and the size " "of the file cannot be represented correctly in an object of " "size off_t."); } } #endif #else *phFile = open((const char*)CHAR_STRING(szFilename), (int)flags, S_IRUSR|S_IWUSR); #endif if(*phFile != -1) { return(NCS_SUCCESS); } else { return(NCS_FILE_OPEN_FAILED); } #else /* SOLARIS||IRIX */ #error ERROR EcwFileCreate() routine is not defined for this platform #endif /* WIN32 */ }
extern "C" NCSError NCSEcwEditWriteInfo(char *pFilename, NCSEcwEditInfo *pInfo, void (*pProgressFunc)(UINT64 nTotal, UINT64 nWritten, void *pClientData), BOOLEAN (*pCancelFunc)(void *pClientData), void *pClientData) { NCSError eError = NCS_SUCCESS; if(pInfo) { // Update HDR NCS_FILE_HANDLE hFile = NCS_NULL_FILE_HANDLE; eError = NCSFileOpen(OS_STRING(pFilename), NCS_FILE_READ_WRITE, &hFile); if(eError == NCS_SUCCESS) { NCSEcwEditInfo *pECWInfo = (NCSEcwEditInfo*)NULL; char buf[ECW_MAX_DATUM_LEN]; UINT32 nWritten = 0; NCSFileSeekNative(hFile, 24, NCS_FILE_SEEK_START); NCSFileWriteUINT8_MSB(hFile, (UINT8)pInfo->eCellSizeUnits); // UINT8 CellSizeUnits NCSFileWriteIEEE8_LSB(hFile, pInfo->fCellIncrementX); // IEEE8 fCellIncrementX NCSFileWriteIEEE8_LSB(hFile, pInfo->fCellIncrementY); // IEEE8 fCellIncrementY NCSFileWriteIEEE8_LSB(hFile, pInfo->fOriginX); // IEEE8 fOriginX NCSFileWriteIEEE8_LSB(hFile, pInfo->fOriginY); // IEEE8 fOriginY memset(buf, 0, sizeof(buf)); strncpy(buf, pInfo->szDatum, sizeof(buf)); NCSFileWrite(hFile, buf, ECW_MAX_DATUM_LEN, &nWritten); // char *Datum memset(buf, 0, sizeof(buf)); strncpy(buf, pInfo->szProjection, sizeof(buf)); NCSFileWrite(hFile, buf, ECW_MAX_DATUM_LEN, &nWritten); // char *Projection NCSFileClose(hFile); eError = NCSEcwEditReadInfo(pFilename, &pECWInfo); if(eError == NCS_SUCCESS) { if(pECWInfo->bCompressedOffsetTable && !pInfo->bCompressedOffsetTable) { // Decompress block table // This means copying the file to a new one... INT64 nCurrentSize = NCSFileSizeBytes(OS_STRING(pFilename)); NCSFile *pFile = (NCSFile*)NULL; eError = NCSecwOpenFile(&pFile, pFilename, FALSE, TRUE); if(eError == NCS_SUCCESS) { NCS_FILE_HANDLE hFile = pFile->pTopQmf->hEcwFile.hFile; UINT32 nCompressedSize = 0; NCSFileSeekNative(hFile, pFile->pTopQmf->nHeaderMemImageLen, NCS_FILE_SEEK_START); eError = NCSFileReadUINT32_MSB(hFile, &nCompressedSize); if(eError == NCS_SUCCESS) { INT64 nNewSize = nCurrentSize - nCompressedSize + get_qmf_tree_nr_blocks(pFile->pTopQmf) * sizeof(UINT64) + 1; char szDir[MAX_PATH]; char *p; char szOutputDir[MAX_PATH] = {'\0'}; INT64 nAvailable; BOOLEAN bUsingTempDir = FALSE; strcpy(szDir, pFilename); p = &(szDir[strlen(szDir) - 1]); while(p > szDir && *p != '\\') { p--; } *p = '\0'; nAvailable = NCSFreeDiskSpaceBytes(szDir, NULL); if(nAvailable < nNewSize) { // No space to copy file, check $TMP char *pTempDir = NCSGetTempDirectory(); if(pTempDir) { nAvailable = NCSFreeDiskSpaceBytes(szDir, NULL); if(nAvailable > nNewSize) { strcpy(szOutputDir, pTempDir); bUsingTempDir = TRUE; } NCSFree(pTempDir); } } else { strcpy(szOutputDir, szDir); } if(szOutputDir[0] != '\0') { char *pTempFileName = NCSGetTempFileName(szOutputDir, "ECW", ERS_WAVELET_DATASET_EXT); // Force it to close pFile->bValid = FALSE; NCSecwCloseFile(pFile); eError = NCSecwOpenFile(&pFile, pFilename, TRUE, TRUE); hFile = pFile->pTopQmf->hEcwFile.hFile; if(eError == NCS_SUCCESS) { UINT8 *pBuffer = (UINT8*)NCSMalloc(NCSMax(256*1024, pFile->pTopQmf->nHeaderMemImageLen), FALSE); if(pBuffer) { NCS_FILE_HANDLE hTempFile = NCS_NULL_FILE_HANDLE; eError = NCSFileOpen(OS_STRING(pTempFileName), NCS_FILE_CREATE|NCS_FILE_READ_WRITE, &hTempFile); if(eError == NCS_SUCCESS) { UINT32 nLen = pFile->pTopQmf->nHeaderMemImageLen; NCSFileSeekNative(hFile, 0, NCS_FILE_SEEK_START); eError = NCSFileRead(hFile, pBuffer, nLen, &nLen); if(eError == NCS_SUCCESS) { eError = NCSFileWrite(hTempFile, pBuffer, nLen, &nLen); if(eError == NCS_SUCCESS) { nLen = get_qmf_tree_nr_blocks(pFile->pTopQmf) * sizeof(UINT64); eError = NCSFileWriteUINT32_MSB(hTempFile, nLen + 1); if(eError == NCS_SUCCESS) { eError = NCSFileWriteUINT8_MSB(hTempFile, ENCODE_RAW); if(eError == NCS_SUCCESS) { #ifdef NCSBO_MSBFIRST NCSByteSwapRange64((UINT64*)pFile->pTopQmf->p_block_offsets, (UINT64*)pFile->pTopQmf->p_block_offsets, nLen / sizeof(pFile->pTopQmf->p_block_offsets[0])); #endif eError = NCSFileWrite(hTempFile, pFile->pTopQmf->p_block_offsets, nLen, &nLen); #ifdef NCSBO_MSBFIRST NCSByteSwapRange64((UINT64*)pFile->pTopQmf->p_block_offsets, (UINT64*)pFile->pTopQmf->p_block_offsets, nLen / sizeof(pFile->pTopQmf->p_block_offsets[0])); #endif if(eError == NCS_SUCCESS) { UINT64 nToWrite; NCSFileSeekNative(hFile, pFile->pTopQmf->nHeaderMemImageLen + nCompressedSize + sizeof(UINT32), NCS_FILE_SEEK_START); nToWrite = nCurrentSize - NCSFileTellNative(hFile); while(nToWrite) { UINT32 nThisRead = (UINT32)NCSMin(256*1024, nToWrite); eError = NCSFileRead(hFile, pBuffer, nThisRead, &nThisRead); if(eError != NCS_SUCCESS) { break; } eError = NCSFileWrite(hTempFile, pBuffer, nThisRead, &nThisRead); if(eError != NCS_SUCCESS) { break; } nToWrite -= nThisRead; if(pCancelFunc && (*pCancelFunc)(pClientData) != FALSE) { // Cancelled! break; } else if(pProgressFunc) { (*pProgressFunc)(nNewSize, nNewSize - nToWrite, pClientData); } } if(nToWrite != 0) { eError = NCS_FILEIO_ERROR; } } } } } } NCSFileClose(hTempFile); } NCSFree(pBuffer); } else { eError = NCS_COULDNT_ALLOC_MEMORY; } } if(eError == NCS_SUCCESS) { // Force it to close pFile->bValid = FALSE; NCSecwCloseFile(pFile); pFile = NULL; // remove original file NCSDeleteFile(pFilename); // rename() can rename from a different directory... if(NCSRenameFile(pTempFileName, pFilename)) { // leave the temp file since we deleted the original! eError = NCS_FILEIO_ERROR; } } else { NCSDeleteFile(pTempFileName); } NCSFree(pTempFileName); } else { eError = NCS_FILEIO_ERROR; } } if(pFile) { // Force it to close pFile->bValid = FALSE; NCSecwCloseFile(pFile); } } } NCSEcwEditFreeInfo(pECWInfo); } } } else { eError = NCS_INVALID_PARAMETER; } return(eError); }