943,806 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2677
  • C++ RSS
Jun 8th, 2009
0

Get size of a remote file

Expand Post »
Hello, I need to get remote file size through the url to a function. The problem is that I'm not getting! Trying with the functions of libcURL, but I am not getting. Is there any way to get the size of a remote file by url?
Last edited by osmano807; Jun 8th, 2009 at 2:01 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
osmano807 is offline Offline
9 posts
since Jun 2009
Jun 8th, 2009
0

Re: Get size of a remote file

Too bad you're not using PHP. http://ca2.php.net/filesize

But it seems like it might not be possible: http://bytes.com/groups/php/595984-f...ing-http-pages

On the other hand, wget determines file sizes. Apparently it requires HTTP 1.1 (not 1.0) to do this.

I really don't know. You'd have to do some research.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Jun 9th, 2009
0

Re: Get size of a remote file

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <curl/curl.h>
  4. #include <boost/regex.hpp>
  5. using namespace boost;
  6. using namespace std;
  7.  
  8. //
  9. // For this demonstration, the contents of the web page are put
  10. // into this global variable.
  11. //
  12. string contents;
  13. //
  14. // This is the callback function that is called by
  15. // curl_easy_perform(curl)
  16. //
  17. size_t handle_data(void *ptr, size_t size, size_t nmemb, void *stream)
  18. {
  19. const boost::regex content_length_regex("Content-Length: [0-9]{1,}"); // Regex do video do youtube...
  20. const boost::regex content_length_remove_regex("Content-Length: ");
  21. int numbytes = size*nmemb;
  22. // The data is not null-terminated, so get the last character, and replace
  23. // it with '\0'.
  24. char lastchar = *((char *) ptr + numbytes - 1);
  25. //string last_char = char_to_string(lastchar);
  26. // {
  27. string nothing = "";
  28. // contents = regex_replace(last_char, content_length_remove_regex, nothing);
  29. *((char *) ptr + numbytes - 1) = '\0';
  30. string last_char = ((char *)ptr);
  31. //cout << last_char << endl;
  32. //cout << endl;
  33. //cout << regex_search(last_char,content_length_regex) << endl;
  34. //cout << endl;
  35. //cout << "antes" << endl;
  36. //cout << ((char*)ptr) << endl;
  37. if (regex_search(last_char,content_length_regex) == 1) // Se for 1, foi retornado sim para o match
  38. {
  39. //cout << "depois" << endl;
  40. //cout << ((char*)ptr) << endl;
  41. //cout << regex_replace(last_char, content_length_remove_regex, nothing) << endl;
  42. contents = regex_replace(last_char, content_length_remove_regex, nothing);
  43. //cout << contents << endl;
  44. }
  45. else
  46. //{
  47. //contents = "-1";
  48. //}
  49. //contents.append((char *)ptr);
  50. //contents.append(1,lastchar);
  51. //*((char *) ptr + numbytes - 1) = lastchar; // Might not be necessary.
  52. // }
  53.  
  54.  
  55. return size*nmemb;
  56. }
  57.  
  58. long remote_filesize(string url)
  59. {
  60. //string url = "http://www.youtube.com/get_video?video_id=3455GI_uGs4&t=vjVQa1PpcFNWk-V5Ndo6HE9KWZGtRxrct9PNWGe5Nhs=&el=detailpage&ps=&fmt=34";
  61. //string url = "http://www.google.com";
  62. CURL *curl;
  63. CURLcode res;
  64. double length = 0.0;
  65. //double length2 = 0.0;
  66. curl = curl_easy_init();
  67. if(curl) {
  68. //static const char *headerfilename = "head.out";
  69. //FILE *headerfile;
  70. //headerfile = fopen(headerfilename,"w");
  71. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  72. //curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
  73. //curl_easy_setopt(curl, CURLOPT_RANGE, "0-4"); // só por garantia, não faz o donwload inteiro não...
  74. curl_easy_setopt(curl, CURLOPT_NOBODY, 1); // acho que é aqui que bloqueia o donwload...
  75. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  76. curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, handle_data);
  77. curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 500);
  78. res = curl_easy_perform(curl);
  79.  
  80. //curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &length);
  81. //cout << length << endl;
  82. //const boost::regex youtube_video_id("^video_id="); // Regex do video do youtube...
  83.  
  84. //cout << contents << endl;
  85. //unlink(headerfilename);
  86. }
  87. curl_easy_cleanup(curl);
  88. //return length;
  89. if (contents == "")
  90. {
  91. return(-1);
  92. }
  93. else
  94. {
  95. return atol(contents.c_str());
  96. }
  97. }
  98.  
  99. int main()
  100. {
  101. // Content-Length: 222
  102. string url = "http://www.google.com.br";
  103. remote_filesize(url);
  104. cout << "contents " << contents << endl;
  105. return 0;
  106. }
g++ example.cpp -lcurl -lboost_regex
Sorry for the comments in Portuguese and snippets of code. I had to use the libboost_regex because I still do not know another way to do a search for regular expression.
Last edited by osmano807; Jun 9th, 2009 at 2:32 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
osmano807 is offline Offline
9 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: BSOD C::B + sdl + vista
Next Thread in C++ Forum Timeline: Need help with this macro...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC