ValuePtr apply(EnvPtr env, ValuePtr procedure, ValuePtr args) { if(procedure->type() == Value::NATIVE_PROCEDURE) { NativeProcedureValue* proc = static_cast<NativeProcedureValue*>(procedure.mValue); return (*proc->mProc)(env, args); } else if(procedure->type() == Value::PROCEDURE) { EnvPtr callEnvironment = new Environment; ProcedureValue* proc = static_cast<ProcedureValue*>(procedure.mValue); callEnvironment->parent = proc->environment; int iParam = 0; while(args->isNull() == false) { if(iParam == static_cast<int>(proc->paramList.size())) { CHECK_FAIL("Too many arguments to procedure"); } callEnvironment->values[proc->paramList[iParam]] = args->car(); iParam++; args = args->cdr(); } if(iParam != static_cast<int>(proc->paramList.size())) { CHECK_FAIL("Too few arguments to procedure"); } return evalSequence(callEnvironment, proc->body); } else { sWrite(env, new PairValue(procedure, new PairValue())); CHECK_FAIL("Wrong type of argument to apply: not procedure"); return NULL; } }
void sWrite(const UTF32* iSource, size_t iCountCU, size_t* oCountCU, size_t iCountCP, size_t* oCountCP, const ChanW_UTF& iChanW) { size_t actualCU; if (iCountCP >= iCountCU) { // iCountCP is the same or larger than iCountCU, and thus the limit // on what can be written is simply iCountCU because a single code unit // maps to at most one code point. actualCU = iCountCU; } else { // We're being asked to write fewer CPs than we have CUs. So we have to // map from CPs to CUs. actualCU = std::min(iCountCU, ZUnicode::sCPToCU(iSource, iCountCU, iCountCP, nullptr)); } const size_t cuWritten = sWrite(iSource, actualCU, iChanW); if (oCountCU) *oCountCU = cuWritten; if (oCountCP) *oCountCP = ZUnicode::sCUToCP(iSource, cuWritten); }
static void spWriteMust(const UTF8* iSource, size_t iCountCU, const ChanW_UTF& iChanW) { if (iCountCU) { const size_t cuConsumed = sWrite(iSource, iCountCU, iChanW); if (cuConsumed != iCountCU) { // We weren't able to write the whole string. // It may be that the final code units are a valid prefix and thus // were not consumed by Imp_WriteUTF8 because it's expecting // that the caller will pass the prefix again, this time with enough // following code units to make up a complete code point, // or with invalid code units which will be consumed and ignored. // For string8 (and string16 and string32) we know there's // nothing more to come. So if the string contains no more // complete valid code points between source + cuConsumed and the // end then we can just return. If there are one or more valid // code points then the short write must be because we've reached // our end, and we throw the end of strim exception. if (0 != ZUnicode::sCUToCP(iSource + cuConsumed, iSource + iCountCU)) sThrowEndOfChanW(); } } }
/* ------------------------------------------------------------------- ------------------------------------------------------------------- */ int SendData2(int sourcefd, int targetfd, unsigned long int filesize) { unsigned long int nread=0, nwrite, ns=0, readsize, i, offset=0; long int nt; char *buffptr, buff[MAXBUFFLEN]; int retval; struct ibp_timer timeout; int fin; char tmpbuf[3]; timeout.ServerSync = 10; timeout.ClientTimeout = 10; fprintf(stderr,"IN SEND DATA 2 IBP_cap:<<%s>>\n",glb.IBP_cap); while(nread < filesize){ readsize = ((filesize-nread)<(MAXBUFFLEN) ? (filesize-nread):(MAXBUFFLEN)); bzero(buff,MAXBUFFLEN); /**ns = sRead(sourcefd, buff, readsize);*/ offset += ns; ns = IBP_load(glb.IBP_cap, &timeout, buff, readsize, offset); if(ns == 0){ /**fprintf(stderr,"TCP client failed readsize= %d nread=%d\n",readsize,nread);*/ perror("smclientTCP: error reading from FILE"); return -1; } nwrite=0; buffptr = buff; fprintf(stderr,"#"); while(nwrite < ns){ nt = ns-nwrite; if((nt = sWrite(targetfd, buffptr, ns-nwrite)) < 0){ perror("error writing to socket"); return -1; /* one fails all fails*/ } buffptr += nt; nwrite += nt; } nread += ns; } fin = read(targetfd, tmpbuf, 3); if(fin > 0){ if(!strcmp(tmpbuf,"FIN")){ fprintf(stderr,"End of data transfer\n"); retval = 1; goto FIN; } else{ retval = -1; goto FIN; } } else{ fprintf(stderr,"Cannot end transmission properly, SendData failed\n"); retval = -1; } FIN: return retval; }
int SendData2(int sourcefd, int *targetfd, unsigned long int filesize,int numtargets) { unsigned long int nread=0, nwrite, ns=0, readsize, i, offset=0; long int nt; char *buffptr, buff[MAXBUFFLEN]; struct ibp_timer timeout; int fin,retval; char tmpbuf[3]; while(nread < filesize){ readsize = ((filesize-nread)<(MAXBUFFLEN) ? (filesize-nread):(MAXBUFFLEN)); bzero(buff,MAXBUFFLEN); /***ns = sRead(sourcefd, buff, readsize);*/ offset += ns; ns = IBP_load(glb.IBP_cap, &timeout, buff, readsize, offset); if(ns == 0){ perror("error reading from FILE"); return -1; } nwrite=0; buffptr = buff; fprintf(stderr,"#"); while(nwrite < ns){ for(i=0; i<numtargets; i++){ if((nt = sWrite(targetfd[i], buffptr, ns-nwrite)) < 0){ fprintf(stderr,"(host %d) ",i); perror("error writing to socket"); return -1; /* one fails, everything else fails*/ } } buffptr += nt; nwrite += nt; } nread += ns; } for(i=0; i<numtargets; i++){ fin = read(targetfd[i], tmpbuf, 3); if(fin > 0){ if(!strcmp(tmpbuf,"FIN")){ fprintf(stderr,"End of data transfer [%d]\n",i); retval = 1; } else{ retval = -1; goto FIN; } } else{ fprintf(stderr,"Cannot end transmission properly, target %d failed\n",i); retval = -1; goto FIN; } } FIN: return retval; }
void Serialport_WriteByte(Serialport * serialport,const unsigned char dataByte){ // // Make sure that the serial port is open. // if ( ! serialport->isOpen ) { if(serial_log) printf("\nERROR: Serial port is not open."); return; } // // Write the byte to the serial port. // sWrite(serialport,(char*)&dataByte,1); return ; }
static void spWriteMust(const UTF16* iSource, size_t iCountCU, const ChanW_UTF& iChanW) { if (iCountCU) { const size_t cuConsumed = sWrite(iSource, iCountCU, iChanW); if (cuConsumed != iCountCU) { // See comments in the UTF-8 variant. if (0 != ZUnicode::sCUToCP(iSource + cuConsumed, iSource + iCountCU)) sThrowEndOfChanW(); } } }
void Serialport_Write(Serialport * serialport,char* dataBuffer, const unsigned int numOfBytes){ // // Make sure that the serial port is open. // if ( ! serialport->isOpen ) { if(serial_log) printf("\nERROR: Serial port is not open."); return; } // // Write to the serial port. // sWrite(serialport,dataBuffer,numOfBytes); return ; }
void handleRequest(char *buffer) { msg_t *msg = (msg_t *)buffer; switch(msg->func){ case LOOKUP: msg->retCode=sLookup(msg->pinum, msg->name); break; case STAT: msg->retCode=sStat(msg->inum, &(msg->stat)); break; case WRITE: //printf("Got\n%s\n", msg->buffer); msg->retCode=sWrite(msg->inum, msg->buffer, msg->block); break; case READ: msg->retCode=sRead(msg->inum, msg->buffer, msg->block); break; case CREAT: {//need this bracket to define a scope for variable declaration //MFS_Stat_t *stat = &(msg->stat); msg->retCode=sCreat(msg->pinum, msg->type, msg->name); break; } case UNLINK: msg->retCode=sUnlink(msg->pinum, msg->name); break; case SHUTDOWN: msg->retCode=0; fsync(fdImage); close(fdImage); UDP_Write(sd,&addr,(char*)msg, sizeof(msg_t)); exit(0); break; default: msg->retCode=-1; break; } }
string8 sAsJSON(const Any& iVal) { string8 result; sWrite(iVal, ChanW_UTF_string8(&result)); return result; }
void sWrite(const Any& iVal, const ChanW_UTF& iChanW) { sWrite(false, iVal, iChanW); }
bool sQWriteCP(UTF32 iCP, const ChanW_UTF& iChanW) { return 1 == sWrite(&iCP, 1, iChanW); }