static void Xml_pushDecode(HSQUIRRELVM v, const char* s, size_t s_size) { SQ_FUNC_VARS_NO_TOP(v); size_t start=0, pos; if(!s_size) s_size=strlen(s); SQBlob b(0, BLOB_BUFSIZE); const char* found = scstrstr(s, _SC("&#")); if(!found) pos = s_size; else pos = found-s; while(found&&(pos+5<s_size)&&(*(found+5)==_SC(';'))&&scisdigit(*(found+2))&&scisdigit(*(found+3))&&scisdigit(*(found+4)) ) { if(pos>start) b.Write(s+start, pos-start); b.WriteChar(100*(s[pos+2]-48)+10*(s[pos+3]-48)+(s[pos+4]-48)); start=pos+6; found = scstrstr(found+6, _SC("&#")); if(!found) pos = s_size; else pos = found-s; } if(pos>start) b.Write(s+start, pos-start); sq_pushstring(v, (const SQChar*)b.GetBuf(), b.Len()); size_t i; for(i=sv_code_size-1; i<sv_code_size; i-=2) { SQ_GET_STRING(v, -1, str); sq_str_replace(v, str, sv_code[i], sv_code[i-1]); sq_remove(v,-2); } }
static const SQChar *sq_str_replace (HSQUIRRELVM v, const SQChar *s, const SQChar *p, const SQChar *r) { const SQChar *wild; size_t l = scstrlen(p); SQBlob b(0, BLOB_BUFSIZE); while ((wild = scstrstr(s, p)) != NULL) { b.Write(s, wild - s); /* push prefix */ b.WriteZstr(r); /* push replacement in place of pattern */ s = wild + l; /* continue after `p' */ } b.WriteZstr(s); /* push last suffix */ sq_pushstring(v, (const SQChar*)b.GetBuf(), b.Len()); return sq_tostring(v, -1); }
bool str2num(const SQChar *s,SQObjectPtr &res) { SQChar *end; if(scstrstr(s,_SC("."))){ SQFloat r = SQFloat(scstrtod(s,&end)); if(s == end) return false; res = r; return true; } else{ SQInteger r = SQInteger(scstrtol(s,&end,10)); if(s == end) return false; res = r; return true; } }
static SQInteger string_find(HSQUIRRELVM v) { SQInteger top,start_idx=0; const SQChar *str,*substr,*ret; if(((top=sq_gettop(v))>1) && SQ_SUCCEEDED(sq_getstring(v,1,&str)) && SQ_SUCCEEDED(sq_getstring(v,2,&substr))){ if(top>2)sq_getinteger(v,3,&start_idx); if((sq_getsize(v,1)>start_idx) && (start_idx>=0)){ ret=scstrstr(&str[start_idx],substr); if(ret){ sq_pushinteger(v,(SQInteger)(ret-str)); return 1; } } return 0; } return sq_throwerror(v,_SC("invalid param")); }
static size_t find(const SQChar* s, const SQChar* pattern, size_t start) { const SQChar* found =scstrstr(s+start, pattern); return found ? found-s : scstrlen(s); }
void sqstd_printcallstack(HSQUIRRELVM v) { SQPRINTFUNCTION pf = sq_getprintfunc(v); if(pf) { SQStackInfos si; SQInteger i; SQBool b; SQFloat f; const SQChar *s; SQInteger level=1; //1 is to skip this function that is level 0 const SQChar *name=0; SQInteger seq=0; pf(v,_SC("\nCALLSTACK\n")); while(SQ_SUCCEEDED(sq_stackinfos(v,level,&si))) { const SQChar *fn=_SC("unknown"); const SQChar *src=_SC("unknown"); if(si.funcname)fn=si.funcname; if(si.source) { /* We don't want to bother users with absolute paths to all AI files. * Since the path only reaches NoAI code in a formatted string we have * to strip it here. Let's hope nobody installs openttd in a subdirectory * of a directory named /ai/. */ src = scstrstr(si.source, _SC("\\ai\\")); if (!src) src = scstrstr(si.source, _SC("/ai/")); if (src) { src += 4; } else { src = si.source; } } pf(v,_SC("*FUNCTION [%s()] %s line [%d]\n"),fn,src,si.line); level++; } level=0; pf(v,_SC("\nLOCALS\n")); for(level=0;level<10;level++){ seq=0; while((name = sq_getlocal(v,level,seq))) { seq++; switch(sq_gettype(v,-1)) { case OT_NULL: pf(v,_SC("[%s] NULL\n"),name); break; case OT_INTEGER: sq_getinteger(v,-1,&i); pf(v,_SC("[%s] %d\n"),name,i); break; case OT_FLOAT: sq_getfloat(v,-1,&f); pf(v,_SC("[%s] %.14g\n"),name,f); break; case OT_USERPOINTER: pf(v,_SC("[%s] USERPOINTER\n"),name); break; case OT_STRING: sq_getstring(v,-1,&s); pf(v,_SC("[%s] \"%s\"\n"),name,s); break; case OT_TABLE: pf(v,_SC("[%s] TABLE\n"),name); break; case OT_ARRAY: pf(v,_SC("[%s] ARRAY\n"),name); break; case OT_CLOSURE: pf(v,_SC("[%s] CLOSURE\n"),name); break; case OT_NATIVECLOSURE: pf(v,_SC("[%s] NATIVECLOSURE\n"),name); break; case OT_GENERATOR: pf(v,_SC("[%s] GENERATOR\n"),name); break; case OT_USERDATA: pf(v,_SC("[%s] USERDATA\n"),name); break; case OT_THREAD: pf(v,_SC("[%s] THREAD\n"),name); break; case OT_CLASS: pf(v,_SC("[%s] CLASS\n"),name); break; case OT_INSTANCE: pf(v,_SC("[%s] INSTANCE\n"),name); break; case OT_WEAKREF: pf(v,_SC("[%s] WEAKREF\n"),name); break; case OT_BOOL:{ sq_getbool(v,-1,&b); pf(v,_SC("[%s] %s\n"),name,b?_SC("true"):_SC("false")); } break; default: assert(0); break; } sq_pop(v,1); } } } }