aboutsummaryrefslogtreecommitdiff
path: root/src/fwriteln.c
blob: 45e45ea65dcf64086b2f1fabf6f865b156b385ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257

/******************************************************
 *
 * zexy - implementation file
 *
 * copyleft (c) Franz Zotter
 *
 *   2105:forum::für::umläute:2007
 *
 *   institute of electronic music and acoustics (iem)
 *
 ******************************************************
 *
 * license: GNU General Public License v.2
 *
 ******************************************************/

#include "zexy.h"

#ifdef __WIN32__
# define snprintf _snprintf
# include <stdlib.h>
#endif

#include <stdio.h>
#include <string.h>

/* fwriteln: writes messages continuously into a file that
 * doesn't necessarily need to fit into the RAM of your system
 *
 * Franz Zotter zotter@iem.at, 2007
 * Institute of Electronic Music and Acoustics
 *
 * parts of this externals were copied from Iohannes zmoelnig's 
 * iemmatrix
 */

static t_class *fwriteln_class;

typedef struct fwriteln
{
   t_object x_ob;
   FILE *x_file;
   char *x_filename;
   char *x_textbuf;
   char linebreak_chr[3];
   char format_string_afloats[10];
} t_fwriteln;


static void fwriteln_close (t_fwriteln *x)
{
   if(x->x_file) 
      fclose(x->x_file);
   x->x_file=0;
   if(x->x_filename)
      free(x->x_filename);
   x->x_filename=0;
   if(x->x_textbuf)
      freebytes(x->x_textbuf, MAXPDSTRING + 1);
   x->x_textbuf=0;
}

static void string_copy(const char* const from, char** to)
{
  if ((*to = malloc(strlen(from) + 1))) {
      strcpy(*to, from);
   }
}

static void fwriteln_open (t_fwriteln *x, t_symbol *s, t_symbol*type)
{
   char* filename;

   string_copy(s->s_name, &filename);

   sys_bashfilename (filename, filename);

   fwriteln_close (x);

/*   if(0==type || type!=gensym("cr")) {
     pd_error(x, "unknown type '%s'", (type)?type->s_name:"");
     return;
   }*/

   if (type==gensym("cr"))
      strcpy(x->linebreak_chr,"\n");
   else
      strcpy(x->linebreak_chr,";\n");

   if (!(x->x_file=fopen(filename, "w"))) {
      pd_error(x, "failed to open %128s",filename);
      free(filename);
      return;
   }
   string_copy(filename, &x->x_filename);
   free(filename);
   x->x_textbuf = (char *) getbytes (MAXPDSTRING + 1);
}

static void fwriteln_write (t_fwriteln *x, t_symbol *s, int argc, t_atom *argv)
{
   int length=0;
   char *text=x->x_textbuf;
   if (x->x_file) {
      if ((s!=gensym("list"))||(argv->a_type==A_SYMBOL)) {
         snprintf(text,MAXPDSTRING,"%s ", s->s_name);
         text[MAXPDSTRING-1]=0;
         length=strlen(text);
         if (fwrite(text, length*sizeof(char),1,x->x_file) < 1) {
            pd_error(x, "failed to write %128s",x->x_filename);
            freebytes (text, MAXPDSTRING * sizeof(char));
            fwriteln_close(x);
            return;
         }
      }
      while (argc--)
      {
         switch (argv->a_type) {
            case A_FLOAT:
              snprintf(text,MAXPDSTRING,x->format_string_afloats,
                       atom_getfloat(argv));
              text[MAXPDSTRING-1]=0;
              length=strlen(text);
              if (fwrite(text, length*sizeof(char),1,x->x_file) < 1) {
                pd_error(x, "failed to write %128s",x->x_filename);
                freebytes (text, MAXPDSTRING * sizeof(char));
                fwriteln_close(x);
                return;
              }
              break;
         case A_SYMBOL:
           snprintf(text,MAXPDSTRING,"%s ", atom_getsymbol(argv)->s_name);
           text[MAXPDSTRING-1]=0;
           length=strlen(text);
           if (fwrite(text, length*sizeof(char),1,x->x_file) < 1) {
             pd_error(x, "failed to write %128s",x->x_filename);
             freebytes (text, MAXPDSTRING * sizeof(char));
             fwriteln_close(x);
             return;
           }
           break;
         case A_COMMA:
           snprintf(text,MAXPDSTRING,", ");
           length=strlen(text);
           if (fwrite(text, length*sizeof(char),1,x->x_file) < 1) {
             pd_error(x, "failed to write %128s",x->x_filename);
             freebytes (text, MAXPDSTRING * sizeof(char));
             fwriteln_close(x);
             return;
           }
           break;
         case A_SEMI:
           snprintf(text,MAXPDSTRING,"; ");
           length=strlen(text);
           if (fwrite(text, length*sizeof(char),1,x->x_file) < 1) {
             pd_error(x, "failed to write %128s",x->x_filename);
             freebytes (text, MAXPDSTRING * sizeof(char));
             fwriteln_close(x);
             return;
           }
           break;
         default:
           break;
         }
         argv++;
      }

      snprintf(text,MAXPDSTRING,"%s", x->linebreak_chr);
      length=strlen(text);
      if (fwrite(text, length*sizeof(char),1,x->x_file) < 1) {
         pd_error(x, "failed to write %128s",x->x_filename);
         freebytes (text, MAXPDSTRING * sizeof(char));
         fwriteln_close(x);
         return;
      }
   }
   else {
      pd_error(x, "no file opened for writing");
   }
}
static void fwriteln_free (t_fwriteln *x)
{
   fwriteln_close(x);
}

