Exemple #1
0
size_t getEntry(const char *name, char *contentFile, char **ptr) {

  size_t size=0;
  ZZIP_DIR *dir;
  ZZIP_DIRENT entry;
  ZZIP_FILE *file;
  char *buf;

  dir = zzip_dir_open(name, 0);
  if (!dir) {
    fprintf(stderr, "failed to open %s\n", name);
    return 0;
  }

  zzip_dir_read(dir, &entry);

  file = zzip_file_open(dir, contentFile, 0);

  (void)zzip_seek(file, 0, SEEK_END);
  size = zzip_tell(file);
  (void)zzip_seek(file, 0, SEEK_SET);
  buf = (char *)malloc(size+1);
  (void)zzip_read(file, buf, size);
  buf[size] = '\0';
  *ptr = buf;

  zzip_file_close(file);
  zzip_dir_close(dir);

  return size;
}
u32 ZIPROMReaderSize(void * file)
{
	u32 size;

	zzip_seek((ZZIP_FILE*)file, 0, SEEK_END);
	size = zzip_tell((ZZIP_FILE*)file);
	zzip_seek((ZZIP_FILE*)file, 0, SEEK_SET);

	return size;
}
Exemple #3
0
 //-----------------------------------------------------------------------
 void ZipDataStream::skip(long count)
 {
     long was_avail = static_cast<long>(mCache.avail());
     if (count > 0)
     {
         if (!mCache.ff(count))
             zzip_seek(mZzipFile, static_cast<zzip_off_t>(count - was_avail), SEEK_CUR);
     }
     else if (count < 0)
     {
         if (!mCache.rewind((size_t)(-count)))
             zzip_seek(mZzipFile, static_cast<zzip_off_t>(count + was_avail), SEEK_CUR);
     }
 }
int CZipArchive::CZipFile::seek(int bytes,EStreamSeek smode){
	switch(smode){
	case ESeek_Set:
		return zzip_seek(m_zipfile,bytes,SEEK_SET);
		
	case ESeek_Cur:
		return zzip_seek(m_zipfile,bytes,SEEK_CUR);

	case ESeek_End:
		return zzip_seek(m_zipfile,bytes,SEEK_END);

	}
	return 0;

}
Exemple #5
0
/** 移动文件指针
@Param 移动的字节数
@Param 指针位置
*/
void FZipFileStream::Seek( long nPos,int flag )
{
    switch( flag )
    {
    case START:
        zzip_seek( m_pZipFile,static_cast<zzip_off_t>(nPos),SEEK_SET );
        break;
    case CURRENT:
        zzip_seek( m_pZipFile,static_cast<zzip_off_t>(nPos),SEEK_CUR );
        break;
    case END:
        zzip_seek( m_pZipFile,static_cast<zzip_off_t>(nPos),SEEK_END );
        break;
    default: FASSERT(0); break;
    }
}
Exemple #6
0
short RasterMapCache::LookupTerrainCacheFile(const long &SeekPos) {
    // put new value in slot tcpmin

    __int16 NewAlt = 0;
    long SeekRes;
    short Alt;

    if(!isMapLoaded())
        return TERRAIN_INVALID;

    Lock();

    SeekRes = zzip_seek(fpTerrain, SeekPos, SEEK_SET);
    if(SeekRes != SeekPos) {
        // error, not found!
        Alt = TERRAIN_INVALID;
    } else {
        if (zzip_fread(&NewAlt, 1, sizeof(__int16), fpTerrain) != sizeof(__int16))
            Alt = TERRAIN_INVALID;
        else {
            // FIX HERE NETHERLAND
            Alt = max((short int)0,NewAlt);
        }
    }
    Unlock();

    return Alt;
}
CORE::Int32
CZipIOAccess::Seek( CORE::Int32 offset ,
                    CORE::Int32 origin )
{GUCEF_TRACE;

    return zzip_seek( m_zzipFile, static_cast<zzip_off_t>( offset ), origin );
}
Exemple #8
0
/**
**	CLseek		Library file seek
**
**	@param file	CLFile pointer.
**	@param offset	Seek position
**	@param whence	How to seek
*/
global int CLseek(CLFile *file, long offset, int whence)
{
    int tp, ret = -1;

    if (file && (tp = file->cl_type) != CLF_TYPE_INVALID) {
	if (tp == CLF_TYPE_PLAIN) {
	    ret = fseek(file->cl_plain, offset, whence);
	}
#ifdef USE_ZLIB
	if (tp == CLF_TYPE_GZIP) {
	    ret = gzseek(file->cl_gz, offset, whence);
	}
#endif	// USE_ZLIB
#ifdef USE_BZ2LIB
	if (tp == CLF_TYPE_BZIP2) {
	    bzseek(file->cl_bz, offset, whence);
	    ret = 0;
	}
#endif	// USE_BZ2LIB
#ifdef USE_ZZIPLIB
	if (tp == CLF_TYPE_ZZIP) {
	    zzip_seek(file->cl_zz, offset, whence);
	    ret = 0;
	}
#endif	// USE_ZZIPLIB
    } else {
	errno = EBADF;
    }
    return ret;
}
Exemple #9
0
/** => zzip_seek
 * This function is provided for users who can not use any largefile-mode.
 */
