aboutsummaryrefslogtreecommitdiff
path: root/filesize.c
blob: 0a878125e2c0871f24bad37ebee0418fe0b25085 (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
#include "ext13.h"
#include "m_pd.h"
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>

#ifdef NT
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#endif

/* -------------------------- filesize ------------------------------ */
static t_class *filesize_class;

typedef struct _filesize
{
    t_object x_obj;
    t_float x_f;
    t_symbol *x_s;
} t_filesize;


static void *filesize_new(t_symbol *s)
{
    t_filesize *x = (t_filesize *)pd_new(filesize_class);
    x->x_s = s;
    outlet_new(&x->x_obj, &s_float);
    return (x);
}

static void filesize_bang(t_filesize *x)
{
  outlet_float(x->x_obj.ob_outlet, x->x_f);
}

static void filesize_symbol(t_filesize *x, t_symbol *s)
{
   struct stat statbuf;
   int ok=(stat(s->s_name, &statbuf) >= 0);
   if (ok>0) outlet_float(x->x_obj.ob_outlet,x->x_f=statbuf.st_size);
   else post ("filesize:file not found");
}

void filesize_setup(void)
{
    filesize_class = class_new(gensym("filesize"), (t_newmethod)filesize_new, 0,
    	sizeof(t_filesize), 0, A_DEFFLOAT, 0);
    class_addcreator((t_newmethod)filesize_new, gensym("fsize"), A_DEFFLOAT, 0);
    class_addbang(filesize_class, filesize_bang);
    class_addsymbol(filesize_class, filesize_symbol);        
}