void init_text(text_data *t, float x, float y, const char *text) { int size=0, i=0; float *data=NULL; float nx=x, ny=y; gen_bufs(1,&t->v); use_text(t); t->len=strlen(text); size=t->len*3*sizeof(float); data=(float*)malloc(size); for(i=0 ; i<t->len ; i++) { const int index=i*3; if(text[i]=='\n') { nx=x; ny+=afont->ch; } data[index]=nx; data[index+1]=ny; data[index+2]=(float)text[i]; nx+=afont->cw[text[i]-32]; } buf_data(size,(const void*)data); free((void*)data); }
/** * Same as xnode_to_string(). */ const char * xnode_to_string2(const xnode_t *xn) { buf_t *b = buf_private(G_STRFUNC, 256); char *p = buf_data(b); xnode_to_string_buf(xn, p, buf_size(b)); return p; }
/** * @return the "address:port" string for a host */ const char * gnet_host_to_string2(const gnet_host_t *h) { buf_t *b = buf_private(G_STRFUNC, HOST_ADDR_PORT_BUFLEN); char *p = buf_data(b); gnet_host_to_string_buf(h, p, buf_size(b)); return p; }
/** * Prints the host address ``ha'' followed by ``port'' to a static buffer. * * @param ha the host address. * @param port the port number. * * @return a pointer to a static buffer holding a NUL-terminated string * representing the given host address and port. */ const char * port_host_addr_to_string(uint16 port, const host_addr_t ha) { buf_t *b = buf_private(G_STRFUNC, HOST_ADDR_PORT_BUFLEN); char *p = buf_data(b); size_t len, n = buf_size(b); len = host_port_addr_to_string_buf(port, ha, p, n); g_assert(len < n); return p; }
/** * Same as host_addr_to_string(), but in another static buffer. */ const char * host_addr_to_string2(const host_addr_t ha) { buf_t *b = buf_private(G_STRFUNC, HOST_ADDR_BUFLEN); char *p = buf_data(b); size_t len, n = buf_size(b); len = host_addr_to_string_buf(ha, p, n); g_assert(len < n); return p; }
/** * Pretty-print the message information. * * @param data start of the G2 message * @param len length of the message * * @return formatted static string. */ const char * g2_msg_infostr(const void *data, size_t len) { buf_t *b = buf_private(G_STRFUNC, 64); char *p = buf_data(b); size_t n, sz = buf_size(b); n = g2_msg_infostr_to_buf(data, len, p, sz); g_assert(n < sz); return p; }
/** * Stringify an UPnP service. * * @return pointer to static buffer. */ const char * upnp_service_to_string(const upnp_service_t *usd) { buf_t *b = buf_private(G_STRFUNC, 128); upnp_service_check(usd); buf_printf(b, "\"%s\" v%u at %s", upnp_service_type_to_string(usd->type), usd->version, usd->control_url); return buf_data(b); }
PGResultSet * pgconn_listTablesMatching(PGConnection *conn, const char *pattern) { PGResultSet *rset; StringBuffer *sb; sb = buf_createDefault(); buf_printf(sb, "SELECT tablename FROM pg_tables WHERE tablename like '%s'",pattern); rset = pgconn_query(conn, buf_data(sb)); buf_free(sb); return rset; }
/** * Convert current header to a string. * * @attention * NB: returns pointer to static data! */ const char * header_fmt_to_string(const header_fmt_t *hf) { buf_t *b = buf_private(G_STRFUNC, HEADER_FMT_MAX_SIZE + 1); char *p = buf_data(b); size_t n = buf_size(b); header_fmt_check(hf); if (str_len(hf->header) >= n) { g_warning("trying to format too long an HTTP line (%zu bytes)", str_len(hf->header)); } clamp_strncpy(p, n, str_2c(hf->header), str_len(hf->header)); return p; }
const char * host_port_to_string(const char *hostname, host_addr_t addr, uint16 port) { buf_t *b = buf_private(G_STRFUNC, MAX_HOSTLEN + HOST_ADDR_PORT_BUFLEN); char *p = buf_data(b); if (hostname != NULL) { char port_buf[UINT32_DEC_BUFLEN]; uint32_to_string_buf(port, port_buf, sizeof port_buf); concat_strings(p, buf_size(b), hostname, ":", port_buf, NULL_PTR); } else { host_addr_port_to_string_buf(addr, port, p, buf_size(b)); } return p; }
char *motopp_uncomment(const char *s) { StringBuffer *buf = buf_createDefault(); char *result; int len; char c1; char c2; log_debug(__FILE__, ">>> motopp_uncomment: %s\n", s); len = strlen(s); if (len <= 2) { buf_puts(buf, s); } else { int state = 0; int addchar = 1; int i = 0; c1 = c2 = 0; while (i < len) { c1 = c2; c2 = s[i++]; if (c1 == '$') { state = '$'; } else if (c1 == '*') { state = '*'; } else if (c1 == '>') { state = '>'; } else if (c1 == '<') { state = '<'; } else { state = 0; } switch (c2) { case '*': case '>': case '<': if (state == '$') { addchar = 0; } break; case '$': if (state == '*' || state == '>' || state == '<') { addchar = 1; if (i < len) { c2 = s[i]; } } break; default: if (addchar) { if (state == '$') { buf_putc(buf, '$'); } else if (state == '*') { buf_putc(buf, '*'); } else if (state == '>') { buf_putc(buf, '>'); } else if (state == '<') { buf_putc(buf, '<'); } buf_putc(buf, c2); } break; } } } log_debug(__FILE__, "<<< motopp_uncomment: %s\n", buf_data(buf)); result = buf_toString(buf); buf_free(buf); return result; }
char * mxdl_find(char *usename) { char *result = NULL; char *libname = NULL; int len; int trlen; char *tr_usename; char *tr_pathname; char *mxpath = mxdl_getMXPath(); StringBuffer *d; /* translate the usename */ trlen = len = strlen(usename); tr_usename = emalloc(len + 1); tr_usename[len] = '\0'; while (--len >= 0) { if (*(usename + len) == '.') { *(tr_usename + len) = '_'; } else { *(tr_usename + len) = *(usename + len); } } if (index(tr_usename, '_') == NULL) { /* leave name alone */ } /* translate the tr_usename to a path */ len = strlen(tr_usename); tr_pathname = emalloc(len + 1); tr_pathname[len] = '\0'; while (--len >= 0) { if (*(tr_usename + len) == '_') { *(tr_pathname + len) = '/'; } else { *(tr_pathname + len) = *(tr_usename + len); } } /* look in all path directories for the library */ d = buf_createDefault(); if (mxpath != NULL) { char *dupmxpath = estrdup(mxpath); char *dir = strtok(dupmxpath, ":"); while (dir != NULL) { buf_clear(d); buf_puts(d, dir); if (dir[strlen(dir) - 1] != '/') { buf_putc(d, '/'); } buf_puts(d, tr_pathname); buf_putc(d, '/'); buf_puts(d, "libmx_"); buf_puts(d, tr_usename); buf_puts(d, ".so"); libname = buf_data(d); if (stat_file(libname) == 0) { result = buf_toString(d); //mman_track(rtime_getRuntime()->mpool, result); buf_free(d); d = NULL; break; } dir = strtok(NULL, ":"); } free(dupmxpath); } else { buf_printf(d, "libmx_%s.so", tr_usename); libname = buf_toString(d); buf_free(d); d = NULL; if (stat_file(libname) == 0) { result = libname; } } if (d != NULL) { buf_free(d); } free(tr_usename); free(tr_pathname); return result; }
static void screenshot_process(void *task) { pixmap_t *pm = task; if(pm == NULL) { screenshot_response(NULL, "Screenshot not supported on this platform"); return; } TRACE(TRACE_DEBUG, "Screenshot", "Processing image %d x %d", pm->pm_width, pm->pm_height); int codecid = AV_CODEC_ID_PNG; if(screenshot_connection) codecid = AV_CODEC_ID_MJPEG; buf_t *b = screenshot_compress(pm, codecid); pixmap_release(pm); if(b == NULL) { screenshot_response(NULL, "Unable to compress image"); return; } if(!screenshot_connection) { char path[512]; char errbuf[512]; snprintf(path, sizeof(path), "%s/screenshot.png", gconf.cache_path); fa_handle_t *fa = fa_open_ex(path, errbuf, sizeof(errbuf), FA_WRITE, NULL); if(fa == NULL) { TRACE(TRACE_ERROR, "SCREENSHOT", "Unable to open %s -- %s", path, errbuf); buf_release(b); return; } fa_write(fa, buf_data(b), buf_len(b)); fa_close(fa); TRACE(TRACE_INFO, "SCREENSHOT", "Written to %s", path); buf_release(b); return; } buf_t *result = NULL; htsbuf_queue_t hq; htsbuf_queue_init(&hq, 0); htsbuf_append(&hq, "image=", 6); htsbuf_append_and_escape_url_len(&hq, buf_cstr(b), buf_len(b)); char errbuf[256]; int ret = http_req("https://api.imgur.com/3/upload", HTTP_FLAGS(FA_CONTENT_ON_ERROR), HTTP_REQUEST_HEADER("Authorization", "Client-ID 7c79b311d4797ed"), HTTP_RESULT_PTR(&result), HTTP_POSTDATA(&hq, "application/x-www-form-urlencoded"), HTTP_ERRBUF(errbuf, sizeof(errbuf)), NULL); if(ret) { screenshot_response(NULL, errbuf); } else { htsmsg_t *response = htsmsg_json_deserialize(buf_cstr(result)); if(response == NULL) { screenshot_response(NULL, "Unable to parse imgur response"); } else { if(htsmsg_get_u32_or_default(response, "success", 0)) { const char *url = htsmsg_get_str_multi(response, "data", "link", NULL); screenshot_response(url, "No link in imgur response"); } else { const char *msg = htsmsg_get_str_multi(response, "data", "error", NULL); if(msg == NULL) { screenshot_response(NULL, "Unkown imgur error"); } else { snprintf(errbuf, sizeof(errbuf), "Imgur error: %s", msg); screenshot_response(NULL, errbuf); } } htsmsg_release(response); } buf_release(result); } buf_release(b); }
TabularData * pgrset_store(PGResultSet *rset) { int i, j, columns, rows; StringBuffer *colnames; StringBuffer *coltypes; TabularData *tdata; char *pgtype; columns = rset->columns; rows = rset->row_count; colnames = buf_createDefault(); coltypes = buf_createDefault(); for (i = 0; i < columns; i++){ pgtype = ihtab_get(pgTypes, rset->column_types[i]); if( estrcmp(pgtype, "int2") == 0 || estrcmp(pgtype, "int4") == 0 || estrcmp(pgtype, "oid") == 0 ) buf_puts(coltypes, "int"); else if ( estrcmp(pgtype, "double") == 0 ) buf_puts(coltypes, "double"); else if ( estrcmp(pgtype, "float4") == 0 || estrcmp(pgtype, "float8") == 0 ) buf_puts(coltypes, "float"); else if ( estrcmp(pgtype, "name") == 0 || estrcmp(pgtype, "text") == 0 || estrcmp(pgtype, "varchar") == 0 ) buf_puts(coltypes, "String"); else { buf_free(coltypes); buf_free(colnames); THROW("SQLException", "Cannot store field '%s'", rset->column_names[i]); } buf_puts(colnames, rset->column_names[i]); if(i < columns-1){ buf_putc(colnames, ','); buf_putc(coltypes, ','); } } tdata = tdata_create(rows, columns, buf_data(colnames), buf_data(coltypes)); buf_free(coltypes); buf_free(colnames); rset->row=-1; for (i = 0, pgrset_next(rset); i < rows; i++, pgrset_next(rset)){ for (j = 0; j < columns; j++){ pgtype = ihtab_get(pgTypes, rset->column_types[j]); if( estrcmp(pgtype, "int2") == 0 || estrcmp(pgtype, "int4") == 0 || estrcmp(pgtype, "oid") == 0 ) tdata_setInt(tdata, i, j, pgrset_getInt(rset, j)); else if ( estrcmp(pgtype, "float4") == 0 || estrcmp(pgtype, "float8") == 0 ) tdata_setFloat(tdata, i, j, pgrset_getFloat(rset, j)); else if ( estrcmp(pgtype, "double") == 0 ) tdata_setDouble(tdata, i, j, pgrset_getDouble(rset, j)); else if ( estrcmp(pgtype, "name") == 0 || estrcmp(pgtype, "text") == 0 || estrcmp(pgtype, "varchar") == 0 ) tdata_setString(tdata,i,j,pgrset_getString(rset,j)); else { THROW_D("Exception"); } } } rset->row = -1; tdata_optimize(tdata); return tdata; }
void motopp_freeFrame(MotoPP *ppenv) { MotoPPFrame *frame; Enumeration *e; if (stack_size(ppenv->frames) <= 1) { frame = stack_pop(ppenv->frames); buf_cat(ppenv->out, frame->out); ppenv->frame = NULL; } else { MotoPPVal *val; MotoPPFrame *pframe; SymbolTable *macros; MotoMacro *m; char *catstr; int catstrlen; frame = stack_pop(ppenv->frames); pframe = stack_peek(ppenv->frames); switch (frame->type) { case MACRO_FRAME: m = frame->macro; val = motopp_createVal(ppenv); if (ppenv->flags & MACRO_DEBUG_FLAG) { buf_printf(frame->out, LINE_FMT_2, pframe->filename, pframe->lineno); } val->sval = buf_toString(frame->out); stack_push(pframe->opstack, val); macros = stack_pop(ppenv->macrostack); e = stab_getKeys(macros); while (enum_hasNext(e)) { char *name = (char *)enum_next(e); MotoMacro *m = (MotoMacro *)stab_get(macros, name); motopp_freeMacro(m); } stab_free(macros); enum_free(e); break; case INCLUDE_FRAME: catstr = buf_data(frame->out); catstrlen = strlen(catstr); if (catstr[catstrlen - 1] == '\n') { catstr[catstrlen - 1] = '\0'; } buf_puts(pframe->out, catstr); if (ppenv->flags & MACRO_DEBUG_FLAG) { buf_printf(pframe->out, LINE_FMT_2, pframe->filename, pframe->lineno); } break; default: buf_cat(pframe->out, frame->out); break; } /* reset pplineno */ pplineno = pframe->lineno; /* reset frame */ ppenv->frame = pframe; } if (frame->yybuf != NULL) { if (shared_check(frame->yybuf)) { motopp_freeYYBuffer(frame->yybuf); } } if (frame->yybufmem != NULL) { free(frame->yybufmem); } buf_free(frame->out); stack_free(frame->opstack); free(frame->filename); free(frame->relative_root); free(frame); }
void moto_emitCHeader(MotoEnv *env, StringBuffer *out) { Enumeration *e; char bts[MAX_DATE_LEN]; char gts[MAX_DATE_LEN]; int maxlen; char *execname = path_alloc(&maxlen); char *filename = env->filename; int i; for (i = 0; i < maxlen; i++) { if (filename[i] == '\0' || filename + i == strrchr(filename,'.')) { execname[i] = '\0'; break; } else if ( !isalpha(filename[i]) && !isdigit(filename[i]) ) { execname[i] = '_'; } else { execname[i] = filename[i]; } } moto_buildTimeStamp(bts); moto_curTimeStamp(gts); buf_puts(out, "/***************** DO NOT EDIT ****************\n"); buf_puts(out, " * FILE GENERATED BY THE MOTO COMPILER\n"); buf_puts(out, " * Moto Build: "); buf_puts(out, bts); buf_puts(out, "\n"); buf_puts(out, " * Generated: "); buf_puts(out, gts); buf_puts(out, "\n"); buf_puts(out, " * Input File: "); buf_puts(out, env->filename); buf_puts(out, "\n"); buf_puts(out, " **********************************************/\n\n"); e = (sset_elements(env->includes)); while (enum_hasNext(e)) { char *incl = (char *)enum_next(e); buf_puts(out, "#include "); buf_puts(out, incl); buf_puts(out, "\n"); } enum_free(e); buf_puts(out, "\n"); buf_puts(out, "#ifdef SHARED_MALLOC\n"); buf_puts(out, "# define STD_FREE_FN shared_free\n"); buf_puts(out, "#else\n"); buf_puts(out, "# define STD_FREE_FN free\n"); buf_puts(out, "#endif\n"); buf_puts(out, "\n"); buf_puts(out, "#ifdef HAVE_FN_NAME_H\n"); buf_puts(out, "#include \"mod_fn.h\"\n"); buf_puts(out, "#endif\n"); buf_puts(out, "#ifndef __MAIN__\n"); buf_puts(out, "# define __MAIN__ main_"); buf_puts(out, execname); buf_puts(out, "\n"); buf_puts(out, "#endif\n"); buf_puts(out, "#define __EXEC__ exec_"); buf_puts(out, execname); buf_puts(out, "\n"); buf_puts(out, buf_data(env->constantPool)); buf_puts(out, "\n\n"); buf_puts(out, "enum {\n"); buf_puts(out, "NOOP_ACTION = 0,\n"); buf_puts(out, "BREAK_ACTION = 1,\n"); buf_puts(out, "CONTINUE_ACTION = 2,\n"); buf_puts(out, "RETURN_ACTION = 3\n"); buf_puts(out, "};\n"); buf_puts(out, "static int _MOTO_ACTION = 0;\n"); moto_emitCStructures(env,out); moto_emitCImplicitConstructorPrototypes(env,out); moto_emitCPrototypes(env,out); buf_puts(out, "StringBuffer *out;\n"); /* FIXME: big hack to get output buffer to functions */ /* FIXME : The following hacks all exist because non-gcc compilers don't like inline array instantiation */ buf_puts(out, "int _MDS_[20];\n\n"); /* FIXME: big hack to allow for array instantiation */ buf_puts(out, "int _PARGI_[20];\n\n"); /* FIXME: big hack to allow for partially applied functions */ buf_puts(out, "FuncTypeKind _PARGT_[20];\n\n"); /* FIXME: big hack to allow for partially applied functions */ moto_emitCGlobals(env,out); moto_emitCImplicitConstructors(env,out); moto_emitCFunctions(env,out); buf_puts(out, "void __EXEC__"); buf_puts(out, "(StringBuffer *outputBuffer) {\n\n"); buf_puts(out, " /* BEGIN GENERATED CODE */\n\n"); free(execname); }