long
zzip_seek32(ZZIP_FILE * fp, long offset, int whence)
{
    if (sizeof(zzip_off_t) == sizeof(long))
    {
        return zzip_seek(fp, offset, whence);
    } else
    {
        off_t off = zzip_seek(fp, offset, whence);
        if (off >= 0) {
            register long off32 = off;
            if (off32 == off) return off32;
#ifdef ZZIP_DISABLED
            errno = EOVERFLOW;
#endif /* ZZIP_DISABLED */
        }
        return -1;
    }
}
Exemple #10
0
static zzip_size_t
zzip_pread_fallback(ZZIP_FILE *file, void *ptr, zzip_size_t size,
                    zzip_off_t offset)
{
    zzip_off_t new_offset = zzip_seek(file, offset, SEEK_SET);
    if (new_offset < 0)
        return -1;

    return zzip_read(file, ptr, size);
}
Exemple #11
0
size_t getEntry(const char *name, char *contentFile, char **ptr) {

  size_t size=0;
  ZZIP_DIR *dir;
  ZZIP_DIRENT entry;
  ZZIP_FILE *file;
  zzip_error_t error;
  char *buf;

  o_log(DEBUGM, "opening %s", name);

  dir = zzip_dir_open(name, &error);
  if (!dir) {
    o_log(ERROR, "failed to open %s", name);
    return 0;
  }

  while(zzip_dir_read(dir, &entry)) {

    if( 0 == strcmp(entry.d_name, contentFile) ) {

      file = zzip_file_open(dir, contentFile, O_RDONLY);

      (void)zzip_seek(file, 0, SEEK_END);
      size = zzip_tell(file);
      (void)zzip_seek(file, 0, SEEK_SET);

      buf = (char *)malloc(size+1);
      (void)zzip_read(file, buf, size);
      buf[size] = '\0';
      *ptr = buf;

      zzip_file_close(file);
    }
  }
  zzip_dir_close(dir);

  return size;
}
Exemple #12
0
static void searchDiskTreeNode(SHPTreeHandle disktree, rectObj aoi, ms_bitarray status) 
{
  int i;
  ms_int32 offset;
  ms_int32 numshapes, numsubnodes;
  rectObj rect;

  int *ids=NULL;

  zzip_fread( &offset, 4, 1, disktree->fp );
  if ( disktree->needswap ) SwapWord ( 4, &offset );

  zzip_fread( &rect, sizeof(rectObj), 1, disktree->fp );
  if ( disktree->needswap ) SwapWord ( 8, &rect.minx );
  if ( disktree->needswap ) SwapWord ( 8, &rect.miny );
  if ( disktree->needswap ) SwapWord ( 8, &rect.maxx );
  if ( disktree->needswap ) SwapWord ( 8, &rect.maxy );

  zzip_fread( &numshapes, 4, 1, disktree->fp );
  if ( disktree->needswap ) SwapWord ( 4, &numshapes );

  if(!msRectOverlap(&rect, &aoi)) { /* skip rest of this node and sub-nodes */
    offset += numshapes*sizeof(ms_int32) + sizeof(ms_int32);
    zzip_seek(disktree->fp, offset, SEEK_CUR);
    return;
  }
  if(numshapes > 0) {
    ids = (int *)msSmallMalloc(numshapes*sizeof(ms_int32));

    zzip_fread( ids, numshapes*sizeof(ms_int32), 1, disktree->fp );
    if (disktree->needswap ) {
      for( i=0; i<numshapes; i++ ) {
        SwapWord( 4, &ids[i] );
        msSetBit(status, ids[i], 1);
      }
    } else {
      for(i=0; i<numshapes; i++)
        msSetBit(status, ids[i], 1);
    }
    free(ids);
  }

  zzip_fread( &numsubnodes, 4, 1, disktree->fp );
  if ( disktree->needswap ) SwapWord ( 4, &numsubnodes );

  for(i=0; i<numsubnodes; i++)
    searchDiskTreeNode(disktree, aoi, status);

  return;
}
Exemple #13
0
// Reads line from UTF-8 encoded text file.
// File must be open in binary read mode.
// ATTENTION: if buffer is not large enough, the zzip_fread will fail
// and ReadULine will return , but you will not be able to know if it returned
// for a string overflow (managed) or because it reached EOF!
bool ReadULine(ZZIP_FILE* fp, TCHAR *unicode, int maxChars)
{
  // This is a char, and we need space for at least MAX_HELP TCHARS!
  // 
  unsigned char buf[1500 * 2]; 

  long startPos = zzip_tell(fp);

  if (startPos < 0) {
    StartupStore(_T(". ftell() error = %d%s"), errno, NEWLINE);
    return(false);
  }

  size_t nbRead = zzip_fread(buf, 1, sizeof(buf) - 1, fp);
  
  if (nbRead == 0)
    return(false);

  buf[nbRead] = '\0';

  // find new line (CR/LF/CRLF) in the string and terminate string at that position
  size_t i;
  for (i = 0; i < nbRead; i++) {
    if (buf[i] == '\n')
    {
      buf[i++] = '\0';
      if (buf[i] == '\r')
        i++;
      break;
    }

    if (buf[i] == '\r')
    {
      buf[i++] = '\0';
      if (buf[i] == '\n')
        i++;
      break;
    }
  }

  // next reading will continue after new line
  zzip_seek(fp, startPos + i, SEEK_SET);

  // skip leading BOM
  char* begin = (char*) buf;
  if (buf[0] == 0xEF && buf[1] == 0xBB && buf[2] == 0xBF)
    begin += 3;

  return(utf2unicode(begin, unicode, maxChars) >= 0);
}
std::future<void*> ResourceManager::LoadSound(const char* filepath, void* _fSystem)
{
#ifdef _CACH_PARSED_DATA_
	for (std::map<const char*, SoundData*>::iterator it = mSoundResource.begin(); it != mSoundResource.end(); it++)
	{
		if (it->first == filepath)
			return mThreadPool.AddTask<LoadSoundTask>(it->second->buffer, it->second->bufferSize, &mGlLock, _fSystem);
	}
#endif

	unsigned int bufferSize;
	unsigned char* buffer;

	switch (mAssetPacketExtension)
	{
	case PACA:
		bufferSize = mPacaReader.GetResourceSize(filepath);
		buffer = new unsigned char[bufferSize];
		if (mPacaReader.GetResource(filepath, buffer, bufferSize))
		{
#ifdef _CACH_PARSED_DATA_
			SoundData* sd = new SoundData();
			sd->buffer = buffer;
			sd->bufferSize = bufferSize;
			mSoundResource.insert(std::pair<const char*, SoundData*>(filepath, sd));
#endif
			return mThreadPool.AddTask<LoadSoundTask>(buffer, bufferSize, &mGlLock, _fSystem);
		}
		break;

	case ZIP:
		ZZIP_FILE* fp = zzip_file_open(mDir, filepath, 0);
		zzip_seek(fp, 0, SEEK_END);
		bufferSize = zzip_tell(fp);
		zzip_rewind(fp);

		buffer = new unsigned char[bufferSize];
		bufferSize = zzip_file_read(fp, buffer, static_cast<int>(bufferSize));

		zzip_file_close(fp);

#ifdef _CACH_PARSED_DATA_
		SoundData* sd = new SoundData();
		sd->buffer = buffer;
		sd->bufferSize = bufferSize;
		mSoundResource.insert(std::pair<const char*, SoundData*>(filepath, sd));
#endif
		return mThreadPool.AddTask<LoadSoundTask>(buffer, bufferSize, &mGlLock, _fSystem);
	}
}
Exemple #15
0
static int ff_seek (lua_State *L) {
  static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
  static const char *const modenames[] = {"set", "cur", "end", NULL};
  ZZIP_FILE *f = tointernalfile(L, 1);
  int op = luaL_checkoption(L, 2, "cur", modenames);
  long offset = luaL_optlong(L, 3, 0);
  luaL_argcheck(L, op != -1, 2, "invalid mode");
  op = zzip_seek(f, offset, mode[op]);
  if (op < 0)
    return pushresult(L, 0, NULL);  /* error */
  else {
    lua_pushnumber(L, zzip_tell(f));
    return 1;
  }
}
TextureResource* ResourceManager::LoadTexture( const char* file)
{	
#ifdef _CACH_PARSED_DATA_
	for (std::map<const char*, TextureResource*>::iterator it = mTextureResource.begin(); it != mTextureResource.end(); it++)
	{
		if (it->first == file)
			return it->second;
	}
#endif

	unsigned int bufferSize;
	unsigned char* buffer;

	switch ( mAssetPacketExtension )
	{
		case PACA:
			bufferSize	= mPacaReader.GetResourceSize(file);
			buffer		= new unsigned char[bufferSize];
			if ( mPacaReader.GetResource(file, buffer, bufferSize ) )
			{
				TextureResource* textureResource = new TextureResource();
				textureResource->future = mThreadPool.AddTask<LoadTextureTask>(buffer, bufferSize, &mGlLock);
#ifdef _CACH_PARSED_DATA_
				mTextureResource.insert(std::pair<const char*, TextureResource*>(file, textureResource));
#endif
				return textureResource;
			}
			break;

		case ZIP:
			ZZIP_FILE* fp = zzip_file_open( mDir, file, 0 );
			zzip_seek( fp, 0, SEEK_END );
			bufferSize = zzip_tell( fp );
			zzip_rewind( fp );

			buffer = new unsigned char[bufferSize];
			bufferSize = zzip_file_read( fp, buffer, static_cast<int>( bufferSize ) );

			zzip_file_close( fp );

			TextureResource* textureResource = new TextureResource();
			textureResource->future = mThreadPool.AddTask<LoadTextureTask>(buffer, bufferSize, &mGlLock);
#ifdef _CACH_PARSED_DATA_
			mTextureResource.insert(std::pair<const char*, TextureResource*>(file, textureResource));
#endif
			return textureResource;
	}
}
Exemple #17
0
BOOL ReadString(ZZIP_FILE *zFile, int Max, TCHAR *String)
{
  char sTmp[READLINE_LENGTH+1];
  char FileBuffer[READLINE_LENGTH+1];
  long dwNumBytesRead=0;
  long dwTotalNumBytesRead=0;
  long dwFilePos;

  String[0] = '\0';
  sTmp[0] = 0;

  #if BUGSTOP
  LKASSERT((unsigned)Max<sizeof(sTmp));
  #endif

  if (Max >= (int)(sizeof(sTmp)))
    return(FALSE);
  if (!zFile)
    return(FALSE);

  dwFilePos = zzip_tell(zFile);

  dwNumBytesRead = zzip_fread(FileBuffer, 1, Max, zFile);
  if (dwNumBytesRead <= 0)
    return(FALSE);

  int i = 0;
  int j = 0;
  while((i<Max) && (j<(int)dwNumBytesRead)) {

    char c = FileBuffer[j];
    j++;
    dwTotalNumBytesRead++;

    if((c == '\n')){
      break;
    }

    sTmp[i] = c;
    i++;
  }

  sTmp[i] = 0;
  zzip_seek(zFile, dwFilePos+j, SEEK_SET);
  sTmp[Max-1] = '\0';
  mbstowcs(String, sTmp, strlen(sTmp)+1);
  return (dwTotalNumBytesRead>0);
}
Exemple #18
0
 //-----------------------------------------------------------------------
 void ZipDataStream::seek( size_t pos )
 {
     zzip_off_t newPos = static_cast<zzip_off_t>(pos);
     zzip_off_t prevPos = static_cast<zzip_off_t>(tell());
     if (prevPos < 0)
     {
         // seek set after invalid pos
         mCache.clear();
         zzip_seek(mZzipFile, newPos, SEEK_SET);
     }
     else
     {
         // everything is going all right, relative seek
         skip((long)(newPos - prevPos));
     }
 }
