aboutsummaryrefslogtreecommitdiff
path: root/readdir/src/readdir.c
blob: a2bdbb843d700935f1a1acf4030a6f9dec54e82f (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* -*- Mode: C -*- */
/*=============================================================================*\
 * File: readdir.c
 * Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
 * Description: general directory access object
 *
 * Copyright (c) 2003-2009 Bryan Jurish.
 *
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * See file LICENSE for further informations on licensing terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *=============================================================================*/

#include <dirent.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

#include <m_pd.h>

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

/*--------------------------------------------------------------------
 * DEBUG
 *--------------------------------------------------------------------*/
//#define READDIR_DEBUG 1


/*=====================================================================
 * Constants
 *=====================================================================*/
#ifdef READDIR_DEBUG
// error-message buffer
#define EBUFSIZE 256
static char readdir_errbuf[EBUFSIZE];
#endif

/*=====================================================================
 * Structures and Types
 *=====================================================================*/

static char *readdir_banner =
  "\n"
  "readdir: simple directory accessor v" PACKAGE_VERSION " by Bryan Jurish\n"
  "readdir: compiled by " PACKAGE_BUILD_USER " on " PACKAGE_BUILD_DATE ;

static t_class *readdir_class;

typedef struct _readdir
{
  t_object       x_obj;
  DIR           *x_dir;         //-- current directory
  t_symbol      *x_dirname;     //-- current directory name
  //struct dirent *x_dirent;      //-- current entry of current directory
  t_atom         x_eatom;       //-- current output atom (symbol)
  t_outlet      *x_ent_outlet;  //-- entry outlet
  t_outlet      *x_eod_outlet;  //-- end-of-directory outlet
} t_readdir;

/*=====================================================================
 * Constants
 *=====================================================================*/
static t_symbol *sp_none;
static t_symbol *sp_unknown;
static t_symbol *sp_file;
static t_symbol *sp_dir;
static t_symbol *sp_fifo;
static t_symbol *sp_sock;
static t_symbol *sp_chrdev;
static t_symbol *sp_blkdev;

/*--------------------------------------------------------------------
 * close
 */
static void readdir_close(t_readdir *x)
{
  if (!x->x_dir) return;
  if (0 != closedir(x->x_dir)) {
    error("readdir: cannot close %s: %s", x->x_dirname->s_name, strerror(errno));
    return;
  }
  x->x_dir = NULL;
  x->x_dirname = sp_none;
}

/*--------------------------------------------------------------------
 * open DIR
 */
static void readdir_open(t_readdir *x, t_symbol *dirname)
{
#ifdef READDIR_DEBUG
  post("readdir: got message: open %s", dirname->s_name);
#endif

  if (x->x_dir) readdir_close(x);
  if ( !(x->x_dir = opendir(dirname->s_name)) ) {
    error("readdir: cannot open %s: %s", dirname->s_name, strerror(errno));
    return;
  }
  x->x_dirname = dirname;
}

/*--------------------------------------------------------------------
 * next : get next entry
 */
static void readdir_next(t_readdir *x)
{
  t_symbol *sel = sp_unknown;
  struct dirent *result = NULL;
  if ( !x->x_dir || !(result = readdir(x->x_dir)) ) {
    if (errno == EBADF) {
      //-- real error
      error("readdir: cannot read from %s: %s", x->x_dirname->s_name, strerror(errno));
    }
    else {
      //-- end of directory
      outlet_bang(x->x_eod_outlet);
    }
    return;
  }

  //-- get type
  switch (result->d_type)
    {
    case DT_REG:
      sel = sp_file;
      break;

    case DT_DIR:
      sel = sp_dir;
      break;

    case DT_FIFO:
      sel = sp_fifo;
      break;

    case DT_SOCK:
      sel = sp_sock;
      break;

    case DT_CHR:
      sel = sp_chrdev;
      break;

    case DT_BLK:
      sel = sp_blkdev;
      break;

    default:
      sel = sp_unknown;
      break;
    }

  x->x_eatom.a_w.w_symbol = gensym(result->d_name);
  outlet_anything(x->x_ent_outlet, sel, 1, &x->x_eatom);
}

/*--------------------------------------------------------------------
 * rewind
 */
static void readdir_rewind(t_readdir *x)
{
  if (x->x_dir) rewinddir(x->x_dir);
}

/*--------------------------------------------------------------------
 * tell
 */
static void readdir_tell(t_readdir *x)
{
  off_t off = 0;
  if (x->x_dir) off = telldir(x->x_dir);
#ifdef READDIR_DEBUG
  post("readdir_tell(): off: %%d=%d, %%f=%f\n", off, (t_float)off);
#endif
  outlet_float(x->x_ent_outlet, (t_float)off);
}

/*--------------------------------------------------------------------
 * seek FLOAT
 */
static void readdir_seek(t_readdir *x, t_floatarg pos)
{
  if (!x->x_dir) {
    error("readdir: seek %f: no directory opened!", pos);
    return;
  }
  seekdir(x->x_dir, (off_t)pos);
}


/*--------------------------------------------------------------------
 * new
 */
static void *readdir_new(void)
{
    t_readdir *x = (t_readdir *)pd_new(readdir_class);

    //-- defaults
    x->x_dir = NULL;
    x->x_dirname = sp_none;
    SETSYMBOL(&x->x_eatom, sp_none);

    //-- outlets
    x->x_ent_outlet = outlet_new(&x->x_obj, &s_symbol);
    x->x_eod_outlet = outlet_new(&x->x_obj, &s_bang);

    return (void *)x;
}

/*--------------------------------------------------------------------
 * free
 */
static void readdir_free(t_readdir *x)
{
  readdir_close(x);
  outlet_free(x->x_ent_outlet);
  outlet_free(x->x_eod_outlet);
  return;
}

/*--------------------------------------------------------------------
 * setup
 */
void readdir_setup(void)
{
  post(readdir_banner);
#ifdef READDIR_DEBUG
  post("readdir: debugging enabled");
#endif

  //-- constants
  sp_none = gensym("none");
  sp_unknown = gensym("unknown");
  sp_file = gensym("file");
  sp_dir = gensym("dir");
  sp_fifo = gensym("fifo");
  sp_sock = gensym("sock");
  sp_chrdev = gensym("chrdev");
  sp_blkdev = gensym("blkdev");

  //-- class
  readdir_class = class_new(gensym("readdir"),
			    (t_newmethod)readdir_new,
			    (t_method)readdir_free,
			    sizeof(t_readdir),
			    CLASS_DEFAULT,
			    0);
  
  //-- methods
  class_addmethod(readdir_class, (t_method)readdir_open, gensym("open"), A_DEFSYMBOL, 0);
  class_addmethod(readdir_class, (t_method)readdir_close, gensym("close"), 0);
  class_addmethod(readdir_class, (t_method)readdir_next, gensym("next"), 0);
  class_addbang(readdir_class, (t_method)readdir_next);
  class_addmethod(readdir_class, (t_method)readdir_rewind, gensym("rewind"), 0);
  class_addmethod(readdir_class, (t_method)readdir_tell, gensym("tell"), 0);
  class_addmethod(readdir_class, (t_method)readdir_seek, gensym("seek"), A_DEFFLOAT, 0);
  
  //-- help symbol
  class_sethelpsymbol(readdir_class, gensym("readdir-help.pd"));
}