int prDirectory_At(struct VMGlobals *g, int numArgsPushed)
{
	PyrSlot *a = g->sp - 2;
	PyrSlot *b = g->sp - 1;
	PyrSlot *c = g->sp;

	PyrSlot *dirPathSlot = slotRawObject(a)->slots + 0;
	int err, index;
	err = slotIntVal(c, &index);
	if (err) {
		SetNil(a);
		return err;
	}

	char name[256], fullPathName[256];
	int nameLength, creationDate, modificationDate, isDirectory, isVisible, sizeIfFile;
	int dirPathLength = slotRawObject(dirPathSlot)->size;

	err = dir_Lookup(slotRawObject(dirPathSlot)s->s, dirPathLength, index+1,
		name, &nameLength, &creationDate, &modificationDate, &isDirectory, &isVisible, &sizeIfFile);
	if (err == 1) {
		SetNil(a);
		return errNone;
	}
	if (err) {
		error("Invalid path\n");
		SetNil(a);
		return errFailed;
	}

	if (dirPathLength + nameLength + 1 > 255) {
		error("Full path name too long.\n");
		SetNil(a);
		return errFailed;
	}

	PyrSlot *entryName = slotRawObject(b)->slots + 0;
	PyrSlot *entryPath = slotRawObject(b)->slots + 1;
	PyrSlot *entryIsDir = slotRawObject(b)->slots + 2;
	PyrSlot *entryIsVisible = slotRawObject(b)->slots + 3;

	PyrString *nameString = newPyrString(g->gc, name, 0, true);
	SetObject(entryName, nameString);
	g->gc->GCWrite(slotRawObject(b), (PyrObject*)nameString);

	memcpy(fullPathName, slotRawObject(dirPathSlot)s->s, dirPathLength);
	fullPathName[dirPathLength] = DELIMITOR;
	strcpy(fullPathName + dirPathLength + 1, name);

	PyrString *pathString = newPyrString(g->gc, fullPathName, 0, true);
	SetObject(entryPath, pathString);
	g->gc->GCWrite(slotRawObject(b), (PyrObject*)pathString);

	if (isDirectory) { SetTrue(entryIsDir); } else { SetFalse(entryIsDir); }
	if (isVisible) { SetTrue(entryIsVisible); } else { SetFalse(entryIsVisible); }

	slotCopy(a,b);

	return errNone;
}
示例#2
0
int prIsPrime(VMGlobals *g, int numArgsPushed)
{
	PyrSlot *a;
	int n, p, sqrtn, i;

	a = g->sp;
	n = slotRawInt(a);
	SetNil(a);
	if (n <= 2) {
		if (n == 2) { SetTrue(a); }
		else { SetFalse(a); }
	} else if (n <= nthPrime(NUMPRIMES-1)) {
		// do a search of the primes table
		i = findPrime(n);
		if (i >= 0) { SetTrue(a); }
		else { SetFalse(a); }
	} else {
#ifdef SC_WIN32
		sqrtn = (int)sqrt(static_cast<double>(n));
#else
		sqrtn = (int)sqrt(n);
#endif
		for (i=0; i<NUMPRIMES; ++i) {
			p = nthPrime(i);
			if (n % p == 0) { SetFalse(a); break; }
			if (p >= sqrtn) { SetTrue(a); break; }
		}
	}
	return errNone;
}
示例#3
0
int prPidRunning(VMGlobals *g, int numArgsPushed)
{
	PyrSlot *a;

	a = g->sp;

#ifdef SC_WIN32
	HANDLE handle;

	handle = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, a->ui);
	if(handle) {
		unsigned long exitCode;

		if(GetExitCodeProcess(handle, &exitCode) == 0)
			SetFalse(a);
		else if(exitCode == STILL_ACTIVE)
			SetTrue(a);

		CloseHandle(handle);
	}
	else
		SetFalse(a);
#else
	if(kill(a->ui, 0) == 0)
		SetTrue(a);
	else
		SetFalse(a);
#endif

	return errNone;
}
示例#4
0
 static void write( PyrSlot *slot, const bool val )
 {
   if(val)
     SetTrue(slot);
   else
     SetFalse(slot);
 }