Exemple #19
0
PSAR_ENTRY *LPP_PsarDecoder_getEntry(const char *filename)
{
    if(!initialized) return NULL;
    zzip_strings_t ext[] = {"", 0};

    ZZIP_FILE *fd = zzip_open_ext_io(filename, O_RDONLY | (0x0), ZZIP_ONLYZIP, ext, &psar_handlers);
    if(fd == NULL)
    {
        #ifdef DEBUG
        dwrite_output("Function %s Line %d : Cannot open the file '%s' for read.\n", __FUNCTION__, __LINE__, filename);
        #endif
        return NULL;
    }

    PSAR_ENTRY *entry = (PSAR_ENTRY*)malloc(sizeof(PSAR_ENTRY));
    if(!entry)
    {
        #ifdef DEBUG
        dwrite_output("Function %s Line %d : Cannot allocate 'entry' to memory.\n", __FUNCTION__, __LINE__);
        #endif
        zzip_close(fd);
        return NULL;
    }

    memset(entry, 0, sizeof(PSAR_ENTRY));

    zzip_seek(fd, 0, SEEK_END);
    entry->len = zzip_tell(fd);
    zzip_rewind(fd);

    if(entry->len <= 0)
    {
        free(entry);
        zzip_fclose(fd);
        #ifdef DEBUG
        dwrite_output("Function %s Line %d : file len is lower than zero.\n", __FUNCTION__, __LINE__);
        #endif
        return NULL;
    }

    entry->data = (u8*)malloc(entry->len);
    zzip_fread(entry->data, 1, entry->len, fd);
    zzip_fclose(fd);

    return(entry);
}
Exemple #20
0
void ZipStream::setPosition(int64 offset, StreamSeekMode mode)
{
	int origin = 0;

	switch(mode)
	{
	case StreamSeekMode::Absolute:
		origin = SEEK_SET;
		break;
	case StreamSeekMode::Relative:
		origin = SEEK_CUR;
		break;
	case StreamSeekMode::RelativeEnd:
		origin = SEEK_END;
		break;
	}

	zzip_seek(handle, (zzip_off_t) offset, origin);
}
	static int _zzip_seek(SDL_RWops *context, int offset, int whence)
	{
	return zzip_seek(SDL_RWOPS_ZZIP_FILE(context), offset, whence);
	}
