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

« back to all changes in this revision

Viewing changes to src/freadln.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 <stdio.h>
 
25
#include <string.h>
 
26
 
 
27
#include <stdlib.h>
 
28
 
 
29
#ifdef __WIN32__
 
30
# include <io.h>
 
31
#else
 
32
# include <unistd.h>
 
33
#endif
 
34
 
 
35
#define MIN_FREADLN_LENGTH 10
 
36
 
 
37
/* freadln: reads messages continuously from the lines of 
 
38
 * a file that doesn't necessarily need to fit 
 
39
 * into the RAM of your system
 
40
 *
 
41
 * Franz Zotter zotter@iem.at, 2007
 
42
 * Institute of Electronic Music and Acoustics
 
43
 *
 
44
 */
 
45
 
 
46
static t_class *freadln_class;
 
47
 
 
48
typedef struct freadln
 
49
{
 
50
   t_object x_ob;
 
51
   FILE *x_file;
 
52
   char *x_filename;
 
53
   char *x_textbuf;
 
54
   int  x_textbuf_length;
 
55
   t_outlet *x_message_outlet;
 
56
   t_outlet *x_readybang_outlet;
 
57
 
 
58
   char linebreak_chr[3];
 
59
 
 
60
   t_canvas *x_canvas;
 
61
} t_freadln;
 
62
 
 
63
 
 
64
static void freadln_close (t_freadln *x)
 
65
{
 
66
   if(x->x_file) 
 
67
      fclose(x->x_file);
 
68
   x->x_file=0;
 
69
   if(x->x_filename)
 
70
      freebytes(x->x_filename, sizeof(char)*MAXPDSTRING);
 
71
   x->x_filename=0;
 
72
   if(x->x_textbuf)
 
73
      freebytes(x->x_textbuf, sizeof(char)*x->x_textbuf_length);
 
74
   x->x_textbuf=0;
 
75
   x->x_textbuf_length=0;
 
76
}
 
77
 
 
78
static void freadln_open (t_freadln *x, t_symbol *s, t_symbol*type)
 
79
{
 
80
   char filenamebuf[MAXPDSTRING], *filenamebufptr;
 
81
   char*dirname=canvas_getdir(x->x_canvas)->s_name;
 
82
   int fd, len;
 
83
 
 
84
   freadln_close(x);
 
85
 
 
86
/*
 
87
   if(type!=gensym("cr")) {
 
88
     pd_error(x, "currently only 'cr' type files are implemented!");
 
89
     return;
 
90
   }
 
91
*/
 
92
   if (type==gensym("cr"))
 
93
      strcpy(x->linebreak_chr,"\n");
 
94
   else
 
95
      strcpy(x->linebreak_chr,";\n");
 
96
      
 
97
 
 
98
   /* directory, filename, extension, dirresult, nameresult, unsigned int size, int bin */
 
99
   if ((fd=open_via_path(dirname,
 
100
               s->s_name,"", filenamebuf, &filenamebufptr, MAXPDSTRING,0)) < 0 ) {
 
101
      pd_error(x, "%s: failed to open %s", s->s_name, filenamebuf);
 
102
      return;
 
103
   }
 
104
   close(fd);
 
105
   len=strlen(filenamebuf);
 
106
   if (!(x->x_filename=(char*)getbytes(sizeof(char)*(len+strlen(s->s_name)+2)))) {
 
107
      pd_error(x, "out of memory");
 
108
      freadln_close(x);
 
109
      return;
 
110
   }
 
111
   strcpy(x->x_filename,filenamebuf);
 
112
   strcpy(x->x_filename+len,"/");
 
113
   strcpy(x->x_filename+len+1,s->s_name);
 
114
   if (!(x->x_file=fopen(x->x_filename, "r"))) {
 
115
      pd_error("freadln: failed to open %128s",filenamebuf);
 
116
      return;
 
117
   }
 
118
   if (!(x->x_textbuf = (char *) getbytes (MIN_FREADLN_LENGTH * sizeof(char)))) {
 
119
      pd_error(x, "out of memory");
 
120
      freadln_close(x);
 
121
      return;
 
122
   }
 
123
   x->x_textbuf_length=MIN_FREADLN_LENGTH;
 
124
}
 
125
 
 
126
static int enlarge_cstr_if_required(const char **c_str, int *len, const int desired_min_length) 
 
127
{
 
128
   if ((!(*c_str))||*len==0) {
 
129
      *c_str = (char*) calloc (1,sizeof(char));
 
130
      return 1;
 
131
   }
 
132
   if (len[0]<desired_min_length) {
 
133
      do {
 
134
         len[0]<<=1;
 
135
      } while ((len[0]<desired_min_length)&&(len[0]!=0));
 
136
      freebytes((char*)*c_str, sizeof(char)*len[0]);
 
137
      if (!(*c_str=(char*)calloc(len[0],sizeof(char))))
 
138
         len[0]=0;
 
139
   } 
 
140
   return len[0];
 
141
}
 
142
 
 
143
static int cstr_char_pos(const char *c_str, const char c) 
 
