LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 11-16-2003, 12:33 PM   #1
alitrix
Member
 
Registered: Jun 2003
Location: Netherlands, The
Distribution: Ubuntu, Kernel 2.6.7
Posts: 169

Rep: Reputation: 30
Problems with matching


The matching isn't working

Code:
/* Reads the incoming line from a socket and try's to split it correct*/
void GetLine(char *line, int servfd) {
        /* line = A line WITHOUT `\n` */
        int i, rawnr;
        char p0[200], p1[200];
        char tmp[512], part[512];
        if (line[0]!='\0') {
                trim(line);
                Split(line,' ',0,p0);
                Split(line,' ',1,p1);
                /*if (strncasecmp(p1,"G",strlen("G"))!=0)*/
                        printf("%s<--\n",line);
                        printf("p0: %s - p1: >%s<\n",p0,p1);
                if (!strncasecmp(p1,"EB",strlen("EB"))) { /* End of Burst */
                        printf("Got EB!!\n");
                        Tok_EA(servfd);
                        if (joined==FALSE) {
                                /*sprintf (tmp, "JOIN %s\r\n", HOMECHAN);
                                SendLine(tmp, TRUE, servfd);
                                joined=TRUE;*/
                        }
                }
        }
}
Output:
AB N AliTriX 1 1068972084 wer alitrix.homelinux.net +oiwg B]AAAB ABAAB :Ali<--
p0: AB - p1: >N<
AB N blaat 1 1068972106 wer alitrix.homelinux.net +iw B]AAAB ABAAC :Ali<--
p0: AB - p1: >N<
AB N AliTriX_ 1 1068972109 wer alitrix.homelinux.net +iw B]AAAB ABAAD :Ali<--
p0: AB - p1: >N<
AB B #vodkaz 1068971831 APAAA<--
p0: AB - p1: >B<
AB B #suikode 1068971831 APAAA<--
p0: AB - p1: >B<
AB B #feds 1068971820 ABAAD,ABAAC,ABAAB,APAAA,ABAAA<--
p0: AB - p1: >B<
AB B #coder-com 1068971818 +l 12 ABAAB,APAAA,ABAAA<--
p0: AB - p1: >B<
AB EB <--
p0: AB - p1: >EB<
AB EA <--
p0: AB - p1: >EA<
APAAA R u :AX<--
p0: APAAA - p1: >R<
AB G !1068994838.36479 go.moo.oh.yes.they.do 1068994838.36479<--
p0: AB - p1: >G<
AB SQ alitrix.homelinux.net 0 :Ping timeout<--
p0: AB - p1: >SQ<
 
Old 11-16-2003, 09:48 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
There is something else wrong.
I can't directly test your code, because you refer to functions not defined (trim and Split),
but my own testcase (as close to yours as possible):
Code:
#include <string.h>
#include <stdio.h>
void GetLine(char *line, int servfd) {
        /* line = A line WITHOUT `\n` */
//        int i, rawnr;
        char p0[200], p1[200];
//        char tmp[512], part[512];
        if (line[0]!='\0') {
//                trim(line);
//                Split(line,' ',0,p0);
//                Split(line,' ',1,p1);
                strncpy(p0,line,3);
                p0[3]=0;
                strncpy(p1,&line[4],2);
                p1[2]=0;
                /*if (strncasecmp(p1,"G",strlen("G"))!=0)*/
                        printf("%s<--\n",line);
                        printf("p0: %s - p1: >%s<\n",p0,p1);
                if (!strncasecmp(p1,"EB",strlen("EB"))) { /* End of Burst */
                        printf("Got EB!!\n");
                        //Tok_EA(servfd);
                        //if (joined==FALSE) {
                                /*sprintf (tmp, "JOIN %s\r\n", HOMECHAN);
                                SendLine(tmp, TRUE, servfd);
                                joined=TRUE;*/
                        //}
                }
        }
}

