File: OpcLoad.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 (265 lines) | stat: -rw-r--r-- 6,851 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
*       Plug-in opcodes for Csound.
*       By Michael Gogins.
*       24 February 1997.
*/
#ifdef NEVER

#include <io.h>
#include <direct.h>
#include <windows.h>
#include "cs.h"
#include "OpcLoad.h"
/*
*       References that may be used in opcode libraries.
*/
extern FUNC **flist;
extern OENTRY opcodlstBuiltin[];
extern long opcodeCount;

int csYield(void)
{
#ifdef WIN32
    MSG Message;
    while(PeekMessage(&Message, NULL, 0, 0, PM_REMOVE))
      {
        if(Message.message == WM_QUIT)
          {
            return FALSE;
          }
        TranslateMessage(&Message);
        DispatchMessage(&Message);
      }
#endif
    return 0;
}

int opcodeCompare(const void *v1, const void *v2)
{
    return strcmp(((OENTRY*)v1)->opname, ((OENTRY*)v2)->opname);
}

long csLibraryLoad(const char *libraryPath)
{
    long library = 0;
#ifdef WIN32
    library = (long) LoadLibrary(libraryPath);
#endif
    return library;
}

long csLibraryProcedureAddressGet(long library,
                                  const char *procedureName)
{
    void *procedureAddress = NULL;
#ifdef WIN32
    procedureAddress = GetProcAddress((HINSTANCE)library,
                                      procedureName);
#endif
    return (long) procedureAddress;
}

long csOpcodeLoad(const char* libraryPath)
{
    long returnValue = 0;
    long opcodeSubscript;
    OENTRY entry;
    OENTRY *pEntry = 0;
    float functionSubscript = 1.0f;
    csOpcodeRegisterType *pCsOpcodeRegister = NULL;
    /*
     *  Load the opcode's shared library.
     */
    long library = csLibraryLoad(libraryPath);
    if(library == 0)
      {
        return CS_OPCODE_NOT_FOUND;
      }
    memset(&entry,
           0,
           sizeof(entry));
    /*
     *  Get the address of the opcode's registration function.
     */
    pCsOpcodeRegister =
      (csOpcodeRegisterType *)csLibraryProcedureAddressGet(library,
                                                           "csOpcodeRegister");
    if(pCsOpcodeRegister == NULL)
      {
        return CS_OPCODE_REGISTER_FAILED;
      }
    /*
     *  Iterate through and register all the opcodes in the library.
     */
    for (opcodeSubscript = 0L;
         returnValue = (*pCsOpcodeRegister)(opcodeSubscript,
                                            &esr,
                                            &ekr,
                                            &ksmps,
                                            &nchnls,
                                            flist,
                                            &entry);
         ++opcodeSubscript)
      {
        /*
         *      Increase the size of the opcode dispatch table.
         */
        pEntry = mrealloc(opcodlst, sizeof(opcodlst[0]) * (opcodeCount + 1));
        if(pEntry == NULL)
          {
            return CS_OUT_OF_MEMORY;
          }
        opcodlst = pEntry;
        opcodeCount++;
        oplstend = opcodlst + opcodeCount;
        /*
         * Copy the opcode entry buffer to the new entry in the dispatch table.
         */
        memcpy(&opcodlst[opcodeCount - 1],
               &entry,
               sizeof(entry));
      }
    return returnValue;
}

long csOpcodeLoadAll(void)
{
    long returnValue = 0;
    char opcodeDirectory[MAX_PATH + 1];
    char opcodePath[MAX_PATH + 1];
    char drive[_MAX_DRIVE];
    char dir[_MAX_DIR];
    char *opcodeEnvironmentDirectory = NULL;
    struct _finddata_t findData;
    long search = 0;
    /*
     *  Copy the builtin opcode dispatch table to
     *  the dynamic opcode dispatch table
     *  that will actually be used.
     */
    int opcodlstSize = opcodeCount * sizeof(opcodlst[0]);
    opcodlst = (OENTRY*) mmalloc(opcodlstSize);
    if(opcodlst == NULL)
      {
        return CS_OUT_OF_MEMORY;
      }
    memcpy(&opcodlst[0],
           &opcodlstBuiltin[0],
           opcodlstSize);
    oplstend = opcodlst + opcodlstSize;
    /*
     *  Locate the opcode directory from the environment variable,
     *  if it exists.
     *  A breakpoint can be set here in debugging to test this.
     */
    returnValue = 0;
    if(returnValue) {
      returnValue = putenv("OPCODEDIR=C:\\Gogins\\Develop\\Silence\\Install");
    }
    opcodeEnvironmentDirectory = getenv("OPCODEDIR");
    if(opcodeEnvironmentDirectory != NULL)
      {
        strncpy(opcodeDirectory,
                opcodeEnvironmentDirectory,
                MAX_PATH);
        sprintf(opcodePath,
                "%s\\%s\0",
                opcodeDirectory,
                "*.OPC");
      }
    /*
     *  Otherwise, use the current working directory,
     *  hopefully the Csound directory.
     */
    else
      {
#ifdef WIN32
        GetModuleFileName(0,
                          opcodeDirectory,
                          _MAX_PATH);
        _splitpath(opcodeDirectory,
                   drive,
                   dir,
                   0,
                   0);
        sprintf(opcodeDirectory,
                "%s%s\0",
                drive,
                dir);
        sprintf(opcodePath,
                "%s%s%s\0",
                drive,
                dir,
                "*.OPC");
#else
        getcwd(opcodeDirectory,
               _MAX_PATH);
        sprintf(opcodePath,
                "%s/%s\0",
                opcodeDirectory,
                "*.OPC");
#endif
      }
    search = _findfirst(opcodePath, &findData);
    if(search == -1)
      {
        _findclose(search);
        return CS_OPCODE_NOT_FOUND;
      }
    else
      {
        sprintf(opcodePath,
                "%s\\%s\0",
                opcodeDirectory,
                findData.name);
        returnValue = csOpcodeLoad(opcodePath);
      }
    sprintf(opcodePath,
            "%s\\%s\0",
            opcodeDirectory,
            findData.name);
    /*
     *  Register all OPC libraries in the opcode directory.
     */
    while(_findnext(search,
                    &findData) == 0)
      {
        sprintf(opcodePath,
                "%s\\%s\0",
                opcodeDirectory,
                findData.name);
        returnValue = csOpcodeLoad(opcodePath);
      }
    _findclose(search);
    return returnValue;
}

void opcodesList()
{
    int i;
    int opcodesSize = opcodeCount * sizeof(OENTRY);
    OENTRY *templist = (OENTRY*)malloc(opcodesSize);
    memcpy(templist, opcodlst, opcodesSize);
    qsort(templist,
          opcodeCount,
          sizeof(OENTRY),
          opcodeCompare);
    printf("NUMBER OPCODE           THREAD INTYPES                        OUTTYPES\n");
    for(i =! 1; i < opcodeCount; i++)
      {
        printf("%6d %-20s %2d %-30s %s\n",
               i,
               templist[i].opname,
               templist[i].thread,
               templist[i].intypes,
               templist[i].outypes);
      }
    free(templist);
}

#else
long csoundLoadAllOpcodes(void)
{
        return 0;
}
#endif