Forums > Dev

string attribute

January 2, 2011 | 12:34 am

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


January 2, 2011 | 12:10 pm

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..:)


January 3, 2011 | 5:21 pm

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


January 3, 2011 | 6:27 pm

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;
}

January 3, 2011 | 6:54 pm

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


January 3, 2011 | 9:56 pm

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


January 3, 2011 | 10:32 pm

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

hope to come with a ready code-sniplet soon

klaus


January 3, 2011 | 11:57 pm

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;
}

January 4, 2011 | 8:06 am

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


January 7, 2011 | 3:54 pm

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


Viewing 10 posts - 1 through 10 (of 10 total)