strcmp.c 4.55 KB
Newer Older
Jonathan Wilkes's avatar
Jonathan Wilkes committed
1
/*
Ivica Bukvic's avatar
Ivica Bukvic committed
2
 *  strcmp    : compare 2 lists as if they were strings
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
3
 *
Ivica Bukvic's avatar
Ivica Bukvic committed
4
 * (c) 1999-2011 IOhannes m zmölnig, forum::für::umläute, institute of electronic music and acoustics (iem)
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
5
 *
Ivica Bukvic's avatar
Ivica Bukvic committed
6
7
8
9
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
Jonathan Wilkes's avatar
Jonathan Wilkes committed
10
 *
Ivica Bukvic's avatar
Ivica Bukvic committed
11
12
13
14
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
Jonathan Wilkes's avatar
Jonathan Wilkes committed
15
 *
Ivica Bukvic's avatar
Ivica Bukvic committed
16
17
18
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
19
20

#include "zexy.h"
21
#include "zexy_strndup.h"
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
22
23
24
25
26
27
28
#include <stdlib.h>
#include <string.h>

/* ------------------------- strcmp ------------------------------- */

/* compare 2 lists ( == for lists) */

29
30
static t_class *strcmp_class=NULL;
static t_class *strcmp_proxy_class=NULL;
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
31
32


Jonathan Wilkes's avatar
Jonathan Wilkes committed
33
typedef struct _strcmp {
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
34
35
36
37
38
39
40
41
  t_object x_obj;
  struct _strcmp_proxy  *x_proxy;

  t_binbuf *bbuf1, *bbuf2;
  char *str1, *str2;
  int n1, n2;
} t_strcmp;

Jonathan Wilkes's avatar
Jonathan Wilkes committed
42
typedef struct _strcmp_proxy {
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
43
44
45
46
47
48
49
50
  t_pd  p_pd;
  t_strcmp    *p_master;
  t_inlet *p_in;
} t_strcmp_proxy;

static void strcmp_bang(t_strcmp *x)
{
  int result = 0;
Jonathan Wilkes's avatar
Jonathan Wilkes committed
51
52
  if(x->str1) {
    if(x->str2) {
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
53
      result = strcmp(x->str1, x->str2);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
54
    } else {
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
55
      result = *x->str1;
Jonathan Wilkes's avatar
Jonathan Wilkes committed
56
    }
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
57
  } else {
Jonathan Wilkes's avatar
Jonathan Wilkes committed
58
    if(x->str2) {
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
59
      result = -*x->str2;
Jonathan Wilkes's avatar
Jonathan Wilkes committed
60
    } else {
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
61
      result = 0;
Jonathan Wilkes's avatar
Jonathan Wilkes committed
62
    }
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
63
64
65
66
67
  }

  outlet_float(x->x_obj.ob_outlet, result);
}

Jonathan Wilkes's avatar
Jonathan Wilkes committed
68
69
static void list2binbuf(t_binbuf**bbuf, int *n, char**str, int argc,
                        t_atom*argv)
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
70
71
72
{
  int i=0;
  char*s=0;
Jonathan Wilkes's avatar
Jonathan Wilkes committed
73
74
75
  if(*str&&*n) {
    freebytes(*str, *n);
  }
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
76
77
78

  binbuf_clear(*bbuf);
  binbuf_add(*bbuf, argc, argv);
Albert Gräf's avatar
Albert Gräf committed
79
  binbuf_gettext(*bbuf, str, &i);
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
80

Albert Gräf's avatar
Albert Gräf committed
81
82
83
84
  s = t_resizebytes(*str, i, i+1);
  s[i] = 0;
  *str = s;
  *n = i+1;
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
85
86
}

Jonathan Wilkes's avatar
Jonathan Wilkes committed
87
88
static void strcmp_list(t_strcmp *x, t_symbol* UNUSED(s), int argc,
                        t_atom *argv)
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
89
90
91
92
93
94
{
  list2binbuf(&x->bbuf1, &x->n1, &x->str1, argc, argv);
  strcmp_bang(x);
}
static void strcmp_symbol(t_strcmp *x, t_symbol *s)
{
Jonathan Wilkes's avatar
Jonathan Wilkes committed
95
96
97
  if(x->str1&&x->n1) {
    freebytes(x->str1, x->n1);
  }
98
99
  x->str1=zexy_strndup(s->s_name, MAXPDSTRING);
  x->n1=strnlen(x->str1, MAXPDSTRING);
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
100
101
102
  strcmp_bang(x);
}

