Ejemplo n.º 1
0
Archivo: String.c Proyecto: darkf/sdk
char * __ecereNameSpace__ecere__sys__RSearchString(char * buffer, char * subStr, int maxLen, unsigned int matchCase, unsigned int matchWord)
{
if(buffer && subStr)
{
int subLen = strlen(subStr);
char * ptr1 = buffer + maxLen - subLen;
char * ptr2 = buffer + maxLen - subLen - 1;
int (* strcompare)(const char *, const char *, unsigned int) = matchCase ? strncmp : (strncasecmp);

for(; ptr1 >= buffer; ptr1--, ptr2--)
{
if(tolower(*subStr) == tolower(*ptr1))
{
if(matchWord)
{
if(!strcompare(ptr1, subStr, subLen) && (!((subStr[subLen - 1]) == '_' || isalnum((subStr[subLen - 1]))) || !((ptr1[subLen]) == '_' || isalnum((ptr1[subLen])))) && (!((subStr[0]) == '_' || isalnum((subStr[0]))) || !((*ptr2) == '_' || isalnum((*ptr2)))))
return ptr1;
}
else
{
if(!strcompare(ptr1, subStr, subLen))
return ptr1;
}
}
}
}
return (((void *)0));
}
Ejemplo n.º 2
0
Archivo: String.c Proyecto: darkf/sdk
char * __ecereNameSpace__ecere__sys__SearchString(char * buffer, int start, char * subStr, unsigned int matchCase, unsigned int matchWord)
{
if(buffer && subStr)
{
char * ptr;
char * strBuffer = buffer + start;
int subLen = strlen(subStr);
char beforeChar = start ? *(strBuffer - 1) : (char)0;
int (* strcompare)(const char *, const char *, unsigned int) = matchCase ? strncmp : (strncasecmp);

for(ptr = strBuffer; *ptr; ptr++)
{
if(matchCase ? (*subStr == *ptr) : (tolower(*subStr) == tolower(*ptr)))
{
if(matchWord)
{
if(!strcompare(ptr, subStr, subLen) && (!((subStr[subLen - 1]) == '_' || isalnum((subStr[subLen - 1]))) || !((ptr[subLen]) == '_' || isalnum((ptr[subLen])))) && (!((subStr[0]) == '_' || isalnum((subStr[0]))) || !((beforeChar) == '_' || isalnum((beforeChar)))))
return ptr;
}
else
{
if(!strcompare(ptr, subStr, subLen))
return ptr;
}
}
beforeChar = ptr[0];
}
}
return (((void *)0));
}
Ejemplo n.º 3
0
/*
*   Implements open interface
*   Parameters:
*       o const char *name - led name
*       o char mode - 'rR','wW' or 'aA' 
*   Return : stream object if successful, otherwise NULL
*   sets my_errno to NOT_A_LED if led name is invalid.
*/
void* led_open(const char *name,char mode)
{
	enum LED_MINOR minor = NLED;
	if(strcompare(ORANGE_LED,name))
	{
		minor = ORANGE;
	}
	else if(strcompare(YELLOW_LED,name))
	{
		minor = YELLOW;
	}
	else if(strcompare(GREEN_LED,name))
	{
		minor = GREEN;
	}
	else if(strcompare(BLUE_LED,name))
	{
		minor = BLUE;
	}
	else
	{
		my_errno = NOT_A_LED;
		return NULL;
	}
	struct led_stream *stream = myMalloc(sizeof(struct led_stream));
	stream->minor = minor;
	return stream;
}
Ejemplo n.º 4
0
/*
*   Implements open interface
*   Parameters:
*       o const char *name - file name
*       o char mode - 'rR','wW' or 'aA' 
*   Return : stream object if successful, otherwise NULL
*/
void* mfs_open(const char *name,char mode)
{
	struct mfs_file* file = Head;
	while(file)
	{
		if(strcompare(file->fname,name))
		{
			struct mfs_stream* stream = (struct mfs_stream*)myMalloc(sizeof(struct mfs_stream));
			stream->offset = 0;
			stream->readoffset = 0;
			stream->file = file;		
			if(mode == 'a' || mode == 'A')
			{
				stream->offset = file->size;
			}
			else if(mode == 'w' || mode == 'W')
			{
				file->size = 0;
			}
			return stream;
		}
		file = file->next;
	}
	return NULL;
}
Ejemplo n.º 5
0
// Return 1 if username/password is allowed, 0 otherwise.
static int check_password(const char *user, const char *password) {
	// In production environment we should ask an authentication system
	// to authenticate the user.
	// Here however we do trivial check that user and password are not empty
	return !strcompare(user, "admin") && !strcasecmp(password, "nimda");
	return (user[0] && password[0]);
}
Ejemplo n.º 6
0
int check_for_commands(char * command) {
	int number_of_commands = 2;
	int i;
	for(i = 0; i < number_of_commands; i++) {
		if(strcompare(commands[i], command)) {
			return i;
		}
	}
	return -1;
}
Ejemplo n.º 7
0
/*
*   Implements open interface
*   Parameters:
*       o const char *name - button name
*       o char mode - 'r','w' or 'a' 
*   Return : stream object if successful, otherwise NULL
*   sets my_errno to NOT_A_BTN if button name is invalid.
*/
void* btn_open(const char *name,char mode)
{
	enum BTN_MINOR minor = NBTN;
	if(strcompare(ONE_BTN,name))
	{
		minor = ONE;
	}
	else if(strcompare(TWO_BTN,name))
	{
		minor = TWO;
	}
	else
	{
		my_errno = NOT_A_BTN;
		return NULL;
	}
	struct btn_stream *stream = myMalloc(sizeof(struct btn_stream));
	stream->minor = minor;
	return stream;
}
int main()
{
	long i,j,n,m,l,tmp;
	n=1;
	memset(str,0,sizeof(str));
	memcpy(str[1],"sun",sizeof("sun"));
	memset(inp,0,sizeof(inp));
	while (scanf("%s",inp)!=EOF)
	{
		if (inp[0]=='+')
		{
			n++;
			memcpy(str[n],inp+1,sizeof(inp)-1);
		}
		else
		{
			memcpy(inp,inp+1,sizeof(inp)-1);
			printf("%s\n",inp);
			memset(ans,0,sizeof(ans));
			for (i=1;i<=n;i++)
			{
				if (strcompare(inp,str[i],strlen(inp)))
				{
					ans[0]++;
					j=ans[0];
					ans[j]=i;
					while ((j>1) && (strcmp(str[ans[j]],str[ans[j-1]])<0))
					{
						tmp=ans[j];
						ans[j]=ans[j-1];
						ans[j-1]=tmp;
						j--;
					}
					if ((j>1) && (strcmp(str[ans[j]],str[ans[j-1]])==0))
					{
						for (;j<ans[0];j++)
						{
							ans[j]=ans[j+1];
						}
						ans[0]--;
					}
					if (ans[0]>20) ans[0]=20;
				}
			}
			for (i=1;i<=ans[0];i++)
			{
				printf("  %s\n",str[ans[i]]);
			}
		}
		memset(inp,0,sizeof(inp));
	}
	return 0;
}
Ejemplo n.º 9
0
int compare(char* firstname,int limit)
{
    int i,temp=-1;
    for(i=0;i<limit;i++)
    {
        if(strcompare(firstname, pers[i].first)==0)
        {
            temp=i;
            break;
        }
    }
    return temp;
}
Ejemplo n.º 10
0
static commandfunc_t find_command(const char* cmdName)
/*
*   Private function to search command name in the data structure. 
*     case-sensitive.
*   Parameters:
*       o const char *cmdName- command name string 
*   Return : corresponding function pointer if found, NULL otherwise
*/
{
	int i;
	for(i = 0;i < NUM_COMMANDS;++i)//iteration on data structure
	{
		if(strcompare(cmdName,commands[i].name))
			return commands[i].functionp;//command name found
	}
	return NULL;//command name not found
}
Ejemplo n.º 11
0
/*
*   Implements create interface. Validates file name, creates a new file block
*   if file does not exist.
*   Parameters:
*       o const char *name - file name to be created
*   sets my_errno to END_OF_FILE read at end of file.
*/
void mfs_create(const char *name)
{
	if(validateFileName(name))
	{
		struct mfs_file* file = Head;
		while(file)
		{
			if(strcompare(file->fname,name))
			{
				return;
			}
			file = file->next;
		}
		struct mfs_file* newfile = mfs_construct(name);
		newfile->next = Head;
		Head = newfile;
	}
	else
	{
		my_errno = INVALID_FILE_NAME;
	}
}
Ejemplo n.º 12
0
/*
*   Implements delete interface. frees up memory used by file data block and removes name for 
*   file system data structure
*   Parameters:
*       o const char *name - file name to be deleted.
*/
void mfs_delete(const char *name)
{
	struct mfs_file* file = Head;
	struct mfs_file* prev = Head;
	while(file)
	{
		if(strcompare(file->fname,name))
		{
			if(prev == file)
			{
				struct mfs_file* temp = Head->next;
				mfs_destroy(Head);
				Head = temp;
			}
			else
			{
				prev = file->next;
				mfs_destroy(Head);
			}
		}
		prev = file;
		file = file->next;
	}
}
Ejemplo n.º 13
0
int compare_2_dates(char *dob1, char *dob2) {
	if (strcompare(getyear(dob1), getyear(dob2)) == 0) {
		if (strcompare(getmonth(dob1), getmonth(dob2)) == 0) {
			if (strcompare(getDate(dob1), getDate(dob2)) == 0) {
				return 0;
			}
			else if (strcompare(getDate(dob1), getDate(dob2)) < 0)
			{
				return 1;
			}
			return 2;
		}
		else if (strcompare(getmonth(dob1), getmonth(dob2))<0)
		{
			return 1;
		}
		return 2;
	}
	else if (strcompare(getyear(dob1), getyear(dob2))<0)
	{
		return 1;
	}
	return 2;
}
Ejemplo n.º 14
0
/* string comparison functions (not case sensitive) */
LVAL xstrilss(void) { return (strcompare('<',TRUE)); } /* string-lessp */
LVAL xstrileq(void) { return (strcompare('L',TRUE)); } /* string-not-greaterp */
LVAL xstrieql(void) { return (strcompare('=',TRUE)); } /* string-equal */
LVAL xstrineq(void) { return (strcompare('#',TRUE)); } /* string-not-equal */
LVAL xstrigeq(void) { return (strcompare('G',TRUE)); } /* string-not-lessp */
Ejemplo n.º 15
0
/* string comparison functions (not case sensitive) */
LVAL xstrilss(void) { return (strcompare('<',TRUE)); } /* string-lessp */
LVAL xstrileq(void) { return (strcompare('L',TRUE)); } /* string-not-greaterp */
LVAL xstrieql(void) { return (strcompare('=',TRUE)); } /* string-equal */
LVAL xstrineq(void) { return (strcompare('#',TRUE)); } /* string-not-equal */
Ejemplo n.º 16
0
/* string comparison functions (not case sensitive) */
LVAL xstrilss(void) { return (strcompare('<',TRUE)); } /* string-lessp */
LVAL xstrileq(void) { return (strcompare('L',TRUE)); } /* string-not-greaterp */
LVAL xstrieql(void) { return (strcompare('=',TRUE)); } /* string-equal */
Ejemplo n.º 17
0
/* string comparison functions (not case sensitive) */
LVAL xstrilss(void) { return (strcompare('<',TRUE)); } /* string-lessp */
LVAL xstrileq(void) { return (strcompare('L',TRUE)); } /* string-not-greaterp */
Ejemplo n.º 18
0
/* string comparison functions (not case sensitive) */
LVAL xstrilss(void) { return (strcompare('<',TRUE)); } /* string-lessp */
Ejemplo n.º 19
0
/* string comparision functions */
LVAL xstrlss(void) { return (strcompare('<',FALSE)); } /* string< */
LVAL xstrleq(void) { return (strcompare('L',FALSE)); } /* string<= */
LVAL xstreql(void) { return (strcompare('=',FALSE)); } /* string= */
LVAL xstrneq(void) { return (strcompare('#',FALSE)); } /* string/= */
LVAL xstrgeq(void) { return (strcompare('G',FALSE)); } /* string>= */
LVAL xstrgtr(void) { return (strcompare('>',FALSE)); } /* string> */
Ejemplo n.º 20
0
/* string comparision functions */
LVAL xstrlss(void) { return (strcompare('<',FALSE)); } /* string< */
LVAL xstrleq(void) { return (strcompare('L',FALSE)); } /* string<= */
LVAL xstreql(void) { return (strcompare('=',FALSE)); } /* string= */
LVAL xstrneq(void) { return (strcompare('#',FALSE)); } /* string/= */
LVAL xstrgeq(void) { return (strcompare('G',FALSE)); } /* string>= */
Ejemplo n.º 21
0
/* string comparision functions */
LVAL xstrlss(void) { return (strcompare('<',FALSE)); } /* string< */
LVAL xstrleq(void) { return (strcompare('L',FALSE)); } /* string<= */
LVAL xstreql(void) { return (strcompare('=',FALSE)); } /* string= */
LVAL xstrneq(void) { return (strcompare('#',FALSE)); } /* string/= */
Ejemplo n.º 22
0
/* string comparision functions */
LVAL xstrlss(void) { return (strcompare('<',FALSE)); } /* string< */
LVAL xstrleq(void) { return (strcompare('L',FALSE)); } /* string<= */
LVAL xstreql(void) { return (strcompare('=',FALSE)); } /* string= */
Ejemplo n.º 23
0
/* string comparison functions (not case sensitive) */
LVAL xstrilss(void) { return (strcompare('<',TRUE)); } /* string-lessp */
LVAL xstrileq(void) { return (strcompare('L',TRUE)); } /* string-not-greaterp */
LVAL xstrieql(void) { return (strcompare('=',TRUE)); } /* string-equal */
LVAL xstrineq(void) { return (strcompare('#',TRUE)); } /* string-not-equal */
LVAL xstrigeq(void) { return (strcompare('G',TRUE)); } /* string-not-lessp */
LVAL xstrigtr(void) { return (strcompare('>',TRUE)); } /* string-greaterp */
Ejemplo n.º 24
0
  main( )
  {
    char inp;
    char *T[255];
    char *S[255];
    struct sstring *s1,*s2,*res;
    int pos,len;
    printf("1-------strassing\n");
    printf("2-------strlength\n");
    printf("3-------strcompare\n");
    printf("4-------clearstring\n");
    printf("5-------concat\n");
    printf("6-------substring\n");
    printf("*-------exit!");
    printf("please in put1--6 or *\n\n")
 while(1){
      scanf("%c",&inp);
      switch(inp){
	      case 1:{
		    scanf("%s",&S);
		    res->ch=strassign(s1,S);
		    if(res->ch==0)
			printf("the string is:%s",s1->ch);
		    else
		       printf("error"); }
	      case 2: {
		    scanf("%s",&S);
		    s1->length=strlen(S);
		    strcpy(s1->ch,S);
		    res->ch=strlength(S);
		    printf("the string is:%d\n",s1->length);}
	      case 3:{
		    scanf("%s",&S);
		    scanf("%s",&T);
		    s1->length=strlen(S);
		    strcpy(s1->ch,S);
		    s2->length=strlen(T);
		    strcpy(s2->ch,T);
		    res->ch=strcompare(S,T);
		    switch(res){
		       case 0:
			    printf("two strings are equle");
		       case 1:
			    printf("the first string > the second string");
		       case -1:
			    printf("the first string < the second string");}
                       }
	     case 4:{
		    res->ch=clearstring(s1);
		    res->ch=clearstring(s2);
		    printf("the string is NULL");}
	     case 5:{
		    scanf("%s",&S);
		    scanf("%s",&T);
		    strcat(&s1,S,T);
		    if(res==0)
		      printf("the string is:%s",s1->ch);
		    }
	     case 6: {
		    scanf("%s_%d_%d",S,&pos,&len);
		    s2->length=strlen(S);
		    strcpy(s2->ch,S);
		    res->ch=substring(s1,s2,pos,len);
		    if(res==0)
			printf("the string is:%s",s1->ch);
		    else printf("error");}
	     case *:
		    exit;
       }
      }
    }
