File: SendHTTP.cpp

package info (click to toggle)
gentle 1.9%2Bcvs20100605%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 12,224 kB
  • sloc: cpp: 41,569; ansic: 3,978; sh: 1,420; makefile: 240
file content (144 lines) | stat: -rw-r--r-- 3,571 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/** \file
	\brief Contains the myExternal class
*/

#include "main.h"
#include "SendHTTP.h"
#include <wx/filesys.h>
#include <wx/protocol/http.h>
#include <wx/protocol/ftp.h>

myExternal::myExternal () { pd = NULL ; } ;

int myExternal::copyFileHTTP ( wxString _url , wxString _file )
{
    wxFileSystem fs ;
    wxFSFile *file = fs.OpenFile ( _url ) ;
    if ( !file ) return 1 ; // Error
    wxInputStream *in_stream = file->GetStream () ;
    if ( !in_stream ) // Error
    {
	delete file ;
	return 1 ;
    }

    wxFile out ( _file , wxFile::write ) ;
    if ( !out.IsOpened() ) // Error
    {
	delete file ;
	return 1 ;
    }
    while ( !in_stream->Eof() )
	{
	    char c = in_stream->GetC() ;
	    out.Write ( &c , 1 ) ;
	}
    out.Close () ;

    delete file ;    
    return 0 ; 
}

wxString myExternal::getTextHTTP ( wxString url ) 
{ 
    wxString ret ;
    wxFileSystem fs ;
    wxFSFile *file = fs.OpenFile ( url ) ;
    if ( !file ) return _T("") ; // Error
    wxInputStream *in_stream = file->GetStream () ;

    if ( !in_stream ) // Error
    	{
		delete file ;
		return _T("") ;
    	}

	 char n[1005] ;
	 while ( !in_stream->Eof() )
	 	{
		in_stream->Read ( n , 1000 ) ;
		n[in_stream->LastRead()] = 0 ;
		ret += wxString ( n , wxConvUTF8 ) ;
		}

    delete file ;
    return ret ; 
}

wxString myExternal::getTextFTP ( wxString server , wxString dir , wxString file )
	{
	wxString ret ;
	wxFTP ftp;

    if ( !ftp.Connect(server) ) return ret ;
    if ( !ftp.ChDir(dir) ) return ret ;

	int size = ftp.GetFileSize ( file ) ;
    wxInputStream *in = ftp.GetInputStream(file);
    if ( in )
	    {
		char data[10001] ;
		int read_total = 0 ;
		wxProgressDialog pd ( txt("t_downloading_rebase") , _T("") , size , NULL , 
				wxPD_AUTO_HIDE|wxPD_APP_MODAL|wxPD_SMOOTH|wxPD_ELAPSED_TIME|wxPD_ESTIMATED_TIME|wxPD_REMAINING_TIME|wxPD_CAN_ABORT ) ;
		while ( read_total < size )
			{
			in->Read ( data , 10000 ) ;
			int read = in->LastRead() ;
			read_total += read ;
			data[read] = 0 ;
			ret += wxString ( data , wxConvUTF8 ) ;
			wxString msg = wxString::Format ( txt("t_downloading_rebase2") , read_total * 100 / size ) ;
			if ( !pd.Update ( read_total , msg ) )
				{
				delete in ;
				ret.Clear() ;
				return ret ;
				}
			}
        delete in;
	    }
    return ret ;
	}

wxString myExternal::getText ( wxString url )
	{
	if ( url.Left(7).Lower() == _T("http://") ) return getTextHTTP ( url ) ;
	if ( url.Left(6).Lower() == _T("ftp://") )
		{
		url = url.Mid ( 6 ) ;
		wxString server = url.BeforeFirst ( '/' ) ;
		wxString file = url.AfterLast ( '/' ) ;
		wxString dir = _T("/") + url.AfterFirst('/').BeforeLast('/') ;
		return getTextFTP ( server , dir , file ) ;
		}
	return getTextLocal ( url ) ; // fallback
	}
     
int myExternal::copyFile ( wxString url , wxString file , int _t )
	{
	targetSize = _t ;
	if ( url.Left(7).Lower() == _T("http://") ) return copyFileHTTP ( url , file ) ;
	return copyFileLocal ( url , file ) ; // fallback
	}    

// ****

wxString myExternal::getTextLocal ( wxString url )
	{
	wxFile in ( url , wxFile::read ) ;
	if ( !in.IsOpened() ) return _T("") ;
	long l = in.Length() ;
	char *c = new char [l+5] ;
	in.Read ( c , l ) ;
	in.Close() ;
	wxString ret = wxString ( c , wxConvUTF8 ) ;
	delete c ;
	return ret ;
	}
     
int myExternal::copyFileLocal ( wxString url , wxString file )
	{
	if ( true == wxCopyFile ( url , file , true ) ) return 0 ;
	return 1 ;
	}