int main(void) {
  GetLine("Foo Bar",0);
  GetLine("Foo EBar",0);
  return 0;
}
Ran as:
Quote:
toni@mario:~/work/tst$ gcc -Wall casecmp.c
toni@mario:~/work/tst$ ./a.out
Foo Bar<--
p0: Foo - p1: >Ba<
Foo EBar<--
p0: Foo - p1: >EB<
Got EB!!
toni@mario:~/work/tst$
Works fine.

Last edited by ToniT; 11-16-2003 at 09:50 PM.
 
Old 11-17-2003, 05:11 AM   #3
alitrix
Member
 
Registered: Jun 2003
Location: Netherlands, The
Distribution: Ubuntu, Kernel 2.6.7
Posts: 169

Original Poster
Rep: Reputation: 30
Hm, oke.. gonna post you the needed things:
Code:
/* Reads the incoming line from a socket and try's to split it correct*/
void GetLine(char *line, int servfd) {
	/* line = A line WITHOUT `\n` */
	int i, rawnr;
	char p0[200], p1[200];
	char tmp[512], part[512];
	if (line[0]!='\0') {
		trim(line);
		Split(line,' ',0,p0);
		Split(line,' ',1,p1);
		/*if (strncasecmp(p1,"G",strlen("G"))!=0)*/
			printf("%s<--\n",line);
			printf("p0: %s - p1: >%s<\n",p0,p1);
		if (strncasecmp(p1,"B",1)==0) { /* End of Burst */
			printf("Got EB!!\n");
			Tok_EA(servfd);
			if (joined==FALSE) {
				/*sprintf (tmp, "JOIN %s\r\n", HOMECHAN);
				SendLine(tmp, TRUE, servfd);
				joined=TRUE;*/
			}
		}
	}
}

/* Splits `line` and kills the loop if x equals die and puts the cached lines in `newline */
void Split(char *line, char stop, int die, char newline[]) {
	int i,j=0, x=0;
	empty(newline);
	for(i=0;i<=strlen(line);i++) {
		if (line[i]!=stop && line[i]!='\0') {
			newline[j++]=line[i];
		} else {
			if (x==die) {
				newline[j]='\0';
				break;
			} else {
				newline[0]='\0';
				j=0;
				x++;
			}
		}
		
	}
}

/* Remove trailing blanks, tabs, newlines */
void trim(char *s) {
	int n;

	for (n = strlen(s)-1; n >= 0; n--)
		if (s[n] != ' ' && s[n] != '\t' && s[n] != '\n')
			break;
	s[n+1] = '\0';
}
I hope you have enough information with those
 
Old 11-18-2003, 08:24 PM   #4
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Yet I would write some things differently and there are some redundancy in the code, there is nothing wrong with any of the code you have shown to me.

Sample input (contents of file named "input"):
Quote:
AB N AliTriX 1 1068972084 wer alitrix.homelinux.net +oiwg B]AAAB ABAAB :Ali
AB N blaat 1 1068972106 wer alitrix.homelinux.net +iw B]AAAB ABAAC :Ali
AB N AliTriX_ 1 1068972109 wer alitrix.homelinux.net +iw B]AAAB ABAAD :Ali
AB B #vodkaz 1068971831 APAAA
AB B #suikode 1068971831 APAAA
AB B #feds 1068971820 ABAAD,ABAAC,ABAAB,APAAA,ABAAA
AB B #coder-com 1068971818 +l 12 ABAAB,APAAA,ABAAA
AB EB
AB EA
APAAA R u :AX
AB G !1068994838.36479 go.moo.oh.yes.they.do 1068994838.36479
AB SQ alitrix.homelinux.net 0 :Ping timeout
The code:
Code:
#include <string.h>
#include <stdio.h>

void empty(char *line) {
  memset(line,0,200);
}

void Split(char *line, char stop, int die, char *newline) {
  int i,j=0, x=0;
  empty(newline);
  for(i=0;i<=strlen(line);i++) {
    if (line[i]!=stop && line[i]!='\0') {
      newline[j++]=line[i];
    } else {
      if (x==die) {
	newline[j]='\0';
	break;
      } else {
	newline[0]='\0';
	j=0;
	x++;
      }
    }    
  }
}

