From fdc892944b6e52e4de44b0cb6f95f2b863724bab Mon Sep 17 00:00:00 2001 From: musil Date: Wed, 4 Jan 2006 01:05:17 +0000 Subject: new malloc svn path=/trunk/externals/iemlib/; revision=4356 --- src/iemlib2/mergefilename.c | 161 +++++++++++++++++---------------- src/iemlib2/splitfilename.c | 213 +++++++++++++++++++++----------------------- 2 files changed, 184 insertions(+), 190 deletions(-) (limited to 'src') diff --git a/src/iemlib2/mergefilename.c b/src/iemlib2/mergefilename.c index 8d22b5e..701b3b6 100644 --- a/src/iemlib2/mergefilename.c +++ b/src/iemlib2/mergefilename.c @@ -25,6 +25,8 @@ typedef struct _mergefilename { t_object x_obj; char x_sep[2]; + char *x_mem; + t_int x_size; } t_mergefilename; static void mergefilename_separator(t_mergefilename *x, t_symbol *s, int ac, t_atom *av) @@ -33,37 +35,35 @@ static void mergefilename_separator(t_mergefilename *x, t_symbol *s, int ac, t_a { if(IS_A_SYMBOL(av, 0)) { - if(strlen(av->a_w.w_symbol->s_name) == 1) - x->x_sep[0] = av->a_w.w_symbol->s_name[0]; - else if(!strcmp(av->a_w.w_symbol->s_name, "backslash")) + char *name=av->a_w.w_symbol->s_name; + + if(strlen(name) == 1) + x->x_sep[0] = name[0]; + else if(!strcmp(name, "backslash")) x->x_sep[0] = '\\'; - else if(!strcmp(av->a_w.w_symbol->s_name, "slash")) + else if(!strcmp(name, "slash")) x->x_sep[0] = '/'; - else if(!strcmp(av->a_w.w_symbol->s_name, "blank")) + else if(!strcmp(name, "blank")) x->x_sep[0] = ' '; - else if(!strcmp(av->a_w.w_symbol->s_name, "space")) + else if(!strcmp(name, "space")) x->x_sep[0] = ' '; - else if(!strcmp(av->a_w.w_symbol->s_name, "dollar")) + else if(!strcmp(name, "dollar")) x->x_sep[0] = '$'; - else if(!strcmp(av->a_w.w_symbol->s_name, "comma")) + else if(!strcmp(name, "comma")) x->x_sep[0] = ','; - else if(!strcmp(av->a_w.w_symbol->s_name, "semi")) + else if(!strcmp(name, "semi")) x->x_sep[0] = ';'; - else if(!strcmp(av->a_w.w_symbol->s_name, "leftbrace")) + else if(!strcmp(name, "leftbrace")) x->x_sep[0] = '{'; - else if(!strcmp(av->a_w.w_symbol->s_name, "rightbrace")) + else if(!strcmp(name, "rightbrace")) x->x_sep[0] = '}'; else x->x_sep[0] = 0; } else if(IS_A_FLOAT(av, 0)) { - int i; - float f=fabs(av->a_w.w_float); + t_int i=atom_getintarg(0, ac, av); - while(f >= 10.0) - f *= 0.1; - i = (int)f; x->x_sep[0] = (char)i + '0'; } } @@ -73,11 +73,11 @@ static void mergefilename_separator(t_mergefilename *x, t_symbol *s, int ac, t_a static void mergefilename_float(t_mergefilename *x, t_floatarg f) { - char fbuf[30]; + char flt_buf[30]; - fbuf[0] = 0; - sprintf(fbuf, "%g", f); - outlet_symbol(x->x_obj.ob_outlet, gensym(fbuf)); + flt_buf[0] = 0; + sprintf(flt_buf, "%g", f); + outlet_symbol(x->x_obj.ob_outlet, gensym(flt_buf)); } static void mergefilename_symbol(t_mergefilename *x, t_symbol *s) @@ -87,35 +87,34 @@ static void mergefilename_symbol(t_mergefilename *x, t_symbol *s) static void mergefilename_list(t_mergefilename *x, t_symbol *s, int ac, t_atom *av) { - char *cbeg, fbuf[30]; - int size=400, i, len, cursize=0; + char flt_buf[30]; + t_int i, length, accu_size=0; - cbeg = (char *)getbytes(size * sizeof(char)); - cbeg[0] = 0; + x->x_mem[0] = 0; if(ac > 0) { if(IS_A_SYMBOL(av, 0)) { - len = strlen(av->a_w.w_symbol->s_name); - if((len + cursize) >= (size-1)) + length = strlen(av->a_w.w_symbol->s_name); + if((length + accu_size) >= (x->x_size-2)) { - cbeg = (char *)resizebytes(cbeg, size*sizeof(char), 2*(len + cursize)*sizeof(char)); - size = 2*(len + cursize); + x->x_mem = (char *)resizebytes(x->x_mem, x->x_size*sizeof(char), 2*(length + accu_size)*sizeof(char)); + x->x_size = 2*(length + accu_size); } - strcat(cbeg, av->a_w.w_symbol->s_name); - cursize += len; + strcat(x->x_mem, av->a_w.w_symbol->s_name); + accu_size += length; } else if(IS_A_FLOAT(av, 0)) { - sprintf(fbuf, "%g", av->a_w.w_float); - len = strlen(fbuf); - if((len + cursize) >= (size-1)) + sprintf(flt_buf, "%g", av->a_w.w_float); + length = strlen(flt_buf); + if((length + accu_size) >= (x->x_size-2)) { - cbeg = (char *)resizebytes(cbeg, size*sizeof(char), 2*(len + cursize)*sizeof(char)); - size = 2*(len + cursize); + x->x_mem = (char *)resizebytes(x->x_mem, x->x_size*sizeof(char), 2*(length + accu_size)*sizeof(char)); + x->x_size = 2*(length + accu_size); } - strcat(cbeg, fbuf); - cursize += len; + strcat(x->x_mem, flt_buf); + accu_size += length; } } @@ -124,84 +123,86 @@ static void mergefilename_list(t_mergefilename *x, t_symbol *s, int ac, t_atom * for(i=1; ix_sep); + strcat(x->x_mem, x->x_sep); if(IS_A_SYMBOL(av, 0)) { - len = strlen(av->a_w.w_symbol->s_name); - if((len + cursize) >= (size-1)) + length = strlen(av->a_w.w_symbol->s_name); + if((length + accu_size) >= (x->x_size-2)) { - cbeg = (char *)resizebytes(cbeg, size*sizeof(char), 2*(len + cursize)*sizeof(char)); - size = 2*(len + cursize); + x->x_mem = (char *)resizebytes(x->x_mem, x->x_size*sizeof(char), 2*(length + accu_size)*sizeof(char)); + x->x_size = 2*(length + accu_size); } - strcat(cbeg, av->a_w.w_symbol->s_name); - cursize += len; + strcat(x->x_mem, av->a_w.w_symbol->s_name); + accu_size += length; } else if(IS_A_FLOAT(av, 0)) { - sprintf(fbuf, "%g", av->a_w.w_float); - len = strlen(fbuf); - if((len + cursize) >= (size-1)) + sprintf(flt_buf, "%g", av->a_w.w_float); + length = strlen(flt_buf); + if((length + accu_size) >= (x->x_size-2)) { - cbeg = (char *)resizebytes(cbeg, size*sizeof(char), 2*(len + cursize)*sizeof(char)); - size = 2*(len + cursize); + x->x_mem = (char *)resizebytes(x->x_mem, x->x_size*sizeof(char), 2*(length + accu_size)*sizeof(char)); + x->x_size = 2*(length + accu_size); } - strcat(cbeg, fbuf); - cursize += len; + strcat(x->x_mem, flt_buf); + accu_size += length; } } } - outlet_symbol(x->x_obj.ob_outlet, gensym(cbeg)); - freebytes(cbeg, size * sizeof(char)); + outlet_symbol(x->x_obj.ob_outlet, gensym(x->x_mem)); } static void mergefilename_anything(t_mergefilename *x, t_symbol *s, int ac, t_atom *av) { - char *cbeg, fbuf[30]; - int size=400, i, len, cursize=0; + char flt_buf[30]; + t_int i, length, accu_size=0; - cbeg = (char *)getbytes(size * sizeof(char)); - cbeg[0] = 0; - len = strlen(s->s_name); - if((len + cursize) >= (size-1)) + x->x_mem[0] = 0; + length = strlen(s->s_name); + if((length + accu_size) >= (x->x_size-2)) { - cbeg = (char *)resizebytes(cbeg, size*sizeof(char), 2*(len + cursize)*sizeof(char)); - size = 2*(len + cursize); + x->x_mem = (char *)resizebytes(x->x_mem, x->x_size*sizeof(char), 2*(length + accu_size)*sizeof(char)); + x->x_size = 2*(length + accu_size); } - strcat(cbeg, s->s_name); - cursize += len; + strcat(x->x_mem, s->s_name); + accu_size += length; if(ac > 0) { for(i=0; ix_sep); + strcat(x->x_mem, x->x_sep); if(IS_A_SYMBOL(av, 0)) { - len = strlen(av->a_w.w_symbol->s_name); - if((len + cursize) >= (size-1)) + length = strlen(av->a_w.w_symbol->s_name); + if((length + accu_size) >= (x->x_size-2)) { - cbeg = (char *)resizebytes(cbeg, size*sizeof(char), 2*(len + cursize)*sizeof(char)); - size = 2*(len + cursize); + x->x_mem = (char *)resizebytes(x->x_mem, x->x_size*sizeof(char), 2*(length + accu_size)*sizeof(char)); + x->x_size = 2*(length + accu_size); } - strcat(cbeg, av->a_w.w_symbol->s_name); - cursize += len; + strcat(x->x_mem, av->a_w.w_symbol->s_name); + accu_size += length; } else if(IS_A_FLOAT(av, 0)) { - sprintf(fbuf, "%g", av->a_w.w_float); - len = strlen(fbuf); - if((len + cursize) >= (size-1)) + sprintf(flt_buf, "%g", av->a_w.w_float); + length = strlen(flt_buf); + if((length + accu_size) >= (x->x_size-2)) { - cbeg = (char *)resizebytes(cbeg, size*sizeof(char), 2*(len + cursize)*sizeof(char)); - size = 2*(len + cursize); + x->x_mem = (char *)resizebytes(x->x_mem, x->x_size*sizeof(char), 2*(length + accu_size)*sizeof(char)); + x->x_size = 2*(length + accu_size); } - strcat(cbeg, fbuf); - cursize += len; + strcat(x->x_mem, flt_buf); + accu_size += length; } av++; } } - outlet_symbol(x->x_obj.ob_outlet, gensym(cbeg)); - freebytes(cbeg, size * sizeof(char)); + outlet_symbol(x->x_obj.ob_outlet, gensym(x->x_mem)); +} + +static void mergefilename_free(t_mergefilename *x) +{ + freebytes(x->x_mem, x->x_size*sizeof(char)); } static void *mergefilename_new(t_symbol *s, int ac, t_atom *av) @@ -212,6 +213,8 @@ static void *mergefilename_new(t_symbol *s, int ac, t_atom *av) x->x_sep[1] = 0; if(ac > 0) mergefilename_separator(x, s, ac, av); + x->x_size = 400; + x->x_mem = (char *)getbytes(x->x_size*sizeof(char)); outlet_new(&x->x_obj, &s_symbol); return (x); } diff --git a/src/iemlib2/splitfilename.c b/src/iemlib2/splitfilename.c index 5f009c8..5f8bf1a 100644 --- a/src/iemlib2/splitfilename.c +++ b/src/iemlib2/splitfilename.c @@ -24,131 +24,122 @@ static t_class *splitfilename_class; typedef struct _splitfilename { - t_object x_obj; - char x_sep[2]; - void *x_outpath; - void *x_outfile; + t_object x_obj; + char x_sep[2]; + char *x_mem; + t_int x_size; + t_outlet *x_outpath; + t_outlet *x_outfile; } t_splitfilename; static void splitfilename_separator(t_splitfilename *x, t_symbol *s, int ac, t_atom *av) { - if(ac > 0) - { - if(IS_A_SYMBOL(av, 0)) - { - if(strlen(av->a_w.w_symbol->s_name) == 1) - x->x_sep[0] = av->a_w.w_symbol->s_name[0]; - else if(!strcmp(av->a_w.w_symbol->s_name, "backslash")) - x->x_sep[0] = '\\'; - else if(!strcmp(av->a_w.w_symbol->s_name, "slash")) - x->x_sep[0] = '/'; - else if(!strcmp(av->a_w.w_symbol->s_name, "blank")) - x->x_sep[0] = ' '; - else if(!strcmp(av->a_w.w_symbol->s_name, "space")) - x->x_sep[0] = ' '; - else if(!strcmp(av->a_w.w_symbol->s_name, "dollar")) - x->x_sep[0] = '$'; - else if(!strcmp(av->a_w.w_symbol->s_name, "comma")) - x->x_sep[0] = ','; - else if(!strcmp(av->a_w.w_symbol->s_name, "semi")) - x->x_sep[0] = ';'; - else if(!strcmp(av->a_w.w_symbol->s_name, "leftbrace")) - x->x_sep[0] = '{'; - else if(!strcmp(av->a_w.w_symbol->s_name, "rightbrace")) - x->x_sep[0] = '}'; - else - x->x_sep[0] = '/'; - } - else if(IS_A_FLOAT(av, 0)) - { - int i; - float f=fabs(av->a_w.w_float); - - while(f >= 10.0) - f *= 0.1; - i = (int)f; - x->x_sep[0] = (char)i + '0'; - } - } - else - x->x_sep[0] = 0; + if(ac > 0) + { + if(IS_A_SYMBOL(av, 0)) + { + char *name=av->a_w.w_symbol->s_name; + + if(strlen(name) == 1) + x->x_sep[0] = name[0]; + else if(!strcmp(name, "backslash")) + x->x_sep[0] = '\\'; + else if(!strcmp(name, "slash")) + x->x_sep[0] = '/'; + else if(!strcmp(name, "blank")) + x->x_sep[0] = ' '; + else if(!strcmp(name, "space")) + x->x_sep[0] = ' '; + else if(!strcmp(name, "dollar")) + x->x_sep[0] = '$'; + else if(!strcmp(name, "comma")) + x->x_sep[0] = ','; + else if(!strcmp(name, "semi")) + x->x_sep[0] = ';'; + else if(!strcmp(name, "leftbrace")) + x->x_sep[0] = '{'; + else if(!strcmp(name, "rightbrace")) + x->x_sep[0] = '}'; + else + x->x_sep[0] = '/'; + } + else if(IS_A_FLOAT(av, 0)) + { + t_int i=atom_getintarg(0, ac, av); + + x->x_sep[0] = (char)i + '0';/* you can set any separator-char by setting a number between -32 ... 223 */ + } + } + else + x->x_sep[0] = 0; } static void splitfilename_symbol(t_splitfilename *x, t_symbol *s) { - int len = strlen(s->s_name); - - if(len) - { - if(x->x_sep[0] != 0) - { - char *str_path = (char *)getbytes(len*sizeof(char)); - char *str_file = (char *)getbytes(len*sizeof(char)); - char *cpp, *cpf; - - strcpy(str_path, s->s_name); - strcpy(str_file, s->s_name); - cpp = strrchr(str_path, x->x_sep[0]); - cpf = strrchr(str_file, x->x_sep[0]); - if(!cpp) /* JMZ: 20050701 */ - { - outlet_symbol(x->x_outfile, gensym(str_file)); - outlet_symbol(x->x_outpath, &s_); - } - else if (!cpf) /* JMZ: 20050701 */ - { - outlet_symbol(x->x_outfile, &s_); - outlet_symbol(x->x_outpath, gensym(str_path)); - } - else if((cpp - str_path) < 0) /* JMZ:removed typecast (char*) to (int); this is not portable */ - { - outlet_symbol(x->x_outfile, gensym(str_file)); - outlet_symbol(x->x_outpath, &s_); - } - else if((cpp - str_path) >= len) /* JMZ: removed typecast (char*) to (int) */ - { - outlet_symbol(x->x_outfile, &s_); - outlet_symbol(x->x_outpath, gensym(str_path)); - } - else - { - *cpp = 0; - cpf++; - outlet_symbol(x->x_outfile, gensym(cpf)); - outlet_symbol(x->x_outpath, gensym(str_path)); - } - freebytes(str_file, len*sizeof(char)); - freebytes(str_path, len*sizeof(char)); - } - else - { - outlet_symbol(x->x_outfile, &s_); - outlet_symbol(x->x_outpath, s); - } - } + t_int length = strlen(s->s_name); + + if(length) + { + if(x->x_sep[0]) + { + char *sep_ptr=x->x_mem; + + if(length > x->x_size) + { + x->x_mem = (char *)resizebytes(x->x_mem, x->x_size*sizeof(char), (length+100)*sizeof(char)); + x->x_size = length + 100; + } + strcpy(x->x_mem, s->s_name); + sep_ptr = strrchr(x->x_mem, x->x_sep[0]);/* points to the leftest separator-char-index of string */ + if((!sep_ptr) || ((sep_ptr - x->x_mem) < 0) || ((sep_ptr - x->x_mem) >= length)) + { /* JMZ: 20050701 : removed typecast (char*) to (int); this is not portable */ + outlet_symbol(x->x_outfile, &s_); + outlet_symbol(x->x_outpath, gensym(x->x_mem)); + } + else + { + *sep_ptr = 0; + sep_ptr++; + outlet_symbol(x->x_outfile, gensym(sep_ptr)); + outlet_symbol(x->x_outpath, gensym(x->x_mem)); + } + } + else + { + outlet_symbol(x->x_outfile, &s_); + outlet_symbol(x->x_outpath, s); + } + } +} + +static void splitfilename_free(t_splitfilename *x) +{ + freebytes(x->x_mem, x->x_size*sizeof(char)); } static void *splitfilename_new(t_symbol *s, int ac, t_atom *av) { - t_splitfilename *x = (t_splitfilename *)pd_new(splitfilename_class); - - x->x_sep[0] = 0; - x->x_sep[1] = 0; - if(ac == 0) - x->x_sep[0] = '/'; - else - splitfilename_separator(x, s, ac, av); - x->x_outpath = outlet_new(&x->x_obj, &s_symbol); - x->x_outfile = outlet_new(&x->x_obj, &s_symbol); - return (x); + t_splitfilename *x = (t_splitfilename *)pd_new(splitfilename_class); + + x->x_sep[0] = 0; + x->x_sep[1] = 0; + if(ac == 0) + x->x_sep[0] = '/'; + else + splitfilename_separator(x, s, ac, av); + x->x_size = 400; + x->x_mem = (char *)getbytes(x->x_size*sizeof(char)); + x->x_outpath = (t_outlet *)outlet_new(&x->x_obj, &s_symbol); + x->x_outfile = (t_outlet *)outlet_new(&x->x_obj, &s_symbol); + return (x); } void splitfilename_setup(void) { - splitfilename_class = class_new(gensym("splitfilename"), (t_newmethod)splitfilename_new, - 0, sizeof(t_splitfilename), 0, A_GIMME, 0); - class_addsymbol(splitfilename_class, splitfilename_symbol); - class_addmethod(splitfilename_class, (t_method)splitfilename_separator, gensym("separator"), A_GIMME, 0); - class_addmethod(splitfilename_class, (t_method)splitfilename_separator, gensym("sep"), A_GIMME, 0); - class_sethelpsymbol(splitfilename_class, gensym("iemhelp/help-splitfilename")); + splitfilename_class = class_new(gensym("splitfilename"), (t_newmethod)splitfilename_new, + 0, sizeof(t_splitfilename), 0, A_GIMME, 0); + class_addsymbol(splitfilename_class, splitfilename_symbol); + class_addmethod(splitfilename_class, (t_method)splitfilename_separator, gensym("separator"), A_GIMME, 0); + class_addmethod(splitfilename_class, (t_method)splitfilename_separator, gensym("sep"), A_GIMME, 0); + class_sethelpsymbol(splitfilename_class, gensym("iemhelp/help-splitfilename")); } -- cgit v1.2.1