aboutsummaryrefslogtreecommitdiff
path: root/split.c
blob: e3d57a1872febf83ee33874c20d57c5a19b02caf (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <m_pd.h>
#include <string.h>
#include <math.h>
#ifdef NT
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#endif

/* this is taken from ggee, where the file was hanging around but the object was not
   funtional. i keep it here for reference since i dont wnat to fix it over and over ;)
   but its not included in the makefile to avoid namespace clash with ggee.prepend
   anyhow, i ll just rename it cxc.prepend
*/

/* ------------------------ split ----------------------------- */

static t_class *split_class;


typedef struct _split
{
     t_object x_obj;
     t_symbol* x_splitter;
} t_split;


void split_symbol(t_split *x, t_symbol *s)
{
  t_atom* a;
  split_anything(x, s, 0, a);
}


// move these to generic location later on
int split_string_isnum(char* s)
{
  // int isnum = 1;
  char tc;
  while((tc = *s++) != '\0') {
    // tc= s;
    switch(tc) {
    case 48: case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57:
      // post("yo numba: %c", tc);
      break;
    default:
      return 0;
      break;
    }
  }
  return 1;
}

float split_string2float(char* s)
{
  // check for '.' ...
  int isnum = 1, cnt = strlen(s) - 1;
  char tc;
  int iret = 0, itmp = 0;
  double factor = 10.;
  float fret;
  while((tc = *s++) != '\0') {
    // tc= s;
    switch(tc) {
    case 48: case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57:
      itmp = (int)pow(factor, (double)cnt);
      // post("tempchar: %c, iret: %d, itmp: %d", tc, iret, itmp);
      iret = iret + ((tc - 48) * itmp);
      // post("tempchar: %c, iret: %d, cnt: %d", tc, iret, cnt);
      break;
    default:
      break;
    }
    cnt--;
  }
  return (float)iret;
}

void split_anything(t_split *x,t_symbol* s,t_int argc,t_atom* argv)
{
     int i = argc; int j;
     t_symbol* cur;
     t_atom a_out[256];
     int    c_out = 0;
     t_atom* a = a_out;
     // char *str;
     char u[MAXPDSTRING];
     char v[MAXPDSTRING];
     // char *v;
     u[0] = '\0';
     v[0] = '\0';
     int isnum = 1;
     float tf;

     for(j=0; j<strlen(s->s_name); j++) {
       u[0] = s->s_name[j];
       if(u[0] == x->x_splitter->s_name[0]) {
	 if(v[0] != '\0') {
	   // check if string is digits only
	   if(split_string_isnum(v)) {
	     split_string2float(v);
	     tf = split_string2float(v);
	     SETFLOAT(a, tf);
	     // tf = 0.0;
	   }
	   else {
	     SETSYMBOL(a, gensym(v));
	   }
	   a++; c_out++;
	   // reset stuff
	   v[0] = '\0';
	   isnum = 1;
	 } // v[0] != '\0'
       } else {
/* 	   if(!(48 <= u[0] <= 57)) { */
/* 	     isnum = 0; */
/* 	   } */
	 strncat(v, u, 1);
       } // char matches splitter
     }

     // have to do this again here, damn.
     if(split_string_isnum(v)) {
       split_string2float(v);
       tf = split_string2float(v);
       SETFLOAT(a, tf);
       tf = 0.0;
     }
     else {
       SETSYMBOL(a, gensym(v));
     }
     a++, c_out++;


/* #if 1 */
/*      //     post("sym: %s",s->s_name); */
/*      SETSYMBOL(a,s); */
/*      a++; */
/*      c_out++; */
/* #endif */

/*      while (i--) { */
/*        switch( argv->a_type) { */
/*        case A_FLOAT: */
/* 	 //	 post("flo: %f",atom_getfloat(argv)); */
/* 	 SETFLOAT(a,atom_getfloat(argv)); */
/* 	 a++; */
/* 	 c_out++; */
/* 	 break; */
/*        case A_SYMBOL: */
/* 	 //	 post("sym: %s",atom_getsymbol(argv)->s_name); */
/* 	 SETSYMBOL(a,atom_getsymbol(argv)); */
/* 	 a++; */
/* 	 c_out++; */
/* 	 break; */
/*        default: */
/* 	 post("split.c: unknown type"); */
/*        } */
/*        argv++; */
/*      } */
     
       outlet_list(x->x_obj.ob_outlet, &s_list, c_out, (t_atom*)&a_out);
       // outlet_anything(x->x_obj.ob_outlet,gensym("list"),c_out,(t_atom*)&a_out);
/*      //post("done"); */
}

void split_list(t_split *x,t_symbol* s,t_int argc,t_atom* argv)
{
     int i = argc;
     t_symbol* cur;
     t_atom a_out[256];
     int    c_out = 0;
     t_atom* a = a_out;

     while (i--) {
       switch( argv->a_type) {
       case A_FLOAT:
	 //	 post("flo: %f",atom_getfloat(argv));
	 SETFLOAT(a,atom_getfloat(argv));
	 a++;
	 c_out++;
	 break;
       case A_SYMBOL:
	 //	 post("sym: %s",atom_getsymbol(argv)->s_name);
	 SETSYMBOL(a,atom_getsymbol(argv));
	 a++;
	 c_out++;
	 break;
       default:
	 post("split.c: unknown type");
       }
       argv++;
     }
     
     outlet_anything(x->x_obj.ob_outlet,x->x_splitter,c_out,(t_atom*)&a_out);
     //post("done");
}

static void *split_new(t_symbol* s)
{
    t_split *x = (t_split *)pd_new(split_class);
    outlet_new(&x->x_obj, &s_float);
    if (s != &s_)
	 x->x_splitter = s;
    else
	 x->x_splitter = gensym("cxc.split");
    return (x);
}

static void split_set(t_split *x, t_symbol *s)
{
  t_symbol *t;
  // init temp splitter
  char u[1]; u[0] = '\0';

  if(strlen(s->s_name) > 1) {
    // t = gensym((char*)s->s_name[0]);
    // post("%d", s->s_name[0]);
    strncat(u, s->s_name, 1);
    t = gensym(u);
  } else 
    t = s;
  x->x_splitter = t;
}

void split_setup(void)
{
    split_class = class_new(gensym("cxc.split"), (t_newmethod)split_new, 0,
				sizeof(t_split), 0,A_DEFSYM,NULL);
    class_addlist(split_class, split_list);
    class_addanything(split_class,split_anything);
    class_addmethod(split_class, (t_method)split_set, gensym("set"), A_SYMBOL, 0);
    class_addsymbol(split_class, split_symbol);
}