//takes a string, oscizes and then turns it back into a string char * validate_osc_string(char * bundle_string){ t_osc_bndl_u *bndl = NULL; t_osc_parser_subst *subs = NULL; long nsubs = 0; //printf("bundle: %s\n", bundle_string); t_osc_err err = osc_parser_parseString(strlen(bundle_string)+2, bundle_string, &bndl, &nsubs, &subs); if (err != 0){ printf("%s\n", osc_error_string(err)); } //^multiple bundles need to be /n delimited. //not using this for now while(subs){ //subs is linked list of $n substitutions that need to take place t_osc_parser_subst *next = subs->next; osc_mem_free(subs); subs = next; } char *sbndl = NULL; long sbndl_len = 0; osc_bundle_u_serialize(bndl, &sbndl_len, &sbndl); //serialize the bundle char *buf = NULL; long buflen = 0; osc_bundle_s_format(sbndl_len, sbndl, &buflen, &buf); return buf; }
// we get the text, convert it to an OSC bundle, and then call the paint // function via qelem_set which converts the OSC bundle back to text. // We do this so that it's formatted nicely with no unnecessary whitespace // and tabbed subbundles, etc. void odisplay_gettext(t_odisplay *x) { long size = 0; char *text = NULL; #ifdef OMAX_PD_VERSION text = x->textbox->text; #else t_object *textfield = jbox_get_textfield((t_object *)x); object_method(textfield, gensym("gettextptr"), &text, &size); #endif odisplay_clearBundles(x); size = strlen(text); // the value returned in text doesn't make sense if(size == 0){ return; } char *buf = text; if(text[size - 1] != '\n'){ buf = alloca(size + 2); memcpy(buf, text, size); buf[size] = '\n'; buf[size + 1] = '\0'; size += 2; } t_osc_bndl_u *bndl_u = NULL; t_osc_err e = osc_parser_parseString(size, buf, &bndl_u); if(e){ #ifdef OMAX_PD_VERSION // x->parse_error = 1; #endif object_error((t_object *)x, "error parsing bundle\n"); return; } t_osc_bndl_s *bs = osc_bundle_u_serialize(bndl_u); t_osc_bndl_s *bndl_s = NULL; osc_bundle_s_deepCopy(&bndl_s, bs); odisplay_newBundle(x, bndl_u, bndl_s); #ifdef OMAX_PD_VERSION x->have_new_data = 1; jbox_redraw((t_jbox *)x); #else x->have_new_data = 1; qelem_set(x->qelem); #endif /* if(size > 2){ int i; char *r = text + 1; char *w = text + 1; char *rm1 = text; for(i = 0; i <= size; i++){ if(*rm1 == ' ' && *r == ' '){ r++; }else{ *w++ = *r++; } rm1++; } } */ }
int main(int argc, char **argv) { // create a bundle and add messages to it t_osc_bndl_u *bndl_u = osc_bundle_u_alloc(); t_osc_msg_u *m1 = osc_message_u_alloc(); osc_message_u_setAddress(m1, "/foo"); osc_message_u_appendFloat(m1, 3.14); osc_bundle_u_addMsg(bndl_u, m1); t_osc_msg_u *m2 = osc_message_u_allocWithString("/bar", "whatevs"); osc_bundle_u_addMsg(bndl_u, m2); t_osc_msg_u *m3 = osc_message_u_allocWithAddress("/bloo"); t_osc_atom_u *a = osc_atom_u_allocWithInt32(12); osc_message_u_appendAtom(m3, a); osc_bundle_u_addMsg(bndl_u, m3); // serialize the bundle long len = osc_bundle_u_nserialize(NULL, 0, bndl_u); char bndl_s[len]; osc_bundle_u_nserialize(bndl_s, len, bndl_u); // free the original unserialized bundle osc_bundle_u_free(bndl_u); bndl_u = NULL; // deserialize the serialized bundle osc_bundle_s_deserialize(len, bndl_s, &bndl_u); // iterate over messages in a serialized bundle t_osc_bndl_it_s *b_it_s = osc_bndl_it_s_get(len, bndl_s); while(osc_bndl_it_s_hasNext(b_it_s)){ t_osc_msg_s *m = osc_bndl_it_s_next(b_it_s); printf("%s\n", osc_message_s_getAddress(m)); } osc_bndl_it_s_destroy(b_it_s); // turn a serialized bundle into printable text long tlen = osc_bundle_s_nformat(NULL, 0, len, bndl_s, 0); char text[tlen + 1]; osc_bundle_s_nformat(text, tlen, len, bndl_s, 0); printf("\nBUNDLE:\n"); printf("%s\n", text); printf("\n"); // turn text into an unserialized bundle t_osc_bndl_u *bndl_u_2 = NULL; char *text2 = "/jean : [1, 2, 3], /john : 6.66, /jeremy : \"is cool\""; osc_parser_parseString(strlen(text2), text2, &bndl_u_2); // iterate over messages in an unserialized bundle t_osc_bndl_it_u *b_it_u = osc_bndl_it_u_get(bndl_u_2); while(osc_bndl_it_u_hasNext(b_it_u)){ t_osc_msg_u *m = osc_bndl_it_u_next(b_it_u); printf("%s has typetags ", osc_message_u_getAddress(m)); // iterate over atoms in list t_osc_msg_it_u *m_it_u = osc_msg_it_u_get(m); while(osc_msg_it_u_hasNext(m_it_u)){ t_osc_atom_u *a = osc_msg_it_u_next(m_it_u); printf("%c", osc_atom_u_getTypetag(a)); } osc_msg_it_u_destroy(m_it_u); printf("\n"); } osc_bndl_it_u_destroy(b_it_u); }
//input an expression and a bundle with data and have it evaluate the expression and compares the results to answer. int test_expression(char *expr, char *bundle_string, char *answer){ t_osc_expr *f = NULL; //f = function = lambda int ret = osc_expr_parser_parseString(expr, &f); //parse expression string. returns valid or not if(ret){ printf("parsing %s failed\n", expr); osc_expr_free(f); return 0; } //text representation of the function tree char *functiongraph = NULL; long len = 0; osc_expr_formatFunctionGraph(f, &len, &functiongraph); //from osc_expr.c t_osc_bndl_u *bndl = NULL; t_osc_parser_subst *subs = NULL; long nsubs = 0; //printf("bundle: %s\n", bundle_string); osc_parser_parseString(strlen(bundle_string)+2, bundle_string, &bndl, &nsubs, &subs); //unserialized oscizer. throw valid bundles at this and see if it parses it. //^multiple bundles need to be /n delimited. while(subs){ //subs is linked list of $n substitutions that need to take place t_osc_parser_subst *next = subs->next; osc_mem_free(subs); subs = next; } char *ser_bundle = NULL; long sbndl_len = 0; osc_bundle_u_serialize(bndl, &sbndl_len, &ser_bundle); //serialize the bundle //char * ser_bundle = bundalize_osc_string(bundle_string); t_osc_atom_ar_u *out = NULL; ret = osc_expr_funcall(f, &sbndl_len, &ser_bundle, &out);//calls the function on the bundle osc_atom_array_u_free(out); char *buf = NULL; long buflen = 0; osc_bundle_s_format(sbndl_len, ser_bundle, &buflen, &buf); char bufcopy[strlen(buf)]; strcpy(bufcopy, buf); char * formated_answer = validate_osc_string(answer); char answer_copy[strlen(formated_answer)]; strcpy(answer_copy, formated_answer); //make sure that the answer provided is the same as the answer computed ret = compare_answer(buf, formated_answer); if (ret==0){ printf("expecting: '%s', but instead got: '%s'\n", answer_copy, bufcopy); } //free all the memory osc_bundle_u_free(bndl); if(ser_bundle){ osc_mem_free(ser_bundle); } if(buf){ osc_mem_free(buf); } osc_expr_free(f); if(functiongraph){ osc_mem_free(functiongraph); } return ret; }