int prPipeOpen(struct VMGlobals *g, int numArgsPushed) { PyrSlot *a, *b, *c; char mode[12]; PyrFile *pfile; FILE *file; a = g->sp - 2; b = g->sp - 1; c = g->sp; if (NotObj(c) || !isKindOf(slotRawObject(c), class_string) || NotObj(b) || !isKindOf(slotRawObject(b), class_string)) return errWrongType; if (slotRawObject(c)->size > 11) return errFailed; pfile = (PyrFile*)slotRawObject(a); char *commandLine = (char*)malloc(slotRawObject(b)->size + 1); memcpy(commandLine, slotRawString(b)->s, slotRawObject(b)->size); commandLine[slotRawString(b)->size] = 0; memcpy(mode, slotRawString(c)->s, slotRawObject(c)->size); mode[slotRawString(c)->size] = 0; pid_t pid; file = sc_popen(commandLine, &pid, mode); free(commandLine); if (file) { SetPtr(&pfile->fileptr, file); SetInt(a, pid); } else { SetNil(a); } return errNone; }
int prFileGetcwd(struct VMGlobals *g, int numArgsPushed) { //PyrSlot* a = g->sp - 1; // File PyrSlot* string = g->sp; if (!isKindOfSlot(string, class_string)) return errWrongType; char * cwd = getcwd(slotRawString(string)->s,255); if (cwd == NULL) { error(strerror(errno)); return errFailed; } slotRawString(string)->size = (int)strlen(slotRawString(string)->s); return errNone; }
int prSFOpenRead(struct VMGlobals *g, int numArgsPushed) { PyrSlot *a, *b; char filename[PATH_MAX]; SNDFILE *file; SF_INFO info; const char *headerstr; const char *sampleformatstr; a = g->sp - 1; b = g->sp; PyrObject *obj1 = slotRawObject(a); if (!isKindOfSlot(b, class_string)) return errWrongType; if (slotRawObject(b)->size > PATH_MAX - 1) return errFailed; memcpy(filename, slotRawString(b)->s, slotRawObject(b)->size); filename[slotRawString(b)->size] = 0; info.format = 0; file = sf_open(filename, SFM_READ, &info); if (file) { SetPtr(obj1->slots + 0, file); sndfileFormatInfoToStrings(&info, &headerstr, &sampleformatstr); //headerFormatToString(&info, &headerstr); PyrString *hpstr = newPyrString(g->gc, headerstr, 0, true); SetObject(obj1->slots+1, hpstr); g->gc->GCWriteNew(obj1, (PyrObjectHdr*)hpstr); // we know hpstr is white so we can use GCWriteNew PyrString *smpstr = newPyrString(g->gc, sampleformatstr, 0, true); SetObject(obj1->slots+2, smpstr); g->gc->GCWriteNew(obj1, (PyrObjectHdr*)smpstr); // we know smpstr is white so we can use GCWriteNew SetInt(obj1->slots + 3, info.frames); SetInt(obj1->slots + 4, info.channels); SetInt(obj1->slots + 5, info.samplerate); SetTrue(a); } else { SetNil(a); SetFalse(a); } return errNone; }
int prFilePutString(struct VMGlobals *g, int numArgsPushed) { PyrSlot *a, *b; PyrFile *pfile; FILE *file; PyrString *string; a = g->sp - 1; b = g->sp; pfile = (PyrFile*)slotRawObject(a); file = (FILE*)slotRawPtr(&pfile->fileptr); if (file == NULL) return errFailed; if (NotObj(b) || slotRawObject(b)->classptr != class_string) return errWrongType; string = slotRawString(b); if (string->size) { fwrite(string->s, 1, string->size, file); } return errNone; }
static int addMsgSlot(big_scpacket *packet, PyrSlot *slot) { switch (GetTag(slot)) { case tagInt : packet->addi(slotRawInt(slot)); break; case tagSym : packet->adds(slotRawSymbol(slot)->name); break; case tagObj : if (isKindOf(slotRawObject(slot), class_string)) { PyrString *stringObj = slotRawString(slot); packet->adds(stringObj->s, stringObj->size); } else if (isKindOf(slotRawObject(slot), class_int8array)) { PyrInt8Array *arrayObj = slotRawInt8Array(slot); packet->addb(arrayObj->b, arrayObj->size); } else if (isKindOf(slotRawObject(slot), class_array)) { PyrObject *arrayObj = slotRawObject(slot); big_scpacket packet2; if (arrayObj->size > 1 && isKindOfSlot(arrayObj->slots+1, class_array)) { makeSynthBundle(&packet2, arrayObj->slots, arrayObj->size, true); } else { int error = makeSynthMsgWithTags(&packet2, arrayObj->slots, arrayObj->size); if (error != errNone) return error; } packet->addb((uint8*)packet2.data(), packet2.size()); } break; case tagNil : case tagTrue : case tagFalse : case tagChar : case tagPtr : break; default : if (gUseDoubles) packet->addd(slotRawFloat(slot)); else packet->addf(slotRawFloat(slot)); break; } return errNone; }
int prFileReadLine(struct VMGlobals *g, int numArgsPushed) { PyrSlot *a, *b; // receiver(a File), string PyrFile *pfile; FILE *file; a = g->sp - 1; b = g->sp; pfile = (PyrFile*)slotRawObject(a); file = (FILE*)slotRawPtr(&pfile->fileptr); if (file == NULL) return errFailed; char* result = fgets(slotRawString(b)->s, (int)(MAXINDEXSIZE(slotRawObject(b)) - 1), file); // kengo: if (!result) { SetNil(a); } else { slotRawString(b)->size = (int)strlen(slotRawString(b)->s); if (slotRawString(b)->s[slotRawString(b)->size-1] == '\n') slotRawString(b)->size--; slotCopy(a,b); } return errNone; }
int prFileWriteLE(struct VMGlobals *g, int numArgsPushed) { PyrSlot *a, *b, *ptr; PyrFile *pfile; FILE *file; PyrObject *obj; char chr; a = g->sp - 1; b = g->sp; pfile = (PyrFile*)slotRawObject(a); file = (FILE*)slotRawPtr(&pfile->fileptr); if (file == NULL) return errFailed; switch (GetTag(b)) { case tagInt : { SC_IOStream<FILE*> scio(file); scio.writeInt32_le(slotRawInt(b)); break; } case tagSym : fwrite(slotRawSymbol(b)->name, sizeof(char), slotRawSymbol(b)->length, file); break; case tagChar : chr = slotRawInt(b); fwrite(&chr, sizeof(char), 1, file); break; case tagNil : case tagFalse : case tagTrue : case tagPtr : return errWrongType; case tagObj : { // writes the indexable part of any non obj_slot format object obj = slotRawObject(b); if (!isKindOf(obj, class_rawarray) || isKindOf(obj, class_symbolarray)) return errWrongType; if (obj->size) { ptr = obj->slots; int elemSize = gFormatElemSize[obj->obj_format]; int numElems = obj->size; #if BYTE_ORDER == BIG_ENDIAN switch (elemSize) { case 1: fwrite(ptr, elemSize, numElems, file); break; case 2: { char *ptr = slotRawString(b)->s; char *ptrend = ptr + numElems*2; for (; ptr < ptrend; ptr+=2) { fputc(ptr[1], file); fputc(ptr[0], file); } break; } case 4: { char *ptr = slotRawString(b)->s; char *ptrend = ptr + numElems*4; for (; ptr < ptrend; ptr+=4) { fputc(ptr[3], file); fputc(ptr[2], file); fputc(ptr[1], file); fputc(ptr[0], file); } break; } case 8: { char *ptr = slotRawString(b)->s; char *ptrend = ptr + numElems*8; for (; ptr < ptrend; ptr+=8) { fputc(ptr[7], file); fputc(ptr[6], file); fputc(ptr[5], file); fputc(ptr[4], file); fputc(ptr[3], file); fputc(ptr[2], file); fputc(ptr[1], file); fputc(ptr[0], file); } break; } } #else fwrite(ptr, elemSize, numElems, file); #endif } break; } default : // double { SC_IOStream<FILE*> scio(file); scio.writeDouble_le(slotRawFloat(b)); break; } } return errNone; }
int prFileOpen(struct VMGlobals *g, int numArgsPushed) { PyrSlot *a, *b, *c; char filename[PATH_MAX]; char mode[12]; PyrFile *pfile; FILE *file; a = g->sp - 2; b = g->sp - 1; c = g->sp; if (NotObj(c) || !isKindOf(slotRawObject(c), class_string) || NotObj(b) || !isKindOf(slotRawObject(b), class_string)) return errWrongType; if (slotRawObject(b)->size > PATH_MAX - 1) return errFailed; if (slotRawObject(c)->size > 11) return errFailed; pfile = (PyrFile*)slotRawObject(a); memcpy(filename, slotRawString(b)->s, slotRawObject(b)->size); filename[slotRawString(b)->size] = 0; memcpy(mode, slotRawString(c)->s, slotRawObject(c)->size); mode[slotRawString(c)->size] = 0; #ifdef SC_WIN32 win32_ReplaceCharInString(filename,PATH_MAX,'/','\\'); if(strcmp(mode,"w") == 0) strcpy(mode,"wb"); if(strcmp(mode,"r") == 0) strcpy(mode,"rb"); #endif //SC_WIN32 file = fopen(filename, mode); if (file) { SetPtr(&pfile->fileptr, file); SetTrue(a); } else { #ifdef SC_WIN32 // check if directory exisits // create a temporary file (somewhere) for a handle // the file is deleted automatically when closed if (sc_DirectoryExists(filename)) { int err; #ifdef _MSC_VER err = tmpfile_s(&file); if (!err) { SetPtr(&pfile->fileptr, file); SetTrue(a); return errNone; } #elif defined(__MINGW32__) file = tmpfile(); if (file) { SetPtr(&pfile->fileptr, file); SetTrue(a); return errNone; } #else #error compiler unsupported #endif } #endif SetNil(a); SetFalse(a); } return errNone; }
int prSFOpenWrite(struct VMGlobals *g, int numArgsPushed) { PyrSlot *a, *b; char filename[PATH_MAX]; SNDFILE *file; SF_INFO info; PyrSlot *headerSlot; PyrSlot *formatSlot; int error; a = g->sp - 1; b = g->sp; headerSlot = (slotRawObject(a)->slots + 1); formatSlot = (slotRawObject(a)->slots + 2); if (!isKindOfSlot(headerSlot, class_string)) return errWrongType; if (!isKindOfSlot(formatSlot, class_string)) return errWrongType; if (!isKindOfSlot(b, class_string)) return errWrongType; if (slotRawObject(b)->size > PATH_MAX - 1) return errFailed; memcpy(filename, slotRawString(b)->s, slotRawObject(b)->size); filename[slotRawString(b)->size] = 0; #ifdef SC_WIN32 char* headerFormat = (char *)malloc(slotRawObject(headerSlot)->size); #else char headerFormat[slotRawString(headerSlot)->size]; #endif memcpy(headerFormat, slotRawString(headerSlot)->s, slotRawObject(headerSlot)->size); headerFormat[slotRawString(headerSlot)->size] = 0; #ifdef SC_WIN32 char* sampleFormat = (char *)malloc(slotRawString(formatSlot)->size); #else char sampleFormat[slotRawString(formatSlot)->size]; #endif memcpy(sampleFormat, slotRawString(formatSlot)->s, slotRawObject(formatSlot)->size); sampleFormat[slotRawString(formatSlot)->size] = 0; error = sndfileFormatInfoFromStrings(&info, headerFormat, sampleFormat); if(error) { #ifdef SC_WIN32 free(sampleFormat); free(headerFormat); #endif return errFailed; } if(error) return errFailed; //slotIntVal(slotRawObject(a)->slots + 3, &info.frames); slotIntVal(slotRawObject(a)->slots + 4, &info.channels); slotIntVal(slotRawObject(a)->slots + 5, &info.samplerate); file = sf_open(filename, SFM_WRITE, &info); sf_command(file, SFC_SET_CLIPPING, NULL, SF_TRUE); if (file) { SetPtr(slotRawObject(a)->slots+0, file); SetTrue(a); } else { SetNil(a); SetFalse(a); } return errNone; }
int prFileReadRawLE(struct VMGlobals *g, int numArgsPushed) { PyrFile *pfile; FILE *file; PyrSlot* a = g->sp - 1; PyrSlot* b = g->sp; if (!isKindOfSlot(b, class_rawarray) || isKindOfSlot(b, class_symbolarray)) return errWrongType; pfile = (PyrFile*)slotRawObject(a); file = (FILE*)slotRawPtr(&pfile->fileptr); if (file == NULL) return errFailed; int elemSize = gFormatElemSize[slotRawObject(b)->obj_format]; int numElems = slotRawObject(b)->size; numElems = (int)fread(slotRawString(b)->s, elemSize, numElems, file); slotRawObject(b)->size = numElems; #if BYTE_ORDER == BIG_ENDIAN switch (elemSize) { case 1: break; case 2: { char *ptr = slotRawString(b)->s; char *ptrend = ptr + numElems*2; for (; ptr < ptrend; ptr+=2) { char temp = ptr[0]; ptr[0] = ptr[1]; ptr[1] = temp; } break; } case 4: { char *ptr = slotRawString(b)->s; char *ptrend = ptr + numElems*4; for (; ptr < ptrend; ptr+=4) { char temp = ptr[0]; ptr[0] = ptr[3]; ptr[3] = temp; temp = ptr[1]; ptr[1] = ptr[2]; ptr[2] = temp; } break; } case 8: { char *ptr = slotRawString(b)->s; char *ptrend = ptr + numElems*8; for (; ptr < ptrend; ptr+=8) { char temp = ptr[0]; ptr[0] = ptr[7]; ptr[7] = temp; temp = ptr[1]; ptr[1] = ptr[6]; ptr[6] = temp; temp = ptr[2]; ptr[2] = ptr[5]; ptr[5] = temp; temp = ptr[3]; ptr[3] = ptr[4]; ptr[4] = temp; } break; } } #endif if (slotRawObject(b)->size==0) SetNil(a); else slotCopy(a,b); return errNone; }