static int reader_zzip_seek(reader_t*reader, int pos)
{
    return zzip_seek((ZZIP_FILE*)reader->internal, pos, SEEK_SET);
}
Exemple #23
0
//从zip文件中读取全部文件内容到文件中或者内存中, 
//zip文件必须是通过addPatchFile加入的patch文件,
bool Updater::_readContentsFromZip(const char* szZipFile, const char* szFileName, 
					const char* szDiskFileName, char** ppMemoryBuf, unsigned int& nFileSize)
{
	//参数检查
	assert(szZipFile && szFileName && (szDiskFileName || ppMemoryBuf) );
	if(!szZipFile || szZipFile[0]==0 || !szFileName || szFileName[0]==0 || (!szDiskFileName && !ppMemoryBuf))
	{
		setLastError(AXP_ERR_PARAM);
		return false;
	}

	//搜索加入的zip文件
	PATCHFILE_MAP::iterator itPatch = 
		m_mapPatchFile.find(normaliseName(szZipFile));

	//无法找到zip文件
	assert(itPatch != m_mapPatchFile.end());
	if(itPatch == m_mapPatchFile.end()) 
	{
		setLastError(AXP_ERR_PARAM, "%s not inserted", szZipFile);
		return false;
	}

	//获得ZIP句柄
	ZZIP_DIR* mZzipDir = (ZZIP_DIR*)(itPatch->second);
	assert(mZzipDir);

	std::string norFileName = normaliseName(szFileName, false, true);

	//得到文件信息
	ZZIP_STAT zstat;
	memset(&zstat, 0, sizeof(ZZIP_STAT));

	//打开文件,如果打不开,是空文件
	ZZIP_FILE* zzipFile = zzip_file_open(mZzipDir, norFileName.c_str(), ZZIP_ONLYZIP | ZZIP_CASELESS);
	if(zzipFile)
	{
		int zip_err = zzip_dir_stat(mZzipDir, norFileName.c_str(), &zstat, ZZIP_CASEINSENSITIVE);
		if(zip_err!=0)
		{
			zzip_file_close(zzipFile);

			setLastError(AXP_ERR_ZIPFILE, "ziperr=%d", mZzipDir->errcode);
			return false;
		}
	}

	//如果需要写入文件
	if(szDiskFileName)
	{
		//确认文件所在目录存在
		char szDiskFilePath[MAX_PATH] = {0};
		strncpy(szDiskFilePath, szDiskFileName, MAX_PATH);
		PathRemoveFileSpec(szDiskFilePath);
		if(szDiskFilePath[0]!=0 && !forceCreatePath(szDiskFilePath))
		{
			if(zzipFile)zzip_file_close(zzipFile);

			setLastError(AXP_ERR_FILE_ACCESS, "Path=%s, WinErr=%d", szDiskFilePath, ::GetLastError());
			return false;
		}

		//创建该文件
		HANDLE hDiskFile = ::CreateFile(szDiskFileName, 
							GENERIC_READ|GENERIC_WRITE,
							FILE_SHARE_READ|FILE_SHARE_WRITE,
							0,
							CREATE_ALWAYS,
							FILE_ATTRIBUTE_ARCHIVE,
							0);

		if(hDiskFile == INVALID_HANDLE_VALUE)
		{
			if(zzipFile)zzip_file_close(zzipFile);

			setLastError(AXP_ERR_FILE_ACCESS, "File=%s, WinErr=%d", szDiskFileName, ::GetLastError());
			return false;
		}

		if(zstat.st_size > 0)
		{
			const int MAX_BUFFER_SIZE = 4096;
			char buffer[MAX_BUFFER_SIZE] = {0};

			zzip_seek(zzipFile, 0, SEEK_SET);
			zzip_ssize_t zReadSize = zzip_file_read(zzipFile, buffer, sizeof(buffer));
		
			//实际已经写入的尺寸
			unsigned int nActWriteSize = 0;

			//分块读写文件内容
			do
			{
				//文件结束
				if(zReadSize==0) break;

				//写入磁盘文件
				DWORD dwBytesWrite;
				if(!WriteFile(hDiskFile, buffer, (DWORD)zReadSize, &dwBytesWrite, 0) || 
					dwBytesWrite != (DWORD)zReadSize)
				{
					zzip_file_close(zzipFile);
					CloseHandle(hDiskFile);

					setLastError(AXP_ERR_FILE_WRITE, "File=%s, WinErr: %d", szDiskFileName, GetLastError());
					return false;
				}

				//文件结束
				if(zzip_tell(zzipFile) >=zstat.st_size) break;

				zReadSize = zzip_file_read(zzipFile, buffer, sizeof(buffer));
			}while(true);
		}
		//关闭句柄
		CloseHandle(hDiskFile); hDiskFile=0;
	}

	//如果需要读入内存
	if(ppMemoryBuf)
	{
		//所需要的内存
		unsigned int nMemoryNeed = (unsigned int)zstat.st_size+1;
		while(nMemoryNeed%4)nMemoryNeed++;	//upbound 4

		//扩大静态内存大小
		static std::vector< unsigned char > s_autoMemory;
		if(s_autoMemory.size() < nMemoryNeed) 
		{
			s_autoMemory.resize(nMemoryNeed);
		}
		s_autoMemory.assign(s_autoMemory.size(), 0);

		//读入文件内容
		if(zstat.st_size > 0)
		{
			zzip_seek(zzipFile, 0, SEEK_SET);
			zzip_ssize_t nZipSize = zzip_file_read(zzipFile, (char*)&(s_autoMemory[0]), zstat.st_size);
			if(nZipSize != zstat.st_size)
			{
				zzip_file_close(zzipFile);
				setLastError(AXP_ERR_ZIPFILE, "ziperr=%d", mZzipDir->errcode);
				return false;
			}
		}
		//返回内容
		*ppMemoryBuf = (char *)(&(s_autoMemory[0]));
	}

	//关闭句柄
	if(zzipFile)zzip_file_close(zzipFile);	zzipFile=0;

	nFileSize = (unsigned int)zstat.st_size;
	return true;
}
Exemple #24
0
static char *msDBFReadAttribute(DBFHandle psDBF, int hEntity, int iField )

