Exemple #1
0
void searchTest(const STRING& text, const STRING *keyTbl, size_t keySize)
{
	FMINDEX f;
	f.init(text.begin(), text.end());
	compareText(f, text, keyTbl, keySize);
	std::stringstream ss;
	f.save(ss);
	{
		FMINDEX ff;
		ff.load(ss);
		compareText(ff, text, keyTbl, keySize);
	}
}
int StrList::compareStrings(const char* str1, const char* str2) const
{
	if (isCaseSensitive_)
		return strcmp(str1, str2);
	else
		return compareText(str1, str2);
}
//Checks if cmd correspond to any of the valid commands defined 
//in VALID_COMMANDS
int validCommand(char* cmd){
	int i;
	int size = sizeof VALID_COMMANDS/ sizeof (char*);
	for(i=0; i<size;i++){
		//printf("Comparing %s to %s...\n",cmd,VALID_COMMANDS[i]);
		if(compareText(cmd,(char*) VALID_COMMANDS[i])) return 1;
	}	
	return 0;
}
int Strings::compareStrings(const char* str1, const char* str2) const
{
	int r = compareText(str1, str2);

	if (r > 0) r = 1;
	else if (r < 0) r = -1;

	return r;
}
Exemple #5
0
int TaskCompare::compare(Task *const &pt1, Task *const &pt2)
{
	// result's intent is to catch a definitive: less than or more
	// it is important for sortable containers to avoid zero (0)
	// as zero represents equality
	//TODO: review for means of generic extensible implementation
    int result = 0;
    int sortLvl = 0;
    do
    {
        switch(m_pFields[sortLvl])
        {
		case COMPARE_TASK_ID:
			result = compareDate(pt1->GetID(), pt2->GetID());
			break;
        case COMPARE_DUE_DATE:
            result = compareDate(pt1->GetDueDate(), pt2->GetDueDate());
            break;
        case COMPARE_PRIORITY:
            result = compareInt(pt1->GetPriority(), pt2->GetPriority());
            break;
		//case COMPARE_PERCENT_COMPLETE:
		//	result = compareInt(pt1->getPercentComplete(), pt2->getPercentComplete());
		//	break;
        case COMPARE_CREATE_DATE:
            result = compareDate(pt1->GetCreatedDate(), pt2->GetCreatedDate());
            break;
		case COMPARE_ESTIMATED_DURATION:
			result = compareDate(pt1->GetEstimatedDuration(), pt2->GetEstimatedDuration());
			break;
		case COMPARE_START_DATE:
			result = compareDate(pt1->GetScheduledStart(), pt2->GetScheduledStart());
			break;
		case COMPARE_TASK_TITLE:
			result = compareText(pt1->GetTitle(), pt2->GetTitle());
			break;
        default:
            break;
        }
        ++sortLvl;
    }
    while(!result && sortLvl < m_numSortLvls);
	if(!result)result = compareDate(pt1->GetID(), pt2->GetID());
    return result;
}
//Main program
int main(void) {
	int receivedBytes;//, sentBytes;
	//unsigned char outBuff[BUFFER_MAX];
	unsigned char inBuff[BUFFER_MAX];
	//Reserve memory
	char* cmd = malloc((BUFFER_MAX+1) * sizeof(char));
	char* par = malloc((BUFFER_MAX+1) * sizeof(char));
	struct commandStructure command;
	
	//Threads variables
	pthread_t logThread;
	pthread_t emailThread;
	//pthread_t debugThread;
	pthread_t webcamThread;
	pthread_t emailPhotoThread;
	
	//Empty buffers
	init();
	
	// Open serial file descriptor
	int fd = openSerial();
		
	//Loop to scan
	while(1){
		receivedBytes = read(fd,inBuff,BUFFER_MAX);
		if(receivedBytes > 0){						// Data found!
			
			if(DEBUG){ 
				printf("\nPayload size: %d\n",receivedBytes);
				int i;
				for(i=0;i<receivedBytes;i++){
					printf("%c",inBuff[i]);
				}
				printf("\n");
			}

			getCmd(inBuff,cmd);
			getPar(inBuff,par);
			int pars = parseParameters(par);
			
			if(!validCommand(cmd)){
				printf("Invalid Command: %s\n\n",cmd);
				continue;
			}else{	
				//printf("Command: %s\n",cmd);
				//int i = 0;
				//printf("Parameters found: %d\n",pars);
				//for(i=0;i<pars;i++) printf("Parameter %d - %s\n",i,PARAMETERS[i]);			
			}
			
			if(compareText(cmd,"Debug")){ //thread is detached so resources can be recycled.
				command.cmd = cmd;
				command.par[0] = PARAMETERS[0];
				
				time_t now = time(NULL);
				printf("%s - %s",PARAMETERS[0],ctime(&now));

				/*
				//=======Call debugFunc in thread======
				int rc;
				pthread_attr_t attr;
				pthread_attr_init(&attr);
				pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
				
				if((rc = pthread_create(&debugThread,&attr,debugFunc,&command))){
					fprintf(stderr,"Error: Could not create thread: %d\n",rc);
				}
				
				pthread_attr_destroy(&attr);
				*/
			}

			if(compareText(cmd,"Log")){
				if(pars < 1){
					printf("Error: No message sent\n");
					continue;
				}
				command.cmd = cmd;
				command.par[0] = PARAMETERS[0];

				//=======Call logFunc in thread======
				int rc;
				pthread_attr_t attr;
				pthread_attr_init(&attr);
				pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
				
				if((rc = pthread_create(&logThread,&attr,logFunc,&command))){
					fprintf(stderr,"Error: Could not create thread: %d\n",rc);
				}
				
				pthread_attr_destroy(&attr);
			}
			
			if(compareText(cmd,"Email")){
				if(pars < 3){  //Need at least the email address and a subject
					printf("Error: Need 3 parameters: address, subject and message\n");
					continue;
				}
				
				command.cmd = cmd;
				command.par[0] = PARAMETERS[0];
				command.par[1] = PARAMETERS[1];
				command.par[2] = PARAMETERS[2];
				
				//=======Call logFunc in thread======
				int rc;
				pthread_attr_t attr;
				pthread_attr_init(&attr);
				pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
				
				if((rc = pthread_create(&emailThread,&attr,emailFunc,&command))){
					fprintf(stderr,"Error: Could not create thread: %d\n",rc);
				}
				
				pthread_attr_destroy(&attr);
			}
			
			if(compareText(cmd,"Webcam")){
				command.cmd = cmd;
				command.par[0] = PARAMETERS[0];

				//=======Call debugFunc in thread======
				int rc;
				pthread_attr_t attr;
				pthread_attr_init(&attr);
				pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
				
				if((rc = pthread_create(&webcamThread,&attr,webcamFunc,&command))){
					fprintf(stderr,"Error: Could not create webcam thread: %d\n",rc);
				}
				
				pthread_attr_destroy(&attr);				
			}

			if(compareText(cmd,"EmailPhoto")){
				command.cmd = cmd;
				command.par[0] = PARAMETERS[0];
				command.par[1] = PARAMETERS[1];
				command.par[2] = PARAMETERS[2];

				//=======Call debugFunc in thread======
				int rc;
				pthread_attr_t attr;
				pthread_attr_init(&attr);
				pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
				
				if((rc = pthread_create(&emailPhotoThread,&attr,emailPhotoFunc,&command))){
					fprintf(stderr,"Error: Could not create emailPhoto thread: %d\n",rc);
				}
				
				pthread_attr_destroy(&attr);
			}
			
		}else if(receivedBytes == 0){				//No data yet! go back to loop
			continue;					
		}else if(receivedBytes < 0){				//Error reading, exit.
			printf("Error reading from file!\n");
			perror("Error: " );
			close(fd);
			return -1;
		}
		usleep(UDOONEO_POLL_DELAY);	// poll time approx 50mS (faster crashes the app)
	}

	//Free reserved memory
	free((void*)cmd);
	free((void*)par);

	//Close serial's file descriptor
	close(fd);	
}