diff options
author | Franz Zotter <fzotter@users.sourceforge.net> | 2006-02-22 12:48:15 +0000 |
---|---|---|
committer | Franz Zotter <fzotter@users.sourceforge.net> | 2006-02-22 12:48:15 +0000 |
commit | 97d9a1382b32a7055f407265d24f8dc900e04741 (patch) | |
tree | b2804dbff72d634e2e0a38507e7c8c6c8768033a /pdoctave_command.c |
These is the beginning of a try to connect the pd to octave. Especially when using the iemmatrix features in pd, this feature becomes sensible. On the whole it shall enable the user to use the command line parser of octave.svn2git-root
The take-over strategy is bad at this starting point. No threading, no practical data transmission.
Perhaps the common work on it can help. (Franz)
svn path=/trunk/externals/iem/pdoctave/; revision=4611
Diffstat (limited to 'pdoctave_command.c')
-rw-r--r-- | pdoctave_command.c | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/pdoctave_command.c b/pdoctave_command.c new file mode 100644 index 0000000..b1f1d6b --- /dev/null +++ b/pdoctave_command.c @@ -0,0 +1,114 @@ +#include <string.h> +#ifndef __pdoctave_command_c_ +#define __pdoctave_command_c_ +#include "pdoctave.h" /* writing a command to octave */ +#include "pdoctave_dataframe.h" +#include "m_pd.h" + +static t_class *pdoctave_command_class; + +typedef struct _PDOctaveCommand_ PDOctaveCommand; +struct _PDOctaveCommand_ +{ + t_object x_obj; + char oct_command[1024]; + int pos; +}; + +static void clearStringCommand (PDOctaveCommand *pdoctcmd_obj) +{ + pdoctcmd_obj->pos=0; + pdoctcmd_obj->oct_command[0] = '\0'; +} + +static void addStringToCommand (PDOctaveCommand *pdoctcmd_obj, char *str) +{ + int n = strlen (str); + strcpy(pdoctcmd_obj->oct_command+pdoctcmd_obj->pos, str); + pdoctcmd_obj->pos += n; +} + +static void addAtomToCommand (PDOctaveCommand *pdoctcmd_obj, t_atom *a) +{ + const unsigned int bufsize = 50; + char str[bufsize]; + + atom_string (a, str, bufsize); + + if (strcmp(str,"\\\\,") == 0) { + strcpy(str,",\0"); + } + else if (strcmp(str,"\\\\;") == 0) { + strcpy(str,";\0"); + } + + addStringToCommand (pdoctcmd_obj, str); +} + +static void removeEscapeSlashes (char *c) +{ + int pos = strlen(c); + c = c+pos-1; + + while (--pos) { + if (*--c == '\\') { + strcpy(c,c+1); + } + } +} +static void pDOctaveCommandBang (PDOctaveCommand *pdoctcmd_obj) +{ + post("command: %s sent", pdoctcmd_obj->oct_command); + writeToOctaveStdIN (pdoctcmd_obj->oct_command); +} + + +static void pDOctaveCommandAny (PDOctaveCommand *pdoctcmd_obj, t_symbol *s, int argc, t_atom *argv) +{ + clearStringCommand (pdoctcmd_obj); + if (argc>0) + while (argc--) { + addAtomToCommand (pdoctcmd_obj, argv++); + addStringToCommand (pdoctcmd_obj, " \0"); + } + + addStringToCommand (pdoctcmd_obj, "\n"); + removeEscapeSlashes (pdoctcmd_obj->oct_command); + pDOctaveCommandBang (pdoctcmd_obj); +} + +static void *newPDOctaveCommand (t_symbol *s, int argc, t_atom *argv) +{ + PDOctaveCommand *pdoctcmd_obj = (PDOctaveCommand *) + pd_new (pdoctave_command_class); + + post("getpdoctaveinstances returned %d", getPDOctaveInstances()); + if (getPDOctaveInstances()<1) { + post("Octave not running, insert a 'pdoctave' object!!"); + } + pdoctcmd_obj->pos = 0; + if (argc>0) + while (argc--) { + addAtomToCommand (pdoctcmd_obj, argv++); + addStringToCommand (pdoctcmd_obj, " \0"); + } + + addStringToCommand (pdoctcmd_obj, "\n"); + removeEscapeSlashes(pdoctcmd_obj->oct_command); + + return ((void *) pdoctcmd_obj); +} +void pdoctave_command_setup (void) +{ + pdoctave_command_class = class_new + (gensym("pdoctave_command"), + (t_newmethod) newPDOctaveCommand, + 0, + sizeof (PDOctaveCommand), + CLASS_DEFAULT, A_GIMME, 0); + class_addbang (pdoctave_command_class, (t_method) pDOctaveCommandBang); + class_addanything (pdoctave_command_class, (t_method) pDOctaveCommandAny); + post("pdoctave_command successfully loaded"); +} +#endif + |