diff options
author | Martin Peach <mrpeach@users.sourceforge.net> | 2014-01-27 18:54:24 +0000 |
---|---|---|
committer | Martin Peach <mrpeach@users.sourceforge.net> | 2014-01-27 18:54:24 +0000 |
commit | 6db48a0774cf83cc860c760a7299ee74c1e58c9e (patch) | |
tree | e1d371a55d63bb9e77f0786879dba14b4b09755d /binfile/binfile.c | |
parent | d0344a49e71fa7d59f5de09fa0c2319ce92a836a (diff) |
Add a length parameter to the read message to set the number of bytes to be read in. This also applies to the creation arguments.
svn path=/trunk/externals/mrpeach/; revision=17258
Diffstat (limited to 'binfile/binfile.c')
-rw-r--r-- | binfile/binfile.c | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/binfile/binfile.c b/binfile/binfile.c index d50a5d4..09c0a79 100644 --- a/binfile/binfile.c +++ b/binfile/binfile.c @@ -1,5 +1,5 @@ /* binfile.c An external for Pure Data that reads and writes binary files -* Copyright (C) 2007 Martin Peach +* Copyright (C) 2007-2014 Martin Peach * * 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 @@ -56,7 +56,7 @@ typedef struct t_binfile static void binfile_rewind (t_binfile *x); static void binfile_free(t_binfile *x); static FILE *binfile_open_path(t_binfile *x, char *path, char *mode); -static void binfile_read(t_binfile *x, t_symbol *path); +static void binfile_read(t_binfile *x, t_symbol *path, t_float max_bytes); static void binfile_write(t_binfile *x, t_symbol *path); static void binfile_bang(t_binfile *x); static void binfile_float(t_binfile *x, t_float val); @@ -81,7 +81,7 @@ void binfile_setup(void) class_addbang(binfile_class, binfile_bang); class_addfloat(binfile_class, binfile_float); class_addlist(binfile_class, binfile_list); - class_addmethod(binfile_class, (t_method)binfile_read, gensym("read"), A_DEFSYMBOL, 0); + class_addmethod(binfile_class, (t_method)binfile_read, gensym("read"), A_DEFSYMBOL, A_DEFFLOAT, 0); class_addmethod(binfile_class, (t_method)binfile_write, gensym("write"), A_DEFSYMBOL, 0); class_addmethod(binfile_class, (t_method)binfile_add, gensym("add"), A_GIMME, 0); class_addmethod(binfile_class, (t_method)binfile_set, gensym("set"), A_GIMME, 0); @@ -106,25 +106,25 @@ static void *binfile_new(t_symbol *s, int argc, t_atom *argv) x->x_fP = NULL; x->x_fPath[0] = '\0'; x->x_our_directory = canvas_getcurrentdir();/* get the current directory to use as the base for relative file paths */ - x->x_buf_length = ALLOC_BLOCK_SIZE; x->x_rd_offset = x->x_wr_offset = x->x_length = 0L; + x->x_buf_length = 0;//ALLOC_BLOCK_SIZE; /* find the first string in the arg list and interpret it as a path to a file */ + /* find the first float in the arg list and interpret it as the size of the buffer */ for (i = 0; i < argc; ++i) { - if (argv[i].a_type == A_SYMBOL) + if (argv[i].a_type == A_FLOAT) { - pathSymbol = atom_getsymbol(&argv[i]); - if (pathSymbol != NULL) - binfile_read(x, pathSymbol); + x->x_buf_length = atom_getfloat(&argv[i]); + break; // take the first number } } - /* find the first float in the arg list and interpret it as the size of the buffer */ for (i = 0; i < argc; ++i) { - if (argv[i].a_type == A_FLOAT) + if (argv[i].a_type == A_SYMBOL) { - x->x_buf_length = atom_getfloat(&argv[i]); - break; + pathSymbol = atom_getsymbol(&argv[i]); + if (pathSymbol != NULL) binfile_read(x, pathSymbol, x->x_buf_length); + break; // only try one path } } if ((x->x_buf = getbytes(x->x_buf_length)) == NULL) @@ -194,8 +194,9 @@ static void binfile_write(t_binfile *x, t_symbol *path) x->x_fP = NULL; } -static void binfile_read(t_binfile *x, t_symbol *path) +static void binfile_read(t_binfile *x, t_symbol *path, t_float max_bytes) /* open the file for reading and load it into the buffer, then close it */ +/* if max_bytes > 0 read only max_bytes into the buffer */ { size_t file_length = 0L; size_t bytes_read = 0L; @@ -205,8 +206,9 @@ static void binfile_read(t_binfile *x, t_symbol *path) error("binfile: Unable to open %s for reading", path->s_name); return; } - /* get length of file */ - while (EOF != getc(x->x_fP)) ++file_length; + /* get length of file up to max_bytes */ + if (max_bytes > 0) while ((EOF != getc(x->x_fP))&&(file_length < max_bytes)) ++file_length; + else while (EOF != getc(x->x_fP)) ++file_length; if (file_length == 0L) return; /* get storage for file contents */ |