2
/******************************************************
4
* zexy - implementation file
6
* copyleft (c) Franz Zotter
8
* 2105:forum::f�r::uml�ute:2007
10
* institute of electronic music and acoustics (iem)
12
******************************************************
14
* license: GNU General Public License v.2
16
******************************************************/
21
# define snprintf _snprintf
28
/* fwriteln: writes messages continuously into a file that
29
* doesn't necessarily need to fit into the RAM of your system
31
* Franz Zotter zotter@iem.at, 2007
32
* Institute of Electronic Music and Acoustics
34
* parts of this externals were copied from Iohannes zmoelnig's
38
static t_class *fwriteln_class;
40
typedef struct fwriteln
46
char linebreak_chr[3];
47
char format_string_afloats[10];
51
static void fwriteln_close (t_fwriteln *x)
60
freebytes(x->x_textbuf, MAXPDSTRING + 1);
64
static void string_copy(const char* const from, char** to)
66
if ((*to = malloc(strlen(from) + 1))) {
71
static void fwriteln_open (t_fwriteln *x, t_symbol *s, t_symbol*type)
75
string_copy(s->s_name, &filename);
77
sys_bashfilename (filename, filename);
81
/* if(0==type || type!=gensym("cr")) {
82
pd_error(x, "unknown type '%s'", (type)?type->s_name:"");
86
if (type==gensym("cr"))
87
strcpy(x->linebreak_chr,"\n");
89
strcpy(x->linebreak_chr,";\n");
91
if (!(x->x_file=fopen(filename, "w"))) {
92
pd_error(x, "failed to open %128s",filename);
96
string_copy(filename, &x->x_filename);
98
x->x_textbuf = (char *) getbytes (MAXPDSTRING + 1);
101
static void fwriteln_write (t_fwriteln *x, t_symbol *s, int argc, t_atom *argv)
104
char *text=x->x_textbuf;
106
if ((s!=gensym("list"))||(argv->a_type==A_SYMBOL)) {
107
snprintf(text,MAXPDSTRING,"%s ", s->s_name);
108
text[MAXPDSTRING-1]=0;
110
if (fwrite(text, length*sizeof(char),1,x->x_file) < 1) {
111
pd_error(x, "failed to write %128s",x->x_filename);
112
freebytes (text, MAXPDSTRING * sizeof(char));
119
switch (argv->a_type) {
121
snprintf(text,MAXPDSTRING,x->format_string_afloats,
122
atom_getfloat(argv));
123
text[MAXPDSTRING-1]=0;
125
if (fwrite(text, length*sizeof(char),1,x->x_file) < 1) {
126
pd_error(x, "failed to write %128s",x->x_filename);
127
freebytes (text, MAXPDSTRING * sizeof(char));
133
snprintf(text,MAXPDSTRING,"%s ", atom_getsymbol(argv)->s_name);
134
text[MAXPDSTRING-1]=0;
136
if (fwrite(text, length*sizeof(char),1,x->x_file) < 1) {
137
pd_error(x, "failed to write %128s",x->x_filename);
138
freebytes (text, MAXPDSTRING * sizeof(char));
144
snprintf(text,MAXPDSTRING,", ");
146
if (fwrite(text, length*sizeof(char),1,x->x_file) < 1) {
147
pd_error(x, "failed to write %128s",x->x_filename);
148
freebytes (text, MAXPDSTRING * sizeof(char));
154
snprintf(text,MAXPDSTRING,"; ");
156
if (fwrite(text, length*sizeof(char),1,x->x_file) < 1) {
157
pd_error(x, "failed to write %128s",x->x_filename);
158
freebytes (text, MAXPDSTRING * sizeof(char));
169
snprintf(text,MAXPDSTRING,"%s", x->linebreak_chr);
171
if (fwrite(text, length*sizeof(char),1,x->x_file) < 1) {
172
pd_error(x, "failed to write %128s",x->x_filename);
173
freebytes (text, MAXPDSTRING * sizeof(char));
179
pd_error(x, "no file opened for writing");
182
static void fwriteln_free (t_fwriteln *x)
187
static void *fwriteln_new(t_symbol *s, int argc, t_atom *argv)
192
char float_format[3]="g ";
193
char width_str[3]="";
194
char precision_str[4]="";
196
t_fwriteln *x = (t_fwriteln *)pd_new(fwriteln_class);
200
for (k=0; k<argc; k++) {
201
if (atom_getsymbol(&argv[k])==gensym("p")) {
202
if ((k+1>=argc)||(argv[k+1].a_type!=A_FLOAT)) {
203
post("fwriteln: no value given for precision!");
206
precision=atom_getint(&argv[++k]);
207
precision=(precision<0)?0:precision;
208
precision=(precision>30)?30:precision;
209
snprintf(precision_str,4,".%d",precision);
212
else if (atom_getsymbol(&argv[k])==gensym("w")) {
213
if ((k+1>=argc)||(argv[k+1].a_type!=A_FLOAT)) {
214
post("fwriteln: no value given for width!");
217
width=atom_getint(&argv[++k]);
218
width=(width<1)?1:width;
219
width=(width>40)?40:width;
220
snprintf(width_str,3,"%d",width);
223
else if (atom_getsymbol(&argv[k])==gensym("g")) {
226
else if (atom_getsymbol(&argv[k])==gensym("f")) {
229
else if (atom_getsymbol(&argv[k])==gensym("e")) {
232
else if (atom_getsymbol(&argv[k])==gensym("-")) {
235
else if (atom_getsymbol(&argv[k])==gensym("+")) {
239
x->format_string_afloats[0]='\0';
240
strncat(x->format_string_afloats,prefix,2);
241
strncat(x->format_string_afloats,width_str,2);
242
strncat(x->format_string_afloats,precision_str,3);
243
strncat(x->format_string_afloats,float_format,2);
247
void fwriteln_setup(void)
249
fwriteln_class = class_new(gensym("fwriteln"), (t_newmethod)fwriteln_new,
250
(t_method) fwriteln_free, sizeof(t_fwriteln), CLASS_DEFAULT, A_GIMME, 0);
251
class_addmethod(fwriteln_class, (t_method)fwriteln_open, gensym("open"), A_SYMBOL, A_DEFSYM, 0);
252
class_addmethod(fwriteln_class, (t_method)fwriteln_close, gensym("close"), A_NULL, 0);
253
class_addanything(fwriteln_class, (t_method)fwriteln_write);
255
zexy_register("fwriteln");