Extract common code out into kdm_for_screen()
[dcpomatic.git] / src / lib / internet.cc
index e0af49b6698e64c75148ec5ecfddee14e1c75969..b993117bb5e93cc3b58e77264c7dcd3ce0b14f79 100644 (file)
@@ -39,6 +39,46 @@ using boost::optional;
 using boost::function;
 using boost::algorithm::trim;
 
+static size_t
+ls_url_data (void* buffer, size_t size, size_t nmemb, void* output)
+{
+       string* s = reinterpret_cast<string*>(output);
+       char* c = reinterpret_cast<char*>(buffer);
+       for (size_t i = 0; i < (size * nmemb); ++i) {
+               *s += c[i];
+       }
+       return nmemb;
+}
+
+list<string>
+ls_url (string url)
+{
+       CURL* curl = curl_easy_init ();
+       curl_easy_setopt (curl, CURLOPT_URL, url.c_str());
+       curl_easy_setopt (curl, CURLOPT_DIRLISTONLY, 1);
+
+       string ls;
+       curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ls_url_data);
+       curl_easy_setopt (curl, CURLOPT_WRITEDATA, &ls);
+       CURLcode const cr = curl_easy_perform (curl);
+
+       if (cr != CURLE_OK) {
+               return list<string>();
+       }
+
+       list<string> result;
+       result.push_back("");
+       for (size_t i = 0; i < ls.size(); ++i) {
+               if (ls[i] == '\n') {
+                       result.push_back("");
+               } else {
+                       result.back() += ls[i];
+               }
+       }
+
+       result.pop_back ();
+       return result;
+}
 
 static size_t
 get_from_url_data (void* buffer, size_t size, size_t nmemb, void* stream)