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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
11-16-2003, 11:33 AM
|
#1
|
|
Member
Registered: Jun 2003
Location: Netherlands, The
Distribution: Ubuntu, Kernel 2.6.7
Posts: 169
Rep:
|
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<
|
|
|
|
11-16-2003, 08:48 PM
|
#2
|
|
Senior Member
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357
Rep:
|
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 08:50 PM.
|
|
|
|
11-17-2003, 04:11 AM
|
#3
|
|
Member
Registered: Jun 2003
Location: Netherlands, The
Distribution: Ubuntu, Kernel 2.6.7
Posts: 169
Original Poster
Rep:
|
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
|
|
|
|
11-18-2003, 07:24 PM
|
#4
|
|
Senior Member
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357
Rep:
|
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 07:25 PM.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:03 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|