Confusion : Warning long code

This is a discussion on Confusion : Warning long code within the C Programming forums, part of the General Programming Boards category; right now im completly confused. im working on a function that is supposed to tag a tag and change its ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    Confusion : Warning long code

    right now im completly confused. im working on a function that is supposed to tag a tag and change its data

    <tag>data</tag>

    so a call like

    Code:
    char test[225];
         strcpy(test, "<crap>testing</crap>");
    
    changetags(test, "TESTING!!!");
        
    printf("%s\n", test);
    would give me back "<crap>TESTING!!!</crap>"

    im completly confused. here my code, sorry it so long. but i have no idea what wrong here, i think ive been looking at it for to long.

    Code:
    int changedata(char *src, char change[]) {
        int x = 0;          /* Current Position. */
        int check;
        int starttag_start;
        int starttag_end;
        int endtag_start;
        int endtag_end;
        
        char *value = malloc(1);
            value[0] = '\0';
        int length = 0;
        
        int change_length = strlen(change);
        
        while(src[x] != '\0') {
            if(src[x] == '<') {
                check = x;
                
                while(src[check] != '>' && src[check] != ' ' && src[check] != '\0')
                    ++check;
                    
                /* Not html. */
                if(src[check] == ' ' || src[check] == '\0') {
                    value = realloc(value, 2);
                    value[length] = src[check];
                    value[(length + 1)] = '\0';
                    
                    ++length;
                }
                
                /* Start html tag found. */
                if(src[check] == '>') {
                    starttag_start = x;
                    starttag_end = check;
                    
                    while(src[check] != '<' && src[check] != '\0')
                        ++check;
                        
                    /* Start of end tag found. */
                    if(src[check] == '<')
                        endtag_start = check;
                    else
                        break;
                        
                    /* End of end tag found. */
                    while(src[check] != '>' && src[check] != ' ' && src[check] != '\0')
                        ++check;
                        
                    if(src[check] == '>') {
                        endtag_end = check;
                        
                        /* Check everything. */
                        if(starttag_start == 0 || starttag_end == 0)
                            break;
                        if(endtag_start == 0 || endtag_end == 0)
                            break;
                            
                        /* Slowly copy the start tag. */
                        while(starttag_start < starttag_end) {
                            value = realloc(value, 2);
                            value[length] = src[starttag_start];
                            value[(length + 1)] = '\0';
                            
                            ++length;
                            ++starttag_start;
                        }
                        
                        /* Copy the changed data. */
                        value =  realloc(value, (change_length + 1));
                        strcat(value, change);
                        length += change_length;
                        
                        /* Copy the end tag. */
                        while(endtag_start < endtag_end) {
                            value = realloc(value, 2);
                            value[length] = src[endtag_start];
                            value[(length + 1)] = '\0';
                            
                            ++length;
                            ++endtag_start;
                        }
                    } else
                        break;
                
                /* Reset. */
                starttag_start = 0;
                starttag_end = 0;
                endtag_start = 0;
                endtag_end = 0;
                
                }
            } else {
                value = realloc(value, 2);
                value[length] = src[x];
                value[(length + 1)] = '\0';
                
                ++length;
            }
        
            ++x;
        }
    	
    	strcpy(src, value);
    	free(value);
    	
    	return 0;
    }
    Last edited by mart_man00; 04-07-2003 at 06:05 PM.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    For one thing, you're using the realloc function incorrectly. The second parameter merely sets the new size of the memory block; realloc doesn't increase the size of the memory block by the value of the second parameter.

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Whoa....

    Why don't you just follow this little algorithm

    Go until '<' start copying over into a buffer until '>'

    //Make sure you copied the < and >s

    Then copy your change string into the buffer then go through the src and check for another '<' and then copy the tag's contents into the buffer until '>'
    again remember to copy the < and >s

    also a side note:
    a ' ' (space) is an HTML character
    its like &spc;
    or something

    anyway all this can be done using a few whiles and a flag to count the tags copied so far.

    Warning: Code hasn't been tested and should be changed and or studied

    Code:
    int changedata(char *src, char change[])
    {
    	char *buf;
    	int len = strlen(src) + strlen(change);
    	int x=0,j=0,l=0,k=0;
    	if(!src)
    		return -1; //let -1 be an error or something
    	buf = (char*) malloc(len);
    	
    	while(src[x] && l<3)
    	{
    		if(src[x]=='<')
    		{
    		    while(src[x]!='>' && src[x])
    		    {
    			strcat(buf,src[x]);
    			x++;
    		    }
    		    strcat(buf,'>'); //the ending tag
    		    l++; // a simple flag to let me know the first tag is done
    		}
    		if(l==1)
    		{
    		    strcat(buf,change);
    		    while(src[x]!='<') x++; //go to the next tag
    		}
    		if(src[x]!='<')
    		x++;
    	}
    	strcat(buf,'\0'); //end the NULL-terminated string
    	strcpy(src, buf);
    	free(buf);
    	return 0;
    }
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    so i should use something like this

    value = realloc(value, ((length + 2) * sizeof(int));

  5. #5
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Don't use realloc,
    in my opinion it is best to stay away from realloc as much as possible. And use malloc sparingly
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    a ' ' (space) is an HTML character
    so <a tag>some crap</a tag> is valid?

    any after think about it, i guess i dont need to check for 2 pairs of <>s. <P> came to mind, it does have data, normal text.

    ok, so i did make this to overly hard, expect version 2 soon

  7. #7
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Yup you even got the marker right too.


    <a href="blahblahURL">Data</a>

    remember?

    or even
    Code:
    <table width="100%">
      <tr>
      <td width="50%">
    
    etc
    .
    .
    .
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    well, does any one have advice on determining rather a string is txt or html. any html guys out there?

    i know theres html "escape" characters, but are they standard or m$'s (like &lt)? any good reference?

    and as every one probally figured out, its for some html libraries. i know before some people said is reinverting the wheel, but i dont care.

  9. #9
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Well TXT is a form of html well basically the way I would do it is just search for a Content-type
    header found in almost all HTML files

    It lets the browser know what kind of file it is opening, therefore if your programming somethign to parse HTML or something for a browser all you should do is Find the Content-type:

    and it should be the first thing there.

    if your talking about text inside and outside tags, write flags to let it know that it is inside or outside a tag and to reach tags use while loops and go through the string to find < and >s


    -Luke

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    well the content type thing is a good idea, but i cant always use it. i ment telling real tags from normal text.

    like <this is not a tag> isnt a real tag but <P> is, i was trying to tell the difference between them. i dont think it can really be done unless i made functions for every tag (how insane is this, im not a html guy?).

  11. #11
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    http://www.willcam.com/cmat/html/crossname.html

    does that site really have every html tag? i thought it would be horible, maybe it would just be bad

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 05:20 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  5. Displaying a 'long long' with printf
    By trinitrotoluene in forum C Programming
    Replies: 10
    Last Post: 12-28-2004, 12:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21