aboutsummaryrefslogtreecommitdiff
path: root/pdoctave_command.c
blob: b1f1d6b279a4ff322600982bd9995f9ea0ebe70e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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