diff options
author | louis <louis.braun@gmx.de> | 2015-01-15 13:12:47 +0100 |
---|---|---|
committer | louis <louis.braun@gmx.de> | 2015-01-15 13:12:47 +0100 |
commit | c7f71f3f2b580714bd9e5a5c773bc2179c0e8567 (patch) | |
tree | a39d3efd5494b60964152e559eaead76c7897af5 /tools | |
download | vdr-plugin-weatherforecast-0.0.1.tar.gz vdr-plugin-weatherforecast-0.0.1.tar.bz2 |
Initial commit0.0.1
Diffstat (limited to 'tools')
-rw-r--r-- | tools/curlfuncs.c | 236 | ||||
-rw-r--r-- | tools/curlfuncs.h | 45 | ||||
-rw-r--r-- | tools/filesystem.c | 53 | ||||
-rw-r--r-- | tools/filesystem.h | 14 | ||||
-rw-r--r-- | tools/jsonhelpers.c | 40 | ||||
-rw-r--r-- | tools/jsonhelpers.h | 17 |
6 files changed, 405 insertions, 0 deletions
diff --git a/tools/curlfuncs.c b/tools/curlfuncs.c new file mode 100644 index 0000000..ed1ea01 --- /dev/null +++ b/tools/curlfuncs.c @@ -0,0 +1,236 @@ +/* +Copyright (c) 2002, Mayukh Bose +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +* Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. + +* Neither the name of Mayukh Bose nor the names of other +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/* + Change History: + 11/23/2004 - Removed the #include <unistd.h> line because I didn't + need it. Wonder why I had it there in the first place :). + 10/20/2004 - Publicly released this code. +*/ +#include <string> +#include <cstdio> +#include <curl/curl.h> +#include <curl/easy.h> +#include <vdr/tools.h> +#include "curlfuncs.h" + +#ifndef TRUE +#define TRUE 1 +#endif + +using namespace std; + +// Local function prototypes +int CurlDoPost(const char *url, string *sOutput, const string &sReferer, + struct curl_httppost *formpost, struct curl_slist *headerlist); + +namespace curlfuncs { + string sBuf; + bool bInitialized = false; + CURL *curl = NULL; +} + +size_t collect_data(void *ptr, size_t size, size_t nmemb, void *stream) +{ + string sTmp; + register size_t actualsize = size * nmemb; + if ((FILE *)stream == NULL) { + sTmp.assign((char *)ptr, actualsize); + curlfuncs::sBuf += sTmp; + } + else { + fwrite(ptr, size, nmemb, (FILE *)stream); + } + return actualsize; +} + +inline void InitCurlLibraryIfNeeded() +{ + if (!curlfuncs::bInitialized) { + curl_global_init(CURL_GLOBAL_ALL); + curlfuncs::curl = curl_easy_init(); + if (!curlfuncs::curl) + throw string("Could not create new curl instance"); + curl_easy_setopt(curlfuncs::curl, CURLOPT_NOPROGRESS, 1); // Do not show progress + curl_easy_setopt(curlfuncs::curl, CURLOPT_WRITEFUNCTION, collect_data); + curl_easy_setopt(curlfuncs::curl, CURLOPT_WRITEDATA, 0); // Set option to write to string + curl_easy_setopt(curlfuncs::curl, CURLOPT_FOLLOWLOCATION, TRUE); + curl_easy_setopt(curlfuncs::curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Mayukh's libcurl wrapper http://www.mayukhbose.com/)"); + curlfuncs::bInitialized = true; + } +} + +int CurlGetUrl(const char *url, string *sOutput, const string &sReferer) +{ + InitCurlLibraryIfNeeded(); + + curl_easy_setopt(curlfuncs::curl, CURLOPT_URL, url); // Set the URL to get + if (sReferer != "") + curl_easy_setopt(curlfuncs::curl, CURLOPT_REFERER, sReferer.c_str()); + curl_easy_setopt(curlfuncs::curl, CURLOPT_HTTPGET, TRUE); + curl_easy_setopt(curlfuncs::curl, CURLOPT_WRITEDATA, 0); // Set option to write to string + curlfuncs::sBuf = ""; + if (curl_easy_perform(curlfuncs::curl) == 0) + *sOutput = curlfuncs::sBuf; + else { + // We have an error here mate! + *sOutput = ""; + return 0; + } + + return 1; +} + +int CurlGetUrlFile(const char *url, const char *filename, const string &sReferer) +{ + int nRet = 0; + InitCurlLibraryIfNeeded(); + + // Point the output to a file + FILE *fp; + if ((fp = fopen(filename, "w")) == NULL) + return 0; + + curl_easy_setopt(curlfuncs::curl, CURLOPT_WRITEDATA, fp); // Set option to write to file + curl_easy_setopt(curlfuncs::curl, CURLOPT_URL, url); // Set the URL to get + if (sReferer != "") + curl_easy_setopt(curlfuncs::curl, CURLOPT_REFERER, sReferer.c_str()); + curl_easy_setopt(curlfuncs::curl, CURLOPT_HTTPGET, TRUE); + if (curl_easy_perform(curlfuncs::curl) == 0) + nRet = 1; + else + nRet = 0; + + curl_easy_setopt(curlfuncs::curl, CURLOPT_WRITEDATA, NULL); // Set option back to default (string) + fclose(fp); + return nRet; +} + +int CurlPostUrl(const char *url, const string &sPost, string *sOutput, const string &sReferer) +{ + InitCurlLibraryIfNeeded(); + + int retval = 1; + string::size_type nStart = 0, nEnd, nPos; + string sTmp, sName, sValue; + struct curl_httppost *formpost=NULL; + struct curl_httppost *lastptr=NULL; + struct curl_slist *headerlist=NULL; + + // Add the POST variables here + while ((nEnd = sPost.find("##", nStart)) != string::npos) { + sTmp = sPost.substr(nStart, nEnd - nStart); + if ((nPos = sTmp.find("=")) == string::npos) + return 0; + sName = sTmp.substr(0, nPos); + sValue = sTmp.substr(nPos+1); + curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, sName.c_str(), CURLFORM_COPYCONTENTS, sValue.c_str(), CURLFORM_END); + nStart = nEnd + 2; + } + sTmp = sPost.substr(nStart); + if ((nPos = sTmp.find("=")) == string::npos) + return 0; + sName = sTmp.substr(0, nPos); + sValue = sTmp.substr(nPos+1); + curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, sName.c_str(), CURLFORM_COPYCONTENTS, sValue.c_str(), CURLFORM_END); + + retval = CurlDoPost(url, sOutput, sReferer, formpost, headerlist); + + curl_formfree(formpost); + curl_slist_free_all(headerlist); + return retval; +} + +int CurlPostRaw(const char *url, const string &sPost, string *sOutput, const string &sReferer) +{ + InitCurlLibraryIfNeeded(); + + int retval; + struct curl_httppost *formpost=NULL; + struct curl_slist *headerlist=NULL; + + curl_easy_setopt(curlfuncs::curl, CURLOPT_POSTFIELDS, sPost.c_str()); + curl_easy_setopt(curlfuncs::curl, CURLOPT_POSTFIELDSIZE, 0); //FIXME: Should this be the size instead, in case this is binary string? + + retval = CurlDoPost(url, sOutput, sReferer, formpost, headerlist); + + curl_formfree(formpost); + curl_slist_free_all(headerlist); + return retval; +} + +int CurlDoPost(const char *url, string *sOutput, const string &sReferer, + struct curl_httppost *formpost, struct curl_slist *headerlist) +{ + headerlist = curl_slist_append(headerlist, "Expect:"); + + // Now do the form post + curl_easy_setopt(curlfuncs::curl, CURLOPT_URL, url); + if (sReferer != "") + curl_easy_setopt(curlfuncs::curl, CURLOPT_REFERER, sReferer.c_str()); + curl_easy_setopt(curlfuncs::curl, CURLOPT_HTTPPOST, formpost); + + curl_easy_setopt(curlfuncs::curl, CURLOPT_WRITEDATA, 0); // Set option to write to string + curlfuncs::sBuf = ""; + if (curl_easy_perform(curlfuncs::curl) == 0) { + *sOutput = curlfuncs::sBuf; + return 1; + } + else { + // We have an error here mate! + *sOutput = ""; + return 0; + } +} + +void FreeCurlLibrary(void) +{ + if (curlfuncs::curl) + curl_easy_cleanup(curlfuncs::curl); + curl_global_cleanup(); + curlfuncs::bInitialized = false; +} + +int CurlSetCookieFile(char *filename) +{ + InitCurlLibraryIfNeeded(); + if (curl_easy_setopt(curlfuncs::curl, CURLOPT_COOKIEFILE, filename) != 0) + return 0; + if (curl_easy_setopt(curlfuncs::curl, CURLOPT_COOKIEJAR, filename) != 0) + return 0; + return 1; +} + +char *CurlEscape(const char *url) { + InitCurlLibraryIfNeeded(); + return curl_escape(url , strlen(url)); +} diff --git a/tools/curlfuncs.h b/tools/curlfuncs.h new file mode 100644 index 0000000..e0f76cf --- /dev/null +++ b/tools/curlfuncs.h @@ -0,0 +1,45 @@ +/* +Copyright (c) 2002, Mayukh Bose +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +* Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. + +* Neither the name of Mayukh Bose nor the names of other +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef __CURLFUNCS_H_20020513__ +#define __CURLFUNCS_H_20020513__ +#include <string> + using namespace std; + +int CurlGetUrl(const char *url, string *sOutput, const string &sReferer=""); +int CurlGetUrlFile(const char *url, const char *filename, const string &sReferer=""); +void FreeCurlLibrary(void); +int CurlSetCookieFile(char *filename); +int CurlPostUrl(const char *url, const string &sPost, string *sOutput, const string &sReferer = ""); +int CurlPostRaw(const char *url, const string &sPost, string *sOutput, const string &sReferer = ""); +char *CurlEscape(const char *url); +#endif diff --git a/tools/filesystem.c b/tools/filesystem.c new file mode 100644 index 0000000..0f04100 --- /dev/null +++ b/tools/filesystem.c @@ -0,0 +1,53 @@ +#include <string> +#include <sstream> +#include <iostream> +#include <fstream> +#include <sys/stat.h> +#include <time.h> +#include "filesystem.h" + +bool FileExists(const string &fullpath) { + struct stat buffer; + return (stat (fullpath.c_str(), &buffer) == 0); +} + +bool FileExists(const string &path, const string &name, const string &ext) { + stringstream fileName; + fileName << path << name << "." << ext; + struct stat buffer; + return (stat (fileName.str().c_str(), &buffer) == 0); +} + +string ReadFromFile(const string &path) { + string line; + stringstream content; + ifstream myfile (path.c_str()); + if (myfile.is_open()) { + while ( getline (myfile,line) ) { + content << line << '\n'; + } + myfile.close(); + } + return content.str(); +} + +bool WriteIntoFile(const string &path, const string &content) { + if (content.size() < 5) + return false; + ofstream myfile; + myfile.open(path.c_str()); + if (myfile.is_open()) { + myfile << content; + myfile.close(); + return true; + } + return false; +} + +time_t FileCreationTime(const string &path) { + struct stat t_stat; + stat(path.c_str(), &t_stat); + struct tm * timeinfo = localtime(&t_stat.st_ctime); + time_t creationTime = mktime(timeinfo); + return creationTime; +} diff --git a/tools/filesystem.h b/tools/filesystem.h new file mode 100644 index 0000000..7ea0954 --- /dev/null +++ b/tools/filesystem.h @@ -0,0 +1,14 @@ +#ifndef __FILESYSTEM_H +#define __FILESYSTEM_H + +#include <vdr/plugin.h> + +using namespace std; + +bool FileExists(const string &fullpath); +bool FileExists(const string &path, const string &name, const string &ext); +string ReadFromFile(const string &path); +bool WriteIntoFile(const string &path, const string &content); +time_t FileCreationTime(const string &path); + +#endif // __FILESYSTEM_H diff --git a/tools/jsonhelpers.c b/tools/jsonhelpers.c new file mode 100644 index 0000000..56cd854 --- /dev/null +++ b/tools/jsonhelpers.c @@ -0,0 +1,40 @@ +#include "jsonhelpers.h" + +int JsonGetInteger(json_t *jNode, string name) { + json_t *jInt = json_object_get(jNode, name.c_str()); + if (json_is_integer(jInt)) { + return json_integer_value(jInt); + } + return 0; +} + +float JsonGetFloat(json_t *jNode, string name) { + json_t *jFloat = json_object_get(jNode, name.c_str()); + if (json_is_real(jFloat)) { + return json_real_value(jFloat); + } + if (json_is_integer(jFloat)) { + return json_integer_value(jFloat); + } + return 0.0; +} + +string JsonGetString(json_t *jNode, string name) { + json_t *jString = json_object_get(jNode, name.c_str()); + if (json_is_string(jString)) { + return json_string_value(jString); + } + return ""; +} + +string FloatToString(float f) { + stringstream sTemp; + sTemp << f; + return sTemp.str(); +} + +string IntToString(int i) { + stringstream sTemp; + sTemp << i; + return sTemp.str(); +} diff --git a/tools/jsonhelpers.h b/tools/jsonhelpers.h new file mode 100644 index 0000000..852d94d --- /dev/null +++ b/tools/jsonhelpers.h @@ -0,0 +1,17 @@ +#ifndef __JSONHELPERS_H +#define __JSONHELPERS_H + +#include <string> +#include <sstream> +#include <jansson.h> + +using namespace std; + +int JsonGetInteger(json_t *jNode, string name); +float JsonGetFloat(json_t *jNode, string name); +string JsonGetString(json_t *jNode, string name); + +string FloatToString(float f); +string IntToString(int i); + +#endif //__JSONHELPERS_H
\ No newline at end of file |