示例#5
0
int prSpeechVoiceIsSpeaking(struct VMGlobals *g, int numArgsPushed){
	PyrSlot *out = g->sp-1;
	PyrSlot *b = g->sp;
    int chan;
    slotIntVal(b, &chan);
	if(speechStrings[chan] != NULL) SetTrue(out);
	else SetFalse(out);
	return errNone;

}
int prSymbolIsSetter(struct VMGlobals *g, int numArgsPushed)
{
	PyrSlot *a;

	a = g->sp;
	if (slotRawSymbol(a)->flags & sym_Setter) {
		SetTrue(a);
	} else {
		SetFalse(a);
	}
	return errNone;
}
示例#7
0
int prSymbol_isMap(struct VMGlobals *g, int numArgsPushed)
{
	PyrSlot *a = g->sp;

	char *str = slotRawSymbol(a)->name;
	if(strlen(str)>1 && (str[0]=='a' || str[0]=='c') && str[1]>='0' && str[1]<='9')
		SetTrue(a);
	else
		SetFalse(a);

	return errNone;
}
示例#8
0
int prPriorityQueueEmpty(struct VMGlobals *g, int numArgsPushed)
{
	PyrSlot *a;

	a = g->sp;	// priority queue

	if (PriorityQueueEmpty(slotRawObject(a))) {
		SetTrue(a);
	} else {
		SetFalse(a);
	}
	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;
}
示例#10
0
int prSymbolIsPrefix(struct VMGlobals *g, int numArgsPushed)
{
	PyrSlot *a, *b;
	int length;

	a = g->sp - 1;
	b = g->sp;
	if (!IsSym(a) || !IsSym(b)) return errWrongType;
	int32 alen = slotRawSymbol(a)->length;
	int32 blen = slotRawSymbol(b)->length;
	length = sc_min(alen, blen);
	if (memcmp(slotRawSymbol(a)->name, slotRawSymbol(b)->name, length) == 0) {
		SetTrue(a);
	} else {
		SetFalse(a);
	}
	return errNone;
}
示例#11
0
int prSymbol_matchOSCPattern(struct VMGlobals *g, int numArgsPushed)
{
	PyrSlot *a, *b;
	int length;
	
	a = g->sp - 1;
	b = g->sp;
	if (!IsSym(a) || !IsSym(b)) return errWrongType;
//	int32 alen = slotRawSymbol(a)->length;
//	int32 blen = slotRawSymbol(b)->length;
//	length = sc_min(alen, blen);
	if (lo_pattern_match(slotRawSymbol(a)->name, slotRawSymbol(b)->name)) {
		SetTrue(a);
	} else {
		SetFalse(a);
	}
	return errNone;
}
示例#12
0
int prFileDelete(struct VMGlobals *g, int numArgsPushed)
{
	PyrSlot *a = g->sp - 1, *b = g->sp;
	char filename[PATH_MAX];

	int error = slotStrVal(b, filename, PATH_MAX);
	if (error != errNone)
		return error;

	boost::system::error_code error_code;
	boost::filesystem::remove(filename, error_code);

	if (error_code)
		SetFalse(a);
	else
		SetTrue(a);

	return errNone;
}
示例#13
0
int prOpenUDPPort(VMGlobals *g, int numArgsPushed)
{
	PyrSlot *a = g->sp - 1;
	PyrSlot *b = g->sp;
	int port;
	int err = slotIntVal(b, &port);
	if (err) return err;

	SC_UdpCustomInPort* newUDPport;

	try {
		SetTrue(a);
		newUDPport = new SC_UdpCustomInPort(port);
		gCustomUdpPorts.push_back(newUDPport);
	} catch (...) {
		SetFalse(a);
		postfl("Could not bind to requested port. This may mean it is in use already by another application.\n");
	}
	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;
}
示例#15
0
PyrObject* ConvertOSCMessage(int inSize, char *inData)
{
	char *cmdName = inData;
	int cmdNameLen = OSCstrlen(cmdName);
	sc_msg_iter msg(inSize - cmdNameLen, inData + cmdNameLen);

	int numElems;
	if (inSize == cmdNameLen) {
		numElems = 0;
	} else {
		if (!msg.tags) {
			numElems = 0;
			error("OSC messages must have type tags.  %s\n", cmdName);
		} else {
			numElems = strlen(msg.tags);
		}
	}
	//post("tags %s %d\n", msg.tags, numElems);

	VMGlobals *g = gMainVMGlobals;
	PyrObject *obj = newPyrArray(g->gc, numElems + 1, 0, false);
	PyrSlot *slots = obj->slots;

	SetSymbol(slots+0, getsym(cmdName));

	for (int i=0; i<numElems; ++i) {
		char tag = msg.nextTag();
		//post("%d %c\n", i, tag);
		switch (tag) {
		case 'i' :
			SetInt(slots+i+1, msg.geti());
			break;
		case 'f' :
			SetFloat(slots+i+1, msg.getf());
			break;
		case 'd' :
			SetFloat(slots+i+1, msg.getd());
			break;
		case 's' :
			SetSymbol(slots+i+1, getsym(msg.gets()));
			//post("sym '%s'\n", slots[i+1].us->name);
			break;
		case 'b' : // fall through
		case 'm' :
			SetObject(slots+i+1, (PyrObject*)MsgToInt8Array(msg));
			break;
		case 'c':
			SetChar(slots+i+1, (char)msg.geti());
			break;
		case 't' :
			SetFloat(slots+i+1, OSCToElapsedTime(msg.gett()));
			break;

			// argument tags without any associated value
		case 'T' :
			SetTrue(slots+i+1);
			msg.count ++;
			break;
		case 'F' :
			SetFalse(slots+i+1);
			msg.count ++;
			break;
		case 'I' :
			SetFloat(slots+i+1, dInfinity);
			msg.count ++;
			break;
		case 'N' :
			SetNil(slots+i+1);
			msg.count ++;
			break;
			// else add the type tag as a char (jrhb 2009)
		default:
			SetChar(slots+i+1, tag);
			msg.gets();
		}
	}
	obj->size = numElems + 1;
	return obj;
}
示例#16
0
int processident(char *token)
{
	char c;
	PyrSymbol *sym;

	PyrSlot slot;
	PyrParseNode *node;

	c = token[0];
	zzval = -1;

#if DEBUGLEX
	if (gDebugLexer) postfl("word: '%s'\n",token);
#endif
	/*
	strcpy(uptoken, token);
	for (str = uptoken; *str; ++str) {
		if (*str >= 'a' && *str <= 'z') *str += 'A' - 'a';
	}*/

	if (token[0] == '_') {
		if (token[1] == 0) {
			node = newPyrCurryArgNode();
			zzval = (long)node;
			return CURRYARG;
		} else {
			sym = getsym(token);
			SetSymbol(&slot, sym);
			node = newPyrSlotNode(&slot);
			zzval = (long)node;
			return PRIMITIVENAME;
		}
	}
	if (token[0] >= 'A' && token[0] <= 'Z') {
		sym = getsym(token);
		SetSymbol(&slot, sym);
		node = newPyrSlotNode(&slot);
		zzval = (long)node;
#if DEBUGLEX
	if (gDebugLexer) postfl("CLASSNAME: '%s'\n",token);
#endif
		return CLASSNAME;
	}
	if (strcmp("var",token) ==0) return VAR;
	if (strcmp("arg",token) ==0) return ARG;
	if (strcmp("classvar",token) ==0) return CLASSVAR;
	if (strcmp("const",token) ==0) return SC_CONST;

	if (strcmp("while",token) ==0) {

		sym = getsym(token);
		SetSymbol(&slot, sym);
		node = newPyrSlotNode(&slot);
		zzval = (long)node;
		return WHILE;
	}
	if (strcmp("pi",token) ==0) {
		SetFloat(&slot, pi);
		node = newPyrSlotNode(&slot);
		zzval = (long)node;
		return PIE;
	}
	if (strcmp("true",token) ==0) {
		SetTrue(&slot);
		node = newPyrSlotNode(&slot);
		zzval = (long)node;
		return TRUEOBJ;
	}
	if (strcmp("false",token) ==0) {
		SetFalse(&slot);
		node = newPyrSlotNode(&slot);
		zzval = (long)node;
		return FALSEOBJ;
	}
	if (strcmp("nil",token) ==0) {
		SetNil(&slot);
		node = newPyrSlotNode(&slot);
		zzval = (long)node;
		return NILOBJ;
	}
	if (strcmp("inf",token) ==0) {
		SetFloat(&slot, std::numeric_limits<double>::infinity());
		node = newPyrSlotNode(&slot);
		zzval = (long)node;
		return SC_FLOAT;
	}

	sym = getsym(token);

	SetSymbol(&slot, sym);
	node = newPyrSlotNode(&slot);
	zzval = (long)node;
	return NAME;
}
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;
}
示例#18
0
static int FuncCompileIfElse( FUNC *func )
    {
    FUNC *table[100], *loop, *temp ;
    int i, j, count, unknown ;
    ifelsedir direction ;

    for( loop = func ; loop ; loop = loop->next )
	loop->unknown = 0 ;

/*
 *  First find an if/else tree
 *
 */

    count = 0 ;
    direction = DIR_UNKNOWN ;
    for( loop = func ; loop ; loop = loop->next )
	{
	if( !loop->ignore && ExprLabelEqual(loop->label, iif) )
	    {
	    if( count && (direction == DIR_FORWARD) )
		break ;
	    if( !count )
		direction = DIR_FORWARD ;
	    table[count] = loop ;
	    loop = SetSkip( loop ) ;
	    count++ ;
	    if( direction == DIR_REVERSE )
		break ;					/* final $if */
	    }
	else
	if( !loop->ignore &&
		( ExprLabelEqual( loop->label, ielse ) || ExprLabelEqual( loop->label, ielseif ) ) )
	    {
	    if( !count )
		direction = DIR_REVERSE ;
	    table[count] = loop ;
	    loop = SetSkip( loop ) ;
	    count++ ;
	    if( (direction == DIR_FORWARD) && ExprLabelEqual(loop->label, ielse) )
		break ;					/* final $else */
	    }
	else
	if( count && !ExprLabelEqual(loop->label, imacro) )
	    break ;
	}
    if( !count )
	return 0 ;

    if( count >= 100 )
	IOerror( FuncBomb, "FuncCompileIfElse", "internal table exceeded" ) ;

    if( direction == DIR_UNKNOWN )
	IOerror( FuncBomb, "FuncCompileIfElse", "cannot determine direction" ) ;

/*
 *  Swap ends if tree is reversed
 *
 */

    if( direction == DIR_REVERSE )
	{
	for( i = 0 ; i < count / 2 ; i++ )
	    {
	    temp = table[i] ;
	    table[i] = table[ count - i - 1 ] ;
	    table[ count - i - 1 ] = temp ;
	    }
    if( !ExprLabelEqual( table[0]->label, iif ) )
	IOerror( FuncBomb, "FuncCompileIfElse", "if/else tree has no if" ) ;
    }

/*
 *  Check some syntax
 *
 */

    if( FuncArgCount( table[0] ) != 1 )
	{
	IOerror( FuncBomb, "FuncCompileIfElse",
	    "wrong number of arguments to $if" ) ;
	}
    for( i = 1 ; i < count - 1 ; i++ )
	{
	if( FuncArgCount( table[i] ) != 1 )
	    {
	    IOerror( FuncBomb, "FuncCompileIfElse",
		"wrong number of arguments to $elseif" ) ;
	    }
	if( ExprLabelEqual( table[i]->label, ielse ) )
	    {
	    IOerror( FuncBomb, "FuncCompileIfElse",
		"$else embedded in $if/$else tree" ) ;
	    }
	}
    if( ExprLabelEqual(table[count - 1]->label, ielse) && (FuncArgCount( table[count - 1] ) != 0) )
	IOerror( FuncBomb, "FuncCompileIfElse",
	    "wrong number of arguments to $else" ) ;

/*
 *  Find the true branch
 *
 */

    unknown = 0 ;
    for( i = 0 ; i < count ; i++ )
	{
	if( ExprNonDepend( FuncArg( table[i], 0 ), VAR_SET, 0 ) )
	    {
	    unknown = 1 ;
	    break ;
	    }
	if( ExprLabelEqual( table[i]->label, ielse ) )			/* leftover */
	    break ;
	if( ExprValue( FuncArg( table[i], 0 ) ) )	/* true */
	    break ;
	}

/*
 *  Set the final state
 *
 */

    for( j = 0 ; j < count ; j++ )
	{
	if( unknown )
	    loop = SetUnknown( table[j] ) ;
	else
	    {
	    if( i == j )
		loop = SetTrue( table[j] ) ;
	    else
		loop = SetFalse( table[j] ) ;
	    }
	if( (direction == DIR_REVERSE) && (j == 0) )
	    func = loop ;
	if( (direction == DIR_FORWARD) && (j == count - 1) )
	    func = loop ;
	}

/*
 *  func is now the last FUNC of the tree
 *
 */

    if( func && func->next )
	return !unknown + FuncCompileIfElse( func->next ) ;
    else
	return !unknown ;
    }
示例#19
0
void SqlBool::SetBool(bool b) {
	if(b) SetTrue();
	else  SetFalse();
}