From 1c6239de294d7b94b0daae415818ba9eb3981575 Mon Sep 17 00:00:00 2001 From: Tebjan Halm Date: Thu, 12 Apr 2012 05:15:40 +0200 Subject: [PATCH] fixed str[0] = '\0'; bug... --- libpd_wrapper/z_libpd.c | 56 ++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/libpd_wrapper/z_libpd.c b/libpd_wrapper/z_libpd.c index 4ab82348d..859c3d884 100644 --- a/libpd_wrapper/z_libpd.c +++ b/libpd_wrapper/z_libpd.c @@ -56,44 +56,45 @@ void libpd_set_messagehook(const t_libpd_messagehook hook){ } //pointer to string memory -char *arg_str; +static char *arg_str; +static char *list_arg_str; -//convert atom list to space separated string +//convert atom list to space seperated string char* args_to_str(char *str, int argc, t_atom *args) { //free mem free(str); int i; - int size; + int size = argc; char float_buff[64]; //determine size for (i = 0; i < argc; i++) { t_atom a = args[i]; if (libpd_is_float(a)) { - sprintf(float_buff, "%f", libpd_get_float(a)); - size = size + strlen(float_buff); + sprintf(float_buff, "%f", libpd_get_float(a)); + size = size + strlen(float_buff); } else if (libpd_is_symbol(a)) { - size = size + strlen(libpd_get_symbol(a)); + size = size + strlen(libpd_get_symbol(a)); } } //get mem - str = malloc(size+argc); - char *space = " "; + str = malloc(size); + str[0] = '\0'; //build string for (i = 0; i < argc; i++) { t_atom a = args[i]; if (libpd_is_float(a)) { - sprintf(float_buff, "%f ", libpd_get_float(a)); - strcat(str, float_buff); + sprintf(float_buff, "%f ", libpd_get_float(a)); + strcat(str, float_buff); } else if (libpd_is_symbol(a)) { - strcat(str, libpd_get_symbol(a)); - strcat(str, space); + strcat(str, libpd_get_symbol(a)); + strcat(str, " "); } } @@ -101,9 +102,8 @@ char* args_to_str(char *str, int argc, t_atom *args) { } //list -void str_listhook(const char *recv, int argc, t_atom *argv) -{ - if (libpd_liststrhook) (*libpd_liststrhook)(recv, argc, args_to_str(arg_str, argc, argv)); +void str_listhook(const char *recv, int argc, t_atom *argv) { + if (libpd_liststrhook) (*libpd_liststrhook)(recv, argc, args_to_str(list_arg_str, argc, argv)); } void libpd_set_liststrhook(const t_libpd_liststrhook hook){ @@ -112,12 +112,11 @@ void libpd_set_liststrhook(const t_libpd_liststrhook hook){ } //message -void str_messagehook(const char *recv, const char *msg, int argc, t_atom *argv) -{ +void str_messagehook(const char *recv, const char *msg, int argc, t_atom *argv) { if (libpd_messagestrhook) (*libpd_messagestrhook)(recv, msg, argc, args_to_str(arg_str, argc, argv)); } -void libpd_set_messagestrhook(const t_libpd_messagestrhook hook){ +void libpd_set_messagestrhook(const t_libpd_messagestrhook hook) { libpd_messagestrhook = hook; libpd_messagehook = (t_libpd_messagehook)str_messagehook; } @@ -130,38 +129,31 @@ t_libpd_aftertouchhook libpd_aftertouchhook = NULL; t_libpd_polyaftertouchhook libpd_polyaftertouchhook = NULL; t_libpd_midibytehook libpd_midibytehook = NULL; -void libpd_set_noteonhook(const t_libpd_noteonhook hook) -{ +void libpd_set_noteonhook(const t_libpd_noteonhook hook) { libpd_noteonhook = hook; } -void libpd_set_controlchangehook(const t_libpd_controlchangehook hook) -{ +void libpd_set_controlchangehook(const t_libpd_controlchangehook hook) { libpd_controlchangehook = hook; } -void libpd_set_programchangehook(const t_libpd_programchangehook hook) -{ +void libpd_set_programchangehook(const t_libpd_programchangehook hook) { libpd_programchangehook = hook; } -void libpd_set_pitchbendhook(const t_libpd_pitchbendhook hook) -{ +void libpd_set_pitchbendhook(const t_libpd_pitchbendhook hook) { libpd_pitchbendhook = hook; } -void libpd_set_aftertouchhook(const t_libpd_aftertouchhook hook) -{ +void libpd_set_aftertouchhook(const t_libpd_aftertouchhook hook) { libpd_aftertouchhook = hook; } -void libpd_set_polyaftertouchhook(const t_libpd_polyaftertouchhook hook) -{ +void libpd_set_polyaftertouchhook(const t_libpd_polyaftertouchhook hook) { libpd_polyaftertouchhook = hook; } -void libpd_set_midibytehook(const t_libpd_midibytehook hook) -{ +void libpd_set_midibytehook(const t_libpd_midibytehook hook) { libpd_midibytehook = hook; }