ZArr<Type> & ZArr<Type>::Delete(int index) throw(ZException) { if ((index >= prSize) || (index < 0)) { throw ZException("ZArr<Type>::Delete: Index out of range",0); } if (prSize == 1) { #ifdef DEBUG_MEMORY DebugMemDestroy((pvoid)prA, "ZArr::Delete(int)"); #endif delete [] prA; prSize = 0; return *this; } Type *pType; int i; pType = new Type[prSize-1]; #ifdef DEBUG_MEMORY DebugMemAllocate((pvoid)pType, "ZArr::Delete(int)"); #endif if (!pType) { throw ZException("ZArr<Type>::Delete: Out of Memory",0); } // copy old array upto but not including index into first part of new array for (i = 0; i < index; i++) pType[i] = prA[i]; // copy rest of old array after index into rest of new array for (i = index+1; i < prSize; i++) pType[i-1] = prA[i]; prSize--; if (prA) { #ifdef DEBUG_MEMORY DebugMemDestroy((pvoid)prA, "ZArr::Delete(int)"); #endif delete [] prA; } prA = pType; return *this; }
void GosuEx::Data::inflate(const Gosu::Buffer& source, Gosu::Buffer& dest, const unsigned int CHUNK) { int ret; unsigned int have; unsigned int sourceOffset = 0; z_stream strm; std::vector<Bytef> in(CHUNK); std::vector<Bytef> out(CHUNK); strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; strm.avail_in = 0; strm.next_in = Z_NULL; ret = inflateInit(&strm); if (ret != Z_OK) throw ZException("inflateInit failed", ret); do { strm.avail_in = source.size()-sourceOffset < CHUNK ? source.size()-sourceOffset : CHUNK; if (strm.avail_in == 0) break; source.read(sourceOffset, strm.avail_in, &in[0]); strm.next_in = &in[0]; sourceOffset += CHUNK; do { strm.avail_out = CHUNK; strm.next_out = &out[0]; ret = inflate(&strm, Z_NO_FLUSH); switch (ret) { case Z_NEED_DICT: ret = Z_DATA_ERROR; case Z_DATA_ERROR: case Z_MEM_ERROR: inflateEnd(&strm); throw ZException("inflate failed", ret); default: break; } have = CHUNK - strm.avail_out; dest.resize(dest.size()+have); dest.write(dest.size()-have, have, &out[0]); } while (strm.avail_out == 0); } while (ret != Z_STREAM_END); inflateEnd(&strm); if (ret != Z_STREAM_END) throw ZException("Expected Z_STREAM_END", ret); }
void GosuEx::Data::deflate(const Gosu::Buffer& source, Gosu::Buffer& dest, int level, const unsigned int CHUNK) { // Vars int ret, flush; unsigned int have; unsigned int sourceOffset = 0; z_stream strm; std::vector<Bytef> in(CHUNK); std::vector<Bytef> out(CHUNK); //unsigned char* in = new unsigned char[CHUNK]; //unsigned char* out = new unsigned char[CHUNK]; // Resetting strm strm.zalloc = Z_NULL; strm.zfree = Z_NULL; strm.opaque = Z_NULL; // Start deflating ret = deflateInit(&strm, level); // Something went wrong? Oops. if (ret != Z_OK) throw ZException("deflateInit failed", ret); // Outer "feed our data" loop. do { strm.avail_in = source.size()-sourceOffset < CHUNK ? source.size()-sourceOffset : CHUNK; flush = strm.avail_in == CHUNK ? Z_NO_FLUSH : Z_FINISH; source.read(sourceOffset, strm.avail_in, &in[0]); strm.next_in = &in[0]; sourceOffset += CHUNK; // inner compress data loop. do { strm.avail_out = CHUNK; strm.next_out = &out[0]; ret = deflate(&strm, flush); if (ret == Z_STREAM_ERROR) throw ZException("deflate failed", ret); have = CHUNK - strm.avail_out; dest.resize(dest.size()+have); dest.write(dest.size()-have, have, &out[0]); } while (strm.avail_out == 0); if (strm.avail_in != 0) throw std::runtime_error("Not all input was proceeded"); } while (flush != Z_FINISH); if (ret != Z_STREAM_END) throw ZException("Expected Z_STREAM_END", ret); deflateEnd(&strm); }
const Type & ZArr<Type>::operator[](int index) const throw(ZException) { #ifdef DEBUG_MSG print_carr_debug_msg("\"const operator[](int index) const\" called"); #endif if ((index < 0) || (index >= prSize)) { throw ZException("ZArr::operator[]:Error: index out of range",0); } // ELSE return prA[index]; }
inline Type Min(const std::vector<Type> & vectType) { Type tMin; if (vectType.size()==0) throw ZException("InlineUtils::Min(vector<Type>): Vector size is 0"); tMin = vectType[0]; for (int __i = 1; __i < vectType.size(); __i++) { if (vectType[i] < tMin) tMin = vectType[i]; } return tMin; }
inline Type Min(const std::list<Type> & listType) { Type tMin; if (listType.size()==0) throw ZException("InlineUtils::Min(list<Type>): Vector size is 0"); std::list<Type>::iterator itList = listType.begin(); tMin = *itList; itList++; while (itList != listType.end()) { if ((*itList) < tMin) tMin = (*itList); } return tMin; }
Status Open() { leveldb::Options options; //options.block_cache = leveldb::NewLRUCache(500 * 1048576); options.write_buffer_size = 500 * 1048576; // 200MB write buffer options.filter_policy = leveldb::NewBloomFilterPolicy(16); options.create_if_missing = true; options.paranoid_checks = false; options.compression = leveldb::kSnappyCompression; leveldb::Status status = leveldb::DB::Open(options, path, &db); if (!status.ok()) { // could possibly attempt to repair and open via // if (leveldb::RepairDB(path, options).ok()) ... if (!leveldb::DB::Open(options, path, &db).ok()) ... throw ... throw ZException("Failed to open leveldb database!"); } return Status::OK(); }
void ZCsl::interpret (Function * aFunc) { ZFUNCTRACE_DEVELOP ("ZCsl::interpret(Function* aFunc)"); ZException *excpt = 0; PackedInstruction *loAddr = aFunc->iPcode; PackedInstruction *hiAddr = loAddr + aFunc->iPsize - 1; PackedInstruction *i = loAddr; ZString v2; Variable *v; double d2; long i2; ZBoolean head (zTrue); while (zTrue) { try { if (iTraceMode & traceCode) { if (head) { trace ('#'); trace ('#', funcHeader (aFunc).constBuffer ()); trace ('#'); trace ('#', "address opcode parameter tos tos-1"); trace ('#', "-------- ------ -------------------- -------------------- --------------------"); head = zFalse; } // if trace ('#', dumpInstr (i - aFunc->iPcode, i, zTrue).constBuffer ()); } switch (i->iCode) { case cdOpen: aFunc->openBlock (i->iText); i++; break; case cdClose: aFunc->closeBlock (); i++; break; case cdSize: push (aFunc->findVar (pop ())->iSize); i++; break; case cdRSize: { ZString varName (pop ()); aFunc->findVar (pureVarName (varName))->resize (varName); i++; break; } case cdTrace: if (iTraceMode & traceMsgs) trace ('>', pop ().constBuffer ()); else pop (); i++; break; case cdExist: { ZString ret (One); try { Variable *var = aFunc->findVar (pop (), zFalse); if (!var || var->iIsExtern || var->iIndex < 0 || var->iIndex >= var->size ()) ret = Zero; } // try catch (const ZException & exc) { ret = Zero; } // catch push (ret); i++; break; } case cdNeg: checkNum (*iTos); *iTos = ZString (-iTos->asDouble ()); i++; break; case cdNot: if (isNumber (iTos->constBuffer ())) *iTos = iTos->asDouble ()? Zero : One; else *iTos = *iTos == "" ? One : Zero; i++; break; case cdLoad: *iTos = aFunc->findVar (*iTos)->value (); i++; break; case cdStore: { Variable *var = aFunc->findVar (pop ()); var->set (pop ()); i++; break; } case cdStorC: { Variable *var = aFunc->findVar (pop ()); var->set (pop (), zTrue); i++; break; } case cdPush: push (decodeString (i->iText)); i++; break; case cdPop: pop (); i++; break; case cdDupl: push (*iTos); i++; break; case cdDivI: i2 = pop ().asInt (); if (!i2) throwExcept (msgDivZero); checkNum (*iTos); *iTos = ZString (iTos->asInt () / i2); i++; break; case cdMod: i2 = pop ().asInt (); if (!i2) throwExcept (msgDivZero); checkNum (*iTos); *iTos = ZString (iTos->asInt () % i2); i++; break; case cdMul: v2 = pop (); checkNum (v2); checkNum (*iTos); *iTos = ZString (iTos->asDouble () * v2.asDouble ()); i++; break; case cdDiv: d2 = pop ().asDouble (); if (!d2) throwExcept (msgDivZero); checkNum (*iTos); *iTos = ZString (iTos->asDouble () / d2); i++; break; case cdAdd: v2 = pop (); if (!isNumber (v2.constBuffer ()) || !isNumber (iTos->constBuffer ())) *iTos += v2; else *iTos = ZString (iTos->asDouble () + v2.asDouble ()); i++; break; case cdSub: v2 = pop (); checkNum (v2); checkNum (*iTos); *iTos = ZString (iTos->asDouble () - v2.asDouble ()); i++; break; case cdCat: v2 = pop (); *iTos += v2; i++; break; case cdMulV: v = aFunc->findVar (pop ()); v2 = pop (); checkNum (v2); checkNum (v->value ()); v->set (v->value ().asDouble () * v2.asDouble ()); i++; break; case cdDivV: v = aFunc->findVar (pop ()); d2 = pop ().asDouble (); if (!d2) throwExcept (msgDivZero); checkNum (v->value ()); v->set (v->value ().asDouble () / d2); i++; break; case cdDivIV: v = aFunc->findVar (pop ()); i2 = pop ().asInt (); if (!i2) throwExcept (msgDivZero); checkNum (v->value ()); v->set (v->value ().asInt () / i2); i++; break; case cdModV: v = aFunc->findVar (pop ()); i2 = pop ().asInt (); if (!i2) throwExcept (msgDivZero); checkNum (v->value ()); v->set (v->value ().asInt () % i2); i++; break; case cdAddV: v = aFunc->findVar (pop ()); v2 = pop (); if (!isNumber (v->value ().constBuffer ()) || !isNumber (v2.constBuffer ())) v->set (v->value () + v2); else v->set (v->value ().asDouble () + v2.asDouble ()); i++; break; case cdSubV: v = aFunc->findVar (pop ()); v2 = pop (); checkNum (v2); checkNum (v->value ()); v->set (v->value ().asDouble () - v2.asDouble ()); i++; break; case cdCatV: v = aFunc->findVar (pop ()); v2 = pop (); v->set (v->value () + v2); i++; break; case cdIncV: v = aFunc->findVar (pop ()); checkNum (v->value ()); v->set (v->value ().asDouble () + 1.0); i++; break; case cdDecV: v = aFunc->findVar (pop ()); checkNum (v->value ()); v->set (v->value ().asDouble () - 1.0); i++; break; case cdLss: v2 = pop (); if (!isNumber (v2.constBuffer ()) || !isNumber (iTos->constBuffer ())) *iTos = *iTos < v2 ? One : Zero; else *iTos = iTos->asDouble () < v2.asDouble ()? One : Zero; i++; break; case cdLeq: v2 = pop (); if (!isNumber (v2.constBuffer ()) || !isNumber (iTos->constBuffer ())) *iTos = *iTos <= v2 ? One : Zero; else *iTos = iTos->asDouble () <= v2.asDouble ()? One : Zero; i++; break; case cdGtr: v2 = pop (); if (!isNumber (v2.constBuffer ()) || !isNumber (iTos->constBuffer ())) *iTos = *iTos > v2 ? One : Zero; else *iTos = iTos->asDouble () > v2.asDouble ()? One : Zero; i++; break; case cdGeq: v2 = pop (); if (!isNumber (v2.constBuffer ()) || !isNumber (iTos->constBuffer ())) *iTos = *iTos >= v2 ? One : Zero; else *iTos = iTos->asDouble () >= v2.asDouble ()? One : Zero; i++; break; case cdEql: v2 = pop (); if (!isNumber (v2.constBuffer ()) || !isNumber (iTos->constBuffer ())) *iTos = *iTos == v2 ? One : Zero; else *iTos = iTos->asDouble () == v2.asDouble ()? One : Zero; i++; break; case cdNeq: v2 = pop (); if (!isNumber (v2.constBuffer ()) || !isNumber (iTos->constBuffer ())) *iTos = *iTos != v2 ? One : Zero; else *iTos = iTos->asDouble () != v2.asDouble ()? One : Zero; i++; break; case cdAnd: v2 = pop (); if (!isNumber (v2.constBuffer ()) || !isNumber (iTos->constBuffer ())) *iTos = (iTos->size () > 0 && v2.size () > 0) ? One : Zero; else *iTos = (iTos->asDouble () != 0.0 && v2.asDouble () != 0.0) ? One : Zero; i++; break; case cdOr: v2 = pop (); if (!isNumber (v2.constBuffer ()) || !isNumber (iTos->constBuffer ())) *iTos = (iTos->size () > 0 || v2.size () > 0) ? One : Zero; else *iTos = (iTos->asDouble () != 0.0 || v2.asDouble () != 0.0) ? One : Zero; i++; break; case cdJmp: i = loAddr + i->iAddr; break; case cdJF: { v2 = pop (); ZBoolean ok; if (isNumber (v2.constBuffer ())) ok = v2.asDouble () == 0.0; else ok = v2 == ""; i = ok ? loAddr + i->iAddr : i + 1; break; } case cdJT: { v2 = pop (); ZBoolean ok; if (isNumber (v2.constBuffer ())) ok = v2.asDouble () != 0.0; else ok = v2 != ""; i = ok ? loAddr + i->iAddr : i + 1; break; } case cdCall: exec ((Function *) i->iFunc); i++; head = zTrue; break; case cdRet: return; case cdTry: aFunc->iBlocks->iCatch = i->iAddr; aFunc->iBlocks->iTos = iTos; i++; break; case cdThrow: throw ZException (pop ()); case cdThroV: { v = aFunc->findVar (pop ()); int cnt (v->iSize); ZException exc (v->value ()); for (int i = 1; i < cnt; i++) exc.addAsLast (v->iVals[v->iIndex + i]); throw exc; } case cdAllTV: case cdAllTC: { if (!excpt) throw ZException (msgInvCatch); int cnt (excpt->count ()); Variable *var = aFunc->iBlocks->addVar (decodeString (i->iText) + "[" + ZString (cnt ? cnt : 1) + "]", "", i->iCode == cdAllTC); for (int c = 0; c < cnt; c++) var->iVals[c] = (*excpt)[c]; delete excpt; excpt = 0; i++; break; } case cdAllV: v2 = pop (); aFunc->iBlocks->addVar (v2, pop ()); i++; break; case cdAllC: v2 = pop (); aFunc->iBlocks->addVar (v2, pop (), zTrue); i++; break; case cdAllVR: v2 = pop (); aFunc->iBlocks->addVar (v2, pop (), zFalse, zTrue); i++; break; case cdAllCR: v2 = pop (); aFunc->iBlocks->addVar (v2, pop (), zTrue, zTrue); i++; break; case cdNop: i++; break; default: throwExcept (msgIllgInstr); } // switch if (i < loAddr || i > hiAddr) throwExcept (msgNoReturn); } // try catch (const ZException & exc) { while (aFunc->iBlocks->iPrev && aFunc->iBlocks->iCatch < 0) aFunc->closeBlock (); if (aFunc->iBlocks->iPrev) { i = loAddr + aFunc->iBlocks->iCatch; iTos = aFunc->iBlocks->iTos; aFunc->closeBlock (); if (excpt) delete excpt; excpt = new ZException (exc); } else throw; } // catch } // while } // interpret