{
    int	       	nRecordOffset, i;
    const uchar *pabyRec;
    char	*pReturnField = NULL;

    /* -------------------------------------------------------------------- */
    /*	Is the request valid?                  				    */
    /* -------------------------------------------------------------------- */
    if( iField < 0 || iField >= psDBF->nFields ) 
    {
        msSetError(MS_DBFERR, "Invalid field index %d.", "msDBFGetItemIndex()",iField );
        return( NULL );
    }

    if( hEntity < 0 || hEntity >= psDBF->nRecords )
    {
        msSetError(MS_DBFERR, "Invalid record number %d.", "msDBFGetItemIndex()",hEntity );
        return( NULL );
    }

    /* -------------------------------------------------------------------- */
    /*	Have we read the record?					    */
    /* -------------------------------------------------------------------- */
    if( psDBF->nCurrentRecord != hEntity )
    {
#ifdef SHAPELIB_DISABLED
	flushRecord( psDBF );
#endif /* SHAPELIB_DISABLED */

	nRecordOffset = psDBF->nRecordLength * hEntity + psDBF->nHeaderLength;

	zzip_seek( psDBF->zfp, nRecordOffset, 0 );
	zzip_fread( psDBF->pszCurrentRecord, psDBF->nRecordLength, 1, psDBF->zfp );

	psDBF->nCurrentRecord = hEntity;
    }

    pabyRec = (const uchar *) psDBF->pszCurrentRecord;

    /* -------------------------------------------------------------------- */
    /*	Ensure our field buffer is large enough to hold this buffer.	    */
    /* -------------------------------------------------------------------- */
    if( psDBF->panFieldSize[iField]+1 > psDBF->nStringFieldLen )
    {
	psDBF->nStringFieldLen = psDBF->panFieldSize[iField]*2 + 10;
	psDBF->pszStringField = (char *) SfRealloc(psDBF->pszStringField,psDBF->nStringFieldLen);
    }

    /* -------------------------------------------------------------------- */
    /*	Extract the requested field.					    */
    /* -------------------------------------------------------------------- */
    strncpy( psDBF->pszStringField,(const char *) pabyRec+psDBF->panFieldOffset[iField], psDBF->panFieldSize[iField] );
    psDBF->pszStringField[psDBF->panFieldSize[iField]] = '\0';

    /*
    ** Trim trailing blanks (SDL Modification)
    */ 
    for(i=strlen(psDBF->pszStringField)-1;i>=0;i--) {
      if(psDBF->pszStringField[i] != ' ') { 
	psDBF->pszStringField[i+1] = '\0'; 
	break;
      }
    }

    if(i == -1) psDBF->pszStringField[0] = '\0'; /* whole string is blank (SDL fix)       */

    /*
    ** Trim/skip leading blanks (SDL/DM Modification - only on numeric types)
    */ 
    if( psDBF->pachFieldType[iField] == 'N' || psDBF->pachFieldType[iField] == 'F' || psDBF->pachFieldType[iField] == 'D' ) {
        for(i=0; psDBF->pszStringField[i] != '\0' ;i++) {
            if(psDBF->pszStringField[i] != ' ')
                break;	
        }
        pReturnField = psDBF->pszStringField+i;
    }
    else
        pReturnField = psDBF->pszStringField;

    return( pReturnField );
}
Exemple #25
0
DBFHandle msDBFOpen(struct zzip_dir *zdir, const char *pszFilename,
                    const char *pszAccess)

