~ubuntu-branches/ubuntu/precise/csound/precise

« back to all changes in this revision

Viewing changes to Engine/corfiles.c

  • Committer: Package Import Robot
  • Author(s): Felipe Sateler
  • Date: 2012-04-19 09:26:46 UTC
  • mfrom: (3.2.19 sid)
  • Revision ID: package-import@ubuntu.com-20120419092646-96xbj1n6atuqosk2
Tags: 1:5.17.6~dfsg-1
* New upstream release
 - Do not build the wiimote opcodes (we need wiiuse).
* Add new API function to symbols file
* Disable lua opcodes, they were broken. Requires OpenMP to be enabled.
* Backport fixes from upstream:
  - Link dssi4cs with dl. Backport
  - Fix building of CsoundAC

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    corfiles.c:
 
3
 
 
4
    Copyright (C) 2011 John ffitch
 
5
 
 
6
    This file is part of Csound.
 
7
 
 
8
    The Csound Library is free software; you can redistribute it
 
9
    and/or modify it under the terms of the GNU Lesser General Public
 
10
    License as published by the Free Software Foundation; either
 
11
    version 2.1 of the License, or (at your option) any later version.
 
12
 
 
13
    Csound is distributed in the hope that it will be useful,
 
14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
    GNU Lesser General Public License for more details.
 
17
 
 
18
    You should have received a copy of the GNU Lesser General Public
 
19
    License along with Csound; if not, write to the Free Software
 
20
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 
21
    02111-1307 USA
 
22
*/
 
23
 
 
24
#include "csoundCore.h"     /*                              CORFILES.C      */
 
25
#include <string.h>
 
26
#include <stdio.h>
 
27
#include <ctype.h>
 
28
 
 
29
extern int csoundFileClose(CSOUND*, void*);
 
30
 
 
31
CORFIL *corfile_create_w(void)
 
32
{
 
33
    CORFIL *ans = (CORFIL*)malloc(sizeof(CORFIL));
 
34
    ans->body = (char*)calloc(100,1);
 
35
    ans->len = 100;
 
36
    ans->p = 0;
 
37
    return ans;
 
38
}
 
39
 
 
40
CORFIL *corfile_create_r(const char *text)
 
41
{
 
42
    char *strdup(const char *);
 
43
    CORFIL *ans = (CORFIL*)malloc(sizeof(CORFIL));
 
44
    ans->body = strdup(text);
 
45
    ans->len = strlen(text)+1;
 
46
    ans->p = 0;
 
47
    return ans;
 
48
}
 
49
 
 
50
void corfile_putc(int c, CORFIL *f)
 
51
{
 
52
    f->body[f->p++] = c;
 
53
    if (f->p >= f->len)
 
54
      f->body = (char*) realloc(f->body, f->len+=100);
 
55
    f->body[f->p] = '\0';
 
56
}
 
57
 
 
58
void corfile_puts(char *s, CORFIL *f)
 
59
{
 
60
    char *c;
 
61
    int n;
 
62
    /* skip and count the NUL chars to the end */
 
63
    for (n=0; f->p > 0 && f->body[f->p-1] == '\0'; n++, f->p--);
 
64
    /* append the string */
 
65
    for (c = s; *c != '\0'; c++) {
 
66
      f->body[f->p++] = *c;
 
67
      if (f->p >= f->len)
 
68
        f->body = (char*) realloc(f->body, f->len+=100);
 
69
    }
 
70
    if (n > 0) {
 
71
      /* put the extra NUL chars to the end */
 
72
      while (--n >= 0) {
 
73
        f->body[f->p++] = '\0';
 
74
        if (f->p >= f->len)
 
75
          f->body = (char*) realloc(f->body, f->len+=100);
 
76
      }
 
77
    }
 
78
    f->body[f->p] = '\0';
 
79
}
 
80
 
 
81
void corfile_flush(CORFIL *f)
 
82
{
 
83
    f->len = strlen(f->body)+1;
 
84
    f->body = (char*)realloc(f->body, f->len);
 
85
    f->p = 0;
 
86
}
 
