File: opcode.c

package info (click to toggle)
csound 1%3A4.23f12-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,132 kB
  • ctags: 17,345
  • sloc: ansic: 101,063; cpp: 7,730; perl: 335; makefile: 318; tcl: 82
file content (140 lines) | stat: -rw-r--r-- 4,090 bytes parent folder | download
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
/*  
    opcode.c:

    Copyright (C) 1997 John ffitch

    This file is part of Csound.

    The Csound Library is free software; you can redistribute it
    and/or modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    Csound 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 Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with Csound; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA
*/

                                /* OPCODE.C */
                                /* Print opcodes in system */

                                /* John ffitch -- 26 Jan 97 */
                                /*  4 april 02 -- ma++ */
                                /*  restructure to retrieve externally  */
#include "opcode.h"
#include "cs.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int mystrcmp(const void *v1, const void *v2)
{
    sortable *s1 = (sortable *)v1;
    sortable *s2 = (sortable *)v2;
    int ans;
/*     printf("Compare %s and %s =>", s1, s2); */
/* Make this stop at _ would improve things */
    ans = strcmp(s1->name, s2->name);
/*     printf(" %d\n", ans); */
    return ans;
}

/* create a list of all the opcodes */
/* caller is responsible for disposing the*/
/* returned list! - use dispose_opcode_list() */
opcodelist *new_opcode_list(void)
{
    OENTRY *ops = opcodlst;
    opcodelist *list = (opcodelist *)mmalloc(sizeof(opcodelist));

                                /* All this hassle 'cos of MAC */
    long n = (long)((char*)oplstend-(char *)opcodlst);
    n /= sizeof(OENTRY);
    n *= sizeof(sortable);

    list->size = 0;
    list->table = (sortable*)mmalloc(n);
                                /* Skip first entry */
    while (++ops<oplstend) {
      char *x = mmalloc(strlen(ops->opname)+1);
      strcpy(x, ops->opname);
      list->table[list->size].name = x;
      if ((x=strchr(x,'_'))) *x = '\0';
      list->table[list->size].ans = ops->outypes;
      list->table[list->size].args = ops->intypes;
      if (ops->outypes == NULL && ops->intypes == NULL)
        n--;
      else
        list->size ++;
    }
                                /* Sort into alphabetical order */
    printf(Str(X_37,"%d opcodes\n"), list->size);
    qsort(list->table, list->size, sizeof(sortable), mystrcmp);

   return list;
}

void dispose_opcode_list(opcodelist *list)
{
    if (list) {
      while (list->size--) {
        mfree(list->table[list->size].name);
      }

      mfree(list->table);
      mfree(list);
    }
}

void list_opcodes(int level)
{
    int j, k;
    int len = 0;

    opcodelist *list = new_opcode_list();

                                /* Print in 4 columns */
    for (j = 0, k = 0; j< list->size; j++) {
      if (level == 0) {
        if (j>0 && strcmp( list->table[j-1].name,  list->table[j].name)==0) continue;
        k++;
        if ((k%3)==0) {
          printf("\n"); len = 0;
        }
        else {
          do {
            printf(" ");
            len++;
          } while (len<20*(k%3));
        }
        printf("%s", list->table[j].name);
        len += strlen(list->table[j].name);
      }
      else {
        char *ans = list->table[j].ans, *arg = list->table[j].args;
        printf("%s", list->table[j].name);
        len = strlen(list->table[j].name);
        do {
          printf(" ");
          len++;
        } while (len<10);
        if (ans==NULL || *ans=='\0') ans = "(null)";
        if (arg==NULL || *arg=='\0') arg = "(null)";
        printf("%s", ans);
        len = strlen(ans);
        do {
          printf(" ");
          len++;
        } while (len<8);
        printf("%s\n", arg);
      }
    }
    printf("\n");
    dispose_opcode_list(list);
}