[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.6

Help needed debuging small C++ class that segfaults

Asked by shofstetter in C++ Programming Language

Tags: class, p, str_replace_once

I am working on a class that I will be using in a much larger program on linux.  I made the class in DevC++ using the MINGW port of the gcc compiler.  The class and test program compile and run fine under windows with mingw, but when I comple it with G++ under linux amd run it I get a segfault.  I have never debuged a program under linux so I don't really know where the problem is, but I think it has something to do with malloc(),strcpy(),or strcat() working differently in linux. So can anyone debug and fix what is neccassary in the program below and maybe tell me what caused it.  The purpos of the class is to parse/extract/manipulate data from html files so the I can insert the data into a database.

#include <iostream.h>
#include <stdlib.h>
#include <alloc.h>
#include <string.h>
class page_type{
    char* html;
    char* title;
    char* body;
    char* plain;
    void set_title(void);
    void set_body(void);
    char* strip_tags(char*);
    void html_tolower(void);
public:
    char* str_replace_once(char*,char*,char*);
    char* str_to_lower(char*);
    char* trunc(char*,int);
    char* remove_symbols(char*);
    char* str_replace(char* ,char* , char*);
    char* get_title(void);
    char* get_body(void);
    void init(char*);
};

char* page_type::str_to_lower(char* string)
{
      char* temp;
      temp = (char*)malloc(strlen(string)+1);
      memcpy(temp,string,strlen(string));
      temp = str_replace("A","a",temp);
      temp = str_replace("B","b",temp);
      temp = str_replace("C","c",temp);
      temp = str_replace("D","d",temp);
      temp = str_replace("E","e",temp);
      temp = str_replace("F","f",temp);
      temp = str_replace("G","g",temp);
      temp = str_replace("H","h",temp);
      temp = str_replace("I","i",temp);
      temp = str_replace("J","j",temp);
      temp = str_replace("K","k",temp);
      temp = str_replace("L","l",temp);
      temp = str_replace("M","m",temp);
      temp = str_replace("N","n",temp);
      temp = str_replace("O","o",temp);
      temp = str_replace("P","p",temp);
      temp = str_replace("Q","q",temp);
      temp = str_replace("R","r",temp);
      temp = str_replace("S","s",temp);
      temp = str_replace("T","t",temp);
      temp = str_replace("U","u",temp);
      temp = str_replace("V","v",temp);
      temp = str_replace("W","w",temp);
      temp = str_replace("X","x",temp);
      temp = str_replace("Y","y",temp);
      temp = str_replace("Z","z",temp);
return(temp);
}



char* page_type::trunc(char* data,int len)
{
      char* temp;
      int size;
      size = strlen(data);
      if(size > len)
      {
            temp = (char*)malloc(len+10);
            memcpy(temp,data,len);
            return(temp);
      }
      else
      {
            return(data);
      }
}


char* page_type::remove_symbols(char* original)
{
      char* tempstr;
      tempstr = (char*)malloc(strlen(original)+1);
      strcpy(tempstr,original);
      tempstr = str_replace("!"," ",tempstr);
      tempstr = str_replace("@"," ",tempstr);
      tempstr = str_replace("#"," ",tempstr);
      tempstr = str_replace("$"," ",tempstr);
      tempstr = str_replace("%"," ",tempstr);
      tempstr = str_replace("^"," ",tempstr);
      tempstr = str_replace("&"," ",tempstr);
      tempstr = str_replace("*"," ",tempstr);
      tempstr = str_replace("("," ",tempstr);
      tempstr = str_replace(")"," ",tempstr);
      tempstr = str_replace("-"," ",tempstr);
      tempstr = str_replace("_"," ",tempstr);
      tempstr = str_replace("="," ",tempstr);
      tempstr = str_replace("+"," ",tempstr);
      tempstr = str_replace("\\"," ",tempstr);
      tempstr = str_replace("|"," ",tempstr);
      tempstr = str_replace("`"," ",tempstr);
      tempstr = str_replace("~"," ",tempstr);
      tempstr = str_replace("["," ",tempstr);
      tempstr = str_replace("]"," ",tempstr);
      tempstr = str_replace("{"," ",tempstr);
      tempstr = str_replace("}"," ",tempstr);
      tempstr = str_replace(";"," ",tempstr);
      tempstr = str_replace(":"," ",tempstr);
      tempstr = str_replace("'"," ",tempstr);
      tempstr = str_replace(","," ",tempstr);
      tempstr = str_replace("."," ",tempstr);
      tempstr = str_replace("<"," ",tempstr);
      tempstr = str_replace(">"," ",tempstr);
      tempstr = str_replace("/"," ",tempstr);
      tempstr = str_replace("?"," ",tempstr);
      return(tempstr);
}

char* page_type::str_replace(char* find, char* replace, char* string)
{
    char* new_string;
    char* test_var;
    int state = 1;
    new_string = (char*) malloc(strlen(string)+1);
    strcpy(new_string,string);
    while ( state == 1)
    {
        if(str_replace_once(find,replace,new_string) != NULL)
        {
                new_string = str_replace_once(find,replace,new_string);
        }
        else
        {
                state = 0;
        }
    }
    return(new_string);
}




char* page_type::str_replace_once(char* find, char* replace, char* string)
{
    int find_len;
    int replace_len;
    int string_len;
    int new_string_len;
    char* new_string;
    char* found_start;
    char* found_end;
    char* tempstring;
    tempstring = (char*)malloc(strlen(string)+1);
    strcpy(tempstring,string);
    find_len = strlen(find);
    replace_len = strlen(replace);
    string_len = strlen(tempstring);
    new_string_len = ((string_len - find_len) + replace_len);
    new_string = (char* ) malloc(new_string_len + 1);

   if(strstr(tempstring,find)!= NULL)
    {
        found_start = strstr(tempstring,find);
        found_end = strstr(tempstring,find) + find_len;
        memcpy(new_string,tempstring,string_len - strlen(found_start));
        strcat(new_string,replace);
        strcat(new_string,found_end);
        return(new_string);
    }
    else
    {
        return(NULL);
    }
}




void page_type::html_tolower(void)
{
html = str_to_lower(html);
}


char* page_type::get_title(void)
{
    if(title != NULL)
    {
        return(title);
    }
    else
    {
        return(NULL);
    }
}

char* page_type::get_body(void)
{
    if(body != NULL)
    {
        return(body);
    }
    else
    {
        return(NULL);
    }
}



void page_type::set_title(void)
{
    char* t_start;
    char* t_end;
    char* t_temp1;
    char* t_temp2;
    int size;
    int t_size;
     size = strlen(html);
    t_temp1 = (char *)malloc(size + 2);
    if(t_temp1 != NULL)
    {
        strcpy(t_temp1,html);
        t_start = strstr(t_temp1,"<title>")+7;
        t_end = strstr(t_temp1,"</title>");
        if((t_start != NULL) && (t_end != NULL))
        {
                t_size = t_end - t_start;
                if(t_size > 0)
                {
                      if(t_temp2 = (char*)malloc(t_size + 1))
                      {
                            memcpy(t_temp2,t_start,t_size);
                            t_temp2 = remove_symbols(t_temp2);
                      if(title = (char*)malloc(strlen(t_temp2)+1))
                            {
                                 strcpy(title,t_temp2);
                            }
                            else
                            {
                                 cout << "Not enough mem for title" << endl;
                            }
                      }
                      else
                      {
                           cout << "not enough mem for t_temp2" << endl;
                      }
                }
                else
                {
                cout << "No title data" << endl;
                }
        }
        else
        {
                cout << "No Title Found!" << endl;
        }
    }
    else
    {
        cout << "Not enought Mem for t-temp1 in function set_title()" << endl;
    }    
}

void page_type::set_body(void)
{
    char* b_start;
    char* b_end;
    char* b_temp1;
    char* b_temp2;
    char* b_temp3;
    char* templen;
    int size;
    int b_size;
    size = strlen(html);
    b_temp1 = (char *)malloc(size+1);
   if(b_temp1 != NULL)
   {
       strcpy(b_temp1,html);
       templen = strstr(b_temp1,"<bod");
       b_start = strstr(templen,">")+1;
       b_end = strstr(b_temp1,"</bod");
       if((b_start != NULL) && (b_end != NULL))
       {
              b_size = b_end - b_start;
              b_temp2 = (char*)malloc(b_size + 1);
              if(b_temp2 != NULL)
              {
                    memcpy(b_temp2,b_start,b_size);
                    b_temp3 = strip_tags(b_temp2);
                    b_temp3 = remove_symbols(b_temp3);
                body = (char*) malloc(strlen(b_temp3));
                    if(body != NULL)
                    {
                  
                   strcpy(body,b_temp3);
                  }
                    else
                    {
                         cout << "Not enough mem for body" << endl;
                    }
              }
              else
              {
                  cout << "not enough room for b_temp2" << endl;
              }
       }
       else
       {
            cout << "No body data" << endl;
       }
   }
   else
   {
        cout << "not enough mem for b_temp1" << endl;
   }
}

void page_type::init(char* html_data)
{
    html = (char *)malloc(strlen(html_data) + 2);
    if(html != NULL)
    {
    memcpy(html,html_data,(strlen(html_data)+1));
    html_tolower();
    set_title();
    set_body();
    }
}

char* page_type::strip_tags(char *data)
{
    char* temp1;
    char* temp2;
    char* temp3;
    char* start;
    char* end;
    int dist;
    int size;
    temp1 = (char*)malloc(strlen(data)+1);
    strcpy(temp1,data);
    while((start != NULL) && (end != NULL))
    {
        size = strlen(temp1);
        start = strstr(temp1,"<");
        end = strstr(temp1,">")+1;
        size = strlen(temp1);
        if((start != NULL) && (end != NULL))
        {
                dist = size - strlen(start);
                temp2 = (char *) malloc(dist + 2);
                memcpy(temp2,temp1,dist);
                temp3 = (char *) malloc(strlen(end)+1);
                memcpy(temp3,end,strlen(end));
                free(temp1);
                temp1 = (char *)malloc((strlen(temp2)+(strlen(temp3)+1)));
                memcpy(temp1,temp2,strlen(temp2));
                strcat(temp1,temp3);
                }
        else
        {
        break;
        }
    }
return(temp1);
}


int main(int argc, char *argv[])
{
 page_type *test;
 page_type testx;
 test = &testx;
 //char* teststr = "Replace Bable fish";
 //char* strtest;
 test->init("<html><head><TITLE>Hello World In order to test my trunc() function I need to make the title longer then 50 charactures I think I have it there now</TITLE></head><BODY bgcolor=\"red\"><b>This isn't a page;</b> about the \"hello world\" program!  Instead it is a page that I am using o test the functionality of my new class wich I am going to use for the new Delta controls search endgine spider.</BODY></html>");
 cout << test->trunc(test->get_title(),50) << endl << endl;
 cout << test->trunc(test->get_body(),200) << endl << endl;
 //cout << teststr << endl;
 //strtest = test->str_replace_once("Bable", "Gold", teststr);
 //cout << strtest << endl;
 system("PAUSE");      
  return 0;
}

[+][-]10/15/03 06:22 PM, ID: 9558744Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: C++ Programming Language
Tags: class, p, str_replace_once
Sign Up Now!
Solution Provided By: grg99
Participating Experts: 4
Solution Grade: A
 
[+][-]10/15/03 12:57 PM, ID: 9557444Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]10/15/03 01:12 PM, ID: 9557538Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]10/16/03 05:17 AM, ID: 9561279Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/16/03 06:06 AM, ID: 9561623Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]10/16/03 07:32 AM, ID: 9562378Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/16/03 01:40 PM, ID: 9565347Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/16/03 01:42 PM, ID: 9565356Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92