bool CNData::init(const std::string & sPath) { if (sPath.empty() || !init()) { return false; } if (!CNFileManager::isExists(sPath.c_str())) { CNLog("file not found: %s", sPath.c_str()); return false; } cocos2d::CCFileData data(sPath.c_str(), "rb"); unsigned char * pBuffer = data.getBuffer(); unsigned long lSize = data.getSize(); if (!pBuffer || lSize <= sizeof(CNDataHead)) { CNLog("invalid data file: %s, size: %ld", sPath.c_str(), lSize); return false; } CNLog("read %ld bytes from file: %s", lSize, sPath.c_str()); m_pDataBuffer = (unsigned char *)malloc(lSize); if (!m_pDataBuffer) { CNLog("not enough memory, size: %ld", lSize); return false; } m_iBufferLength = lSize; memset(m_pDataBuffer, 0, m_iBufferLength); memcpy(m_pDataBuffer, pBuffer, m_iBufferLength); m_bIsDataValid = checkDataFormat(); return m_bIsDataValid; }
bool CNData::saveToFile(const std::string & sPath) const { if (sPath.empty()) { return false; } if (!checkDataFormat()) { CNLog("data format error"); return false; } CNLog("saving to binary file: %s", sPath.c_str()); FILE * fp = fopen(sPath.c_str(), "wb"); if (!fp) { CNLog("failed to open file for write: %s", sPath.c_str()); return false; } unsigned long length = fwrite(m_pDataBuffer, sizeof(unsigned char), m_iBufferLength, fp); fclose(fp); CNLog("buffer length: %ld, written length: %ld", m_iBufferLength, length); return length == m_iBufferLength; }
unsigned char * CNData::getStringsBuffer(unsigned long * pBufferLength) const { if (!checkDataFormat()) { return NULL; } CNDataHead * pHead = (CNDataHead *)m_pDataBuffer; *pBufferLength = pHead->body.stringsBuffer.length; return &m_pDataBuffer[pHead->body.stringsBuffer.offset]; }
SLresult checkDataSink(const char *name, const SLDataSink *pDataSink, DataLocatorFormat *pDataLocatorFormat, SLuint32 allowedDataLocatorMask, SLuint32 allowedDataFormatMask) { assert(NULL != name && NULL != pDataLocatorFormat); pDataLocatorFormat->u.mSink.pLocator = &pDataLocatorFormat->mLocator; pDataLocatorFormat->u.mSink.pFormat = &pDataLocatorFormat->mFormat; if (NULL == pDataSink) { pDataLocatorFormat->mLocator.mLocatorType = SL_DATALOCATOR_NULL; pDataLocatorFormat->mFormat.mFormatType = SL_DATAFORMAT_NULL; if ((allowedDataLocatorMask & DATALOCATOR_MASK_NULL) && (allowedDataFormatMask & DATAFORMAT_MASK_NULL)) { return SL_RESULT_SUCCESS; } SL_LOGE("%s: data sink cannot be NULL", name); return SL_RESULT_PARAMETER_INVALID; } SLDataSink myDataSink = *pDataSink; SLresult result; result = checkDataLocator(name, myDataSink.pLocator, &pDataLocatorFormat->mLocator, allowedDataLocatorMask); if (SL_RESULT_SUCCESS != result) { return result; } switch (pDataLocatorFormat->mLocator.mLocatorType) { case SL_DATALOCATOR_URI: allowedDataFormatMask &= DATAFORMAT_MASK_MIME; break; case SL_DATALOCATOR_ADDRESS: case SL_DATALOCATOR_BUFFERQUEUE: allowedDataFormatMask &= DATAFORMAT_MASK_PCM | DATAFORMAT_MASK_PCM_EX; break; // Per the spec, the pFormat field is ignored in some cases case SL_DATALOCATOR_IODEVICE: case SL_DATALOCATOR_OUTPUTMIX: case XA_DATALOCATOR_NATIVEDISPLAY: myDataSink.pFormat = NULL; // fall through case SL_DATALOCATOR_NULL: case SL_DATALOCATOR_MIDIBUFFERQUEUE: allowedDataFormatMask &= DATAFORMAT_MASK_NULL; break; #ifdef ANDROID case SL_DATALOCATOR_ANDROIDFD: allowedDataFormatMask = DATAFORMAT_MASK_NONE; break; case SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE: allowedDataFormatMask &= DATAFORMAT_MASK_PCM | DATAFORMAT_MASK_PCM_EX; break; case SL_DATALOCATOR_ANDROIDBUFFERQUEUE: allowedDataFormatMask = DATAFORMAT_MASK_NONE; break; #endif default: // invalid data locator type is caught earlier assert(false); allowedDataFormatMask = DATAFORMAT_MASK_NONE; break; } result = checkDataFormat(name, myDataSink.pFormat, &pDataLocatorFormat->mFormat, allowedDataFormatMask, SL_BOOLEAN_FALSE /*isOutputFormat*/); if (SL_RESULT_SUCCESS != result) { freeDataLocator(&pDataLocatorFormat->mLocator); return result; } return SL_RESULT_SUCCESS; }