144
{
 
145
   int cnt=1;
 
146
   if (c_str) {
 
147
      do {
 
148
         if (*c_str==c)
 
149
            return cnt;
 
150
         cnt++;
 
151
      } while (*c_str++!='\0');
 
152
   }
 
153
   return -1;
 
154
}
 
155
 
 
156
static void freadln_done(t_freadln*x)
 
157
{
 
158
  outlet_bang(x->x_readybang_outlet);
 
159
}
 
160
 
 
161
static void freadln_readline (t_freadln *x)
 
162
{
 
163
   int min_length=(x->x_textbuf_length < 1)?1:x->x_textbuf_length;
 
164
   int linebreak_pos=0;
 
165
   int items_read;
 
166
   t_binbuf *bbuf;
 
167
   t_atom *abuf;
 
168
   int abuf_length;
 
169
   int rewind_after;
 
170
 
 
171
   if (!x->x_file) {
 
172
     pd_error(x, "no file opened for reading");
 
173
     freadln_done(x);
 
174
     return;
 
175
   }
 
176
   
 
177
   do {
 
178
     if (linebreak_pos==-1) {
 
179
       min_length<<=1;
 
180
       fseek(x->x_file,-(long)(x->x_textbuf_length),SEEK_CUR);
 
181
     }
 
182
     if (!enlarge_cstr_if_required((const char**) &x->x_textbuf, &x->x_textbuf_length, min_length)) {
 
183
       pd_error(x, "out of memory");
 
184
       x->x_textbuf_length=0;
 
185
       freadln_close(x);
 
186
       freadln_done(x);
 
187
       return;
 
188
     }
 
189
     if (!(items_read=fread(x->x_textbuf,sizeof(char),x->x_textbuf_length,x->x_file))) {
 
190
       freadln_close(x);
 
191
       freadln_done(x);
 
192
       return;
 
193
     }
 
194
     x->x_textbuf[x->x_textbuf_length-1]=0;
 
195
   } while (((linebreak_pos=cstr_char_pos(x->x_textbuf,x->linebreak_chr[0]))==-1) && 
 
196
            !(items_read < x->x_textbuf_length));
 
197
   
 
198
   if (linebreak_pos-1  < items_read - strlen(x->linebreak_chr)) {
 
199
     rewind_after=items_read-linebreak_pos;
 
200
     fseek(x->x_file,-(long)(rewind_after),SEEK_CUR);
 
201
   }
 
202
   if (linebreak_pos==-1) 
 
203
     linebreak_pos=items_read;
 
204
   x->x_textbuf[linebreak_pos-1]='\0';
 
205
   if (!(bbuf=binbuf_new())) {
 
206
     pd_error(x, "out of memory");
 
207
     freadln_close(x);
 
208
     freadln_done(x);
 
209
     return;
 
210
   }
 
211
   binbuf_text(bbuf, x->x_textbuf, linebreak_pos-1);
 
212
   abuf = binbuf_getvec(bbuf);
 
213
   abuf_length = binbuf_getnatom(bbuf);
 
214
   if (abuf_length>0) {
 
215
     if (abuf->a_type==A_SYMBOL) {
 
216
       outlet_anything(x->x_message_outlet, atom_getsymbol(abuf), abuf_length-1, abuf+1);
 
217
     }
 
218
     else {
 
219
       outlet_list(x->x_message_outlet, gensym("list"), abuf_length, abuf);
 
220
     }
 
221
   }
 
222
   else {
 
223
     outlet_list(x->x_message_outlet, atom_getsymbol(abuf), 0, abuf);
 
224
   }
 
225
   /* NOTE: the following line might be a problem in recursions
 
226
    * and could be performed before to outlet_* as well,
 
227
    * but(!) atom buffer abuf must be copied if doing so.
 
228
    */
 
229
   binbuf_free(bbuf);
 
230
}
 
231
static void freadln_free (t_freadln *x)
 
232
{
 
233
   freadln_close(x);
 
234
   outlet_free (x->x_message_outlet);
 
235
   outlet_free (x->x_readybang_outlet);
 
236
}
 
237
 
 
238
static void *freadln_new(void)
 
239
{
 
240
   t_freadln *x = (t_freadln *)pd_new(freadln_class);
 
241
   x->x_message_outlet = outlet_new(&x->x_ob, &s_list);
 
242
   x->x_readybang_outlet = outlet_new(&x->x_ob, &s_bang);
 
243
   x->x_filename=0;
 
244
   x->x_file=0;
 
245
   x->x_textbuf=0;
 
246
   x->x_canvas = canvas_getcurrent();
 
247
   return (void *)x;
 
248
}
 
249
 
 
250
void freadln_setup(void)
 
251
{
 
252
   freadln_class = class_new(gensym("freadln"), (t_newmethod)freadln_new, 
 
253
         (t_method) freadln_free, sizeof(t_freadln), 0, 0);
 
254
   class_addmethod(freadln_class, (t_method)freadln_open, gensym("open"), A_SYMBOL, A_DEFSYM, 0);
 
255
   class_addmethod(freadln_class, (t_method)freadln_close, gensym("close"), A_NULL, 0);
 
256
   class_addbang(freadln_class, (t_method)freadln_readline);
 
257
 
 
258
   zexy_register("freadln");
 
259
}
 
260