examples/curlParse/main.cc


Home Back

#include <wx/wxprec.h>
#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif
#include "wx/xrc/xmlres.h"
#include <wx/utils.h> 

#include "gui.cpp" 

#include <wx/process.h>


#include <vector>


 
class threadProgress : public wxThread
{
   public:
    threadProgress();
    wxString dest;
    long totalSize=0;
    long currentSize=0;
    wxGauge *bar;
    wxStaticText *label;
   wxFrame *frame;
    int stop=0;
 
 
   private:
    virtual void *Entry();
};
 
threadProgress::threadProgress()
{
 
}
 
void *threadProgress::Entry()
{
    
   wxFileName fileName(dest);
   while (!fileName.FileExists())sleep(1);
   
   wxFile ff; 
    ff.Open(dest); 
    wxString m;
    long percento=0;
   
   
    while(totalSize>=currentSize)
    {
        if (stop==1) continue;
        sleep(1);
        currentSize=ff.Length();
      wxString spercento=wxString::Format("%ld", percento);
      
        if (totalSize>0 && currentSize>0) percento= currentSize*100/totalSize;
        m=spercento+"% Current: "+wxString::Format(wxT("%ld"),currentSize)+"/"+wxString::Format(wxT("%ld"),totalSize)+" bytes";
        wxTheApp->CallAfter( [=] 
        {
          this->bar->SetValue(percento);
          this->label->SetLabel(m);
        this->frame->SetTitle(spercento+"% Downloded");
        });
    
      if (percento>=100)
      {
          stop=1;
          this->frame->Close();
      }
   }
    
    
 
 
 
 
 return 0;
}
 
class dwnFrame{
    public:
        threadProgress *progress;
        wxFrame frame0;
        wxStaticText *label0;
        wxGauge *bar;
        long totalSize=0;
        wxProcess *p;
        wxString url,dest;
      bool saveAs;
        void ShowFrame();
        void quit(wxCloseEvent& event);
} ;


void dwnFrame::quit(wxCloseEvent& event)
{
    
        printf("Quit.");
        long pid=p->GetPid();
        p->Kill(pid, wxSIGKILL,wxKILL_NOCHILDREN);
        progress->stop=1;
        //this->frame0.Close();
        exit(0);
}

 
void dwnFrame::ShowFrame()
{
    wxXmlResource::Get()->LoadFrame(&frame0, NULL,  wxT("frame0"));
   label0 = XRCCTRL(frame0, "label0", wxStaticText);
    bar = XRCCTRL(frame0, "bar", wxGauge);
    frame0.Bind(wxEVT_CLOSE_WINDOW, &dwnFrame::quit, this);
    
    wxArrayString output;
    wxArrayString errors;
    
   try{
      const wxString cmdTotalSize="curl -sI "+url;
      wxExecute(cmdTotalSize,  output, errors,wxEXEC_HIDE_CONSOLE );
      
      for (size_t i=0;i<=output.GetCount()-1;i++)
      {
         if (output[i].BeforeFirst(':').IsSameAs("content-length"))
         {
            totalSize=wxAtoi(output[i].AfterLast(':'));
            label0->SetLabel(output[i].AfterLast(':'));
         }
      }
      
      wxSize s(600,80);
      frame0.SetTitle("Downloading...");
      frame0.SetMinSize(s);
      frame0.Show();
      
      
      p = new wxProcess(wxPROCESS_DEFAULT);
      
      if (saveAs)
      {
         const wxString cmdDownload="curl "+url+" -o "+dest;
         wxExecute(cmdDownload,wxEXEC_ASYNC ,p );
      }else{
         const wxString cmdDownload="curl -O "+url;
         wxExecute(cmdDownload,wxEXEC_ASYNC ,p );
      }
      
      
      
      
      threadProgress *thread = new threadProgress();
      progress=thread;
       thread->bar=this->bar;
       thread->label=this->label0;
       thread->frame=&this->frame0;
       thread->totalSize=totalSize;
       thread->dest=dest;
       thread->Create();
      thread->Run();
   }catch (...) {
        // Catch all other exceptions
        std::cerr << "Problem..." << std::endl;
         
    }
}
 

std::vector<std::string> split(std::string s, char d, int max=0)
{
     std::string e;
     std::vector<std::string> list;
     int token=0;
 
     for (std::string::size_type i = 0; i<s.size(); ++i)
     {
            if (s[i]==d)
            {
              list.push_back(e);
              e.clear();
              token++ ;
 
            }else{
                e.push_back(s[i]);
            }
            if (max!=0 && token==max) break;
     }
     if(max==0) list.push_back(e); 
     return list;
}
 



class MyApp: public wxApp
{
    virtual bool OnInit();
}; 
bool MyApp::OnInit()
{
    wxXmlResource::Get()->InitAllHandlers();  
    InitXmlResource(); 
 
    dwnFrame *MainWin = new dwnFrame();
    
    
   MainWin->saveAs=false;
   
   if (argc==1)
   {
      std::cout<<"Usage: gcurl <url> [path downloaded file]"<<std::endl;
      exit(-1);
   }else{
      MainWin->url=argv[1];
   }
   
   if (argc>2)
   {
      MainWin->saveAs=true;
      MainWin->dest=argv[2];
   }else{
      MainWin->saveAs=false;
      std::string arg_1=argv[1].ToStdString();
      std::vector<std::string> v =split(arg_1,'/');
      MainWin->dest=v[v.size()-1];
   }
   
   
   
   
 
   MainWin->ShowFrame();

   return true;
    
    
   
    
    
}
 
 
IMPLEMENT_APP(MyApp)

Powered by Code, a simple repository browser by Fabio Di Matteo