LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Problems with matching (http://www.linuxquestions.org/questions/programming-9/problems-with-matching-116698/)

alitrix 11-16-2003 11:33 AM

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:o<--
p0: AB - p1: >B<
AB B #suikode 1068971831 APAAA:o<--
p0: AB - p1: >B<
AB B #feds 1068971820 ABAAD,ABAAC,ABAAB:o,APAAA,ABAAA<--
p0: AB - p1: >B<
AB B #coder-com 1068971818 +l 12 ABAAB:o,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<

ToniT 11-16-2003 08:48 PM

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.

alitrix 11-17-2003 04:11 AM

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

ToniT 11-18-2003 07:24 PM

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).


All times are GMT -5. The time now is 12:18 PM.