Ejemplo n.º 25
0
/*
Function: 
	quicksort
Arguments:
	A = the stack we want to be sorted
	B = the stack where we put the bigger values
	C = the stack where we put the smaller values
	flipped = for keeping the sort stable
	location = where the sorted stack is, from 1 to 3
	counter = how many values have been pushed on this level
	pos = the value of POS
	len = the value of LEN
Return Value:
	This function returns void
Outside Effect:
	This function sorts the values in the A stack, which
	persists outside the function
Description:
	This function performs quicksort using three stacks
*/
void quicksort(Stack *A, Stack *B, Stack *C, bool flipped,
		int location, int counter, int pos, int len) {

		// Base case
		if (counter == 0) {
			return;
		}

		char *splitter;		// The splitter string

		// Pop off the splitter
		if (!isEmptyS(A)) {
			if (!popS(A, &splitter)) {
				DIE("Pop failed");
			}
			counter--;
		}

		char *next = NULL;	// The next string to be sorted
		int bigger = 0;		// The number of pushes to B
		int smaller = 0;	// The number of pushes to C

		// Split the stack into bigger and smaller
		while (counter > 0) {
			if (!popS(A, &next)) {
				DIE("Pop failed1");
			}

			int comp;	// The value of the comparsion
			comp = strcompare(splitter, next, pos, len);

			// Push the string to the bigger stack
			if (comp < 0) {
				if(!pushS(B, next)) {
					DIE("Push failed");
				}
				bigger++;
			}
			// Push the string to the smaller stack
			else if (comp > 0) {
				if(!pushS(C, next)) {
					DIE("Push failed");
				}
				smaller++;
			}
			// If strings are equal, perform stability check
			else {
				// If flipped, push the splitter
				if (flipped) {
					if(!pushS(B, splitter)) {
						DIE("Push failed");
					}
					splitter = next;
					bigger++;
				}
				// Otherwise push the string
				else {
					if(!pushS(B, next)) {
						DIE("Push failed");
					}
					bigger++;
				}
			}  
			counter--;
		}

		// Swap the value of flipped each iteration
		flipped = !flipped;

		// Recursive step
		if (bigger == 0) {
			// Push to A, swap location 1 and 2, sort smallers
			if (location == 1) {
				if(!pushS(A, splitter)) {
					DIE("Push failed");
				}
				if (smaller == 0) {
					return;
				}
				quicksort(C, A, B, flipped, 2, smaller, pos, len);
			}
			// Push to B, keep location the same, sort smallers
			else if (location == 2) {
				if(!pushS(B, splitter)) {
					DIE("Push failed");
				}
				if (smaller == 0) {
					return;
				}
				quicksort(C, B, A, flipped, 2, smaller, pos, len);
			}
		}
		else {
			// Swap location 1 and 2, sort biggers, push to A, sort smallers
			if (location == 1) {
				quicksort(B, A, C, flipped, 2, bigger, pos, len);
				if(!pushS(A, splitter)) {
					DIE("Push failed");
				}
				if (smaller == 0) {
					return;
				}
				quicksort(C, A, B, flipped, 2, smaller, pos, len);
			}
			// Keep location the same, sort biggers, push to B, sort smallers
			else if (location == 2) {
				quicksort(B, A, C, flipped, 1, bigger, pos, len);
				if(!pushS(B, splitter)) {
					DIE("Push failed");
				}
				if (smaller == 0) {
					return;
				}
				quicksort(C, B, A, flipped, 2, smaller, pos, len);
			}
		}
		return;
}
Ejemplo n.º 26
0
euint32 main(void)
{
	//initialise variables
	euint8 buffersd[size];
	euint32 i;
	currDir.ParentAdd = 0;
	currDir.name[0] = '/';
	currDir.name[1] = 0;
	currDir.startH = 0;
	currDir.startL = 2;
	root.ParentAdd = 0;
	root.name[0] = '/';
	root.name[1] = 0;
	root.startH = 0;
	root.startL = 2;

	audio_initialise();

	uart_file = open("/dev/uart_0",O_RDWR);
	// Prepare UART for command input
	printf("UART Ready.\n");
	UART_write("UART Ready.\r\n");
	// Initialise file system
	if(SD_init()==1)
		{
			printf("Could not open filesystem.\n");
			UART_write("File System could not mount.\r\n\t");
		}
		else
		{
			//FS mounted
			printf("File System Ready.\r\n");
			UART_write("File System Ready.\r\n");
		}
		
	read_mboot(buffersd);
	//calculates LBAbegin
	for(i=454;i<459;i++)
	{
		*((euint8*)&LBAbegin+(i-454)) = buffersd[i];
	}

	read_part_boot(LBAbegin,&Part_Boot); //reads the partition boot and fills the global variable
	char buffer[128];
	char* array[10];
	char* temp[10];
	while(1)
	{
		UART_write(">");
		if(uart_file == 0)
		{
			printf("Sorry UART open failed\r\n");
			return(-1);//error
		}
		euint32 number;
		euint32 xx=0;
		for(xx=0;xx<128;xx++)
		{
			buffer[xx]=0;//empty buffer
		}
		for(xx=0;xx<10;xx++)
		{
			array[xx] = 0;
			temp[xx]=0;
		}
		UARTListener(&buffer,uart_file);
		number = string_parser(buffer,array);
		euint32 i = 0;
		// search through commands to find match
		if(array[0]!=0)
		{
			for(i=0;i<3;i++)
			{
				//if string match found
				 if (strcompare(array[0],list[i].com_string)==0)
				{
					//extract the arguments of command
					euint32 j;
					for( j=0;j<number-1;j++)
					{
						temp[j] = array[j+1];
						//free(array[j+1]);
					}
					//call relative function
					list[i].com_fun((number-1),temp);
					for( j=0;j<number-1;j++)
					{
						free(array[j+1]);
					}
					i=56;//exit loop
				}
			}

			if(i==3)
			{
				UART_write("Error : Command Not Found!\r\n");
			}
		}
		free(array[0]);
	}
	return 0;
}
Ejemplo n.º 27
0
int main(int argc, const char * argv[])
{
   char* hello = "12345678";
   char test[] = "4444";
    
   printf("Length of %s is %d \n", hello, strlength(hello));
  printf("Length of %s is %d \n", test, strlength(test));
    
    // 6. Given two strings: print true if they are equal and false if they are not equal.
    
    printf("strcompare(%s,%s) is %s \n", hello, hello, strcompare(hello,hello) ? "true" : "false");
    printf("strcompare(%s,%s) is %s \n", hello, test, strcompare(hello,test) ? "true" : "false");
    printf("strcompare(%s,%s) is %s \n", "hello", "jello", strcompare("hello","jello") ? "true" : "false");
    printf("strcompare(%s,%s) is %s \n\n", "hello", "hella", strcompare("hello","hella") ? "true" : "false");

    // 7. Given a string “This is a test”, replace "te" with "gho"
    
//    char test2[] = "This is a test";
    
//    printf("Before test: test2 = %s\n", test2);
    
    // locate insertion point
    
//    char* insert = strstr(test2, "te");
//    strcopy(test2, "te","gho");

//    printf("After test: test2 = %s\n", test2);
    
    // 8. You have an array of strings. Create a new string, which is a concatenation of all the array elements.
    
    char* myarray[4];
    myarray[0] = "Butch ";
    myarray[1] = "Cassidy ";
    myarray[2] = "and the Sundance ";
    myarray[3] = "Kid";
    
    printf("%s%s%s\n", myarray[0],myarray[1],myarray[2]);
    char *result8 = malloc((strlength(myarray[0])
                           +strlength(myarray[1])
                           +strlength(myarray[2])
                           +strlength(myarray[3])+1)*sizeof(char));
    
    
    for(int i=0;i<4;i++){
        strcat(result8, myarray[i]);
    }
    printf("Result 8 is %s\n", result8);
    
    int match  = (strlength(result8) == strlength(myarray[0]) +
                  strlength(myarray[1]) +
                  strlength(myarray[2]) +
                  strlength(myarray[3]));
    
    printf("String lengths of original and concatted arrays %s\n", match ? "match" : "do not match");
    

    free(result8);
    
    return 0;
    
} // end of main
Ejemplo n.º 28
0
/* string comparision functions */
LVAL xstrlss(void) { return (strcompare('<',FALSE)); } /* string< */
LVAL xstrleq(void) { return (strcompare('L',FALSE)); } /* string<= */
Ejemplo n.º 29
0
/*************************************************************************
 *
 *N  query_table
 *
 *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 *
 *   Purpose:
 *P
 *     This function returns the set of selected rows of a VPF table
 *     based upon the evaluation of the given selection expression string.
 *
 *     The expression is strictly evaluated left to right.  No nesting
 *     is supported, so parentheses are not allowed.  The expression
 *     must match the form:
 *        <field><log op><value> [ <join> <field><log op><value>]
 *     where,
 *        <field> is a valid field name of the table.
 *        <log op> is one of the following: =, <, >, <=, >=, <> (not equal).
 *        <value> is a valid value for the field.
 *        <join> is either " AND " or " OR ".
 *     Any number of clauses (<field><log op><value>) may be joined
 *     together with AND or OR to form the expression.
 *E
 *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 *
 *   Parameters:
 *A
 *    expression <input>==(char *) selection expression string.
 *    table      <input>==(vpf_table_type) VPF table structure.
 *    return    <output>==(set_type) set of selected rows.
 *E
 *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 *
 *   History:
 *H
 *    Barry Michaels    May 1991                          DOS Turbo C
 *E
 *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 *
 *   External Variables:
 *X
 *    None
 *E
 *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 *
 *   Functions Called:
 *F
 *    set_type set_init( rspf_int32 n ) SET.C
 *    void set_insert( rspf_int32 element, set_type set ) SET.C
 *    linked_list_type parse_expression( char *expression,
 *                     vpf_table_type table ) VPFQUERY.C
 *    row_type read_next_row( vpf_table_type table ) VPFREAD.C
 *    position_type ll_first( linked_list_type list ) LINKLIST.C
 *    int ll_end( position_type position ) LINKLIST.C
 *    void ll_element( position_type position, void *element ) LINKLIST.C
 *    void *get_table_element( rspf_int32 field_number,
 * 			 row_type row,
 *			 vpf_table_type table,
 *			 void *value,
 *			 rspf_int32  *count ) VPFREAD.C
 *    void display_message( char *info ) USER DEFINED
 *    static int strcompare( char *val1, char *val2, char op ) VPFQUERY.C
 *    static int icompare( rspf_int32 val1, rspf_int32 val2, char op ) VPFQUERY.C
 *    static int fcompare( float val1, float val2, char op ) VPFQUERY.C
 *    void ll_reset( linked_list_type list ) LINKLIST.C
      void free_row( row_type row, vpf_table_type table) VPFREAD.C
 *E
 *************************************************************************/
