KDbool AssetsManager::downLoad ( KDvoid )
{
    // Create a file to save package.
	std::string  sOutFileName = m_sStoragePath + TEMP_PACKAGE_FILE_NAME;
    KDFile*  pFile = kdFopen ( sOutFileName.c_str ( ), "wb" );
    if ( !pFile )
    {
        CCLOG ( "can not create file %s", sOutFileName.c_str ( ) );
        return KD_FALSE;
    }
    
    // Download pacakge
    CURLcode   eRes;
    curl_easy_setopt ( m_pCurl, CURLOPT_URL, m_sPackageUrl.c_str ( ) );
    curl_easy_setopt ( m_pCurl, CURLOPT_WRITEFUNCTION, downLoadPackage );
    curl_easy_setopt ( m_pCurl, CURLOPT_WRITEDATA, pFile );
    curl_easy_setopt ( m_pCurl, CURLOPT_NOPROGRESS, KD_FALSE );
    curl_easy_setopt ( m_pCurl, CURLOPT_PROGRESSFUNCTION, progressFunc );
    eRes = curl_easy_perform ( m_pCurl );
    curl_easy_cleanup ( m_pCurl );
    if ( eRes != 0 )
    {
        CCLOG ( "error when download package" );
        kdFclose ( pFile );
        return KD_FALSE;
    }
    
    CCLOG ( "succeed downloading package %s", m_sPackageUrl.c_str ( ) );
    
    kdFclose ( pFile );
    return KD_TRUE;
}
Beispiel #2
0
/* kdGetImageATX, kdGetImageFromStreamATX: Read and decode an image from a file or stream, returning a decoded image object. */
KD_API KDImageATX KD_APIENTRY kdGetImageATX(const KDchar *pathname, KDint format, KDint flags)
{
    KDFile *file = kdFopen(pathname, "rb");
    if(file == KD_NULL)
    {
        kdSetError(KD_EIO);
        return KD_NULL;
    }
    KDImageATX image = kdGetImageFromStreamATX(file, format, flags);
    kdFclose(file);
    return image;
}
bool  WBaseStream::open( const string& filename )
{
    close();
    allocate();
    
    m_file = kdFopen( filename.c_str(), "wb" );
    if( m_file )
    {
        m_is_opened = true;
        m_block_pos = 0;
        m_current = m_start;
    }
    return m_file != 0;
}
bool  RBaseStream::open( const string& filename )
{
    close();
    allocate();

    m_file = kdFopen( filename.c_str(), "rb" );
    if( m_file )
    {
        m_is_opened = true;
        setPos(0);
        readBlock();
    }
    return m_file != 0;
}
KDbool Image::initWithStringShadowStroke (	const KDchar*	szText,
											KDint           nWidth,
											KDint           nHeight,
											TextAlign       eAlignMask,
											const KDchar*	szFontName,
											KDint			nSize,
											KDfloat			fTextTintR,
											KDfloat			fTextTintG,
											KDfloat			fTextTintB,
											KDbool			bShadow,
											KDfloat			fShadowOffsetX,
											KDfloat			fShadowOffsetY,
											KDfloat			fShadowOpacity,
											KDfloat			fShadowBlur,
											KDbool			bStroke,
											KDfloat			fStrokeR,
											KDfloat			fStrokeG,
											KDfloat			fStrokeB,
											KDfloat			fStrokeSize )
{
	KDbool		bRet = KD_FALSE;

    KDFile*		pFile = kdFopen ( FileUtils::getInstance ( )->fullPathForFilename ( szFontName ).c_str( ), "r" );
	if ( pFile )
	{
		kdFclose ( pFile );
		bRet = this->initWithFreeTypeFont ( szText, nWidth, nHeight, eAlignMask, szFontName, nSize );
	}
	#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_IOS
	else
	{
		bRet = this->initWithPlatformFont
		(
			szText, nWidth, nHeight, eAlignMask, szFontName, nSize, 
			fTextTintR, fTextTintG, fTextTintB, 
			bShadow, fShadowOffsetX, fShadowOffsetY, fShadowOpacity, fShadowBlur,
			bStroke, fStrokeR, fStrokeG, fStrokeB, fStrokeSize
		);
	}
	#endif

	return bRet;
}
KDvoid TestLocation::onEnter ( KDvoid )
{	
	TestBasic::onEnter ( );

	m_pLabel = CCLabelTTF::create 
	(
		"", "fonts/Thonburi.ttf", 20, CCPointZero, kCCAlignmentLeft
	);	
	this->addChild ( m_pLabel );
	m_pLabel->setPositionWithParent ( kCCAlignmentCenter );

	this->setEventEnabled ( KD_TRUE ); 

	KDbool  bFromGPS;
	if ( s_nSubTestIndex == 0 )
	{
		m_pFile = kdFopen ( "/res/xm_supports/GPS_LOG.txt", "rb" );
		this->schedule ( schedule_selector ( TestLocation::onReadFile ), 0.5f );

		bFromGPS = KD_FALSE;
        xmResetNMEA ( bFromGPS );
	}
	else
	{
		m_pFile  = KD_NULL;
		bFromGPS = KD_TRUE;
	}

	this->onRefresh ( bFromGPS );

	if ( CCApplication::sharedApplication ( )->getTargetPlatform ( ) == kCCTargetAndroid )
	{
		xmStartGPSLog ( "/storage/GPS_LOG.txt" );
	}
	else
	{
		xmStartGPSLog ( "/data/GPS_LOG.txt" );
	}		
}
void
xmlMemDisplay(KDFile *fp)
{
#ifdef MEM_LIST
    MEMHDR *p;
    unsigned idx;
    int     nb = 0;
#if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME)
    time_t currentTime;
    char buf[500];
    struct tm * tstruct;
#endif
#endif
    KDFile *old_fp = fp;

    if (fp == KD_NULL) {
	fp = kdFopen ( ".memorylist", "w" );
	if (fp == KD_NULL)
	    return;
    }

#ifdef MEM_LIST
#if defined(HAVE_LOCALTIME) && defined(HAVE_STRFTIME)
    currentTime = time(KD_NULL);
    tstruct = localtime(&currentTime);
    strftime(buf, sizeof(buf) - 1, "%I:%M:%S %p", tstruct);
    fprintf(fp,"      %s\n\n", buf);
#endif


    fprintf(fp,"      MEMORY ALLOCATED : %lu, MAX was %lu\n",
            debugMemSize, debugMaxMemSize);
    fprintf(fp,"BLOCK  NUMBER   SIZE  TYPE\n");
    idx = 0;
    xmlMutexLock(xmlMemMutex);
    p = memlist;
    while (p) {
	  fprintf(fp,"%-5u  %6lu %6lu ",idx++,p->mh_number,
		  (unsigned long)p->mh_size);
        switch (p->mh_type) {
           case STRDUP_TYPE:fprintf(fp,"strdup()  in ");break;
           case MALLOC_TYPE:fprintf(fp,"kdMalloc()  in ");break;
           case REALLOC_TYPE:fprintf(fp,"kdRealloc() in ");break;
           case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomickdMalloc()  in ");break;
           case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomickdRealloc() in ");break;
           default:
	        fprintf(fp,"Unknown memory block, may be corrupted");
		xmlMutexUnlock(xmlMemMutex);
		if (old_fp == KD_NULL)
		    fclose(fp);
		return;
        }
	if (p->mh_file != KD_NULL) fprintf(fp,"%s(%u)", p->mh_file, p->mh_line);
        if (p->mh_tag != MEMTAG)
	      fprintf(fp,"  INVALID");
        nb++;
	if (nb < 100)
	    xmlMemContentShow(fp, p);
	else
	    fprintf(fp," skip");

        fprintf(fp,"\n");
        p = p->mh_next;
    }
    xmlMutexUnlock(xmlMemMutex);
#else
    kdFprintfKHR ( fp,"Memory list not compiled (MEM_LIST not defined !)\n");
#endif
    if (old_fp == KD_NULL)
	kdFclose ( fp );
}
void
xmlMemDisplayLast(KDFile *fp, long nbBytes)
{
#ifdef MEM_LIST
    MEMHDR *p;
    unsigned idx;
    int     nb = 0;
#endif
    KDFile *old_fp = fp;

    if (nbBytes <= 0)
        return;

    if (fp == KD_NULL) {
	fp = kdFopen(".memorylist", "w");
	if (fp == KD_NULL)
	    return;
    }

#ifdef MEM_LIST
    fprintf(fp,"   Last %li MEMORY ALLOCATED : %lu, MAX was %lu\n",
            nbBytes, debugMemSize, debugMaxMemSize);
    fprintf(fp,"BLOCK  NUMBER   SIZE  TYPE\n");
    idx = 0;
    xmlMutexLock(xmlMemMutex);
    p = memlist;
    while ((p) && (nbBytes > 0)) {
	  fprintf(fp,"%-5u  %6lu %6lu ",idx++,p->mh_number,
		  (unsigned long)p->mh_size);
        switch (p->mh_type) {
           case STRDUP_TYPE:fprintf(fp,"strdup()  in ");break;
           case MALLOC_TYPE:fprintf(fp,"kdMalloc()  in ");break;
           case REALLOC_TYPE:fprintf(fp,"kdRealloc() in ");break;
           case MALLOC_ATOMIC_TYPE:fprintf(fp,"atomickdMalloc()  in ");break;
           case REALLOC_ATOMIC_TYPE:fprintf(fp,"atomickdRealloc() in ");break;
           default:
	        fprintf(fp,"Unknown memory block, may be corrupted");
		xmlMutexUnlock(xmlMemMutex);
		if (old_fp == KD_NULL)
		    fclose(fp);
		return;
        }
	if (p->mh_file != KD_NULL) fprintf(fp,"%s(%u)", p->mh_file, p->mh_line);
        if (p->mh_tag != MEMTAG)
	      fprintf(fp,"  INVALID");
        nb++;
	if (nb < 100)
	    xmlMemContentShow(fp, p);
	else
	    fprintf(fp," skip");

        fprintf(fp,"\n");
	nbBytes -= (unsigned long)p->mh_size;
        p = p->mh_next;
    }
    xmlMutexUnlock(xmlMemMutex);
#else
    kdFprintfKHR ( fp,"Memory list not compiled (MEM_LIST not defined !)\n");
#endif
    if (old_fp == KD_NULL)
	kdFclose ( fp );
}
KDbool AssetsManager::uncompress_ ( KDvoid )
{
    // Open the zip file
	std::string  sOutFileName = m_sStoragePath + TEMP_PACKAGE_FILE_NAME;
    unzFile      pZipFile = unzOpen ( sOutFileName.c_str ( ) );
    if ( !pZipFile )
    {
        CCLOG ( "can not open downloaded zip file %s", sOutFileName.c_str ( ) );
        return KD_FALSE;
    }
    
    // Get info about the zip file
    unz_global_info  tGlobalInfo;
    if ( unzGetGlobalInfo ( pZipFile, &tGlobalInfo ) != UNZ_OK )
    {
        CCLOG ( "can not read file global info of %s", sOutFileName.c_str ( ) );
        unzClose ( pZipFile );
    }
    
    // Buffer to hold data read from the zip file
    KDchar  aReadBuffer [ BUFFER_SIZE ];
    
    CCLOG ( "start uncompressing" );
    
    // Loop to extract all files.
    uLong i;
    for ( i = 0; i < tGlobalInfo.number_entry; ++i )
    {
        // Get info about current file.
        unz_file_info  tFileInfo;
        KDchar  szFileName[MAX_FILENAME];

        if ( unzGetCurrentFileInfo ( pZipFile,
                                   &tFileInfo,
                                   szFileName,
                                   MAX_FILENAME,
                                   NULL,
                                   0,
                                   NULL,
                                   0 ) != UNZ_OK )
        {
            CCLOG ( "can not read file info" );
            unzClose ( pZipFile );
            return KD_FALSE;
        }
        
		std::string  sFullPath = m_sStoragePath + szFileName;
        
        // Check if this entry is a directory or a file.
        const KDsize  uFilenameLength = kdStrlen ( szFileName );
        if ( szFileName [ uFilenameLength - 1 ] == '/' )
        {
            // Entry is a direcotry, so create it.
            // If the directory exists, it will failed scilently.
            if ( !createDirectory ( sFullPath.c_str ( ) ) )
            {
                CCLOG ( "can not create directory %s", sFullPath.c_str ( ) );
                unzClose ( pZipFile );
                return KD_FALSE;
            }
        }
        else
        {
            // Entry is a file, so extract it.
            
            // Open current file.
            if ( unzOpenCurrentFile ( pZipFile ) != UNZ_OK )
            {
                CCLOG ( "can not open file %s", szFileName );
                unzClose ( pZipFile );
                return KD_FALSE;
            }
            
            // Create a file to store current file.
            KDFile*  pOut = kdFopen ( sFullPath.c_str ( ), "wb" );
            if ( !pOut )
            {
                CCLOG ( "can not open destination file %s", sFullPath.c_str ( ) );
                unzCloseCurrentFile ( pZipFile );
                unzClose ( pZipFile );
                return KD_FALSE;
            }
            
            // Write current file content to destinate file.
            KDint  nError = UNZ_OK;
            do
            {
                nError = unzReadCurrentFile ( pZipFile, aReadBuffer, BUFFER_SIZE );
                if ( nError < 0 )
                {
                    CCLOG ( "can not read zip file %s, error code is %d", szFileName, nError );
                    unzCloseCurrentFile ( pZipFile );
                    unzClose( pZipFile );
                    return KD_FALSE;
                }
                
                if ( nError > 0 )
                {
                    kdFwrite ( aReadBuffer, nError, 1, pOut );
                }

            } while ( nError > 0 );
            
            kdFclose ( pOut );
        }
        
        unzCloseCurrentFile ( pZipFile );
        
        // Goto next entry listed in the zip file.
        if ( ( i + 1 ) < tGlobalInfo.number_entry )
        {
            if ( unzGoToNextFile ( pZipFile ) != UNZ_OK )
            {
                CCLOG ( "can not read next file" );
                unzClose ( pZipFile );
                return KD_FALSE;
            }
        }
    }
    
    CCLOG ( "end uncompressing" );
    
    return KD_TRUE;
}