diff options
Diffstat (limited to 'lib/common.h')
-rw-r--r-- | lib/common.h | 122 |
1 files changed, 95 insertions, 27 deletions
diff --git a/lib/common.h b/lib/common.h index 614dfe6..1829471 100644 --- a/lib/common.h +++ b/lib/common.h @@ -10,6 +10,7 @@ #include <stdint.h> // uint_64_t #include <stdlib.h> +#include <string.h> #include <string> #include <openssl/md5.h> // MD5_* @@ -25,7 +26,7 @@ #endif //*************************************************************************** -// +// Misc //*************************************************************************** #ifndef VDR_PLUGIN @@ -56,49 +57,106 @@ enum Misc tmeSecondsPerDay = 24 * tmeSecondsPerHour }; +enum Case +{ + cUpper, + cLower +}; + +const char* toCase(Case cs, char* str); + //*************************************************************************** // Tell //*************************************************************************** -void tell(int eloquence, const char* format, ...); +void __attribute__ ((format(printf, 2, 3))) tell(int eloquence, const char* format, ...); //*************************************************************************** -// MemoryStruct for curl callbacks +// //*************************************************************************** -struct MemoryStruct -{ - MemoryStruct() { memory = 0; clear(); } - ~MemoryStruct() { clear(); } - - // data +char* srealloc(void* ptr, size_t size); - char* memory; - size_t size; - - // tag attribute - - char tag[100]; // the tag to be compared - char name[100]; // content name (filename) - int headerOnly; +//*************************************************************************** +// MemoryStruct +//*************************************************************************** - int isEmpty() { return memory == 0; } +struct MemoryStruct +{ + public: - void clear() - { - free(memory); - memory = 0; - size = 0; - *tag = 0; - *name = 0; - headerOnly = no; - } + MemoryStruct() { expireAt = time(0); memory = 0; clear(); } + MemoryStruct(const MemoryStruct* o) + { + size = o->size; + memory = (char*)malloc(size); + memcpy(memory, o->memory, size); + + copyAttributes(o); + } + + void copyAttributes(const MemoryStruct* o) + { + strcpy(tag, o->tag); + strcpy(name, o->name); + strcpy(contentType, o->contentType); + strcpy(contentEncoding, o->contentEncoding); + strcpy(mimeType, o->mimeType); + headerOnly = o->headerOnly; + modTime = o->modTime; + expireAt = o->expireAt; + } + + ~MemoryStruct() { clear(); } + + int append(const char* buf, int len) + { + memory = srealloc(memory, size+len); + memcpy(memory+size, buf, len); + size += len; + + return success; + } + + // data + + char* memory; + size_t size; + + // tag attribute + + char tag[100]; // the tag to be compared + char name[100]; // content name (filename) + char contentType[100]; // e.g. text/html + char mimeType[100]; // + char contentEncoding[100]; // + int headerOnly; + time_t modTime; + time_t expireAt; + + int isEmpty() { return memory == 0; } + + void clear() + { + free(memory); + memory = 0; + size = 0; + *tag = 0; + *name = 0; + *contentType = 0; + *contentEncoding = 0; + *mimeType = 0; + modTime = time(0); + // !!!! expireAt = time(0); + headerOnly = no; + } }; //*************************************************************************** // Tools //*************************************************************************** +double usNow(); unsigned int getHostId(); const char* getHostName(); const char* getFirstIp(); @@ -120,14 +178,23 @@ std::string num2Str(int num); std::string l2pTime(time_t t); std::string ms2Dur(uint64_t t); const char* c2s(char c, char* buf); +int urlUnescape(char* dst, const char* src, int normalize = yes); + +int storeToFile(const char* filename, const char* data, int size); +int loadFromFile(const char* infile, MemoryStruct* data); int fileExists(const char* path); int fileSize(const char* path); +time_t fileModTime(const char* path); int createLink(const char* link, const char* dest, int force); int isLink(const char* path); +const char* suffixOf(const char* path); int isEmpty(const char* str); +const char* notNull(const char* str); +int isZero(const char* str); int removeFile(const char* filename); int chkDir(const char* path); +int downloadFile(const char* url, int& size, MemoryStruct* data, int timeout = 30, const char* userAgent = "libcurl-agent/1.0"); #ifdef USELIBXML xsltStylesheetPtr loadXSLT(const char* name, const char* path, int utf8); @@ -145,6 +212,7 @@ int chkDir(const char* path); //*************************************************************************** int gunzip(MemoryStruct* zippedData, MemoryStruct* unzippedData); +int gzip(MemoryStruct* data, MemoryStruct* zippedData); void tellZipError(int errorCode, const char* op, const char* msg); #ifdef USELIBARCHIVE |