static cx_int16 serializeText(cx_value *value, cx_string *out) { cx_type type = cx_valueType(value); cx_void *v = cx_valueValue(value); cx_primitiveKind kind = cx_primitive(type)->kind; if (kind == CX_CHARACTER || (kind == CX_TEXT && (*(cx_string *)v))) { cx_string raw; size_t length; int needEscape = 0; if (cx_convert(cx_primitive(type), v, cx_primitive(cx_string_o), &raw)) { goto error; } if (*raw == '@') { needEscape = 1; } length = stresc(NULL, 0, raw); *out = cx_malloc(length + 3 + needEscape); (*out)[0] = '"'; (*out)[1] = '@'; stresc(*out + 1 + needEscape, length, raw); (*out)[length + needEscape + 1] = '"'; (*out)[length + needEscape + 2] = '\0'; cx_dealloc(raw); } else { *out = cx_malloc(sizeof("null")); strcpy(*out, "null"); } return 0; error: return -1; }
static cx_int16 serializeBoolean(cx_value *value, cx_string *out) { cx_bool b = *(cx_bool *)cx_valueValue(value); if (b) { *out = cx_malloc(sizeof("true")); strcpy(*out, "true"); } else { *out = cx_malloc(sizeof("false")); strcpy(*out, "false"); } return 0; }
/* Append string to serializer-result */ static cx_bool cx_ser_appendstr(cx_json_ser_t* data, cx_string fmt, ...) { char alloc[1024]; char *buff = alloc; va_list args; cx_uint32 memRequired; cx_uint32 result = TRUE; if (data) { va_list argcpy; va_copy(argcpy, args); va_start(args, fmt); memRequired = vsnprintf(buff, 1024, fmt, args); if (memRequired >= 1024) { buff = cx_malloc(memRequired + 1); vsprintf(buff, fmt, argcpy); } va_end(args); result = cx_ser_appendstrbuff(data, buff); if (buff != alloc) { cx_dealloc(buff); } } return result; }
static void web_SockJsServer_message(web_SockJsServer _this, struct mg_connection *conn) { web_SockJsServer_Connection c = web_SockJsServer_Connection(conn->connection_param); if (_this->onMessage._parent.procedure) { if (conn->content_len) { char *msg = cx_malloc(conn->content_len + 1); memcpy(msg, conn->content, conn->content_len); msg[conn->content_len] = '\0'; /* Parse & unpack message */ JSON_Value *root = json_parse_string(msg); if (!root) { goto error; } if (json_value_get_type(root) == JSONArray) { JSON_Array *messages = json_value_get_array(root); cx_uint32 i; for (i = 0; i < json_array_get_count(messages); i++) { const char *message = json_array_get_string(messages, i); cx_call(_this->onMessage._parent.procedure, NULL, _this->onMessage._parent.instance, c, message); } } json_value_free(root); } } error:; }
cx_json_tree cx_json_tree_create (const char *data, cxu32 size) { CX_ASSERT (data); cx_json_tree_internal *tree = NULL; json_settings settings; memset (&settings, 0, sizeof(settings)); char errorBuffer [512]; json_value *root = json_parse_ex (&settings, data, size, errorBuffer, 512); if (root) { tree = cx_malloc (sizeof (cx_json_tree_internal)); tree->root = root; } CX_LOG_CONSOLE (CX_JSON_DEBUG_LOG_ENABLED && (root == NULL), errorBuffer); return tree; }
inp_stub* make_stub(cd* func) { inp_stub* i_s = cx_malloc(sizeof(inp_stub)); i_s->tag = TAG_I_S; i_s->func = func; return i_s; }
binding* make_binding(int id, void* value) { binding* make_bind = cx_malloc(sizeof(binding)); make_bind->tag = TAG_BIND; make_bind->id = id; make_bind->value = value; return make_bind; }
/* ::cortex::web::DDPServer::Session::connected() */ cx_void web_DDPServer_Session_connected(web_DDPServer_Session _this) { /* $begin(::cortex::web::DDPServer::Session::connected) */ int msgLength = snprintf(NULL, 0, "{\"msg\":\"connected\",\"session\":\"%s\"}", cx_nameof(_this)); cx_string msg = cx_malloc(msgLength + 1); sprintf(msg, "{\"msg\":\"connected\",\"session\":\"%s\"}", cx_nameof(_this)); web_SockJsServer_Connection_send(_this->conn, msg); cx_dealloc(msg); /* $end */ }
/* ::cortex::io::file::read(uint32 bytes) */ cx_octet_seq io_file_read(io_file _this, cx_uint32 bytes) { /* $begin(::cortex::io::file::read) */ cx_octet_seq result; result.buffer = cx_malloc(bytes); result.length = fread(result.buffer, bytes, 1, (FILE*)_this->handle); return result; /* $end */ }
void cx_varmod_register_var (cx_varmod_var_type type, void *val, const char *name) { cx_varmod_var *var = (cx_varmod_var *) cx_malloc (sizeof (cx_varmod_var)); var->type = type; var->val = val; var->name = name; // add to list g_variables = NULL; }
/* ::cortex::web::DDPServer::Session::pong(string id) */ cx_void web_DDPServer_Session_pong(web_DDPServer_Session _this, cx_string id) { /* $begin(::cortex::web::DDPServer::Session::pong) */ if (id) { int msgLength = snprintf(NULL, 0, "{\"msg\":\"pong\",\"id\":\"%s\"}", id); cx_string msg = cx_malloc(msgLength + 1); sprintf(msg, "{\"msg\":\"pong\",\"id\":\"%s\"}", id); web_SockJsServer_Connection_send(_this->conn, msg); cx_dealloc(msg); } else { web_SockJsServer_Connection_send(_this->conn, "{\"msg\":\"pong\"}"); } /* $end */ }
static cx_errThreadData* cx_getThreadData(void){ cx_errThreadData* result; if (!cx_errKey) { cx_threadTlsKey(&cx_errKey, cx_lasterrorFree); } result = cx_threadTlsGet(cx_errKey); if (!result) { result = cx_malloc(sizeof(cx_errThreadData)); memset(result, 0, sizeof(cx_errThreadData)); result->echo = TRUE; cx_threadTlsSet(cx_errKey, result); } return result; }
/* Register exithandler */ void cx_onexit(void(*handler)(void*), void* userData) { struct cx_exitHandler* h; h = cx_malloc(sizeof(struct cx_exitHandler)); h->handler = handler; h->userData = userData; cx_mutexLock(&cx_adminLock); if (!cx_exitHandlers) { cx_exitHandlers = cx_llNew(); } cx_llInsert(cx_exitHandlers, h); cx_mutexUnlock(&cx_adminLock); }
/* Do metawalk on type */ cx_int16 cx_metaWalk(cx_serializer s, cx_type type, void* userData) { cx__object* o; cx_int16 result; /* Instantiate dummy-object */ o = cx_malloc(sizeof(cx__object) + type->size); /* alloca is dangerous here because objects can get large, causing stack overflows. */ memset(o, 0, sizeof(cx__object) + type->size); o->type = cx_type(type); o->refcount = 1; result = cx_serialize(s, CX_OFFSET(o, sizeof(cx__object)), userData); cx_dealloc(o); return result; }
int main(int ac, char **av) { t_glob *glob; glob = cx_malloc(1 * sizeof(*glob)); glob->graph = cx_malloc(1 * sizeof(t_graph)); glob->universe = cx_malloc(1 * sizeof(t_uni)); glob->option = cx_malloc(1 * sizeof(t_option)); if (ac < 2) { fprintf(stderr, "Usage: ./%s [file.xml] [flags]\n", av[0]); put_error(USAGE_FLAG, 1); } parse_conf(av[1], glob); get_args(glob, ac, av); check_parse(glob); init_methodes(glob); init(glob); tracemyray(glob); mlx_expose_hook(glob->graph->win_ptr, &expose, glob); mlx_key_hook(glob->graph->win_ptr, &key_hook, glob); mlx_loop(glob->graph->mlx_ptr); return (EXIT_SUCCESS); }
/* ::cortex::web::SockJsServer::Connection::send(string msg) */ cx_void web_SockJsServer_Connection_send(web_SockJsServer_Connection _this, cx_string msg) { /* $begin(::cortex::web::SockJsServer::Connection::send) */ int escapedLength; cx_string sockJsMsg; /* Escape & pack message in SockJS header */ escapedLength = stresc(NULL, 0, msg); sockJsMsg = cx_malloc(escapedLength + strlen("a[\"\"]") + 1); sprintf(sockJsMsg, "a[\"%*s\"]", escapedLength, " "); stresc(sockJsMsg + 3, escapedLength, msg); mg_websocket_printf((struct mg_connection *)_this->conn, WEBSOCKET_OPCODE_TEXT, sockJsMsg); cx_dealloc(sockJsMsg); /* $end */ }
ui_context_t *ui_init (float width, float height) { ui_context_t *ctx = (ui_context_t *) cx_malloc (sizeof (ui_context_t)); memset (ctx, 0, sizeof (ui_context_t)); ctx->canvasWidth = width; ctx->canvasHeight = height; ctx->hover = NULL; ctx->focus = NULL; ctx->font = NULL; cx_list2_init (&ctx->intrList); return ctx; }
static int cx_appendStringAttr(cx_string key, cx_string value, void* userData) { size_t length; /* Escape value */ cx_string escapedValue = cx_malloc((length = stresc(NULL, 0, value)) + 1); stresc(escapedValue, length, value); if (!cx_ser_appendstr(userData, "\"%s\":\"%s\",", key, escapedValue)) { goto finished; } cx_dealloc(escapedValue); return 1; finished: cx_dealloc(escapedValue); return 0; }
void ui_custom_set_callbacks (ui_custom_t *custom, const ui_custom_callbacks_t *callbacks) { CX_ASSERT (custom); ui_custom_callbacks_t *cb = NULL; if (!custom->_callbacks) { cb = (ui_custom_callbacks_t *) cx_malloc (sizeof (ui_custom_callbacks_t)); custom->_callbacks = cb; } else { cb = (ui_custom_callbacks_t *) custom->_callbacks; } *cb = *callbacks; }
void ui_button_set_callbacks (ui_button_t *button, const ui_button_callbacks_t *callbacks) { CX_ASSERT (button); ui_button_callbacks_t *cb = NULL; if (!button->_callbacks) { cb = (ui_button_callbacks_t *) cx_malloc (sizeof (ui_button_callbacks_t)); button->_callbacks = cb; } else { cb = (ui_button_callbacks_t *) button->_callbacks; } *cb = *callbacks; }
ui_button_t *ui_button_create (ui_context_t *ctx, int uid) { CX_FATAL_ASSERT (ctx); ui_button_t *button = (ui_button_t *) cx_malloc (sizeof (ui_button_t)); memset (button, 0, sizeof (ui_button_t)); button->intr.uid = uid; button->intr.wtype = UI_WIDGET_BUTTON; button->intr._widget = button; button->intr.opacity = 1.0f; button->intr.show = 1; button->intr.enable = 1; ui_ctx_add_intrinsic (ctx, &button->intr); return button; }
ui_custom_t *ui_custom_create (ui_context_t *ctx, int uid) { CX_FATAL_ASSERT (ctx); ui_custom_t *custom = (ui_custom_t *) cx_malloc (sizeof (ui_custom_t)); memset (custom, 0, sizeof (ui_custom_t)); custom->intr.uid = uid; custom->intr.wtype = UI_WIDGET_CUSTOM; custom->intr._widget = custom; custom->intr.opacity = 1.0f; custom->intr.show = 1; custom->intr.enable = 1; ui_ctx_add_intrinsic (ctx, &custom->intr); return custom; }
static cx_int16 serializeNumericWithPrefix(cx_value *value, cx_string *out, const char *prefix) { cx_string raw; cx_void *v = cx_valueValue(value); if (cx_convert(cx_primitive(cx_valueType(value)), v, cx_primitive(cx_string_o), &raw)) { goto error; } int length = snprintf(NULL, 0, "\"%s\" %s", prefix, raw); if (length < 0) { goto error; } *out = cx_malloc(length + 1); if (sprintf(*out, "\"%s %s\"", prefix, raw) < 0) { goto error; } cx_dealloc(raw); return 0; error: cx_dealloc(raw); return -1; }
void worker_init (void) { g_taskPoolArray = (task_t *) cx_malloc (sizeof (task_t) * TASK_MAX_COUNT); unsigned int i; for (i = 0; i < TASK_MAX_COUNT; ++i) { task_t *task = &g_taskPoolArray [i]; g_taskFreeList = task_list_insert_back (g_taskFreeList, task, &g_taskFreeListCount); } cx_thread_mutex_init (&g_sharedDataMutex); cx_thread_monitor_init (&g_threadMonitor); g_thread = cx_thread_create ("earthnews worker thread", CX_THREAD_TYPE_JOINABLE, worker_thread_func, NULL); cx_thread_start (g_thread); }
cx_err cx_logv(cx_err kind, unsigned int level, char* fmt, va_list arg, FILE* f) { char buff[CX_MAX_LOG + 1]; unsigned int written; size_t n = 0; cx_string alloc = NULL; cx_string msg = buff; va_list argcpy; va_copy(argcpy, arg); /* Make copy of arglist in * case vsnprintf needs to be called twice */ CX_UNUSED(level); if ((n = (vsnprintf(buff, CX_MAX_LOG, fmt, arg) + 1)) > CX_MAX_LOG) { alloc = cx_malloc(n + 2); vsnprintf(alloc, n, fmt, argcpy); msg = alloc; } n = strlen(msg); /* Set last error without \n */ cx_setLasterror(msg); strcat(msg, "\n"); n++; if (cx_getEcho() || ((kind == CX_CRITICAL) || (kind == CX_ASSERT))){ if ((written = fwrite(msg, 1, n, f)) != n) { fprintf(f, "Error in cx_logv: number of bytes written (%u)"\ " does not match length of message (%zu).\n", written, n); } } if (alloc) { cx_dealloc(alloc); } return kind; }
static cx_int16 serializeReference(cx_serializer s, cx_value *v, void *userData) { CX_UNUSED(s); cx_json_ser_t *data; void *o; cx_object object; cx_id id; data = userData; o = cx_valueValue(v); object = *(cx_object*)o; if (object) { if (cx_checkAttr(object, CX_ATTR_SCOPED) || (cx_valueObject(v) == object)) { cx_uint32 length; cx_fullname(object, id); /* Escape value */ cx_string escapedValue = cx_malloc((length = stresc(NULL, 0, id)) + 1); stresc(escapedValue, length, id); if (!cx_ser_appendstr(data, "\"@R %s\"", escapedValue)) { cx_dealloc(escapedValue); goto finished; } cx_dealloc(escapedValue); } else { cx_ser_appendstr(data, "\"anonymous\""); } } else { if (!cx_ser_appendstrbuff(data, "null")) { goto finished; } } return 0; finished: return 1; }
/* ::cortex::lang::function::stringToParameterSeq(string name,object scope) */ cx_parameterSeq cx_function_stringToParameterSeq(cx_string name, cx_object scope) { /* $begin(::cortex::lang::function::stringToParameterSeq) */ cx_parameterSeq result = {0, NULL}; cx_char* ptr; ptr = strchr(name, '('); if (!ptr) { cx_error("missing argumentlist in name for signature '%s'", name); goto error; } ptr++; /* Check if function has arguments */ if (*ptr != ')') { cx_int32 count = 0, i = 0; cx_id id; int flags = 0; /* Count number of parameters for function */ count = cx_signatureParamCount(name); if (count == -1) { goto error; } /* Allocate size for parameters */ result.length = count; result.buffer = cx_malloc(sizeof(cx_parameter) * count); memset(result.buffer, 0, sizeof(cx_parameter) * count); /* Parse arguments */ for(i=0; i<count; i++) { if (cx_signatureParamType(name, i, id, &flags)) { cx_error("error occurred while parsing type of parameter '%d' for signature '%s'", i, name); goto error; } /* Set reference */ result.buffer[i].passByReference = (flags & CX_PARAMETER_REFERENCE) != 0; /* Assign type */ result.buffer[i].type = cx_resolve_ext(NULL, scope, id, FALSE, "Resolve parameter-type for function"); if (!result.buffer[i].type) { cx_error("type '%s' of parameter %d in signature %s not found", id, i, name); goto error; } /* Validate whether reference is not redundantly applied */ if (result.buffer[i].passByReference && result.buffer[i].type->reference) { cx_id id; cx_error("redundant '&' qualifier for parameter %d, type '%s' is already a reference", i, cx_fullname(result.buffer[i].type, id)); goto error; } /* Parse name */ if (cx_signatureParamName(name, i, id)) { cx_error("error occurred while parsing name of argument '%s' for signature '%s'", name); goto error; } result.buffer[i].name = cx_strdup(id); } } return result; error: result.length = -1; cx_dealloc(result.buffer); result.buffer = NULL; return result; /* $end */ }
static cx_int16 serializeAlias(cx_value *value, cx_string *out) { CX_UNUSED(value); *out = cx_malloc(sizeof("null")); strcpy(*out, "null"); return 0; }
/* strdup is not a standard C function, so provide own implementation. */ char* cx_strdup(const char* str) { char *result = cx_malloc(strlen(str) + 1); strcpy(result, str); return result; }