{
    DBFHandle		psDBF;
    uchar		*pabyBuf;
    int			nFields, nRecords, nHeadLen, nRecLen, iField;
    char	        *pszDBFFilename;

    /* -------------------------------------------------------------------- */
    /*      We only allow the access strings "rb" and "r+".                 */
    /* -------------------------------------------------------------------- */
    if( strcmp(pszAccess,"r") != 0 && strcmp(pszAccess,"r+") != 0 
        && strcmp(pszAccess,"rb") != 0 && strcmp(pszAccess,"r+b") != 0 )
        return( NULL );
    
    /* -------------------------------------------------------------------- */
    /*	Ensure the extension is converted to dbf or DBF if it is 	    */
    /*	currently .shp or .shx.						    */
    /* -------------------------------------------------------------------- */
    pszDBFFilename = (char *) malloc(strlen(pszFilename)+1);
    strcpy( pszDBFFilename, pszFilename );
    
    if( strcmp(pszFilename+strlen(pszFilename)-4,".shp") 
	|| strcmp(pszFilename+strlen(pszFilename)-4,".shx") )
    {
        strcpy( pszDBFFilename+strlen(pszDBFFilename)-4, ".dbf");
    }
    else if( strcmp(pszFilename+strlen(pszFilename)-4,".SHP") 
	     || strcmp(pszFilename+strlen(pszFilename)-4,".SHX") )
    {
        strcpy( pszDBFFilename+strlen(pszDBFFilename)-4, ".DBF");
    }

    /* -------------------------------------------------------------------- */
    /*      Open the file.                                                  */
    /* -------------------------------------------------------------------- */
    psDBF = (DBFHandle) calloc( 1, sizeof(DBFInfo) );
    psDBF->zfp = zzip_open_rb(zdir, pszDBFFilename);
    if( psDBF->zfp == NULL )
        return( NULL );

#ifdef SHAPELIB_DISABLED
    psDBF->bNoHeader = MS_FALSE;
#endif /* SHAPELIB_DISABLED */
    psDBF->nCurrentRecord = -1;
#ifdef SHAPELIB_DISABLED
    psDBF->bCurrentRecordModified = MS_FALSE;
#endif /* SHAPELIB_DISABLED */

    psDBF->pszStringField = NULL;
    psDBF->nStringFieldLen = 0;    

    free( pszDBFFilename );

    /* -------------------------------------------------------------------- */
    /*  Read Table Header info                                              */
    /* -------------------------------------------------------------------- */
    pabyBuf = (uchar *) malloc(500);
    zzip_fread( pabyBuf, 32, 1, psDBF->zfp );

    psDBF->nRecords = nRecords = 
     pabyBuf[4] + pabyBuf[5]*256 + pabyBuf[6]*256*256 + pabyBuf[7]*256*256*256;

    psDBF->nHeaderLength = nHeadLen = pabyBuf[8] + pabyBuf[9]*256;
    psDBF->nRecordLength = nRecLen = pabyBuf[10] + pabyBuf[11]*256;
    
    psDBF->nFields = nFields = (nHeadLen - 32) / 32;

    psDBF->pszCurrentRecord = (char *) malloc(nRecLen);

    /* -------------------------------------------------------------------- */
    /*  Read in Field Definitions                                           */
    /* -------------------------------------------------------------------- */
    pabyBuf = (uchar *) SfRealloc(pabyBuf,nHeadLen);
    psDBF->pszHeader = (char *) pabyBuf;

    zzip_seek( psDBF->zfp, 32, 0 );
    zzip_fread( pabyBuf, nHeadLen, 1, psDBF->zfp );

    psDBF->panFieldOffset = (int *) malloc(sizeof(int) * nFields);
    psDBF->panFieldSize = (int *) malloc(sizeof(int) * nFields);
    psDBF->panFieldDecimals = (int *) malloc(sizeof(int) * nFields);
    psDBF->pachFieldType = (char *) malloc(sizeof(char) * nFields);

    for( iField = 0; iField < nFields; iField++ )
    {
	uchar		*pabyFInfo;

	pabyFInfo = pabyBuf+iField*32;

	if( pabyFInfo[11] == 'N' || pabyFInfo[11] == 'F' )
	{
	    psDBF->panFieldSize[iField] = pabyFInfo[16];
	    psDBF->panFieldDecimals[iField] = pabyFInfo[17];
	}
	else
	{
	    psDBF->panFieldSize[iField] = pabyFInfo[16] + pabyFInfo[17]*256;
	    psDBF->panFieldDecimals[iField] = 0;
	}

	psDBF->pachFieldType[iField] = (char) pabyFInfo[11];
	if( iField == 0 )
	    psDBF->panFieldOffset[iField] = 1;
	else
	    psDBF->panFieldOffset[iField] = 
	      psDBF->panFieldOffset[iField-1] + psDBF->panFieldSize[iField-1];
    }

    return( psDBF );
}
Exemple #26
0
    //-----------------------------------------------------------------------
    void ZipDataStream::seek( size_t pos )
    {
		zzip_seek(mZzipFile, static_cast<zzip_off_t>(pos), SEEK_SET);
    }
