aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Peach <mrpeach@users.sourceforge.net>2014-01-27 18:54:24 +0000
committerMartin Peach <mrpeach@users.sourceforge.net>2014-01-27 18:54:24 +0000
commit6db48a0774cf83cc860c760a7299ee74c1e58c9e (patch)
treee1d371a55d63bb9e77f0786879dba14b4b09755d
parentd0344a49e71fa7d59f5de09fa0c2319ce92a836a (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
-rw-r--r--binfile/binfile-help.pd147
-rw-r--r--binfile/binfile.c32
2 files changed, 93 insertions, 86 deletions
diff --git a/binfile/binfile-help.pd b/binfile/binfile-help.pd
index f87e31b..3be34e5 100644
--- a/binfile/binfile-help.pd
+++ b/binfile/binfile-help.pd
@@ -1,69 +1,65 @@
-#N canvas 1 53 875 617 12;
-#X msg 83 78 rewind;
-#X obj 195 465 textfile;
-#X msg 60 55 bang;
-#X msg 129 124 clear;
-#X msg 220 215 set 2 4 6 8;
+#N canvas 237 152 1080 699 12;
+#X msg 103 98 rewind;
+#X obj 267 577 textfile;
+#X msg 80 75 bang;
+#X msg 179 174 clear;
+#X msg 282 277 set 2 4 6 8;
#X obj 181 -89 binfile;
#X text 246 -90 read and write binary files;
-#X text 403 330 You can also use this object simply for storing heterogeneous
-sequences of bytes.;
-#X msg 152 147 add 10 20 255;
-#X msg 106 101 read afile.bin;
-#X obj 338 353 binfile;
-#X text 272 464 to read and write text files;
-#X msg 243 238 write afile.bin;
-#X text 478 367 This outlet gives a bang when you hit the end of the
+#X msg 202 197 add 10 20 255;
+#X obj 410 425 binfile;
+#X text 344 576 to read and write text files;
+#X msg 305 300 write afile.bin;
+#X text 534 449 This outlet gives a bang when you hit the end of the
sequence.;
-#X text 409 427 This outlet gives the bytes in sequence \, or bangs
+#X text 481 539 This outlet gives the bytes in sequence \, or bangs
if no more bytes.;
-#X text 308 213 clear the buffer and then add some bytes;
-#X text 174 123 empty the buffer;
-#X text 135 78 set the read pointer to the beginnning of the buffer
+#X text 370 275 clear the buffer and then add some bytes;
+#X text 224 173 empty the buffer;
+#X text 155 98 set the read pointer to the beginnning of the buffer
;
-#X text 99 55 output one byte from the buffer as a float;
-#X text 360 236 write the entire buffer to a file;
-#X obj 338 433 print one;
-#X obj 15 40 bng 15 250 50 0 empty empty empty 17 7 0 10 -4034 -257985
+#X text 119 75 output one byte from the buffer as a float;
+#X text 422 298 write the entire buffer to a file;
+#X obj 410 545 print one;
+#X obj 35 60 bng 15 250 50 0 empty empty empty 17 7 0 10 -4034 -257985
-1;
-#X text 194 447 See also:;
-#X obj 37 -13 openpanel;
-#X obj 17 -48 bng 15 250 50 0 empty empty read_any_file_into_buffer
+#X text 266 559 See also:;
+#X obj 57 -13 openpanel;
+#X obj 57 -37 bng 15 250 50 0 empty empty read_any_file_into_buffer
17 7 0 10 -4032 -258113 -1;
-#X msg 37 32 read \$1;
-#X obj 17 292 bng 15 250 50 0 empty empty save_buffer_as_any_file 17
+#X msg 57 52 read \$1;
+#X obj 39 354 bng 15 250 50 0 empty empty save_buffer_as_any_file 17
7 0 10 -4032 -258113 -1;
-#X msg 335 330 write \$1;
-#X obj 338 307 savepanel;
-#X msg 175 170 77 128 129 130;
+#X msg 397 392 write \$1;
+#X obj 400 369 savepanel;
+#X msg 237 232 77 128 129 130;
#X text 254 -50 The binfile object reads and writes binary files to
and from a buffer in memory. You can read a file and output its contents
one byte at a time as floats.;
-#X text 422 26 To add bytes to the buffer \, first send "clear" to
+#X text 442 46 To add bytes to the buffer \, first send "clear" to
empty the buffer and then "add" to add bytes. Finally \, "write" will
save the entire buffer as a binary file.;
-#X floatatom 197 193 5 0 0 0 - - -;
-#X text 237 191 add one byte \, same as "add";
-#X text 287 169 add a list of bytes \, same as "add";
+#X floatatom 259 255 5 0 0 0 - - -;
+#X text 299 253 add one byte \, same as "add";
+#X text 349 231 add a list of bytes \, same as "add";
#X msg 15 10 info;
-#X obj 361 403 print info;
-#X obj 410 373 print end;
-#X msg 266 261 readat 3;
-#X msg 289 284 writeat 3;
-#X text 363 284 set the write position;
-#X text 333 261 set the read position;
-#X text 440 397 This outlet gives a list of current parameters from
+#X obj 433 515 print info;
+#X obj 466 455 print end;
+#X msg 328 323 readat 3;
+#X msg 351 346 writeat 3;
+#X text 425 346 set the write position;
+#X text 395 323 set the read position;
+#X text 512 509 This outlet gives a list of current parameters from
the info message.;
-#X text 255 136 add a list of byte-floats to the buffer at the current
+#X text 303 197 add a list of byte-floats to the buffer at the current
write offset;
-#X text 108 6 output current buffer length and pointer values through
+#X text 65 10 output current buffer length and pointer values through
the middle outlet.;
-#X text 213 101 read a file into the buffer and make it the new buffer
+#X text 233 121 read a file into the buffer and make it the new buffer
;
-#X obj 384 381 tgl 15 0 empty empty empty 17 7 0 10 -258113 -257985
+#X obj 456 493 tgl 15 0 empty empty empty 17 7 0 10 -258113 -257985
-1 0 1;
-#X text 688 468 Martin Peach 2009/10/13;
-#N canvas 506 311 494 344 META 0;
+#N canvas 507 310 494 344 META 0;
#X text 12 175 HELP_PATCH_AUTHORS "pd meta" information added by Jonathan
Wilkes for Pd version 0.42.;
#X text 12 25 LICENSE GPL v2 or later;
@@ -75,27 +71,36 @@ write readat writeat symbol;
#X text 12 115 OUTLET_1 list;
#X text 12 135 OUTLET_2 bang;
#X text 12 155 AUTHOR Martin Peach;
-#X restore 812 497 pd META;
-#X connect 0 0 10 0;
-#X connect 2 0 10 0;
-#X connect 3 0 10 0;
-#X connect 4 0 10 0;
-#X connect 8 0 10 0;
-#X connect 9 0 10 0;
-#X connect 10 0 20 0;
-#X connect 10 1 36 0;
-#X connect 10 2 37 0;
-#X connect 10 2 46 0;
-#X connect 12 0 10 0;
-#X connect 21 0 2 0;
-#X connect 23 0 25 0;
-#X connect 24 0 23 0;
-#X connect 25 0 10 0;
-#X connect 26 0 28 0;
-#X connect 27 0 10 0;
-#X connect 28 0 27 0;
-#X connect 29 0 10 0;
-#X connect 32 0 10 0;
-#X connect 35 0 10 0;
-#X connect 38 0 10 0;
-#X connect 39 0 10 0;
+#X restore 882 607 pd META;
+#X text 525 363 You can also use this object simply for storing heterogeneous
+sequences of bytes.;
+#X text 481 404 Optional arguments for filename and length to be read
+(same as [read( message;
+#X msg 151 146 read test.txt 5;
+#X msg 126 121 read test.txt;
+#X text 267 145 read the first 5 bytes of a file into the buffer;
+#X text 760 580 Martin Peach 2014/01/27;
+#X connect 0 0 8 0;
+#X connect 2 0 8 0;
+#X connect 3 0 8 0;
+#X connect 4 0 8 0;
+#X connect 7 0 8 0;
+#X connect 8 0 18 0;
+#X connect 8 1 34 0;
+#X connect 8 2 35 0;
+#X connect 8 2 44 0;
+#X connect 10 0 8 0;
+#X connect 19 0 2 0;
+#X connect 21 0 23 0;
+#X connect 22 0 21 0;
+#X connect 23 0 8 0;
+#X connect 24 0 26 0;
+#X connect 25 0 8 0;
+#X connect 26 0 25 0;
+#X connect 27 0 8 0;
+#X connect 30 0 8 0;
+#X connect 33 0 8 0;
+#X connect 36 0 8 0;
+#X connect 37 0 8 0;
+#X connect 48 0 8 0;
+#X connect 49 0 8 0;
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 */