87
 
 
88
#undef corfile_length
 
89
int corfile_length(CORFIL *f)
 
90
{
 
91
    return strlen(f->body);
 
92
}
 
93
 
 
94
void corfile_rm(CORFIL **ff)
 
95
{
 
96
    CORFIL *f = *ff;
 
97
    if (f!=NULL) {
 
98
      free(f->body);
 
99
      free(f);
 
100
      *ff = NULL;
 
101
    }
 
102
}
 
103
 
 
104
int corfile_getc(CORFIL *f)
 
105
{
 
106
    int c = f->body[f->p];
 
107
    if (c=='\0') return EOF;
 
108
    f->p++;
 
109
    return c;
 
110
}
 
111
 
 
112
#undef corfile_ungetc
 
113
void corfile_ungetc(CORFIL *f)
 
114
{
 
115
    --f->p;
 
116
}
 
117
 
 
118
MYFLT corfile_get_flt(CORFIL *f)
 
119
{
 
120
    int n = f->p;
 
121
    MYFLT ans;
 
122
    while (!isspace(f->body[++f->p]));
 
123
    ans = (MYFLT) atof(&f->body[n]);
 
124
    return ans;
 
125
}
 
126
 
 
127
#undef corfile_rewind
 
128
void corfile_rewind(CORFIL *f)
 
129
{
 
130
    f->p = 0;
 
131
}
 
132
 
 
133
#undef corfile_reset
 
134
void corfile_reset(CORFIL *f)
 
135
{
 
136
    f->p = 0;
 
137
    f->body[0] = '\0';
 
138
}
 
139
 
 
140
#undef corfile_tell
 
141
int corfile_tell(CORFIL *f)
 
142
{
 
143
    return f->p;
 
144
}
 
145
 
 
146
#undef corfile_set
 
147
void corfile_set(CORFIL *f, int n)
 
148
{
 
149
    f->p = n;
 
150
}
 
151
 
 
152
void corfile_seek(CORFIL *f, int n, int dir)
 
153
{
 
154
    if (dir == SEEK_SET) f->p = n;
 
155
    else if (dir == SEEK_CUR) f->p += n;
 
156
    else if (dir == SEEK_END) f->p = strlen(f->body)-n;
 
157
    if (f->p < 0 || f->p > strlen(f->body)) {
 
158
      printf("INTERNAL ERROR: Corfile seek out of range\n");
 
159
      exit(1);
 
160
    }
 
161
}
 
162
 
 
163
 
 
164
#undef corfile_body
 
165
char *corfile_body(CORFIL *f)
 
166
{
 
167
    return f->body;
 
168
}
 
169
 
 
170
#undef corfile_current
 
171
char *corfile_current(CORFIL *f)
 
172
{
 
173
    return f->body+f->p;
 
174
}
 
175
 
 
176
/* *** THIS NEEDS TO TAKE ACCOUNT OF SEARCH PATH *** */
 
177
void *fopen_path(CSOUND *csound, FILE **fp, char *name, char *basename,
 
178
                 char *env, int fromScore);
 
179
CORFIL *copy_to_corefile(CSOUND *csound, char *fname, char *env, int fromScore)
 
180
{
 
181
    CORFIL *mm;
 
182
    FILE *ff;
 
183
    void *fd;
 
184
    int n;
 
185
    char buffer[1024];
 
186
 
 
187
    fd = fopen_path(csound, &ff, fname, NULL, env, fromScore);
 
188
    if (ff==NULL) return NULL;
 
189
    mm = corfile_create_w();
 
190
    memset(buffer, '\0', 1024);
 
191
    while ((n = fread(buffer, 1, 1023, ff))) {
 
192
      corfile_puts(buffer, mm);
 
193
      memset(buffer, '\0', 1024);
 
194
    }
 
195
    corfile_putc('\0', mm);     /* For use in bison/flex */
 
196
    corfile_putc('\0', mm);     /* For use in bison/flex */
 
197
    if (fromScore) corfile_flush(mm);
 
198
    csoundFileClose(csound, fd);
 
199
    return mm;
 
200
}
 
201