Jonathan Wilkes's avatar
Jonathan Wilkes committed
103
104
static void strcmp_secondlist(t_strcmp *x, t_symbol* UNUSED(s), int argc,
                              t_atom *argv)
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
105
106
107
108
109
{
  list2binbuf(&x->bbuf2, &x->n2, &x->str2, argc, argv);
}
static void strcmp_secondsymbol(t_strcmp *x, t_symbol *s)
{
Jonathan Wilkes's avatar
Jonathan Wilkes committed
110
111
112
  if(x->str2&&x->n2) {
    freebytes(x->str2, x->n2);
  }
113
114
  x->str2=zexy_strndup(s->s_name, MAXPDSTRING);
  x->n2=strnlen(x->str2, MAXPDSTRING);
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
115
116
}

Jonathan Wilkes's avatar
Jonathan Wilkes committed
117
118
static void strcmp_proxy_list(t_strcmp_proxy *y, t_symbol *s, int argc,
                              t_atom *argv)
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
119
120
121
122
123
{
  strcmp_secondlist(y->p_master, s, argc, argv);
}
static void strcmp_proxy_symbol(t_strcmp_proxy *y, t_symbol *s)
{
Jonathan Wilkes's avatar
Jonathan Wilkes committed
124
125
126
  if(s) {
    strcmp_secondsymbol(y->p_master, s);
  }
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
127
128
}

Ivica Bukvic's avatar
Ivica Bukvic committed
129
static void *strcmp_new(t_symbol* UNUSED(s), int argc, t_atom *argv)
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
{
  t_strcmp *x = (t_strcmp *)pd_new(strcmp_class);

  x->x_proxy=(t_strcmp_proxy*)pd_new(strcmp_proxy_class);
  x->x_proxy->p_master = x;
  x->x_proxy->p_in = inlet_new ((t_object*)x, (t_pd*)x->x_proxy, 0,0);

  outlet_new(&x->x_obj, 0);

  x->bbuf1 = binbuf_new();
  x->bbuf2 = binbuf_new();

  x->str1=0;
  x->str2=0;
  x->n1=0;
  x->n2=0;

Jonathan Wilkes's avatar
Jonathan Wilkes committed
147
148
149
  if(argc) {
    strcmp_secondlist(x, gensym("list"), argc, argv);
  }
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
150
151
152
153
154
155
156
157

  return (x);
}

static void strcmp_free(t_strcmp *x)
{
  binbuf_free(x->bbuf1);
  binbuf_free(x->bbuf2);
Jonathan Wilkes's avatar
Jonathan Wilkes committed
158
159
160
161
162
163
  if(x->str1&&x->n1) {
    freebytes(x->str1, x->n1);
  }
  if(x->str2&&x->n2) {
    freebytes(x->str2, x->n2);
  }
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
164
165
166
167
168
169
170

  inlet_free(x->x_proxy->p_in);
  x->x_proxy->p_master=0;
  pd_free(&x->x_proxy->p_pd);
}


171
static void strcmp_help(t_strcmp*UNUSED(x))
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
172
{
Jonathan Wilkes's avatar
Jonathan Wilkes committed
173
  post("\n"HEARTSYMBOL " strcmp\t\t:: compare to lists as strings");
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
174
175
176
}


177
ZEXY_SETUP void strcmp_setup(void)
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
178
{
179
180
  strcmp_class = zexy_new("strcmp",
                          strcmp_new, strcmp_free, t_strcmp, 0, "*");
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
181
182
183
184
185

  class_addbang    (strcmp_class, strcmp_bang);
  class_addsymbol  (strcmp_class, strcmp_symbol);
  class_addlist    (strcmp_class, strcmp_list);

186
187
  strcmp_proxy_class = zexy_new("strcmp proxy",
                                0, 0, t_strcmp_proxy, CLASS_PD | CLASS_NOINLET, "");
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
188
189
  class_addsymbol(strcmp_proxy_class, strcmp_proxy_symbol);
  class_addlist(strcmp_proxy_class, strcmp_proxy_list);
190
  zexy_addmethod(strcmp_class, (t_method)strcmp_help, "help", "");
Ivica Ico Bukvic's avatar
Ivica Ico Bukvic committed
191
192
  zexy_register("strcmp");
}