Link to home
Start Free TrialLog in
Avatar of pzozulka
pzozulka

asked on

C Programming: string compare

I'm having trouble with a method that compares two strings. Specifically I need my program to understand the difference between "http://www.csun.edu/" and "http://www.csun.edu/~steve/" (see config file below).

If I pass "http://www.csun.edu/", how can I make my program understand I'm referring to the redirect line in my config file and not the cached line.

I tried doing strcat( URL, " " ) as well as strcat( URL, "\t" ) to add an extra space or tab character at the end of my URL, but then later in my code when I do strstr( URL_ConfigFile, URL ), it returns a NULL pointer saying that a the URL string doesn't exist inside of my config file.

I think it has something to do with the additional space ( " " ) I added.

 

# Example Configuration File for deflect:  
# deflect:  Daemon for HTTP Egress, Filtering, Load balancing, and Caching Trivially
#
# URL-prefix			Disposition	Host list

http://www.csun.edu/		redirected	130.166.238.195, redwing.csun.edu
http://facebook.com/		filtered
http://www.csun.edu/~steve/	cached		www.csun.edu

# Note that any URL that is not covered by any rule is transferred through the system 
# without any additional processing.

Open in new window

Avatar of ozo
ozo
Flag of United States of America image

What was in URL before the strcat, and how was URL_ConfigFile set?
Avatar of pzozulka
pzozulka

ASKER

The URL before strcat was "http://www.csun.edu/".

The URL_ConfigFile was created using nano on a Linux machine. I tried editing that file to have tabs and spaces between the URL and the Disposition columns. I tried both ways. As of right now, I'm using tabs to separate the columns.
What was in URL_ConfigFile when strstr( URL_ConfigFile, URL ) was executed?

Can you do strstr( URL_ConfigFile, "http://www.csun.edu/") and then check the following character?
The config file mentioned above was read() into a buffer. I then scanned the buffer, and replaced all \r or \n with \0. This broke the file into lines where each line is a string.

 Then there is a for-loop that cycles through each line, treating each line as a string.

So to answer your question, during one of the iterations of the loop:
URL_ConfigFile = "http://www.csun.edu/            redirected      130.166.238.195, redwing.csun.edu"

URL = "http://www.csun.edu/            " //tab character at the end was added with strcat()
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
For simplicity sake I said URL_ConfigFile, but in my code it's actually Lines[j]. See below.

So I tried strstr(Lines[j], "www.csun.edu/"), and it returns the line in the config file that says cached.

I tried comparing to "www.csun.edu/ " <-- (space char at the end), but no luck.
I also tried "www.csun.edu/     " <-- (tab char at the end), also no luck.

if(strstr(Lines[j], "www.csun.edu/") != NULL) {
					if(strstr(Lines[j], "redirected") != NULL) {
						m = 1;
					}
					else if(strstr(Lines[j], "filtered") != NULL) {
						m = 2;
					}
					else if(strstr(Lines[j], "cached") != NULL) {
						m = 3;
					}
				}
				else
					m = -1;

Open in new window

You can't do strcat( "www.csun.edu/","\t" )
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here's what I get, very strange:
http://www.csun.edu/            redirected      130.166.238.195, redwing.csun.edu
R:       9
http://facebook.com/            filtered
http://www.csun.edu/~steve/     cached          www.csun.edu

Open in new window


Code:
fprintf(configfp,"%s\n",Lines[i]);

				char *p;
				if( (p=strstr(Lines[i], "www.csun.edu/")) != NULL && p[13] != '~' )
				{
					fprintf(configfp, "R:%c %d\n",p[13],p[13]);
				}
				if(strstr(Lines[i], "www.csun.edu/") != NULL) {
					if(strstr(Lines[i], "redirected") != NULL) {
						m = 1;
					}
					else if(strstr(Lines[i], "filtered") != NULL) {
						m = 2;
					}
					else if(strstr(Lines[i], "cached") != NULL) {
						m = 3;
					}
				}
				else
					m = -1;

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks much, I think I know what happened. I kept on looping even after finding the correct line in my config file. Since the loop continued it would override my m variable that keeps track of this, and at the end would return the wrong thing.