void trim(char *s) {
  int n; 
  for (n = strlen(s)-1; n >= 0; n--)
    if (s[n] != ' ' && s[n] != '\t' && s[n] != '\n')
      break;
  s[n+1] = '\0';
}

void GetLine(char *line, int servfd) {
  char p0[200], p1[200];
  if (line[0]!='\0') {
    trim(line);
    Split(line,' ',0,p0);
    Split(line,' ',1,p1);
    printf("%s<--\n",line);
    printf("p0: %s - p1: >%s<\n",p0,p1);
    if (strncasecmp(p1,"EB",2)==0) {
      printf("Got EB!!\n");
    }
  }
}

int main(void) {
  char line[201];
  while(fgets(line,199,stdin) != NULL) GetLine(line,0);
  return 0;
}
Sample run:
Quote:
toni@mario:~/work/tst/cmp$ gcc -v
Reading specs from /usr/lib/gcc-lib/i486-linux/3.3.2/specs
Configured with: ../src/configure -v --enable-languages=c,c++,java,f77,pascal,objc,ada,treelang --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info
--with-gxx-include-dir=/usr/include/c++/3.3 --enable-shared --with-system-zlib --enable-nls --without-included-gettext --enable-__cxa_atexit --enable-clocale=
gnu --enable-debug --enable-java-gc=boehm --enable-java-awt=xlib --enable-objc-gc i486-linux
Thread model: posix
gcc version 3.3.2 (Debian)
toni@mario:~/work/tst/cmp$ gcc -Wall casecmp.c
toni@mario:~/work/tst/cmp$ ./a.out < input
AB N AliTriX 1 1068972084 wer alitrix.homelinux.net +oiwg B]AAAB ABAAB :Ali<--
p0: AB - p1: >N<
AB N blaat 1 1068972106 wer alitrix.homelinux.net +iw B]AAAB ABAAC :Ali<--
p0: AB - p1: >N<
AB N AliTriX_ 1 1068972109 wer alitrix.homelinux.net +iw B]AAAB ABAAD :Ali<--
p0: AB - p1: >N<
AB B #vodkaz 1068971831 APAAA<--
p0: AB - p1: >B<
AB B #suikode 1068971831 APAAA<--
p0: AB - p1: >B<
AB B #feds 1068971820 ABAAD,ABAAC,ABAAB,APAAA,ABAAA<--
p0: AB - p1: >B<
AB B #coder-com 1068971818 +l 12 ABAAB,APAAA,ABAAA<--
p0: AB - p1: >B<
AB EB<--
p0: AB - p1: >EB<
Got EB!!
AB EA<--
p0: AB - p1: >EA<
APAAA R u :AX<--
p0: APAAA - p1: >R<
AB G !1068994838.36479 go.moo.oh.yes.they.do 1068994838.36479<--
p0: AB - p1: >G<
AB SQ alitrix.homelinux.net 0 :Ping timeout<--
p0: AB - p1: >SQ<
toni@mario:~/work/tst/cmp$
As said, the code is ok. Works for me. Either you mess up something in other parts of the program or your programming environment is plain broken.

If the testcase above does't give same results in your computer, you have either broken gcc or broken glibc (or both).

Last edited by ToniT; 11-18-2003 at 08:25 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find/grep command to find matching files, print filename, then print matching content stefanlasiewski Programming 8 12-18-2013 06:36 PM
Matching with SQL Ephracis Programming 4 12-16-2004 06:09 AM
pattern matching in perl ludeKing Programming 9 04-02-2004 10:53 AM
Addition with matching arrays (or something) slakmagik Programming 2 02-25-2004 10:52 PM
Matching with jokers alitrix Programming 5 12-02-2003 03:46 PM


All times are GMT -5. The time now is 03:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration