string attribute

Jan 2, 2011 at 12:34am

string attribute

i want to declare a string attribute in the way annotation or hint is defined
(any string including blanks can be entered by the user).

which type should i use – i guess t_symbol ??
which CLASS_ATTR is to be used here ?

thanks a lot
klaus

#54190
Jan 2, 2011 at 7:40am

Hello klaus filip,

CLASS_ATTR_SYM (c, "something", ATTR_FLAGS_NONE, t_myobject, toto) ;

with :

t_symbol *toto ;

in the t_myobject structure.

HTH.

#195050
Jan 2, 2011 at 12:10pm

i tried this before, but like this, i loose all values after a blank.
a typed “a b c” turns into “a” in the inspector.
maybe i miss another thing?
(its hard but fun learning C and the MaxAPI at the same time..:)

#195051
Jan 2, 2011 at 5:19pm

Hi,

you are right ; but if you type between ” ” (f.e : “il faut pendre toto”) that works ; but it may have another (best) way ; anybody have a clue ? CLASS_ATTR_SYM_VARSIZE ???

#195052
Jan 3, 2011 at 4:48pm

Hi,

i did few investigations with custom getters/setters and it seems that if you type “toto est un idiot” you get 4 atoms … so maybe could be collapsed in a string …

#195053
Jan 3, 2011 at 5:21pm

thanks nicolas!
actually i can live with the parenthesis of course.
i was just wondering how they are avoided at annotation.
currently i am more trying to parse these items into an atomarray, which is still blowing my head…
klaus

#195054
Jan 3, 2011 at 6:27pm

i am stuck here:
i do get my attributes into a setter.
(i am expecting any types, int, float symbol, so i defined the attribute as t_atom)
in the end, i want an array filled with the arguments of the attribute.
here is my clumsy try…
thanks for having a look
klaus

t_atom myattr;

t_max_err myobj_setattr_myattr(t_myobj *x, void *attr, long ac, t_atom *av)
{
	long i;
	t_atom *ap;
	t_atom args[16];

	post("there are %ld arguments",ac);

	for (i = 0, ap = av; i < ac; i++, ap++) {
		postatom(ap);
		//all fine until here
		args[i] = ap;
		//gives unmatched type in compiler
		post("na? %s",atom_getsym(ap));
		//gives nonesense in the max window

	}
	atom_setsym(&x->myattr, atom_getsym(ap));
	//just a try to set the attribute back into the inspector, since it turns "undefined" with this setter. 

	return MAX_ERR_NONE;
}
#195055
Jan 3, 2011 at 6:54pm

Hi Klaus.

Your problem here is that you’re assigning an atom pointer (ap) to an atom variable (args[i]). This is what your compiler is trying to tell you.

What you want to do is
args[i] = *ap

btw, you can copy all your atoms in one blow with
sysmem_copyptr(args, ap, ac * sizeof(t_atom));
provided that you have allocated enough memory (a check for ac being < 16, or a dynamic allocation based upon ac would be definitely recommended!)

best
aa

#195056
Jan 3, 2011 at 6:57pm

Hi,

why not just use CLASS_ATTR_ATOM_ARRAY ?

#195057
Jan 3, 2011 at 9:56pm

thanks andrea,
using the pointer definitely helped.))
strangely the one-blow did not work out for me.
(crashes max)

thanks nicolas, but CLASS_ATTR_ATOM_ARRAY produces a defined amount of “undefined” in the inspector window, i don’t want to predefine this amount…

i am still stuck, how to parse my atoms into a string.
with this i could set-back the attribute:

atom_setsym(&x->myattr, “string of all my agrguments”);

sprintf(str, “%s”, ,atom_getsym(ap))in the for-loop above does not give the expected result.

klaus

#195058
Jan 3, 2011 at 10:32pm

oups, got it:
post(“currentarg %s”, atom_getsym(ap)->s_name);
works

hope to come with a ready code-sniplet soon

klaus

#195059
Jan 3, 2011 at 11:57pm

this works
thanks a lot
klaus

t_atom myattr;
t_atom myatomarray[1024];

CLASS_ATTR_ATOM(c, "myattr", ATTR_FLAGS_NONE, t_myobj, myattr);
CLASS_ATTR_ACCESSORS(c, "myattr", (method)NULL, (method)myobj_getattr_myattr);

t_max_err myobj_getattr_myattr(t_myobj *x, void *attr, long ac, t_atom *av)
{
	long i;
	t_atom *ap;
	char str[16] = "";
	char allstr[1024] = "";
	for (i = 0, ap = av; i < ac; i++, ap++) {
		x->myatomarray[i] = *ap;
		switch (atom_gettype(ap)) {
            case A_LONG:
				sprintf(str, "%ld ", atom_getlong(ap));
				break;
			case A_FLOAT:
				sprintf(str, "%f ", atom_getfloat(ap));
				break;
            case A_SYM:
				sprintf(str, "%s ", atom_getsym(ap)->s_name);
				break;
            default:
				post("%ld: unknown atom type (%ld)", i+1, atom_gettype(ap));
				break;
		}
		strcat(allstr, str);
	}
	post("all %s", allstr);
	atom_setsym(&x->myattr, gensym(allstr));
	return MAX_ERR_NONE;
}
#195060
Jan 4, 2011 at 8:06am

Hi Klaus
of course the one-blow solution won’t work… it should be
sysmem_copyptr(args, av, ac * sizeof(t_atom));
- av, not ap! in fact, since you copy all your atoms at once you don’t need the loop or ap anymore!
but if you want to post your atoms one after the other, or strcat them together, you need the loop.

one more observation: in some cases, your loop might not do what you want.
e.g., it won’t take into account multiple spaces into your input string; it won’t behave as expected with floating point numbers (you might find that 0.123 changed into 0.129999 or something like that); it won’t deal with leading 0s in numbers (0123 changed into 123)…
In fact, I suspect that the only solution for having a string with spaces is to quote it, and treat it as a single symbol.

best
aa

#195061
Jan 4, 2011 at 9:23am

Hello klaus filip,

may be you can try with :

t_atom   data[25] ;
long     data_size ;

CLASS_ATTR_ATOM_VARSIZE (c, "data", ATTR_FLAGS_NONE, t_myobj, data, data_size, 25) ;

doesn’t deal with string but you can have a cosmetic way for filling an array of atoms ;-)

HTH.

Attachments:
  1. flop.c
#195062
Jan 7, 2011 at 3:54pm

thanks nicolas,
this looks very very good to me!
klaus

#195063

You must be logged in to reply to this topic.