static void *fwriteln_new(t_symbol *s, int argc, t_atom *argv)
{
   int k;
   int width;
   int precision;
   char float_format[3]="g ";
   char width_str[3]="";
   char precision_str[4]="";
   char prefix[2]="%";
   t_fwriteln *x = (t_fwriteln *)pd_new(fwriteln_class);
   x->x_filename=0;
   x->x_file=0;
   x->x_textbuf=0;
   for (k=0; k<argc; k++) {
      if (atom_getsymbol(&argv[k])==gensym("p")) {
         if ((k+1>=argc)||(argv[k+1].a_type!=A_FLOAT)) {
            post("fwriteln: no value given for precision!");
         }
         else {
            precision=atom_getint(&argv[++k]);
            precision=(precision<0)?0:precision;
            precision=(precision>30)?30:precision;
            snprintf(precision_str,4,".%d",precision);
         }
      }
      else if (atom_getsymbol(&argv[k])==gensym("w")) {
         if ((k+1>=argc)||(argv[k+1].a_type!=A_FLOAT)) {
            post("fwriteln: no value given for width!");
         }
         else {
            width=atom_getint(&argv[++k]);
            width=(width<1)?1:width;
            width=(width>40)?40:width;
            snprintf(width_str,3,"%d",width);
         }
      }
      else if (atom_getsymbol(&argv[k])==gensym("g")) {
            float_format[0]='g';
      }
      else if (atom_getsymbol(&argv[k])==gensym("f")) {
            float_format[0]='f';
      }
      else if (atom_getsymbol(&argv[k])==gensym("e")) {
            float_format[0]='e';
      }
      else if (atom_getsymbol(&argv[k])==gensym("-")) {
	 strcpy(prefix,"%-");
      }
      else if (atom_getsymbol(&argv[k])==gensym("+")) {
	 strcpy(prefix,"%+");
      }
   }
   x->format_string_afloats[0]='\0';
   strncat(x->format_string_afloats,prefix,2);
   strncat(x->format_string_afloats,width_str,2);
   strncat(x->format_string_afloats,precision_str,3);
   strncat(x->format_string_afloats,float_format,2);
   return (void *)x;
}

void fwriteln_setup(void)
{
   fwriteln_class = class_new(gensym("fwriteln"), (t_newmethod)fwriteln_new, 
         (t_method) fwriteln_free, sizeof(t_fwriteln), CLASS_DEFAULT, A_GIMME, 0);
   class_addmethod(fwriteln_class, (t_method)fwriteln_open, gensym("open"), A_SYMBOL, A_DEFSYM, 0);
   class_addmethod(fwriteln_class, (t_method)fwriteln_close, gensym("close"), A_NULL, 0);
   class_addanything(fwriteln_class, (t_method)fwriteln_write);

   zexy_register("fwriteln");
}