Update of /cvsroot/pure-data/externals/mrpeach/binfile
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1592
Modified Files:
binfile-help.pd binfile.c
Log Message:
Added readat and writeat messages to set pointers.
Index: binfile.c
===================================================================
RCS file: /cvsroot/pure-data/externals/mrpeach/binfile/binfile.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** binfile.c 24 Sep 2007 21:01:24 -0000 1.1
--- binfile.c 26 Sep 2007 21:18:16 -0000 1.2
***************
*** 40,43 ****
--- 40,44 ----
char *x_buf; /* read/write buffer in memory for file contents */
size_t x_buf_length; /* current length of buf */
+ size_t x_length; /* current length of valid data in buf */
size_t x_rd_offset; /* current read offset into the buffer */
size_t x_wr_offset; /* current write offset into the buffer */
***************
*** 56,59 ****
--- 57,62 ----
static void binfile_info(t_binfile *x);
static void binfile_set(t_binfile *x, t_symbol *s, int argc, t_atom *argv);
+ static void binfile_set_read_index(t_binfile *x, t_float offset);
+ static void binfile_set_write_index(t_binfile *x, t_float offset);
static void *binfile_new(t_symbol *s, int argc, t_atom *argv);
void binfile_setup(void);
***************
*** 74,77 ****
--- 77,82 ----
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);
+ class_addmethod(binfile_class, (t_method)binfile_set_read_index, gensym("readat"), A_DEFFLOAT, 0);
+ class_addmethod(binfile_class, (t_method)binfile_set_write_index, gensym("writeat"), A_DEFFLOAT, 0);
class_addmethod(binfile_class, (t_method)binfile_clear, gensym("clear"), 0);
class_addmethod(binfile_class, (t_method)binfile_rewind, gensym("rewind"), 0);
***************
*** 93,96 ****
--- 98,102 ----
x->x_fPath[0] = '\0';
x->x_buf_length = ALLOC_BLOCK_SIZE;
+ x->x_rd_offset = x->x_wr_offset = x->x_length = 0L;
/* find the first string in the arg list and interpret it as a path to a file */
for (i = 0; i < argc; ++i)
***************
*** 152,157 ****
if (0==(x->x_fP = binfile_open_path(x, path->s_name, "wb")))
error("binfile: Unable to open %s for writing", path->s_name);
! bytes_written = fwrite(x->x_buf, 1L, x->x_wr_offset, x->x_fP);
! if (bytes_written != x->x_wr_offset) post("binfile: %ld bytes written != %ld", bytes_written, x->x_wr_offset);
else post("binfile: wrote %ld bytes to %s", bytes_written, path->s_name);
fclose(x->x_fP);
--- 158,163 ----
if (0==(x->x_fP = binfile_open_path(x, path->s_name, "wb")))
error("binfile: Unable to open %s for writing", path->s_name);
! bytes_written = fwrite(x->x_buf, 1L, x->x_length, x->x_fP);
! if (bytes_written != x->x_length) post("binfile: %ld bytes written != %ld", bytes_written, x->x_length);
else post("binfile: wrote %ld bytes to %s", bytes_written, path->s_name);
fclose(x->x_fP);
***************
*** 188,193 ****
bytes_read = fread(x->x_buf, 1L, file_length, x->x_fP);
x->x_buf_length = bytes_read;
! x->x_wr_offset = x->x_buf_length;
! x->x_rd_offset = 0L;
fclose (x->x_fP);
x->x_fP = NULL;
--- 194,200 ----
bytes_read = fread(x->x_buf, 1L, file_length, x->x_fP);
x->x_buf_length = bytes_read;
! x->x_wr_offset = x->x_buf_length; /* write new data at end of file */
! x->x_length = x->x_buf_length; /* file length is same as buffer size 7*/
! x->x_rd_offset = 0L; /* read from start of file */
fclose (x->x_fP);
x->x_fP = NULL;
***************
*** 201,208 ****
unsigned char c;
! if (x->x_rd_offset < x->x_wr_offset)
{
c = x->x_buf[x->x_rd_offset++];
! if (x->x_rd_offset == x->x_wr_offset) outlet_bang(x->x_bang_outlet);
outlet_float(x->x_bin_outlet, (float)c);
}
--- 208,215 ----
unsigned char c;
! if (x->x_rd_offset < x->x_length)
{
c = x->x_buf[x->x_rd_offset++];
! if (x->x_rd_offset == x->x_length) outlet_bang(x->x_bang_outlet);
outlet_float(x->x_bin_outlet, (float)c);
}
***************
*** 249,252 ****
--- 256,260 ----
}
x->x_buf[x->x_wr_offset++] = j;
+ if (x->x_length < x->x_wr_offset) x->x_length = x->x_wr_offset;
}
else
***************
*** 270,277 ****
--- 278,305 ----
}
+ static void binfile_set_read_index(t_binfile *x, t_float offset)
+ /* set the read offset, always < length */
+ {
+ size_t intoffset = offset;
+
+ if (intoffset < x->x_length) x->x_rd_offset = intoffset;
+ else if (x->x_length > 0) x->x_rd_offset = x->x_length-1;
+ else x->x_rd_offset = 0L;
+ }
+
+ static void binfile_set_write_index(t_binfile *x, t_float offset)
+ /* set the write offset, always <= length */
+ {
+ size_t intoffset = offset;
+
+ if (intoffset <= x->x_length) x->x_wr_offset = intoffset;
+ else x->x_wr_offset = x->x_length;
+ }
+
static void binfile_clear(t_binfile *x)
{
x->x_wr_offset = 0L;
x->x_rd_offset = 0L;
+ x->x_length = 0L;
}
***************
*** 295,298 ****
--- 323,328 ----
SETFLOAT(output_atom, x->x_buf_length);
outlet_anything( x->x_info_outlet, gensym("buflength"), 1, output_atom);
+ SETFLOAT(output_atom, x->x_length);
+ outlet_anything( x->x_info_outlet, gensym("length"), 1, output_atom);
SETFLOAT(output_atom, x->x_rd_offset);
outlet_anything( x->x_info_outlet, gensym("readoffset"), 1, output_atom);
Index: binfile-help.pd
===================================================================
RCS file: /cvsroot/pure-data/externals/mrpeach/binfile/binfile-help.pd,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** binfile-help.pd 24 Sep 2007 21:01:24 -0000 1.1
--- binfile-help.pd 26 Sep 2007 21:18:16 -0000 1.2
***************
*** 1,47 ****
! #N canvas 364 300 1105 584 12;
! #X msg 546 150 rewind;
#X obj 85 549 textfile;
! #X msg 536 120 bang;
! #X msg 545 204 clear;
! #X msg 545 305 set 2 4 6 8;
#X obj 25 15 binfile;
#X text 88 14 read and write binary files;
#X text 25 144 You can also use this object simply for storing heterogeneous
sequences of bytes.;
! #X msg 545 231 add 10 20 255;
! #X msg 545 177 read afile.bin;
#X obj 480 408 binfile;
#X text 814 548 Martin Peach 2007/09/24;
#X text 149 548 to read and write text files;
! #X msg 545 331 write afile.bin;
#X text 612 426 This outlet gives a bang when you hit the end of the
sequence.;
#X text 478 502 This outlet gives the bytes in sequence \, or bangs
if no more bytes.;
! #X text 645 230 add a list of byte-floats to the buffer;
! #X text 630 304 clear the buffer and then add some bytes;
! #X text 590 203 empty the buffer;
! #X text 597 149 set the read pointer to the beginnning of the buffer
;
! #X text 575 119 output one byte from the buffer as a float;
! #X text 661 330 write the entire buffer to a file;
! #X text 653 176 read a file into the buffer \, replacing any previous
contents;
#X obj 480 481 print one;
#X obj 526 437 bng 15 250 50 0 empty empty empty 17 7 0 10 -258113
-257985 -1;
! #X obj 495 123 bng 15 250 50 0 empty empty empty 17 7 0 10 -4034 -257985
-1;
#X text 16 548 See also:;
! #X obj 480 53 openpanel;
! #X obj 480 32 bng 15 250 50 0 empty empty read_any_file_into_buffer
17 7 0 10 -4032 -258113 -1;
! #X msg 480 79 read \$1;
! #X obj 324 216 bng 15 250 50 0 empty empty save_buffer_as_any_file
17 7 0 10 -4032 -258113 -1;
! #X msg 324 264 write \$1;
! #X obj 324 238 savepanel;
! #X text 514 120 or;
! #X msg 545 257 77 128 129 130;
#X text 25 38 The binfile object reads and writes binary files to and
from a buffer in memory. You can read a file and output its contents
--- 1,46 ----
! #N canvas 144 281 1220 588 12;
! #X msg 628 98 rewind;
#X obj 85 549 textfile;
! #X msg 618 68 bang;
! #X msg 627 152 clear;
! #X msg 627 253 set 2 4 6 8;
#X obj 25 15 binfile;
#X text 88 14 read and write binary files;
#X text 25 144 You can also use this object simply for storing heterogeneous
sequences of bytes.;
! #X msg 627 179 add 10 20 255;
! #X msg 627 125 read afile.bin;
#X obj 480 408 binfile;
#X text 814 548 Martin Peach 2007/09/24;
#X text 149 548 to read and write text files;
! #X msg 627 279 write afile.bin;
#X text 612 426 This outlet gives a bang when you hit the end of the
sequence.;
#X text 478 502 This outlet gives the bytes in sequence \, or bangs
if no more bytes.;
! #X text 712 252 clear the buffer and then add some bytes;
! #X text 672 151 empty the buffer;
! #X text 679 97 set the read pointer to the beginnning of the buffer
;
! #X text 657 67 output one byte from the buffer as a float;
! #X text 743 278 write the entire buffer to a file;
! #X text 735 124 read a file into the buffer \, replacing any previous
contents;
#X obj 480 481 print one;
#X obj 526 437 bng 15 250 50 0 empty empty empty 17 7 0 10 -258113
-257985 -1;
! #X obj 577 71 bng 15 250 50 0 empty empty empty 17 7 0 10 -4034 -257985
-1;
#X text 16 548 See also:;
! #X obj 470 40 openpanel;
! #X obj 470 19 bng 15 250 50 0 empty empty read_any_file_into_buffer
17 7 0 10 -4032 -258113 -1;
! #X msg 470 66 read \$1;
! #X obj 383 139 bng 15 250 50 0 empty empty save_buffer_as_any_file
17 7 0 10 -4032 -258113 -1;
! #X msg 383 187 write \$1;
! #X obj 383 161 savepanel;
! #X text 596 68 or;
! #X msg 627 205 77 128 129 130;
#X text 25 38 The binfile object reads and writes binary files to and
from a buffer in memory. You can read a file and output its contents
***************
*** 50,62 ****
the buffer and then "add" to add bytes. Finally \, "write" will save
the entire buffer as a binary file.;
! #X floatatom 545 282 5 0 0 0 - - -;
! #X text 586 281 add one byte \, same as "add";
! #X text 654 256 add a list of bytes \, same as "add";
! #X msg 545 357 info;
#X obj 503 455 print info;
#X obj 543 432 print end;
! #X text 583 454 This outlet gives a list of info from the info message.
! ;
! #X text 583 356 output current buffer length and pointer values.;
#X connect 0 0 10 0;
#X connect 2 0 10 0;
--- 49,68 ----
the buffer and then "add" to add bytes. Finally \, "write" will save
the entire buffer as a binary file.;
! #X floatatom 627 230 5 0 0 0 - - -;
! #X text 668 229 add one byte \, same as "add";
! #X text 736 204 add a list of bytes \, same as "add";
! #X msg 123 287 info;
#X obj 503 455 print info;
#X obj 543 432 print end;
! #X msg 626 334 readat 3;
! #X msg 626 361 writeat 3;
! #X text 705 360 set the write position;
! #X text 696 333 set the read position;
! #X text 583 454 This outlet gives a list of current parameters from
! the info message.;
! #X text 727 171 add a list of byte-floats to the buffer at the current
! write offset;
! #X text 21 269 output current buffer length and pointer values through
! the middle outlet.;
#X connect 0 0 10 0;
#X connect 2 0 10 0;
***************
*** 65,81 ****
#X connect 8 0 10 0;
#X connect 9 0 10 0;
! #X connect 10 0 23 0;
! #X connect 10 1 41 0;
! #X connect 10 2 24 0;
! #X connect 10 2 42 0;
#X connect 13 0 10 0;
! #X connect 25 0 10 0;
! #X connect 27 0 29 0;
! #X connect 28 0 27 0;
! #X connect 29 0 10 0;
! #X connect 30 0 32 0;
! #X connect 31 0 10 0;
! #X connect 32 0 31 0;
! #X connect 34 0 10 0;
! #X connect 37 0 10 0;
! #X connect 40 0 10 0;
--- 71,89 ----
#X connect 8 0 10 0;
#X connect 9 0 10 0;
! #X connect 10 0 22 0;
! #X connect 10 1 40 0;
! #X connect 10 2 23 0;
! #X connect 10 2 41 0;
#X connect 13 0 10 0;
! #X connect 24 0 10 0;
! #X connect 26 0 28 0;
! #X connect 27 0 26 0;
! #X connect 28 0 10 0;
! #X connect 29 0 31 0;
! #X connect 30 0 10 0;
! #X connect 31 0 30 0;
! #X connect 33 0 10 0;
! #X connect 36 0 10 0;
! #X connect 39 0 10 0;
! #X connect 42 0 10 0;
! #X connect 43 0 10 0;