~ubuntu-branches/ubuntu/natty/pd-zexy/natty

« back to all changes in this revision

Viewing changes to src/fwriteln.c

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard, IOhannes m zmölnig, Jonas Smedegaard
  • Date: 2010-08-20 12:17:41 UTC
  • mfrom: (2.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100820121741-4kxozn8b9rhee9fr
Tags: 2.2.3-1
* New upstream version

[ IOhannes m zmölnig ]
* Adopt package, on behalf of Multimedia Team.
  Closes: #546964
* Simply debian/rules with CDBS, and don't unconditionally strip
  binaries.
  Closes: #437763
* Install into /usr/lib/pd/extra/zexy/. Document usage in REAME.Debian
  and warn about change in NEWS.
* git'ify package. Add Vcs-* stanzas to control file.
* Use dpkg source format 3.0 (quilt). Drop build-dependency on quilt.

[ Jonas Smedegaard ]
* Enable CDBS copyright-check routine.
* Add copyright and licensing header to debian/rules.
* Add myself as uploader.
* Rewrite debian/copyright using rev. 135 of draft DEP5 format.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/******************************************************
 
3
 *
 
4
 * zexy - implementation file
 
5
 *
 
6
 * copyleft (c) Franz Zotter
 
7
 *
 
8
 *   2105:forum::f�r::uml�ute:2007
 
9
 *
 
10
 *   institute of electronic music and acoustics (iem)
 
11
 *
 
12
 ******************************************************
 
13
 *
 
14
 * license: GNU General Public License v.2
 
15
 *
 
16
 ******************************************************/
 
17
 
 
18
#include "zexy.h"
 
19
 
 
20
#ifdef __WIN32__
 
21
# define snprintf _snprintf
 
22
#endif
 
23
 
 
24
#include <stdlib.h>
 
25
#include <stdio.h>
 
26
#include <string.h>
 
27
 
 
28
/* fwriteln: writes messages continuously into a file that
 
29
 * doesn't necessarily need to fit into the RAM of your system
 
30
 *
 
31
 * Franz Zotter zotter@iem.at, 2007
 
32
 * Institute of Electronic Music and Acoustics
 
33
 *
 
34
 * parts of this externals were copied from Iohannes zmoelnig's 
 
35
 * iemmatrix
 
36
 */
 
37
 
 
38
static t_class *fwriteln_class;
 
39
 
 
40
typedef struct fwriteln
 
41
{
 
42
   t_object x_ob;
 
43
   FILE *x_file;
 
44
   char *x_filename;
 
45
   char *x_textbuf;
 
46
   char linebreak_chr[3];
 
47
   char format_string_afloats[10];
 
48
} t_fwriteln;
 
49
 
 
50
 
 
51
static void fwriteln_close (t_fwriteln *x)
 
52
{
 
53
   if(x->x_file) 
 
54
      fclose(x->x_file);
 
55
   x->x_file=0;
 
56
   if(x->x_filename)
 
57
      free(x->x_filename);
 
58
   x->x_filename=0;
 
59
   if(x->x_textbuf)
 
60
      freebytes(x->x_textbuf, MAXPDSTRING + 1);
 
61
   x->x_textbuf=0;
 
62
}
 
63
 
 
64
static void string_copy(const char* const from, char** to)
 
65
{
 
66
  if ((*to = malloc(strlen(from) + 1))) {
 
67
      strcpy(*to, from);
 
68
   }
 
69
}
 
70
 
 
71
static void fwriteln_open (t_fwriteln *x, t_symbol *s, t_symbol*type)
 
72
{
 
73
   char* filename;
 
74
 
 
75
   string_copy(s->s_name, &filename);
 
76
 
 
77
   sys_bashfilename (filename, filename);
 
78
 
 
79
   fwriteln_close (x);
 
80
 
 
81
/*   if(0==type || type!=gensym("cr")) {
 
82
     pd_error(x, "unknown type '%s'", (type)?type->s_name:"");
 
83
     return;
 
84
   }*/
 
85
 
 
86
   if (type==gensym("cr"))
 
87
      strcpy(x->linebreak_chr,"\n");
 
88
   else
 
89
      strcpy(x->linebreak_chr,";\n");
 
90
 
 
91
   if (!(x->x_file=fopen(filename, "w"))) {
 
92
      pd_error(x, "failed to open %128s",filename);
 
93
      free(filename);
 
94
      return;
 
95
   }
 
96
   string_copy(filename, &x->x_filename);
 
97
   free(filename);
 
98
   x->x_textbuf = (char *) getbytes (MAXPDSTRING + 1);
 
99
}
 
100
 
 
101
static void fwriteln_write (t_fwriteln *x, t_symbol *s, int argc, t_atom *argv)
 
102
{
 
103
   int length=0;
 
104
   char *text=x->x_textbuf;
 
105
   if (x->x_file) {
 
106
      if ((s!=gensym("list"))||(argv->a_type==A_SYMBOL)) {
 
107
         snprintf(text,MAXPDSTRING,"%s ", s->s_name);
 
108
         text[MAXPDSTRING-1]=0;
 
109
         length=strlen(text);
 
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));
 
113
            fwriteln_close(x);
 
114
            return;
 
115
         }
 
116
      }
 
