bool get_next_packet(void *arte_packet, int sourcename, packetType_t sourcetype){ bool ok_packet; char buff_head[4]; char buff[4000]; char the_type; uint16_t the_length; char the_next_byte; spike_net_t *spike; lfp_bank_net_t *lfp; char tmp_spike[MAX_PACKET_BYTES]; //spike_net_t spike; //fread(buff, sizeof(char), 1000, in_f); //buffToSpike( spike, buff, false); //printf("spike->n_chans: %d\n", spike->n_chans); //printf("still ok.\n"); fflush(stdout); fread (buff_head, sizeof(char), 4, in_f); fseek( in_f, -4, SEEK_CUR ); the_type = buff_head[0]; memcpy( &the_length, buff_head+1, 2 ); memcpy( &the_next_byte, buff_head+3, 1); //printf("Wanted sourcetype: %c, buff_type:%c length:%d next_byte:%d\n", // sourcetype, the_type, the_length, the_next_byte); fread (buff, sizeof(char),the_length, in_f); //printf("still ok after fread.\n");fflush(stdout); if ( charToType(the_type) != sourcetype){ return false; } if ( charToType(the_type) == NETCOM_UDP_SPIKE ){ spike = (spike_net_t*)arte_packet; //printf("ok after spike= assignment\n"); fflush(stdout); buffToSpike( spike, buff, false ); //printf("ok after buffToSpike.\n"); fflush(stdout); ok_packet = ( spike->name == sourcename ); //spike_net_t *this_spike = (spike_net_t *)arte_packet; for(int i = 0; i < spike->n_chans * spike->n_samps_per_chan; i++){ spike->data[i] = spike->data[i] / 16; } if(spike->ts > (UINT32_MAX - 30000) ){ // is it later than 10 seconds before the end of the valid time range ok_packet = false; //printf("Found spike with bad_ts:%d Current spike_count is:%d Dropping it.\n", spike->ts, spike_count); } if(false){ printf("sought-after sourcename:%d sourcetype:%c found name:%d type:%c\n", sourcename, sourcetype, spike->name, the_type); } if(false & ok_packet & (sourcetype == NETCOM_UDP_SPIKE)){ printf("test in get_next_spike: spike->ts:%d, name:%d n_chans:%d\n", spike->ts, spike->name, spike->n_chans); } if(ok_packet & (sourcetype == NETCOM_UDP_SPIKE) & verbose){ printf("OK_PACKET! spikename:%d sourcename:%d\n", spike->name, sourcename); fflush(stdout); } //printf("ABOUT TO RETURN\n"); return (ok_packet & (sourcetype == NETCOM_UDP_SPIKE)); } if (sourcetype == NETCOM_UDP_LFP){ lfp = (lfp_bank_net_t *)arte_packet; buffToWave( lfp, buff, false ); ok_packet = ( lfp->name == sourcename ); for(int i = 0; i < (lfp->n_chans * lfp->n_samps_per_chan); i++){ lfp->data[i] = lfp->data[i]/16; } if(lfp->ts > (UINT32_MAX - 10000)){ ok_packet = false; //printf("dropped a bad wave packet. timestamp was:%d\n", lfp->ts); } if(false){ printf("lfp_packet: sought-after sourcename:%d sourcetype:%c found name:%d type:%c\n", sourcename, sourcetype, lfp->name, the_type); } return (ok_packet & (sourcetype == NETCOM_UDP_LFP)); } }
token_t nextTok(buffer_t *buffer) { //printf(" %i | %i",buffer->offset+1, buffer->size); if(buffer->offset+1 >= buffer->size) //+1 needed because offset wont save EOF { return (token_t){-1,-1,NULL}; } token_t token = {-1,-1, NULL}; int lexeme_length = 0; int state = 0; int prevState = 0; char transition = 0; rmWhites(&buffer); do { prevState = state; //protect from buffer over-offseting, for example by never ending string if((buffer->advance+1 >= buffer->size) ) { buffer->advance++; break; } transition = charToType(buffer->buf[buffer->advance++]); state = tranMat[state][transition]; buffer->charNum++; }while(state != AC && state != ER); buffer->charNum--; //remove lookahead //setup the token buffer->advance--; lexeme_length = buffer->advance - buffer->offset ; token.lexeme = malloc((sizeof(char)) * (lexeme_length + 1)); strncpy(token.lexeme,&((buffer->buf)[buffer->offset]), lexeme_length); token.lexeme[lexeme_length] = '\0'; buffer->offset = buffer->advance; //classify token category and type switch(prevState) { case 1: token.type = AND; token.category = OP_LOG_BIN; break; case 2: token.type = OR; token.category = OP_LOG_BIN; break; // case 4: token.type = RA; token.category = OP_LOG_BIN; break; case 5: token.type = LA; token.category = OP_LOG_BIN; break; case 6: token.type = LT; token.category = REL_OP; break; // case 8: token.type = DA; token.category = OP_LOG_BIN;break; case 9: token.type = UNIVERSAL;break; case 10: token.type = EXISTENTIAL;break; case 11: token.type = EQ; token.category = REL_OP; break; // case 13: token.type = DIFF; token.category = REL_OP; break; // case 15: token.type = GT; token.category = REL_OP; break; case 16: token.type = LTE; token.category = REL_OP; break; case 17: token.type = GTE; token.category = REL_OP; break; case 18: token.type = VAR;break; case 19: token.type = OBJ_FUNC;break; case 20: token.type = LP;break; case 21: token.type = RP;break; case 22: token.type = NEG;break; //error states case 3: printf("ERROR invalid token -");exit(0); case 7: printf("ERROR invalid state 7");exit(0); case 14: printf("ERROR invalid state 14");exit(0); default: printf(" This symbol is not part of the language: %s\n",token.lexeme); } if(state == ER){ return (token_t){-1,-1,token.lexeme}; } //DONT RETURN TOKEN COMMENTS! maybe something can be done with these here..write to a file for a "javadoc" type of crap if(token.category == CMNT) { free(token.lexeme); return nextTok(buffer); } else return token; }