From 9e84c5fa2d2704ac35d7ca5c600e80f8ae49c32d Mon Sep 17 00:00:00 2001 From: Martin Peach Date: Tue, 7 Nov 2006 21:20:20 +0000 Subject: added file send svn path=/trunk/externals/mrpeach/; revision=6223 --- net/tcpsend.c | 58 ++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 14 deletions(-) (limited to 'net/tcpsend.c') diff --git a/net/tcpsend.c b/net/tcpsend.c index cf8c33d..13e707c 100755 --- a/net/tcpsend.c +++ b/net/tcpsend.c @@ -7,6 +7,8 @@ #include "m_pd.h" #include "s_stuff.h" +#include +#include #ifdef MSW #include @@ -16,8 +18,6 @@ #include #include #include -#include -#include #endif static t_class *tcpsend_class; @@ -28,9 +28,6 @@ typedef struct _tcpsend int x_fd; } t_tcpsend; -#ifdef MSW -__declspec(dllexport) -#endif void tcpsend_setup(void); static void tcpsend_free(t_tcpsend *x); static void tcpsend_send(t_tcpsend *x, t_symbol *s, int argc, t_atom *argv); @@ -115,8 +112,9 @@ static void tcpsend_disconnect(t_tcpsend *x) static void tcpsend_send(t_tcpsend *x, t_symbol *s, int argc, t_atom *argv) { - static char byte_buf[65536];// arbitrary maximum similar to max IP packet size - int i, d; +#define BYTE_BUF_LEN 65536 // arbitrary maximum similar to max IP packet size + static char byte_buf[BYTE_BUF_LEN]; + int i, j, d; char c; float f, e; char *bp; @@ -127,12 +125,14 @@ static void tcpsend_send(t_tcpsend *x, t_symbol *s, int argc, t_atom *argv) double timebefore; double timeafter; int late; + char fpath[MAX_PATH]; + FILE *fptr; #ifdef DEBUG post("s: %s", s->s_name); post("argc: %d", argc); #endif - for (i = 0; i < argc; ++i) + for (i = j = 0; i < argc; ++i) { if (argv[i].a_type == A_FLOAT) { @@ -153,16 +153,49 @@ static void tcpsend_send(t_tcpsend *x, t_symbol *s, int argc, t_atom *argv) #ifdef DEBUG post("tcpsend_send: argv[%d]: %d", i, c); #endif - byte_buf[i] = c; + byte_buf[j++] = c; + } + else if (argv[i].a_type == A_SYMBOL) + { + + atom_string(&argv[i], fpath, MAX_PATH); +#ifdef DEBUG + post ("tcpsend fname: %s", fpath); +#endif + fptr = fopen(fpath, "rb"); + if (fptr == NULL) + { + post("tcpsend: unable to open \"%s\"", fpath); + return; + } + rewind(fptr); +#ifdef DEBUG + post("tcpsend: d is %d", d); +#endif + while ((d = fgetc(fptr)) != EOF) + { + byte_buf[j++] = (char)(d & 0x0FF); +#ifdef DEBUG + post("tcpsend: byte_buf[%d] = %d", j-1, byte_buf[j-1]); +#endif + if (j >= BYTE_BUF_LEN) + { + post ("tcpsend: file too long, truncating at %lu", BYTE_BUF_LEN); + break; + } + } + fclose(fptr); + fptr = NULL; + post("tcpsend: read \"%s\" length %d byte%s", fpath, j, ((d==1)?"":"s")); } else { - error("tcpsend_send: item %d is not a float", i); + error("tcpsend_send: item %d is not a float or a file name", i); return; } } - length = i; + length = j; if ((x->x_fd >= 0) && (length > 0)) { for (bp = byte_buf, sent = 0; sent < length;) @@ -203,9 +236,6 @@ static void tcpsend_free(t_tcpsend *x) tcpsend_disconnect(x); } -#ifdef MSW -__declspec(dllexport) -#endif void tcpsend_setup(void) { tcpsend_class = class_new(gensym("tcpsend"), (t_newmethod)tcpsend_new, -- cgit v1.2.1