void append_to_client_buf(UT_string *f) { assert(utarray_len(cfg.outbufs) > 0); UT_string **s=NULL; size_t l,least,c; char *b; int i=0,lx; b = utstring_body(f); l = utstring_len(f); switch(cfg.mode) { case fan: // send to ALL clients while ( (s=(UT_string**)utarray_next(cfg.outbufs,s))) { utstring_bincpy(*s,b,l); } break; case round_robin: // send to ONE client while ( (s=(UT_string**)utarray_next(cfg.outbufs,s))) { c = utstring_len(*s); if ((i==0) || (c < least)) {least=c; lx=i;} i++; } s = (UT_string**)utarray_eltptr(cfg.outbufs,lx); utstring_bincpy(*s,b,l); break; } }
int main() { UT_string *s,*t; char V_TestStr[] = "There are two needle\0s in this \0haystack with needle\0s."; char V_NeedleStr[] = "needle\0s"; long *V_KMP_Table; long V_FindPos; size_t V_StartPos; size_t V_FindCnt; utstring_new(s); utstring_new(t); utstring_bincpy(s, V_TestStr, sizeof(V_TestStr)-1); printf("\"%s\" len=%u\n", utstring_body(s), utstring_len(s)); utstring_bincpy(t, V_NeedleStr, sizeof(V_NeedleStr)-1); printf("\"%s\" len=%u\n", utstring_body(t), utstring_len(t)); V_KMP_Table = (long *)malloc(sizeof(long) * (utstring_len(t) + 1)); if (V_KMP_Table != NULL) { _utstring_BuildTable(utstring_body(t), utstring_len(t), V_KMP_Table); V_FindCnt = 0; V_FindPos = 0; V_StartPos = 0; do { V_FindPos = _utstring_find(utstring_body(s) + V_StartPos, utstring_len(s) - V_StartPos, utstring_body(t), utstring_len(t), V_KMP_Table); if (V_FindPos >= 0) { V_FindPos += V_StartPos; V_FindCnt++; V_StartPos = V_FindPos + 1; } printf("utstring_find()=%ld\n", V_FindPos); } while (V_FindPos >= 0); printf("FindCnt=%u\n", V_FindCnt); free(V_KMP_Table); } else { printf("malloc() failed...\n"); } utstring_free(s); utstring_free(t); return 0; }
static void EXPORT_in_polygon(struct crabql *crabql, msgpack_object *o, UT_string *s, int nargs) { ERROR_ASSERT(nargs == 3); ERROR_ASSERT(o[2].type == MSGPACK_OBJECT_ARRAY); msgpack_object *p = o[2].via.array.ptr; size_t len = o[2].via.array.size; uint64_t *vertexes = slab_alloc(len * sizeof(vertexes[0])); for (size_t i = 0; i < len; i++) { ERROR_ASSERT(p[i].type == MSGPACK_OBJECT_POSITIVE_INTEGER); vertexes[i] = p[i].via.u64; } struct polygon *polygon = polygon_make((struct point *) vertexes, len); char *binstr = dump_binstrescape((const char *) polygon, polygon_size(polygon)); uts_printf_concat(s, "cb_point_in_polygon((uint64_t) "); crabql_generate_code(crabql, &o[1], s); uts_printf_concat(s, ", \""); utstring_bincpy(s, binstr, strlen(binstr)); uts_printf_concat(s, "\")"); slab_free(vertexes); slab_free(polygon); slab_free(binstr); }
luarest_status invoke_application(application* apps, UT_string* url, luarest_method m, luarest_response* res_code, luarest_content_type* con_type, UT_string* res_buf) { application* app = NULL; service *service; char* pch = NULL; char* tmp = utstring_body(url); UT_string* app_name; UT_string* key; utstring_new(app_name); pch = strchr(++tmp, '/'); if (pch == NULL) { return(LUAREST_ERROR); } utstring_bincpy(app_name, tmp, pch-tmp); HASH_FIND(hh, apps, utstring_body(app_name), utstring_len(app_name), app); utstring_free(app_name); if (app == NULL) { return(LUAREST_ERROR); } utstring_new(key); utstring_printf(key, "M%d#P%s", m, pch); HASH_FIND(hh, app->s, utstring_body(key), utstring_len(key), service); if (service == NULL) { return(LUAREST_ERROR); } invoke_lua(app->lua_state, service->callback_ref, res_code, con_type, res_buf); return(LUAREST_SUCCESS); }
int main() { int i; UT_string *t; UT_vector v; utvector_init(&v, utstring_mm); UT_string s; utstring_init(&s); for(i=0; i<16; i++) { utstring_printf(&s, "."); utvector_push(&v, &s); } dump(&v); t = (UT_string*)utvector_head(&v); printf("head: %s %s\n", t?"non-null":"null", t?utstring_body(t):"-"); t = (UT_string*)utvector_tail(&v); printf("tail: %s %s\n", t?"non-null":"null", t?utstring_body(t):"-"); printf("extend\n"); t = (UT_string*)utvector_extend(&v); utstring_bincpy(t, "hello", 5); dump(&v); t = (UT_string*)utvector_head(&v); printf("head: %s %s\n", t?"non-null":"null", t?utstring_body(t):"-"); t = (UT_string*)utvector_tail(&v); printf("tail: %s %s\n", t?"non-null":"null", t?utstring_body(t):"-"); utvector_fini(&v); utstring_done(&s); return 0; }
int set_to_binary(void *set, UT_string *bin) { uint32_t l, u, a,b,c,d, abcd; uint16_t s; uint8_t g; double h; utstring_clear(bin); l=0; utstring_bincpy(bin,&l,sizeof(l)); // placeholder for size prefix int rc=-1,i=0,*t; kv_t *kv, kvdef; char **k=NULL,**def; while( (k=(char**)utarray_next(output_keys,k))) { kv = kv_get(set,*k); t = (int*)utarray_eltptr(output_types,i); assert(t); def = (char**)utarray_eltptr(output_defaults,i); assert(def); if (kv==NULL) { /* no such key */ kv=&kvdef; if (*def) {kv->val=*def; kv->vlen=strlen(*def);} /* default */ else if (*t == str) {kv->val=NULL; kv->vlen=0;} /* zero len string */ else { fprintf(stderr,"required key %s not present in spool frame\n", *k); goto done; } } switch(*t) { case d64: h=atof(kv->val); utstring_bincpy(bin,&h,sizeof(h)); break; case i8: g=atoi(kv->val); utstring_bincpy(bin,&g,sizeof(g)); break; case i16: s=atoi(kv->val); utstring_bincpy(bin,&s,sizeof(s)); break; case i32: u=atoi(kv->val); utstring_bincpy(bin,&u,sizeof(u)); break; case str: l=kv->vlen; utstring_bincpy(bin,&l,sizeof(l)); /* length prefix */ utstring_bincpy(bin,kv->val,kv->vlen); /* string itself */ break; case ipv4: if ((sscanf(kv->val,"%u.%u.%u.%u",&a,&b,&c,&d) != 4) || (a > 255 || b > 255 || c > 255 || d > 255)) { fprintf(stderr,"invalid IP for key %s: %s\n",*k,kv->val); goto done; } abcd = (a << 24) | (b << 16) | (c << 8) | d; abcd = htonl(abcd); utstring_bincpy(bin,&abcd,sizeof(abcd)); break; default: assert(0); break; } i++; } uint32_t len = utstring_len(bin); len -= sizeof(len); // length does not include itself char *length_prefix = utstring_body(bin); memcpy(length_prefix, &len, sizeof(len)); rc = 0; done: return rc; }
int ATP_arraySetString(ATP_Array *p_array, unsigned int p_index, const char *p_value) { Value *l_entry = findOrCreateEntry(p_array, p_index); if (l_entry == NULL) { return 0; } DBG("setting array[%u] = '%s'\n", p_index, p_value); Value_changeType(l_entry, e_ATP_ValueType_string); utstring_bincpy(&l_entry->m_value.m_string, p_value, strlen(p_value)); return 1; }
static UT_string* escape_fields(CURL* ch, const char* fields, va_list ap) { UT_string* res = 0; utstring_new(res); for (;*fields;*fields++) { if (*fields == '?') { char* f,*arg; arg = va_arg(ap,char*); f = curl_easy_escape(ch,arg,0); utstring_bincpy(res,f,strlen(f)); curl_free(f); } else
static void EXPORT_in_circle(struct crabql *crabql, msgpack_object *o, UT_string *s, int nargs) { ERROR_ASSERT(nargs == 3); ERROR_ASSERT(o[2].type == MSGPACK_OBJECT_ARRAY); ERROR_ASSERT(o[2].via.array.size == 2); ERROR_ASSERT(o[2].via.array.ptr[0].type == MSGPACK_OBJECT_POSITIVE_INTEGER); ERROR_ASSERT(o[2].via.array.ptr[1].type == MSGPACK_OBJECT_POSITIVE_INTEGER); struct circle *circle = circle_make(*(struct point *) &o[2].via.array.ptr[0].via.u64, o[2].via.array.ptr[1].via.u64); char *binstr = dump_binstrescape((const char *) circle, sizeof(struct circle)); uts_printf_concat(s, "cb_point_in_circle((uint64_t) "); crabql_generate_code(crabql, &o[1], s); uts_printf_concat(s, ", \""); utstring_bincpy(s, binstr, strlen(binstr)); uts_printf_concat(s, "\")"); slab_free(circle); slab_free(binstr); }
void o_putlong(orientdb *o, orientdb_con *c, unsigned long x) { OM_DEFINE_OBJECT(o_con,och); char *buf; buf = (char *) OM_MALLOC(sizeof(char *) * 8); if (!buf) return; int64_to_bytearray(x, (unsigned char *)buf); OM_MUTEX_LOCK(o_handler, ohandler); och = OM_DECAPSULATE(o_con, c->och); utstring_bincpy(och->send_stream, buf, 8); OM_MUTEX_UNLOCK(o_handler, ohandler); OM_MEMFREE(buf); debug_note(o, ORIENT_DEBUG, "put a long number: %i bytes: %ld\n", 8, x); return; }
int main() { int i; UT_string *t; UT_vector v; utvector_init(&v, utvector_utstring); UT_string s; utstring_init(&s); for(i=0; i<16; i++) { utstring_printf(&s, "."); utvector_push(&v, &s); } dump(&v); printf("extend\n"); t = (UT_string*)utvector_extend(&v); utstring_bincpy(t, "hello", 5); dump(&v); utvector_fini(&v); utstring_done(&s); return 0; }
int o_putnumber(orientdb *o, orientdb_con *c, unsigned short len, int x) { OM_DEFINE_OBJECT(o_con,och); char *buf; if (len > 4) { // we do not want to overflow a int32 (4 bytes) return -1; } buf = (char *) OM_MALLOC(sizeof(char *) * len); if (!buf) return -1; int32_to_bytearray((unsigned char *)buf, len, x); OM_MUTEX_LOCK(o_handler, ohandler); och = OM_DECAPSULATE(o_con, c->och); utstring_bincpy(och->send_stream, buf, len); OM_MUTEX_UNLOCK(o_handler, ohandler); OM_MEMFREE(buf); debug_note(o, ORIENT_DEBUG, "put a number: %i bytes: %i\n", len, x); return len; }
static void EXPORT_in(struct crabql *crabql, msgpack_object *o, UT_string *s, int nargs) { ERROR_ASSERT(nargs == 3); if (o[2].type == MSGPACK_OBJECT_ARRAY) { msgpack_object *p = o[2].via.array.ptr; uts_printf_concat(s, "bs64((int64_t) "); crabql_generate_code(crabql, &o[1], s); uts_printf_concat(s, ", \""); size_t len = o[2].via.array.size; int64_t *nums = slab_alloc(len * 8); for (size_t i = 0; i < len; i++) { ERROR_ASSERT(p[i].type == MSGPACK_OBJECT_POSITIVE_INTEGER || p[i].type == MSGPACK_OBJECT_NEGATIVE_INTEGER); if (p[i].type == MSGPACK_OBJECT_POSITIVE_INTEGER) ERROR_ASSERT(p[i].via.u64 < INT64_MAX); nums[i] = p[i].via.i64; } qsort(nums, len, 8, cmp64); // \x01\x00\x00\x00\x00\x00\x00\x00 char *binstr = dump_binstrescape((const char *) nums, len * sizeof(nums[0])); utstring_bincpy(s, binstr, strlen(binstr)); slab_free(nums); slab_free(binstr); uts_printf_concat(s, "\", (size_t) %zu)", o[2].via.array.size); } else if (o[2].type == MSGPACK_OBJECT_RAW) { char *bucket_name; int64_t table_id; if (parse_bucket_and_table(o[2].via.raw.ptr, o[2].via.raw.size, &bucket_name, &table_id) == 0) { struct bucket *bucket = bucket_get(bucket_name, CAN_RETURN_NULL); free(bucket_name); if (bucket) { struct table *table = bucket_get_table(bucket, table_id, CAN_RETURN_NULL); if (table) { lock_table(crabql, table); struct schema *schema = table->schema; if (schema) { struct field *field = schema_field_get_primary(schema); size_t datasize = ceildiv(schema->nbits, 8); uts_printf_concat(s, "bsintable("); crabql_generate_code(crabql, &o[1], s); uts_printf_concat(s, ", (size_t) %pULL, (size_t) %pULL, (size_t) %zu, (size_t) %zu)", table->data, field, datasize, table->len); } else { log_warn("schema is NULL, return 0."); uts_printf_concat(s, "0"); } } else { uts_printf_concat(s, "0"); } } else { log_warn("bucket is NULL, return 0."); uts_printf_concat(s, "0"); } } else { log_warn("cannot parse bucket and table"); crabql->error = 34; } } };
int main() { UT_string *s,*t; char V_TestStr[] = "There are two needle\0s in this \0haystack with needle\0s."; char V_NeedleStr[] = "needle\0s"; long *V_KMP_Table; long V_FindPos; size_t V_StartPos; size_t V_FindCnt; utstring_new(s); utstring_new(t); utstring_bincpy(s, V_TestStr, sizeof(V_TestStr)-1); printf("\"%s\" len=%u\n", utstring_body(s), utstring_len(s)); utstring_bincpy(t, V_NeedleStr, sizeof(V_NeedleStr)-1); printf("\"%s\" len=%u\n", utstring_body(t), utstring_len(t)); V_KMP_Table = (long *)malloc(sizeof(long) * (utstring_len(t) + 1)); if (V_KMP_Table != NULL) { _utstring_BuildTableR(utstring_body(t), utstring_len(t), V_KMP_Table); V_FindCnt = 0; V_FindPos = 0; V_StartPos = utstring_len(s) - 1; do { V_FindPos = _utstring_findR(utstring_body(s), V_StartPos + 1, utstring_body(t), utstring_len(t), V_KMP_Table); if (V_FindPos >= 0) { V_FindCnt++; V_StartPos = V_FindPos - 1; } printf("utstring_find()=%ld\n", V_FindPos); } while (V_FindPos >= 0); printf("FindCnt=%u\n", V_FindCnt); free(V_KMP_Table); } else { printf("malloc() failed...\n"); } utstring_free(t); utstring_clear(s); utstring_printf(s,"ABC ABCDAB ABCDABCDABDE"); int o; o=utstring_find( s, -9, "ABC", 3 ) ; printf("expect 15 %d\n",o); o=utstring_find( s, 3, "ABC", 3 ) ; printf("expect 4 %d\n",o); o=utstring_find( s, 16, "ABC", 3 ) ; printf("expect -1 %d\n",o); o=utstring_findR( s, -9, "ABC", 3 ) ; printf("expect 11 %d\n",o); o=utstring_findR( s, 12, "ABC", 3 ) ; printf("expect 4 %d\n",o); o=utstring_findR( s, 13, "ABC", 3 ) ; printf("expect 11 %d\n",o); o=utstring_findR( s, 2, "ABC", 3 ) ; printf("expect 0 %d\n",o); utstring_free(s); return 0; }
void cxStringAppend(cxString string,cxConstChars d,cxInt l) { CX_ASSERT(l > 0 && d != NULL, "args error"); utstring_bincpy(&string->strptr, d, l); }