Exemple #27
0
 //-----------------------------------------------------------------------
 void ZipDataStream::skip(long count)
 {
     zzip_seek(mZzipFile, static_cast<zzip_off_t>(count), SEEK_CUR);
 }
ModelFileParser* ResourceManager::LoadModel(const char* file)
{
#ifdef _CACH_PARSED_DATA_
	for (std::map<const char*, ModelFileParser*>::iterator it = mModelFileParsers.begin(); it != mModelFileParsers.end(); it++)
	{
		if (it->first == file)
			return it->second;
	}
#endif

	unsigned int bufferSize;
	char* buffer;

	switch ( mAssetPacketExtension )
	{
		case PACA:
		{
			bufferSize	= mPacaReader.GetResourceSize( file );
			buffer		= new char[bufferSize];
			if ( !mPacaReader.GetResource( file, buffer, bufferSize ) )
				return nullptr;
			break;
		}
		break;

		case ZIP:
		{
			ZZIP_FILE* fp = zzip_file_open( mDir, file, 0 );
			zzip_seek( fp, 0, SEEK_END );
			bufferSize = zzip_tell( fp );
			zzip_rewind( fp );

			buffer = new char[bufferSize];
			bufferSize = zzip_file_read( fp, buffer, bufferSize );

			zzip_file_close( fp );
		}
		break;
	}
	addToMemCount(bufferSize);

	ModelFileParser* mParser;
	std::string fileString;
	fileString = std::string(file);
	if (fileString.substr(fileString.find_last_of(".") + 1) == "mesh")
	{
		mParser = new MeshParser();
		addToMemCount(sizeof(mParser));
	}
	else if (fileString.substr(fileString.find_last_of(".") + 1) == "obj")
	{
		mParser = new ObjParser();
		addToMemCount(sizeof(mParser));
	}
	else
	{
		delete[] buffer;
		addToMemCount(-bufferSize);
		return NULL;
	}

	mParser->Load(buffer, bufferSize);
	addToMemCount(mParser->memory);
	delete[] buffer;
	addToMemCount(-bufferSize);

#ifdef _CACH_PARSED_DATA_
	mModelFileParsers.insert(std::pair<const char*, ModelFileParser*>(file, mParser));
#endif

	return mParser;
}
Exemple #29
0
static void
ReadAirspace(AirspaceDatabase &airspace_database, ZZIP_FILE *fp)
{
  StartupStore(TEXT("ReadAirspace\n"));
  int	Tock = 0;
  DWORD	dwStep;
  DWORD	dwPos;
  DWORD	dwOldPos = 0L;
  int	nLineType;

  unsigned NumberOfAirspacePointsPass[2] = { 0, 0 };
  unsigned NumberOfAirspaceAreasPass[2] = { 0, 0 };
  unsigned NumberOfAirspaceCirclesPass[2] = { 0, 0 };

  LineCount = 0;

  airspace_database.SetQNH(QNH);

  XCSoarInterface::CreateProgressDialog(gettext(TEXT("Loading Airspace File...")));
  // Need step size finer than default 10
  XCSoarInterface::SetProgressStepSize(5);
  dwStep = zzip_file_size(fp) / 10L;

  TempArea.FirstPoint = airspace_database.NumberOfAirspacePoints;

  bFillMode = false;
  bWaiting = true;
  XCSoarInterface::StepProgressDialog();
  while((nLineType = GetNextLine(fp, TempString)) >= 0) {
    Tock++;
    Tock %= 50;
    if (Tock == 0) {
      dwPos = zzip_tell(fp);
      if ((dwPos - dwOldPos) >= dwStep) {
        XCSoarInterface::StepProgressDialog();
        dwOldPos = dwPos;
      }
    }

    if (!ParseLine(airspace_database, nLineType, NumberOfAirspacePointsPass[0],
                   NumberOfAirspaceAreasPass[0],
                   NumberOfAirspaceCirclesPass[0])) {
      CloseAirspace(airspace_database);
      return;
    }
  }

  // Process final area (if any). bFillMode is false.  JG 10-Nov-2005
  if (!bWaiting)
    NumberOfAirspaceAreasPass[0]++; // ????

  // allocate new memory
  size_t screen_point_size = (airspace_database.NumberOfAirspacePoints +
                              NumberOfAirspacePointsPass[0]) *
    sizeof(AirspaceScreenPoint[0]);
  POINT *new_points = AirspaceScreenPoint == NULL
    ? (POINT *)LocalAlloc(LMEM_FIXED, screen_point_size)
    : (POINT *)LocalReAlloc(AirspaceScreenPoint, LMEM_FIXED,
                            screen_point_size);
  if (new_points != NULL)
    AirspaceScreenPoint = new_points;

  if ((screen_point_size > 0 && new_points == NULL) ||
      !airspace_database.GrowPoints(NumberOfAirspacePointsPass[0]) ||
      !airspace_database.GrowAreas(NumberOfAirspaceAreasPass[0]) ||
      !airspace_database.GrowCircles(NumberOfAirspaceCirclesPass[0])) {
    // can't allocate memory, so delete everything
    airspace_database.Clear();

    if (AirspaceScreenPoint != NULL) {
      LocalFree((HLOCAL)AirspaceScreenPoint);
      AirspaceScreenPoint = NULL;
    }

    return;
  }

  // ok, start the read
  TempArea.FirstPoint = airspace_database.NumberOfAirspacePoints;
  zzip_seek(fp, 0, SEEK_SET );
  LineCount = -1;

  bFillMode = true;
  bWaiting = true;
  dwOldPos = 0L;
  XCSoarInterface::StepProgressDialog();
  CenterY = CenterX = 0;
  Rotation = 1;

  while((nLineType = GetNextLine(fp, TempString)) >= 0) {
    Tock++;
    Tock %= 50;

    if (Tock == 0) {
      dwPos = zzip_tell(fp);
      if ((dwPos - dwOldPos) >= dwStep) {
        XCSoarInterface::StepProgressDialog();
        dwOldPos = dwPos;
      }
    }

    ParseLine(airspace_database, nLineType, NumberOfAirspacePointsPass[1],
              NumberOfAirspaceAreasPass[1],
              NumberOfAirspaceCirclesPass[1]);
  }

  // Process final area (if any). bFillMode is true.  JG 10-Nov-2005
  if (!bWaiting)
    AddArea(airspace_database, &TempArea, NumberOfAirspaceAreasPass[1]);

  if (NumberOfAirspacePointsPass[0] != NumberOfAirspacePointsPass[1]
      || NumberOfAirspaceAreasPass[0] != NumberOfAirspaceAreasPass[1]
      || NumberOfAirspaceCirclesPass[0] != NumberOfAirspaceCirclesPass[1]){

    if (MessageBoxX(gettext(TEXT("Internal Airspace Parser Error!\r\nPlease send this Airspacefile to Support")),
                    gettext(TEXT("Airspace")), MB_OKCANCEL) == IDCANCEL) {
    }

  }

#ifndef NDEBUG
  // only do this if debugging
  DumpAirspaceFile(airspace_database);
#endif

//  if(AirspacePoint != NULL)  LocalFree((HLOCAL)AirspacePoint);

}
int ZIPROMReaderSeek(void * file, int offset, int whence)
{
	return zzip_seek((ZZIP_FILE*)file, offset, whence);
}