void lunascan::init() { int i; if (CODE==NULL) if ((CODE=(char **)malloc(CODESTACKMAX*sizeof(char *)))==NULL) MEMERROR(); if (POINTER==NULL) if ((POINTER=(int *)malloc(CODESTACKMAX*sizeof(int)))==NULL) MEMERROR(); if (HASH==NULL) { if ((HASH=(LUNA_HASH *)malloc(HASHSIZE*sizeof(LUNA_HASH)))==NULL) MEMERROR(); for (i=0; i<HASHSIZE; i++) { HASH[i].num=HASH[i].maxnum=0; HASH[i].serial=NULL; } } if (STRING==NULL) { if ((STRING=(char *)malloc(STRINGMAX*sizeof(char)))==NULL) MEMERROR(); STRINGSIZE=0; } if (SCOPESTACK==NULL) { if ((SCOPESTACK=(int *)malloc(SCOPESTACKMAX*sizeof(int)))==NULL) MEMERROR(); SCOPESTACKSIZE=0; } }
// add waypoint BOOLINT minipoint::add(minipointdata *point) { int i; float posx,posy; int col,row; if (TILE==NULL) ERRORMSG(); if (POINTS==NULL) { COLS=TILE->getcols(); ROWS=TILE->getrows(); if ((POINTS=(minipointdata **)malloc(COLS*ROWS*sizeof(minipointdata *)))==NULL) MEMERROR(); if ((NUM=(int *)malloc(COLS*ROWS*sizeof(int)))==NULL) MEMERROR(); if ((MAXNUM=(int *)malloc(COLS*ROWS*sizeof(int)))==NULL) MEMERROR(); for (i=0; i<COLS*ROWS; i++) { POINTS[i]=NULL; MAXNUM[i]=NUM[i]=0; } } posx=(point->x-TILE->getcenterx())/TILE->getcoldim()+COLS/2.0f; posy=(-point->y-TILE->getcenterz())/TILE->getrowdim()+ROWS/2.0f; if (posx<0.0f || posx>COLS || posy<0.0f || posy>ROWS) return(FALSE); col=ftrc(posx); row=ftrc(posy); if (col==COLS) col--; if (row==ROWS) row--; if (NUM[col+row*COLS]>=MAXNUM[col+row*COLS]) { MAXNUM[col+row*COLS]=2*MAXNUM[col+row*COLS]+1; if (POINTS[col+row*COLS]==NULL) {if ((POINTS[col+row*COLS]=(minipointdata *)malloc(MAXNUM[col+row*COLS]*sizeof(minipointdata)))==NULL) MEMERROR();} else {if ((POINTS[col+row*COLS]=(minipointdata *)realloc(POINTS[col+row*COLS],MAXNUM[col+row*COLS]*sizeof(minipointdata)))==NULL) MEMERROR();} } point->number=PNUM++; if (strlen(point->comment)>0) parsecomment(point); point->size=0.0f; point->offset=0.0f; POINTS[col+row*COLS][NUM[col+row*COLS]++]=*point; return(TRUE); }
void lunascan::addhash(int serial) { int i; unsigned int hash; hash=gethash(POOL[serial].string)%HASHSIZE; for (i=0; i<HASH[hash].num; i++) if (HASH[hash].serial[i]>=POOLSIZE) { HASH[hash].serial[i]=serial; return; } if (HASH[hash].num>=HASH[hash].maxnum) if (HASH[hash].serial==NULL) { if ((HASH[hash].serial=(int *)malloc(2*sizeof(int)))==NULL) MEMERROR(); HASH[hash].maxnum=2; } else { if ((HASH[hash].serial=(int *)realloc(HASH[hash].serial,2*HASH[hash].maxnum*sizeof(int)))==NULL) MEMERROR(); HASH[hash].maxnum*=2; } HASH[hash].serial[HASH[hash].num]=serial; HASH[hash].num++; }
int lunascan::addstring(const char *string,unsigned int id) { int i; int serial; if (POOL==NULL) { POOLMAX=2; if ((POOL=(LUNA_TOKEN *)malloc(POOLMAX*sizeof(LUNA_TOKEN)))==NULL) MEMERROR(); for (i=0; i<POOLMAX; i++) { POOL[i].string=NULL; POOL[i].id=LUNA_UNKNOWN; } POOLSIZE=0; } if (POOLSIZE>=POOLMAX) { if ((POOL=(LUNA_TOKEN *)realloc(POOL,2*POOLMAX*sizeof(LUNA_TOKEN)))==NULL) MEMERROR(); for (i=POOLMAX; i<2*POOLMAX; i++) { POOL[i].string=NULL; POOL[i].id=LUNA_UNKNOWN; } POOLMAX*=2; } serial=POOLSIZE; if (POOL[serial].string!=NULL) free(POOL[serial].string); POOL[serial].string=strdup(string); POOL[serial].id=id; addhash(serial); POOLSIZE++; return(serial); }
// add character to string char *minipoint::addch(char *str,char ch) { const int space=8; char *str2; if (str==NULL) { if ((str2=(char *)malloc(space))==NULL) MEMERROR(); str2[0]='\0'; } else if (strlen(str)%space>=space-1) { if ((str2=(char *)malloc(strlen(str)+1+space))==NULL) MEMERROR(); strcpy(str2,str); free(str); } else str2=str; str2[strlen(str2)+1]='\0'; str2[strlen(str2)]=ch; return(str2); }
void lunascan::setcode(const char *code,int bytes) { char *code0; if (CODESTACKSIZE>0) ERRORMSG(); if ((code0=(char *)malloc(bytes+1))==NULL) MEMERROR(); memcpy(code0,code,bytes); code0[bytes]='\0'; pushcode(code0); free(code0); }
static ZZJSON *parse_string2(ZZJSON_CONFIG *config) { ZZJSON *zzjson = NULL; char *str; str = parse_string(config); if (str) { zzjson = config->calloc(1, sizeof(ZZJSON)); if (!zzjson) { MEMERROR(); config->free(str); return NULL; } zzjson->type = ZZJSON_STRING; zzjson->value.string.string = str; } return zzjson; }
static int new_text(const sasl_utils_t *utils, context_t **text) { context_t *ret = (context_t *) utils->malloc(sizeof(context_t)); if (ret == NULL) { MEMERROR(utils); return SASL_NOMEM; } memset(ret, 0, sizeof(context_t)); ret->state = 1; ret->utils = utils; *text = ret; return SASL_OK; }
static ZZJSON *parse_literal(ZZJSON_CONFIG *config, char *s, ZZJSON_TYPE t) { char b[strlen(s)+1]; unsigned int i; for (i=0; i<strlen(s); i++) b[i] = GETC(); b[i] = 0; if (!strcmp(b,s)) { ZZJSON *zzjson; zzjson = config->calloc(1, sizeof(ZZJSON)); if (!zzjson) { MEMERROR(); return NULL; } zzjson->type = t; return zzjson; } ERROR("literal: expected %s", s); return NULL; }
static ZZJSON *parse_object(ZZJSON_CONFIG *config) { ZZJSON *retval = NULL; int c; ZZJSON **next = &retval; SKIPWS(); c = GETC(); if (c != '{') { ERROR("object: expected '{'"); return NULL; } SKIPWS(); c = GETC(); while (c > 0 && c != '}') { ZZJSON *zzjson = NULL, *val = NULL; char *str; UNGETC(c); str = parse_string(config); if (!str) { ERROR("object: expected string"); errout_with_str: config->free(str); goto errout; } SKIPWS(); c = GETC(); if (c != ':') { ERROR("object: expected ':'"); goto errout_with_str; } SKIPWS(); val = parse_value(config); if (!val) { ERROR("object: value expected"); goto errout_with_str; } SKIPWS(); c = GETC(); if (c != ',' && c != '}') { ERROR("object: expected ',' or '}'"); errout_with_str_and_val: zzjson_free(config, val); goto errout_with_str; } if (c == ',') { SKIPWS(); c = GETC(); if (c == '}' && !ALLOW_EXTRA_COMMA) { ERROR("object: expected pair after ','"); goto errout_with_str_and_val; } } UNGETC(c); zzjson = config->calloc(1, sizeof(ZZJSON)); if (!zzjson) { MEMERROR(); goto errout_with_str_and_val; } zzjson->type = ZZJSON_OBJECT; zzjson->value.object.label = str; zzjson->value.object.val = val; *next = zzjson; next = &zzjson->next; c = GETC(); } if (c != '}') { ERROR("object: expected '}'"); goto errout; } if (!retval) { /* empty object, { } */ retval = config->calloc(1, sizeof(ZZJSON)); if (!retval) { MEMERROR(); return NULL; } retval->type = ZZJSON_OBJECT; } return retval; errout: zzjson_free(config, retval); return NULL; }
static ZZJSON *parse_array(ZZJSON_CONFIG *config) { ZZJSON *retval = NULL, **next = &retval; int c; SKIPWS(); c = GETC(); if (c != '[') { ERROR("array: expected '['"); return NULL; } SKIPWS(); c = GETC(); while (c > 0 && c != ']') { ZZJSON *zzjson = NULL, *val = NULL; UNGETC(c); SKIPWS(); val = parse_value(config); if (!val) { ERROR("array: value expected"); goto errout; } SKIPWS(); c = GETC(); if (c != ',' && c != ']') { ERROR("array: expected ',' or ']'"); errout_with_val: zzjson_free(config, val); goto errout; } if (c == ',') { SKIPWS(); c = GETC(); if (c == ']' && !ALLOW_EXTRA_COMMA) { ERROR("array: expected value after ','"); goto errout_with_val; } } UNGETC(c); zzjson = config->calloc(1, sizeof(ZZJSON)); if (!zzjson) { MEMERROR(); zzjson_free(config, val); goto errout_with_val; } zzjson->type = ZZJSON_ARRAY; zzjson->value.array.val = val; *next = zzjson; next = &zzjson->next; c = GETC(); } if (c != ']') { ERROR("array: expected ']'"); goto errout; } if (!retval) { /* empty array, [ ] */ retval = config->calloc(1, sizeof(ZZJSON)); if (!retval) { MEMERROR(); return NULL; } retval->type = ZZJSON_ARRAY; } return retval; errout: zzjson_free(config, retval); return NULL; }
static ZZJSON *parse_number(ZZJSON_CONFIG *config) { ZZJSON *zzjson; unsigned long long ival = 0, expo = 0; double dval = 0.0, frac = 0.0, fracshft = 10.0; int c, dbl = 0, sign = 1, signexpo = 1; SKIPWS(); c = GETC(); if (c == '-') { sign = -1; c = GETC(); } if (c == '0') { c = GETC(); goto skip; } if (!isdigit(c)) { ERROR("number: digit expected"); return NULL; } while (isdigit(c)) { ival *= 10; ival += c - '0'; c = GETC(); } skip: if (c != '.') goto skipfrac; dbl = 1; c = GETC(); if (!isdigit(c)) { ERROR("number: digit expected"); return NULL; } while (isdigit(c)) { frac += (double)(c - '0') / fracshft; fracshft *= 10.0; c = GETC(); } skipfrac: if (c != 'e' && c != 'E') goto skipexpo; dbl = 1; c = GETC(); if (c == '+') c = GETC(); else if (c == '-') { signexpo = -1; c = GETC(); } if (!isdigit(c)) { ERROR("number: digit expected"); return NULL; } while (isdigit(c)) { expo *= 10; expo += c - '0'; c = GETC(); } skipexpo: UNGETC(c); if (dbl) { dval = sign * (long long) ival; dval += sign * frac; dval *= pow(10.0, (double) signexpo * expo); } zzjson = config->calloc(1, sizeof(ZZJSON)); if (!zzjson) { MEMERROR(); return NULL; } if (dbl) { zzjson->type = ZZJSON_NUMBER_DOUBLE; zzjson->value.number.val.dval = dval; } else { zzjson->type = sign < 0 ? ZZJSON_NUMBER_NEGINT : ZZJSON_NUMBER_POSINT; zzjson->value.number.val.ival = ival; } return zzjson; }
// calculate visible waypoints void minipoint::calcvdata() { int i,j,k; minipointdata *point; if (TILE==NULL) return; VNUM=0; for (i=TILE->getvisibleleft(); i<=TILE->getvisibleright(); i++) for (j=TILE->getvisiblebottom(); j<=TILE->getvisibletop(); j++) if (TILE->isvisible(i,j)) for (k=0; k<NUM[i+j*COLS]; k++) { point=&POINTS[i+j*COLS][k]; point->height=TILE->getheight(point->x,-point->y); if (VNUM>=MAXVNUM) { MAXVNUM=2*MAXVNUM+1; if (VPOINTS==NULL) {if ((VPOINTS=(minipointdata **)malloc(MAXVNUM*sizeof(minipointdata *)))==NULL) MEMERROR();} else {if ((VPOINTS=(minipointdata **)realloc(VPOINTS,MAXVNUM*sizeof(minipointdata *)))==NULL) MEMERROR();} } VPOINTS[VNUM]=point; VNUM++; } }
// register renderer void minipoint::registerrndr(minipointrndr *rndr) { if (RNUM>=MAXRNUM) { MAXRNUM=2*MAXRNUM+1; if (RNDRS==NULL) {if ((RNDRS=(minipointrndr **)malloc(MAXRNUM*sizeof(minipointrndr *)))==NULL) MEMERROR();} else {if ((RNDRS=(minipointrndr **)realloc(RNDRS,MAXRNUM*sizeof(minipointrndr *)))==NULL) MEMERROR();} } RNDRS[RNUM]=rndr; RNUM++; }
static char *parse_string(ZZJSON_CONFIG *config) { unsigned int len = 16, pos = 0; int c; char *str = NULL; SKIPWS(); c = GETC(); if (c != '"') { ERROR("string: expected \" at the start"); return NULL; } str = config->malloc(len); if (!str) { MEMERROR(); return NULL; } c = GETC(); while (c > 0 && c != '"') { if (!ALLOW_CONTROL_CHARS && c >= 0 && c <= 31) { ERROR("string: control characters not allowed"); goto errout; } if (c == '\\') { c = GETC(); switch (c) { case 'b': c = '\b'; break; case 'f': c = '\f'; break; case 'n': c = '\n'; break; case 'r': c = '\r'; break; case 't': c = '\t'; break; case 'u': { UNGETC(c); /* ignore \uHHHH, copy verbatim */ c = '\\'; break; } case '\\': case '/': case '"': break; default: if (!ALLOW_ILLEGAL_ESCAPE) { ERROR("string: illegal escape character"); goto errout; } } } str[pos++] = c; if (pos == len-1) { void *tmp = str; len *= 2; str = config->realloc(str, len); if (!str) { MEMERROR(); str = tmp; goto errout; } } c = GETC(); } if (c != '"') { ERROR("string: expected \" at the end"); goto errout; } str[pos] = 0; return str; errout: config->free(str); return NULL; }
void threadbase::initmultithread(int id) { if (MAXMULTITHREAD==0) { MAXMULTITHREAD=id+1; if ((MULTITHREAD=(MULTITHREAD_TYPE **)malloc(MAXMULTITHREAD*sizeof(MULTITHREAD_TYPE *)))==NULL) MEMERROR(); } if (id>=MAXMULTITHREAD) { MAXMULTITHREAD=id+1; if ((MULTITHREAD=(MULTITHREAD_TYPE **)realloc(MULTITHREAD,MAXMULTITHREAD*sizeof(MULTITHREAD_TYPE *)))==NULL) MEMERROR(); } MULTITHREAD[id]=new MULTITHREAD_TYPE; NUMMULTITHREAD++; }
static void *_pgsql_open(char *host, char *port, int usessl, const char *user, const char *password, const char *database, const sasl_utils_t *utils) { PGconn *conn = NULL; char *conninfo, *sep; /* create the connection info string */ /* The 64 represents the number of characters taken by * the keyword tokens, plus a small pad */ conninfo = utils->malloc(64 + sql_len(host) + sql_len(port) + sql_len(user) + sql_len(password) + sql_len(database)); if (!conninfo) { MEMERROR(utils); return NULL; } /* add each term that exists */ conninfo[0] = '\0'; sep = ""; if (sql_exists(host)) { strcat(conninfo, sep); strcat(conninfo, "host='"); strcat(conninfo, host); strcat(conninfo, "'"); sep = " "; } if (sql_exists(port)) { strcat(conninfo, sep); strcat(conninfo, "port='"); strcat(conninfo, port); strcat(conninfo, "'"); sep = " "; } if (sql_exists(user)) { strcat(conninfo, sep); strcat(conninfo, "user='******'"); sep = " "; } if (sql_exists(password)) { strcat(conninfo, sep); strcat(conninfo, "password='******'"); sep = " "; } if (sql_exists(database)) { strcat(conninfo, sep); strcat(conninfo, "dbname='"); strcat(conninfo, database); strcat(conninfo, "'"); sep = " "; } if (usessl) { strcat(conninfo, sep); strcat(conninfo, "requiressl='1'"); } conn = PQconnectdb(conninfo); free(conninfo); if ((PQstatus(conn) != CONNECTION_OK)) { utils->log(NULL, SASL_LOG_ERR, "sql plugin: %s", PQerrorMessage(conn)); return NULL; } return conn; }