static cxBool cxMp3StreamOpen(cxAny stream) { cxMp3Stream this = stream; this->fd = open(cxStringBody(this->path), O_RDONLY, 0600);; if(this->fd <= 0){ CX_ERROR("open strean file %s failed",cxStringBody(this->path)); return false; } int error = 0; this->mh = mpg123_new(NULL, &error); if(this->mh == NULL){ CX_ERROR("mpg123 new failed"); return false; } if(mpg123_open_fd(this->mh, this->fd) != MPG123_OK){ CX_ERROR("open mpg file failed %s",cxStringBody(this->path)); return false; } allocator->free(this->buffer); this->bufsiz = mpg123_outblock(this->mh); this->buffer = allocator->malloc(this->bufsiz); mpg123_getformat(this->mh, &this->freq, &this->channels, &this->encoding); this->super.canRead = true; this->super.canSeek = true; this->super.isOpen = true; return true; }
cxBool cxDBOpen(cxAny db,cxString file,cxString table,cxBool rdonly) { CX_ASSERT(file != NULL && table != NULL, "args error"); cxDBEnv env = cxDBGetEnv(db); cxDB this = db; if(rdonly){ this->flags = DB_RDONLY; }else{ this->flags = DB_CREATE; } this->ret = db_create(&this->dbptr, env->env, 0); CX_RETURN(this->ret != 0,false); this->ret = CX_METHOD_FIRE(this->ret, this->OpenBefore,db); CX_RETURN(this->ret != 0,false); cxDBTxn txn = cxStackTop(env->txn); this->ret = this->dbptr->open(this->dbptr,cxDBTxnPtr(txn),cxStringBody(file),cxStringBody(table),this->type,this->flags,0); if(this->ret != 0){ CX_ERROR("open db error %s:%s",cxStringBody(file),cxStringBody(table)); return false; } CX_RETURN(this->ret != 0,false); CX_RETAIN_SWAP(this->file, file); CX_RETAIN_SWAP(this->table, table); return CX_METHOD_FIRE(true, this->OpenAfter,db); }
cxBool cxDBSet(cxAny db,cxString key,cxString value) { cxDB this = db; cxDBEnv env = cxDBGetEnv(db); CX_ASSERT(this->flags & DB_RDONLY, "only env ,can't set data"); cxDBTxn txn = cxStackTop(env->txn); DBT k={0}; k.data = (void *)cxStringBody(key); k.size = cxStringLength(key); k.flags = DB_DBT_USERMEM; DBT v={0}; v.data = (void *)cxStringBody(value); v.size = cxStringLength(value); v.flags = DB_DBT_USERMEM; return this->dbptr->put(this->dbptr,cxDBTxnPtr(txn),&k,&v,0) == 0; }
static void cxHashRootReadDB(cxDBEnv env,cxHashRoot root,xmlTextReaderPtr reader) { cxReaderAttrInfo *info = cxReaderAttrInfoMake(reader, root, env); int depth = xmlTextReaderDepth(reader); while(xmlTextReaderRead(reader) && depth != xmlTextReaderDepth(reader)){ if(xmlTextReaderNodeType(reader) != XML_READER_TYPE_ELEMENT){ continue; } cxConstChars temp = cxXMLReadElementName(reader); if(!ELEMENT_IS_TYPE(cxDB)){ continue; } cxConstChars file = cxXMLAttr(reader,"file"); cxConstChars table = cxXMLAttr(reader,"table"); cxConstChars type = cxXMLAttr(reader,"type"); cxConstChars sid = cxXMLAttr(reader,"id"); cxConstChars path = cxXMLAttr(reader,"path"); //assert file copy ->to document cxBool copy = cxXMLReadBoolAttr(info, "copy", false); if(copy && file != NULL){ cxCopyFile(file, NULL, NULL); } cxBool rdonly = cxXMLReadBoolAttr(info, "rdonly", false); if(sid == NULL){ CX_WARN("db id not set,will can't add dataset"); } cxString sfile = NULL; if(cxConstCharsEqu(path, "assert")){ sfile = cxAssetsPath(file); //assert must set true rdonly = true; }else if(cxConstCharsEqu(path, "document")){ sfile = cxDocumentPath(file); }else{ CX_ERROR("must set path assert or document"); } cxAny db = NULL; if(file != NULL && table != NULL && type != NULL){ db = cxDBCreate(env, cxStringBody(sfile), table, type, rdonly); } if(db != NULL && sid != NULL){ cxHashSet(root->items, cxHashStrKey(sid), cxDBTypesCreate(db)); }else{ CX_ERROR("open dbenv type %s,db %s:%s failed",cxStringBody(env->type),file,table); } } }
static cxInt cxStringLuaPrint(lua_State *L) { CX_LUA_DEF_THIS(cxString); if(this != NULL){ CX_LOGGER("%s",cxStringBody(this)); } return 0; }
CX_METHOD_DEF(cxTextureJPG,Load,cxBool,cxStream stream) { cxBool ret = false; struct jpeg_decompress_struct cinfo; cxJPEGError error; cinfo.err = jpeg_std_error(&error.pub); error.error = false; error.pub.error_exit = cxJPGErrorExit; jpeg_create_decompress(&cinfo); if(error.error){ CX_ERROR("jpg create decompress failed"); goto finished; } cxString bytes = CX_CALL(stream, AllBytes, CX_M(cxString)); if(bytes == NULL){ CX_ERROR("jpg read stream data error"); goto finished; } jpeg_mem_src(&cinfo, (cxUChar *)cxStringBody(bytes), cxStringLength(bytes)); jpeg_read_header(&cinfo, true); if(error.error){ CX_ERROR("jpg read head failed"); goto finished; } this->cxTexture.size = cxSize2fv(cinfo.image_width, cinfo.image_height); this->cxTexture.hasAlpha = false; jpeg_start_decompress(&cinfo); if(error.error){ CX_ERROR("jpg start decompress failed"); goto finished; } int row_stride = cinfo.output_width * cinfo.output_components; cxAny data = allocator->malloc(row_stride * cinfo.output_height); JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); int line = 0; while(cinfo.output_scanline < cinfo.output_height){ jpeg_read_scanlines(&cinfo, buffer, 1); memcpy(data + line * row_stride, buffer[0], row_stride); line++; } GLint unpack = 0; glGetIntegerv(GL_UNPACK_ALIGNMENT, &unpack); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); cxOpenGLGenTextures(1, &this->cxTexture.textureId); cxOpenGLBindTexture(this->cxTexture.textureId, 0); cxOpenGLSetTexParameters(this->cxTexture.texParam); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, cinfo.image_width, cinfo.image_height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glPixelStorei(GL_UNPACK_ALIGNMENT, unpack); cxOpenGLBindTexture(0, 0); allocator->free(data); ret = true; finished: jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); return ret; }
void cxStringReplace(cxString string,cxChar find,cxChar rep) { cxChar *ptr = (cxChar *)cxStringBody(string); cxInt len = cxStringLength(string); for(cxInt i=0; i < len; i++){ if(ptr[i] == find){ ptr[i] = rep; } } }
cxBool cxDBHas(cxAny db,cxString key) { cxDB this = db; cxDBEnv env = cxDBGetEnv(db); cxDBTxn txn = cxStackTop(env->txn); DBT k={0}; k.data = (void *)cxStringBody(key); k.size = cxStringLength(key); k.flags = DB_DBT_USERMEM; return (this->dbptr->exists(this->dbptr,cxDBTxnPtr(txn),&k,0) == 0); }
cxString cxEngineLocalizedText(cxConstChars url) { cxEngine this = cxEngineInstance(); cxUrlPath path = cxUrlPathParse(url); cxTypes langTypes= cxEngineTypes("appConfig.xml?lang"); CX_ASSERT(langTypes != NULL, "appConfig.xml must set lang filed"); cxString dir = cxHashGet(langTypes->any, cxHashStrKey(cxStringBody(this->lang))); if(dir == NULL){ dir = cxHashFirst(langTypes->any); } CX_ASSERT(dir != NULL, "get lang dir error"); cxConstChars file = CX_CONST_STRING("%s/%s",cxStringBody(dir),path->path); cxTypes types = cxEngineTypes(CX_CONST_STRING("%s?%s",file,path->key)); CX_RETURN(types == NULL || !cxTypesIsType(types, cxTypesString), NULL); return types->any; }
cxJson cxJsonCreate(cxString json) { CX_ASSERT(json != NULL, "args error"); cxJson this = CX_CREATE(cxJson); json_error_t error = {0}; if(cxStringLength(json) <= 0){ return this; } this->json = json_loadb(cxStringBody(json), cxStringLength(json), JSON_DECODE_ANY, &error); CX_ASSERT(this->json != NULL, "cxJson load error (%d:%d) %s:%s",error.line,error.column,error.source,error.text); return this; }
cxBool cxDBDel(cxAny db,cxString key) { cxDB this = db; cxDBEnv env = cxDBGetEnv(db); CX_ASSERT(this->flags & DB_RDONLY, "only env ,can't del data"); cxDBTxn txn = cxStackTop(env->txn); DBT k={0}; k.data = (void *)cxStringBody(key); k.size = cxStringLength(key); k.flags = DB_DBT_USERMEM; return (this->dbptr->del(this->dbptr,cxDBTxnPtr(txn),&k,0) == 0); }
//texture="res/a.xml?green.png" void cxSpriteReadAttr(cxReaderAttrInfo *info) { cxSprite this = info->object; //set texture cxTextureAttr texAttr = cxXMLReadTextureAttr(info, "cxSprite.texture"); cxSpriteSetTextureAttr(this, texAttr); //flipx flipy cxSpriteSetFlipX(this, cxXMLReadBoolAttr(info, "cxSprite.flipX", this->isFlipX)); cxSpriteSetFlipY(this, cxXMLReadBoolAttr(info, "cxSprite.flipY", this->isFlipY)); //shader cxString shader = cxXMLReadStringAttr(info, "cxSprite.shader"); cxSpriteSetShader(this, cxStringBody(shader)); // cxViewReadAttr(info); }
cxString cxMD5(cxString v) { static const char hex[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; CX_ASSERT(v != NULL, "args error"); mongo_md5_state_t state={0}; mongo_md5_byte_t digest[MD5_DIGEST_LENGTH]; mongo_md5_init(&state); mongo_md5_append(&state, (const mongo_md5_byte_t *)cxStringBody(v), cxStringLength(v)); mongo_md5_finish(&state, digest); cxChar md5[MD5_DIGEST_LENGTH*2 + 1]={0}; for(cxInt i = 0; i < MD5_DIGEST_LENGTH; i++){ md5[2*i] = hex[(digest[i] & 0xf0)>> 4]; md5[2*i + 1] = hex[digest[i] & 0x0f]; } return cxStringConstChars(md5); }
cxString cxDBGet(cxAny db,cxString key) { cxDB this = db; cxDBEnv env = cxDBGetEnv(db); cxDBTxn txn = cxStackTop(env->txn); DBT k={0}; k.data = (void *)cxStringBody(key); k.size = cxStringLength(key); k.flags = DB_DBT_USERMEM; DBT v={0}; v.flags = DB_DBT_MALLOC; if(this->dbptr->get(this->dbptr, cxDBTxnPtr(txn), &k, &v,0) != 0){ return NULL; } if(v.size == 0 || v.data == NULL){ return NULL; } return cxStringAttach(v.data, v.size); }
CX_METHOD_DEF(cxAssetsStream,Open,cxBool) { CX_ASSERT(this->cxStream.isOpen == false,"stream repeat open"); cxConstChars path = cxStringBody(this->cxStream.path); this->asset = fopen(path, "rb"); if(this->asset == NULL){ CX_ERROR("open assets %s stream failed",path); return false; } struct stat stat={0}; if(lstat(path, &stat) != 0){ CX_ERROR("lstat assets %s stream failed",path); return false; } this->cxStream.Length = (cxInt)stat.st_size; this->cxStream.canRead = true; this->cxStream.canSeek = true; this->cxStream.canWrite = false; this->cxStream.isOpen = true; return true; }
static cxInt cxEngineLuaLoader(lua_State *L) { cxChar path[PATH_MAX] = {0}; cxConstChars file = luaL_checkstring(L, 1); cxConstChars ext = strrchr(file, '.'); cxString data = NULL; if(ext == NULL || !cxConstCharsEqu(ext, ".lua")){ snprintf(path, PATH_MAX, "%s.lua",file); data = cxEngineGetLuaScript(path); } if(data == NULL){ snprintf(path, PATH_MAX, "%s",file); data = cxEngineGetLuaScript(path); } if(data != NULL){ luaL_loadbuffer(L, cxStringBody(data), cxStringLength(data), file); return 1; } luaL_error(L, "error loading file %s error",path); return 1; }
//int AAsset_openFileDescriptor(AAsset* asset, off_t* outStart, off_t* outLength); //int AAsset_isAllocated(AAsset* asset); //const void* AAsset_getBuffer(AAsset* asset); CX_METHOD_DEF(cxMMapStream,Open,cxBool) { CX_ASSERT(this->cxStream.isOpen == false,"stream repeat open"); cxConstChars path = cxStringBody(this->cxStream.path); this->asset = AAssetManager_open(cxEngineGetAssetManager(), path, AASSET_MODE_UNKNOWN); if(this->asset == NULL){ CX_ERROR("open asset file %s failed",path); return false; } this->map = (cxAny)AAsset_getBuffer(this->asset); if(this->map == NULL){ CX_ERROR("asset get buffer error"); return false; } this->off = 0; this->cxStream.Length = (cxInt)AAsset_getLength(this->asset); this->cxStream.canRead = true; this->cxStream.canSeek = true; this->cxStream.canWrite = false; this->cxStream.isOpen = true; return true; }
cxArray cxStringSplit(cxString string,cxConstChars sp) { CX_ASSERT(string != NULL, "args error"); cxConstChars body = cxStringBody(string); cxInt length = cxStringLength(string) + 1; cxChar buffer[length]; cxInt idx = 0; cxArray ret = CX_CREATE(cxArray); for(cxInt i=0; i < length; i++){ if(!cxConstCharsHasChar(sp, body[i]) && body[i] != '\0'){ buffer[idx++] = body[i]; continue; } if(idx == 0){ continue; } cxString item = CX_CREATE(cxString); cxStringAppend(item, buffer, idx); cxArrayAppend(ret, item); idx = 0; } return ret; }
static cxAny cxReadValues(cxHashRoot root,cxConstChars temp,xmlTextReaderPtr reader) { cxNumberValue vm={0}; cxInt b4[4] = {255,255,255,255}; cxString text = cxXMLReadString(reader); CX_RETURN(text == NULL, NULL); cxConstChars value = cxStringBody(text); cxAny rv = NULL; if(ELEMENT_IS_TYPE(cxPoint) && cxReadFloats(value, &vm.vp.vertices.x) == 9){ rv = cxNumberPoint(vm.vp); }else if(ELEMENT_IS_TYPE(cxBool)){ rv = cxNumberBool(cxConstCharsEqu(value, "true")); }else if(ELEMENT_IS_TYPE(cxInt)){ rv = cxNumberInt(atoi(value)); }else if(ELEMENT_IS_TYPE(cxFloat)){ rv = cxNumberFloat(atof(value)); }else if (ELEMENT_IS_TYPE(cxDouble)){ rv = cxNumberDouble(atof(value)); }else if (ELEMENT_IS_TYPE(cxVec2f) && cxReadFloats(value, &vm.vec2f.x) == 2){ rv = cxNumberVec2f(vm.vec2f); }else if (ELEMENT_IS_TYPE(cxSize2f) && cxReadFloats(value, &vm.size2f.w) == 2){ rv = cxNumberSize2f(vm.size2f); }else if(ELEMENT_IS_TYPE(cxColor4f) && cxReadFloats(value, &vm.color4f.r) >= 3){ rv = cxNumberColor4f(vm.color4f); }else if(ELEMENT_IS_TYPE(cxVec2i) && cxReadInts(value, &vm.vec2i.x) == 2){ rv = cxNumberVec2i(vm.vec2i); }else if(ELEMENT_IS_TYPE(cxColor4b) && cxReadInts(value, b4) >= 3){ vm.color4f.r = ((cxFloat)b4[0])/255.0f; vm.color4f.g = ((cxFloat)b4[1])/255.0f; vm.color4f.b = ((cxFloat)b4[2])/255.0f; vm.color4f.a = ((cxFloat)b4[3])/255.0f; rv = cxNumberColor4f(vm.color4f); }else if(ELEMENT_IS_TYPE(cxHash)){ rv = cxHashRootReadHash(root,reader); } return rv; }
// cxEngine // // Created by xuhua on 9/27/13. // Copyright (c) 2013 xuhua. All rights reserved. // #include <unistd.h> #include <sys/stat.h> #include <core/cxUtil.h> #include "cxAssetsStream.h" #include "cxAssetsLuaImp.c" static cxBool cxAssetsStreamOpen(cxAny this) { cxAssetsStream asserts = this; CX_ASSERT(asserts->super.isOpen == false,"stream repeat open"); cxConstChars path = cxStringBody(asserts->super.path); asserts->asset = fopen(path, "rb"); if(asserts->asset == NULL){ CX_ERROR("open assets %s stream failed",path); return false; } struct stat stat={0}; if(lstat(path, &stat) != 0){ CX_ERROR("lstat assets %s stream failed",path); return false; } asserts->super.length = (cxInt)stat.st_size; asserts->super.canRead = true; asserts->super.canSeek = true; asserts->super.canWrite = false; asserts->super.isOpen = true;
#include <unistd.h> #include <sys/stat.h> #include <core/cxUtil.h> #include "cxFileStream.h" void __cxFileStreamTypeInit() { } static cxBool cxFileStreamOpen(cxAny this) { cxFileStream file = this; CX_ASSERT(file->super.isOpen == false,"stream repeat open"); cxConstChars path = cxStringBody(file->super.path); file->fd = fopen(path, "wb"); if(file->fd == NULL){ CX_ERROR("open file %s stream failed",path); return false; } struct stat stat={0}; lstat(path, &stat); file->super.length = (cxInt)stat.st_size; file->super.canRead = true; file->super.canSeek = true; file->super.canWrite = true; file->super.isOpen = true; return true; }
static void cxDBEnvFeedback(DB_ENV *env, int flags, int progress) { cxDBEnv this = env->app_private; CX_LOGGER("dbenv %s progress=%d",cxStringBody(this->type),progress); this->progress = progress; }
cxBool cxEngineLuaRunString(cxString code) { return cxEngineLuaRunChars(cxStringBody(code)); }
CX_OBJECT_FREE(cxRegex, cxObject) { if(this->regex != NULL){ pcre_free(this->regex); } CX_RELEASE(this->input); } CX_OBJECT_TERM(cxRegex, cxObject) cxString cxRegexReplace(cxRegex this,cxRegexReplaceFunc replaceFunc,cxAny arg) { CX_ASSERT(replaceFunc != NULL, "args error"); cxString ret = CX_CREATE(cxString); cxInt start = 0; cxInt length = cxStringLength(this->input); cxConstChars ptr = cxStringBody(this->input); while(cxRegexNext(this)){ cxRegexPos pos = cxRegexPosition(this, 0); if((pos.start - start) > 0){ cxStringAppend(ret, ptr + start, pos.start - start); } cxString replace = replaceFunc(this,arg); if(cxStringOK(replace)){ cxStringConcat(ret, replace); } start = pos.end; } if(start < length){ cxStringAppend(ret, ptr + start, length - start); } return cxStringLength(ret) > 0 ? ret : NULL;