4
Copyright (C) 2011 John ffitch
6
This file is part of Csound.
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.
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.
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
24
#include "csoundCore.h" /* CORFILES.C */
29
extern int csoundFileClose(CSOUND*, void*);
31
CORFIL *corfile_create_w(void)
33
CORFIL *ans = (CORFIL*)malloc(sizeof(CORFIL));
34
ans->body = (char*)calloc(100,1);
40
CORFIL *corfile_create_r(const char *text)
42
char *strdup(const char *);
43
CORFIL *ans = (CORFIL*)malloc(sizeof(CORFIL));
44
ans->body = strdup(text);
45
ans->len = strlen(text)+1;
50
void corfile_putc(int c, CORFIL *f)
54
f->body = (char*) realloc(f->body, f->len+=100);
58
void corfile_puts(char *s, CORFIL *f)
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++) {
68
f->body = (char*) realloc(f->body, f->len+=100);
71
/* put the extra NUL chars to the end */
73
f->body[f->p++] = '\0';
75
f->body = (char*) realloc(f->body, f->len+=100);
81
void corfile_flush(CORFIL *f)
83
f->len = strlen(f->body)+1;
84
f->body = (char*)realloc(f->body, f->len);
89
int corfile_length(CORFIL *f)
91
return strlen(f->body);
94
void corfile_rm(CORFIL **ff)
104
int corfile_getc(CORFIL *f)
106
int c = f->body[f->p];
107
if (c=='\0') return EOF;
112
#undef corfile_ungetc
113
void corfile_ungetc(CORFIL *f)
118
MYFLT corfile_get_flt(CORFIL *f)
122
while (!isspace(f->body[++f->p]));
123
ans = (MYFLT) atof(&f->body[n]);
127
#undef corfile_rewind
128
void corfile_rewind(CORFIL *f)
134
void corfile_reset(CORFIL *f)
141
int corfile_tell(CORFIL *f)
147
void corfile_set(CORFIL *f, int n)
152
void corfile_seek(CORFIL *f, int n, int dir)
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");
165
char *corfile_body(CORFIL *f)
170
#undef corfile_current
171
char *corfile_current(CORFIL *f)
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)
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);
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);