static int Load_LP_File_(CSOUND *csound, const char *filnam, char **allocp, int32 *len) { FILE *f; int length = 4096; unsigned int i = 0; int j = 0; MYFLT x; char *all, *p; LPHEADER lph = {0,0,0,0,0.0,0.0,0.0,{0}}; char buff[120]; f = fopen(filnam, "r"); csoundNotifyFileOpened(csound, filnam, CSFTYPE_LPC, 0, 0); all = (char *)csound->Malloc(csound, (size_t) length); for (i=0; i<6; i++) fgetc(f); /* Skip LPANAL */ if (4!=fscanf(f, "%d %d %d %d\n", &lph.headersize, &lph.lpmagic, &lph.npoles, &lph.nvals)) return csound->InitError(csound, Str("Ill-formed LPC file\n")); fgets(buff, 120, f); lph.framrate = (MYFLT)cs_strtod(buff, &p); lph.srate = (MYFLT)cs_strtod(p, &p); lph.duration = (MYFLT)cs_strtod(p, &p); /* lph.text[0] = (char)strtol(p, &p, 0); */ /* lph.text[1] = (char)strtol(p, &p, 0); */ /* lph.text[2] = (char)strtol(p, &p, 0); */ /* lph.text[3] = (char)strtol(p, &p, 0); */ /* printf("LPHeader %d %d %d %d\n%f %f %f\n", */ /* lph.headersize, lph.lpmagic, lph.npoles, lph.nvals, */ /* lph.framrate, lph.srate, lph.duration); */ /* fscanf(f, "%f %f %f %.2x %.2x %.2x %.2x\n", */ /* &lph.framrate, &lph.srate, &lph.duration, */ /* &lph.text[0], &lph.text[1], &lph.text[2], &lph.text[3]); */ // This needs surgery if in/out different MYFLT sizes *** FIX ME *** lph.headersize = sizeof(int32)*4+sizeof(MYFLT)*3; memcpy(&all[0], &lph, lph.headersize); /* Read data until end, pack as MYFLTs */ for (i=lph.headersize;;i+=sizeof(MYFLT)) { /* Expand as necessary */ if (i>=length-sizeof(MYFLT)-8) { //printf("expanding from %p[%d] to\n", all, length); all = csound->ReAlloc(csound, all, length+=4096); //printf("i=%d %p[%d]\n", i, all, length); } x = read_ieee(f, &j); if (j) break; memcpy(&all[i], &x, sizeof(MYFLT)); } fclose(f); /* and close it */ printf("length=%d i=%u\n", length, i); *len = i; all = csound->ReAlloc(csound, all, i); *allocp = all; return 0; /* return 0 for OK */ }
static int Load_CV_File_(CSOUND *csound, const char *filnam, char **allocp, int32 *len) { FILE *f; int length = 4096; unsigned int i = 0; int j = 0; MYFLT x; char *all; CVSTRUCT cvh = {0,0,0,0,0.0,0,0,0,0,{0}}; char buff[120]; char *p; f = fopen(filnam, "r"); csoundNotifyFileOpened(csound, filnam, CSFTYPE_CVANAL, 0, 0); all = (char *)csound->Malloc(csound, (size_t) length); (void)fgets(buff, 120, f); /* Skip CVANAL */ cvh.magic = CVMAGIC; p = fgets(buff, 120, f); if (p==NULL) return csoundInitError(csound, Str("Ill-formed CV file\n")); cvh.headBsize = strtol(p, &p, 10); cvh.dataBsize = strtol(p, &p, 10); cvh.dataFormat = strtol(p, &p, 10); cvh.samplingRate = (MYFLT)cs_strtod(p, &p); cvh.src_chnls = strtol(p, &p, 10); cvh.channel = strtol(p, &p, 10); cvh.Hlen = strtol(p, &p, 10); cvh.Format = strtol(p, &p, 10); /* fscanf(f, "%d %d %d %g %d %d %d %d\n", */ /* &cvh.headBsize, &cvh.dataBsize, &cvh.dataFormat, */ /* &cvh.samplingRate, &cvh.src_chnls, &cvh.channel, */ /* &cvh.Hlen, &cvh.Format); */ cvh.headBsize = sizeof(int32)*8 + sizeof(MYFLT); memcpy(&all[0], &cvh, sizeof(CVSTRUCT)); /* Read data until end, pack as MYFLTs */ for (i=sizeof(CVSTRUCT);;i+=sizeof(MYFLT)) { /* Expand as necessary */ if (i>=length-sizeof(MYFLT)-4) { //printf("expanding from %p[%d] to\n", all, length); all = csound->ReAlloc(csound, all, length+=4096); //printf("i=%d %p[%d]\n", i, all, length); } x = read_ieee(f, &j); if (j) break; memcpy(&all[i], &x, sizeof(MYFLT)); } fclose(f); /* and close it */ //printf("length=%d i=%d\n", length, i); *len = i; all = csound->ReAlloc(csound, all, i); *allocp = all; return 0; /* return 0 for OK */ }
static MYFLT read_ieee(FILE* f, int *end) { char buff[120]; double x; char *p = fgets(buff, 120, f); if (p==NULL || feof(f)) { *end = 1; return FL(0.0); } x = cs_strtod(buff, NULL); return (MYFLT)x; /* union { */ /* double d; */ /* struct {int j,k;} n; */ /* int64_t i; */ /* } x; */ /* int sign=1, ex; */ /* int64_t man; */ /* int64_t bit = 1; */ /* char buff[32]; */ /* char *p; */ /* bit <<= 62; */ /* p = fgets(buff, 32, f); */ /* printf("... %s", buff); */ /* if (p==NULL || feof(f)) { */ /* printf("ending\n"); */ /* *end = 1; */ /* return FL(0.0); */ /* } */ /* if (strstr(p, "0x0p+0")) { */ /* return FL(0.0); */ /* } */ /* if (buff[0]=='-') sign=-1; */ /* p = strchr(buff, '.')+1; */ /* sscanf(p, "%lxp%d", &man, &ex); */ /* x.i = man; */ /* if (man!=(int64_t)0) x.i |= bit; */ /* x.d = ldexp(x.d, ex-1); */ /* if (sign<0) x.d =-x.d; */ /* return (MYFLT)x.d; */ }
/* Callback for json_walk() */ static void frozen_cb(void *data, const char *name, size_t name_len, const char *path, const struct json_token *token) { struct json_parse_ctx *ctx = (struct json_parse_ctx *) data; v7_val_t v = V7_UNDEFINED; (void) path; v7_own(ctx->v7, &v); switch (token->type) { case JSON_TYPE_STRING: v = v7_mk_string(ctx->v7, token->ptr, token->len, 1 /* copy */); break; case JSON_TYPE_NUMBER: v = v7_mk_number(ctx->v7, cs_strtod(token->ptr, NULL)); break; case JSON_TYPE_TRUE: v = v7_mk_boolean(ctx->v7, 1); break; case JSON_TYPE_FALSE: v = v7_mk_boolean(ctx->v7, 0); break; case JSON_TYPE_NULL: v = V7_NULL; break; case JSON_TYPE_OBJECT_START: v = v7_mk_object(ctx->v7); break; case JSON_TYPE_ARRAY_START: v = v7_mk_array(ctx->v7); break; case JSON_TYPE_OBJECT_END: case JSON_TYPE_ARRAY_END: { /* Object or array has finished: deallocate its frame */ ctx->frame = free_json_frame(ctx, ctx->frame); } break; default: LOG(LL_ERROR, ("Wrong token type %d\n", token->type)); break; } if (!v7_is_undefined(v)) { if (name != NULL && name_len != 0) { /* Need to define a property on the current object/array */ if (v7_is_object(ctx->frame->val)) { v7_set(ctx->v7, ctx->frame->val, name, name_len, v); } else if (v7_is_array(ctx->v7, ctx->frame->val)) { /* * TODO(dfrank): consult name_len. Currently it's not a problem due to * the implementation details of frozen, but it might change */ int idx = (int) cs_strtod(name, NULL); v7_array_set(ctx->v7, ctx->frame->val, idx, v); } else { LOG(LL_ERROR, ("Current value is neither object nor array\n")); } } else { /* This is a root value */ assert(ctx->frame == NULL); /* * This value will also be the overall result of JSON parsing * (it's already owned by the `v7_alt_json_parse()`) */ ctx->result = v; } if (token->type == JSON_TYPE_OBJECT_START || token->type == JSON_TYPE_ARRAY_START) { /* New object or array has just started, so we need to allocate a frame * for it */ struct json_parse_frame *new_frame = alloc_json_frame(ctx, v); new_frame->up = ctx->frame; ctx->frame = new_frame; } } v7_disown(ctx->v7, &v); }
static void sensLine(CSOUND *csound, void *userData) { char *cp, *Linestart, *Linend; int c, n, pcnt; IGN(userData); while (1) { Linend = STA(Linep); if (csound->Linefd >= 0) { n = read(csound->Linefd, Linend, STA(Linebufend) - Linend); Linend += (n > 0 ? n : 0); } if (Linend <= STA(Linebuf)) break; Linestart = STA(Linebuf); cp = Linestart; while (containsLF(Linestart, Linend)) { EVTBLK e; char *sstrp = NULL; int scnt = 0; int strsiz = 0; memset(&e, 0, sizeof(EVTBLK)); e.strarg = NULL; e.scnt = 0; c = *cp; while (c == ' ' || c == '\t') /* skip initial white space */ c = *(++cp); if (c == LF) { /* if null line, bugout */ Linestart = (++cp); continue; } switch (c) { /* look for legal opcode */ case 'e': /* Quit realtime */ case 'i': case 'q': case 'f': case 'a': e.opcod = c; break; default: csound->ErrorMsg(csound, Str("unknown opcode %c"), c); goto Lerr; } /* for params that follow: */ pcnt = 0; do { char *newcp; do { /* skip white space */ c = *(++cp); } while (c == ' ' || c == '\t'); if (c == LF) break; pcnt++; if (c == '"') { /* if find character string */ if (e.strarg == NULL) e.strarg = sstrp = csound->Malloc(csound, strsiz=SSTRSIZ); n = scnt; while (n-->0) sstrp += strlen(sstrp)+1; n = 0; while ((c = *(++cp)) != '"') { if (UNLIKELY(c == LF)) { csound->ErrorMsg(csound, Str("unmatched quotes")); goto Lerr; } sstrp[n++] = c; /* save in private strbuf */ if (UNLIKELY((sstrp-e.strarg)+n >= strsiz-10)) { e.strarg = csound->ReAlloc(csound, e.strarg, strsiz+=SSTRSIZ); sstrp = e.strarg+n; } } sstrp[n] = '\0'; { union { MYFLT d; int32 i; } ch; ch.d = SSTRCOD; ch.i += scnt++; e.p[pcnt] = ch.d; /* set as string with count */ } e.scnt = scnt; continue; } if (UNLIKELY(!(isdigit(c) || c == '+' || c == '-' || c == '.'))) goto Lerr; if (c == '.' && /* if lone dot, */ ((n = cp[1]) == ' ' || n == '\t' || n == LF)) { if (UNLIKELY(e.opcod != 'i' || STA(prve).opcod != 'i' || pcnt > STA(prve).pcnt)) { csound->ErrorMsg(csound, Str("dot carry has no reference")); goto Lerr; } /* pfld carry */ e.p[pcnt] = STA(prve).p[pcnt]; if (UNLIKELY(ISSTRCOD(e.p[pcnt]))) { csound->ErrorMsg(csound, Str("cannot carry string p-field")); goto Lerr; } continue; } e.p[pcnt] = (MYFLT) cs_strtod(cp, &newcp); cp = newcp - 1; } while (pcnt < PMAX); if (e.opcod =='f' && e.p[1]<FL(0.0)); /* an OK case */ else /* check sufficient pfields */ if (UNLIKELY(pcnt < 3 && e.opcod != 'e')) { csound->ErrorMsg(csound, Str("too few pfields")); goto Lerr; } if (UNLIKELY(pcnt > 1 && e.p[2] < FL(0.0))) { csound->ErrorMsg(csound, Str("-L with negative p2 illegal")); goto Lerr; } e.pcnt = pcnt; /* & record pfld count */ if (e.opcod == 'i') { /* do carries for instr data */ memcpy((void*) &STA(prve), (void*) &e, (size_t) ((char*) &(e.p[pcnt + 1]) - (char*) &e)); /* FIXME: how to carry string args ? */ STA(prve).strarg = NULL; } if (UNLIKELY(pcnt >= PMAX && c != LF)) { csound->ErrorMsg(csound, Str("too many pfields")); while (*(++cp) != LF) /* flush any excess data */ ; } Linestart = (++cp); insert_score_event_at_sample(csound, &e, csound->icurTime); continue; Lerr: n = cp - Linestart; /* error position */ while (*cp != LF) cp++; /* go on to LF */ *cp = '\0'; /* & insert NULL */ csound->ErrorMsg(csound, Str("illegal RT scoreline:\n%s\n%*s"), Linestart, n + 1, "^"); /* mark the error */ Linestart = (++cp); } if (Linestart != &(STA(Linebuf)[0])) { int len = (int) (Linend - Linestart); /* move any remaining characters to the beginning of the buffer */ for (n = 0; n < len; n++) STA(Linebuf)[n] = Linestart[n]; n = (int) (Linestart - &(STA(Linebuf)[0])); STA(Linep) -= n; Linend -= n; } if (Linend == STA(Linep)) /* return if no more data is available */ break; STA(Linep) = Linend; /* accum the chars */ } }