117
      while (argc--)
 
118
      {
 
119
         switch (argv->a_type) {
 
120
            case A_FLOAT:
 
121
              snprintf(text,MAXPDSTRING,x->format_string_afloats,
 
122
                       atom_getfloat(argv));
 
123
              text[MAXPDSTRING-1]=0;
 
124
              length=strlen(text);
 
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));
 
128
                fwriteln_close(x);
 
129
                return;
 
130
              }
 
131
              break;
 
132
         case A_SYMBOL:
 
133
           snprintf(text,MAXPDSTRING,"%s ", atom_getsymbol(argv)->s_name);
 
134
           text[MAXPDSTRING-1]=0;
 
135
           length=strlen(text);
 
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));
 
139
             fwriteln_close(x);
 
140
             return;
 
141
           }
 
142
           break;
 
143
         case A_COMMA:
 
144
           snprintf(text,MAXPDSTRING,", ");
 
145
           length=strlen(text);
 
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));
 
149
             fwriteln_close(x);
 
150
             return;
 
151
           }
 
152
           break;
 
153
         case A_SEMI:
 
154
           snprintf(text,MAXPDSTRING,"; ");
 
155
           length=strlen(text);
 
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));
 
159
             fwriteln_close(x);
 
160
             return;
 
161
           }
 
162
           break;
 
163
         default:
 
164
           break;
 
165
         }
 
166
         argv++;
 
167
      }
 
168
 
 
169
      snprintf(text,MAXPDSTRING,"%s", x->linebreak_chr);
 
170
      length=strlen(text);
 
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));
 
174
         fwriteln_close(x);
 
175
         return;
 
176
      }
 
177
   }
 
178
   else {
 
179
      pd_error(x, "no file opened for writing");
 
180
   }
 
181
}
 
182
static void fwriteln_free (t_fwriteln *x)
 
183
{
 
184
   fwriteln_close(x);
 
185
}
 
186
 
 
187
static void *fwriteln_new(t_symbol *s, int argc, t_atom *argv)
 
188
{
 
189
   int k;
 
190
   int width;
 
191
   int precision;
 
192
   char float_format[3]="g ";
 
193
   char width_str[3]="";
 
194
   char precision_str[4]="";
 
195
   char prefix[3]="%";
 
196
   t_fwriteln *x = (t_fwriteln *)pd_new(fwriteln_class);
 
197
   x->x_filename=0;
 
198
   x->x_file=0;
 
199
   x->x_textbuf=0;
 
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!");
 
204
         }
 
205
         else {
 
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);
 
210
         }
 
211
      }
 
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!");
 
215
         }
 
216
         else {
 
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);
 
221
         }
 
222
      }
 
223
      else if (atom_getsymbol(&argv[k])==gensym("g")) {
 
224
            float_format[0]='g';
 
225
      }
 
226
      else if (atom_getsymbol(&argv[k])==gensym("f")) {
 
227
            float_format[0]='f';
 
228
      }
 
229
      else if (atom_getsymbol(&argv[k])==gensym("e")) {
 
230
            float_format[0]='e';
 
231
      }
 
232
      else if (atom_getsymbol(&argv[k])==gensym("-")) {
 
233
         strcpy(prefix,"%-");
 
234
      }
 
235
      else if (atom_getsymbol(&argv[k])==gensym("+")) {
 
236
         strcpy(prefix,"%+");
 
237
      }
 
238
   }
 
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);
 
244
   return (void *)x;
 
245
}
 
246
 
 
247
void fwriteln_setup(void)
 
248
{
 
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);
 
254
 
 
255
   zexy_register("fwriteln");
 
256
}
 
257