set_type query_table( char *expression, vpf_table_type table )
{
   row_type row;
   position_type pos;
   expr_type expr;
   register rspf_int32 i;
   int boolval=FALSE, booltemp=0, join = OR;
   rspf_int32 lval, lval2, count;
   float fval, fval2;
   char tval, tval2, *tptr;
   linked_list_type exprlist;
   set_type select_set;

   select_set = set_init(table.nrows+1);

   if (strcmp(expression,"*")==0) {
      set_on(select_set);
      return select_set;
   }

   exprlist = parse_expression( expression, table );

   if (!exprlist) return select_set;

   if (table.storage == DISK)
      fseek( table.fp, index_pos(1,table), SEEK_SET );

   for (i=1;i<=table.nrows;i++) {

      if (table.storage == DISK)
	 row = read_next_row(table);
      else
	 row = get_row( i, table );

      pos = ll_first(exprlist);
      while (!ll_end(pos)) {
	 ll_element( pos, &expr );
	 switch (table.header[expr.field].type) {
	    case 'I':
	       if (table.header[expr.field].count == 1) {
		  get_table_element( expr.field, row, table, &lval, &count );
		  lval2 = atol(expr.value);
		  booltemp = icompare( lval, lval2, expr.op );
	       } else {
		  display_message(
		     "Selection may not be performed upon arrays");
		  i=table.nrows+1;
	       }
	       break;
	    case 'T':
	       if (table.header[expr.field].count == 1) {
		  get_table_element( expr.field, row, table, &tval, &count );
		  tval2 = expr.value[0];
		  booltemp = comp( &tval, &tval2, sizeof(tval), expr.op );
	       } else {
		  tptr = (char *)get_table_element( expr.field, row, table,
				   NULL, &count );
		  booltemp = strcompare( tptr, expr.value, expr.op );
		  free(tptr);
	       }
	       break;
	    case 'F':
	       if (table.header[expr.field].count == 1) {
		  get_table_element( expr.field, row, table, &fval, &count );
		  if (!is_vpf_null_float(fval)) {
		     fval2 = (float)atof(expr.value);
		     booltemp = fcompare( fval, fval2, expr.op );
		  } else booltemp = FALSE;
	       } else {
		  display_message(
		     "Selection may not be performed upon arrays");
		  i=table.nrows+3;
	       }
	       break;
	    default:
	       display_message("Field type not supported for query");
	       i=table.nrows+3;
	       break;
	 }

	 if (i>table.nrows) break;

	 if (join==OR)
	    boolval = boolval || booltemp;
	 else
	    boolval = boolval && booltemp;

	 join = expr.join;

	 pos = pos->next;
      }
      free_row( row, table );
      if (boolval) set_insert(i,select_set);
      boolval = FALSE;
      join = OR;

      if (i==table.nrows+3) break;

   }

   ll_reset(exprlist);

   return select_set;
}
Ejemplo n.º 30
0
/* string comparision functions */
LVAL xstrlss(void) { return (strcompare('<',FALSE)); } /* string< */