diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/curl.c | 2 | ||||
-rw-r--r-- | lib/db.c | 9 | ||||
-rw-r--r-- | lib/db.h | 20 | ||||
-rw-r--r-- | lib/dbdict.c | 3 | ||||
-rw-r--r-- | lib/json.c | 72 | ||||
-rw-r--r-- | lib/json.h | 14 | ||||
-rw-r--r-- | lib/thread.c | 24 | ||||
-rw-r--r-- | lib/thread.h | 81 |
8 files changed, 163 insertions, 62 deletions
@@ -467,7 +467,7 @@ int cCurl::downloadFile(const char* url, int& size, MemoryStruct* data, int time curl_easy_getinfo(handle, CURLINFO_HTTP_CODE, &code); data->statusCode = code; - if (code == 404) + if (code != 200) { data->clear(); exit(); @@ -355,8 +355,7 @@ int cDbStatement::bindCmp(const char* ctable, const char* fname, cDbValue* value return success; } -int cDbStatement::bindText(const char* text, cDbValue* value, - const char* comp, const char* delim) +int cDbStatement::bindText(const char* text, cDbValue* value, const char* comp, const char* delim) { if (!value) { @@ -1522,12 +1521,16 @@ int cDbTable::truncate() tmp = "delete from " + std::string(TableName()); if (connection->query("%s", tmp.c_str())) + return connection->errorSql(connection, "truncate() 'delete from'", 0, tmp.c_str()); + + tmp = "truncate table " + std::string(TableName()); + + if (connection->query("%s", tmp.c_str())) return connection->errorSql(connection, "truncate()", 0, tmp.c_str()); return success; } - //*************************************************************************** // Store //*************************************************************************** @@ -463,18 +463,13 @@ class cDbStatement : public cDbService int bind(cDbFieldDef* field, int mode, const char* delim = 0); int bindAllOut(const char* delim = 0); - int bindCmp(const char* ctable, cDbValue* value, - const char* comp, const char* delim = 0); - int bindCmp(const char* ctable, cDbFieldDef* field, cDbValue* value, - const char* comp, const char* delim = 0); - int bindCmp(const char* ctable, const char* fname, cDbValue* value, - const char* comp, const char* delim = 0); - int bindText(const char* text, cDbValue* value, - const char* comp, const char* delim = 0); - int bindTextFree(const char* text, cDbValue* value, int mode = bndIn); + int bindCmp(const char* ctable, cDbValue* value, const char* comp, const char* delim = 0); + int bindCmp(const char* ctable, cDbFieldDef* field, cDbValue* value, const char* comp, const char* delim = 0); + int bindCmp(const char* ctable, const char* fname, cDbValue* value, const char* comp, const char* delim = 0); - int bindInChar(const char* ctable, const char* fname, - cDbValue* value = 0, const char* delim = 0); + int bindText(const char* text, cDbValue* value, const char* comp, const char* delim = 0); + int bindTextFree(const char* text, cDbValue* value, int mode = bndIn); + int bindInChar(const char* ctable, const char* fname, cDbValue* value = 0, const char* delim = 0); int appendBinding(cDbValue* value, BindType bt); // use this interface method seldom from external and with care! @@ -684,6 +679,7 @@ class cDbRow : public cDbService float getFloatValue(cDbFieldDef* f) const { return dbValues[f->getIndex()].getFloatValue(); } int isNull(cDbFieldDef* f) const { return dbValues[f->getIndex()].isNull(); } + time_t getTimeValue(const char* n) const { GET_FIELD_RES(n, 0); return dbValues[f->getIndex()].getTimeValue(); } const char* getStrValue(const char* n) const { GET_FIELD_RES(n, ""); return dbValues[f->getIndex()].getStrValue(); } long getIntValue(const char* n) const { GET_FIELD_RES(n, 0); return dbValues[f->getIndex()].getIntValue(); } int64_t getBigintValue(const char* n) const { GET_FIELD_RES(n, 0); return dbValues[f->getIndex()].getBigintValue(); } @@ -1145,12 +1141,14 @@ class cDbTable : public cDbService long getIntValue(cDbFieldDef* f) const { return row->getIntValue(f); } int64_t getBigintValue(cDbFieldDef* f) const { return row->getBigintValue(f); } float getFloatValue(cDbFieldDef* f) const { return row->getFloatValue(f); } + time_t getTimeValue(cDbFieldDef* f) const { return row->getTimeValue(f); } int isNull(cDbFieldDef* f) const { return row->isNull(f); } const char* getStrValue(const char* n) const { return row->getStrValue(n); } long getIntValue(const char* n) const { return row->getIntValue(n); } int64_t getBigintValue(const char* n) const { return row->getBigintValue(n); } float getFloatValue(const char* n) const { return row->getFloatValue(n); } + time_t getTimeValue(const char* n) const { return row->getTimeValue(n); } int isNull(const char* n) const { return row->isNull(n); } cDbValue* getValue(cDbFieldDef* f) { return row->getValue(f); } diff --git a/lib/dbdict.c b/lib/dbdict.c index 1052a13..009adeb 100644 --- a/lib/dbdict.c +++ b/lib/dbdict.c @@ -469,8 +469,7 @@ int cDbDict::parseField(const char* line) } else { - tell(2, "Info: Ignoring field '%s' due to filter configiuration", - f->getName()); + tell(2, "Info: Ignoring field '%s' due to filter configiuration", f->getName()); delete f; } @@ -158,6 +158,16 @@ const char* getStringFromJson(json_t* obj, const char* name, const char* def) return json_string_value(o); } +int getBoolFromJson(json_t* obj, const char* name, bool def) +{ + json_t* o = json_object_get(obj, name); + + if (!o) + return def; + + return json_boolean_value(o); +} + int getIntFromJson(json_t* obj, const char* name, int def) { json_t* o = json_object_get(obj, name); @@ -168,5 +178,67 @@ int getIntFromJson(json_t* obj, const char* name, int def) return json_integer_value(o); } +long getLongFromJson(json_t* obj, const char* name, long def) +{ + json_t* o = json_object_get(obj, name); + + if (!o) + return def; + + return json_integer_value(o); +} + +double getDoubleFromJson(json_t* obj, const char* name, double def) +{ + json_t* o = json_object_get(obj, name); + + if (!o) + return def; + + return json_real_value(o); +} + + +int jStringValid(const char* s) +{ + json_t* obj = json_string(s); + + if (!obj) + return no; + + json_decref(obj); + + return yes; +} + +//*************************************************************************** +// Add Element +//*************************************************************************** + +int addToJson(json_t* obj, const char* name, const char* value, const char* def) +{ + json_t* j = json_string(value ? value : def); + + if (!j) + j = json_string(""); + + return json_object_set_new(obj, name, j); +} + +int addToJson(json_t* obj, const char* name, long value) +{ + return json_object_set_new(obj, name, json_integer(value)); +} + +int addToJson(json_t* obj, const char* name, int value) +{ + return json_object_set_new(obj, name, json_integer(value)); +} + +int addToJson(json_t* obj, const char* name, json_t* o) +{ + return json_object_set_new(obj, name, o); +} + //*************************************************************************** #endif // USEJSON @@ -5,8 +5,7 @@ * */ -#ifndef __JSON_H -#define __JSON_H +#pragma once //*************************************************************************** // Include @@ -17,6 +16,7 @@ #include <jansson.h> #include "db.h" +#include "common.h" //*************************************************************************** // JSON Helper Functions @@ -28,9 +28,17 @@ int addFieldToJson(json_t* obj, cDbTable* table, const char* fname, int ignoreEm int addFieldToJson(json_t* obj, cDbValue* value, int ignoreEmpty = yes, const char* extName = 0); int getFieldFromJson(json_t* obj, cDbRow* row, const char* fname, const char* extName = 0); +int jStringValid(const char* s); + const char* getStringFromJson(json_t* obj, const char* name, const char* def = 0); int getIntFromJson(json_t* obj, const char* name, int def = na); +int getBoolFromJson(json_t* obj, const char* name, bool def = false); +long getLongFromJson(json_t* obj, const char* name, long def = na); +double getDoubleFromJson(json_t* obj, const char* name, double def = na); + +int addToJson(json_t* obj, const char* name, const char* value, const char* def = ""); +int addToJson(json_t* obj, const char* name, long value); +int addToJson(json_t* obj, const char* name, json_t* o); #endif // USEJSON -#endif // __JSON_H diff --git a/lib/thread.c b/lib/thread.c index 4f2cbfe..7728db6 100644 --- a/lib/thread.c +++ b/lib/thread.c @@ -10,6 +10,8 @@ #include <sys/syscall.h> #include <sys/wait.h> #include <sys/time.h> +#include <unistd.h> +#include <stdarg.h> #include "thread.h" @@ -61,12 +63,14 @@ cThread::cThread(const char* Description, bool LowPriority) SetDescription("%s", Description); lowPriority = LowPriority; + pthread_attr_init(&attr); } cThread::~cThread() { Cancel(); // just in case the derived class didn't call it free(description); + pthread_attr_destroy(&attr); } void cThread::SetDescription(const char *Description, ...) @@ -135,7 +139,7 @@ void cThread::SetIOPriority(int priority) #define THREAD_STOP_TIMEOUT 3000 // ms to wait for a thread to stop before newly starting it #define THREAD_STOP_SLEEP 30 // ms to sleep while waiting for a thread to stop -bool cThread::Start(int s) +bool cThread::Start(int s, int stackSize) { silent = s; @@ -153,9 +157,21 @@ bool cThread::Start(int s) } if (!active) { + int res; + active = running = true; - if (pthread_create(&childTid, NULL, (void *(*) (void *))&StartThread, (void *)this) == 0) + if (stackSize == na) + { + res = pthread_create(&childTid, 0, (void*(*)(void*))&StartThread, (void*)this); + } + else + { + pthread_attr_setstacksize(&attr, stackSize); + res = pthread_create(&childTid, &attr, (void*(*)(void*))&StartThread, (void*)this); + } + + if (res == 0) { pthread_detach(childTid); // auto-reap } @@ -195,6 +211,8 @@ void cThread::Cancel(int WaitSeconds) { running = false; + waitCondition.Broadcast(); + if (active && WaitSeconds > -1) { if (WaitSeconds > 0) @@ -241,7 +259,7 @@ cCondWait::~cCondWait() void cCondWait::SleepMs(int TimeoutMs) { cCondWait w; - w.Wait(max(TimeoutMs, 3)); // making sure the time is >2ms to avoid a possible busy wait + w.Wait(std::max(TimeoutMs, 3)); // making sure the time is >2ms to avoid a possible busy wait } bool cCondWait::Wait(int TimeoutMs) diff --git a/lib/thread.h b/lib/thread.h index 1598ff2..fcaaf4b 100644 --- a/lib/thread.h +++ b/lib/thread.h @@ -10,6 +10,41 @@ #include "common.h" +class cCondWait +{ + public: + + cCondWait(); + ~cCondWait(); + + bool Wait(int TimeoutMs = 0); + void Signal(); + + static void SleepMs(int TimeoutMs); + + private: + + pthread_mutex_t mutex; + pthread_cond_t cond; + bool signaled; +}; + +class cCondVar +{ + public: + + cCondVar(); + ~cCondVar(); + + void Wait(cMyMutex &Mutex); + bool TimedWait(cMyMutex &Mutex, int TimeoutMs); + void Broadcast(); + + private: + + pthread_cond_t cond; +}; + //*************************************************************************** // Class cThread //*************************************************************************** @@ -22,10 +57,11 @@ class cThread virtual ~cThread(); void SetDescription(const char* Description, ...) __attribute__ ((format (printf, 2, 3))); - bool Start(int s = no); + bool Start(int s = no, int stackSize = na); bool Active(); void SetPriority(int priority); void SetIOPriority(int priority); + void Cancel(int WaitSeconds = 0); static pid_t ThreadId(); @@ -33,14 +69,12 @@ class cThread static void* StartThread(cThread *Thread); - bool active; - bool running; pthread_t childTid; pid_t childThreadId; - cMyMutex mutex; char* description; bool lowPriority; int silent; + pthread_attr_t attr; protected: @@ -49,42 +83,11 @@ class cThread void Lock() { mutex.Lock(); } void Unlock() { mutex.Unlock(); } bool Running() { return running; } - void Cancel(int WaitSeconds = 0); -}; - -class cCondWait -{ - public: - - cCondWait(); - ~cCondWait(); - - bool Wait(int TimeoutMs = 0); - void Signal(); - - static void SleepMs(int TimeoutMs); - - private: - - pthread_mutex_t mutex; - pthread_cond_t cond; - bool signaled; -}; - -class cCondVar -{ - public: - cCondVar(); - ~cCondVar(); - - void Wait(cMyMutex &Mutex); - bool TimedWait(cMyMutex &Mutex, int TimeoutMs); - void Broadcast(); - - private: - - pthread_cond_t cond; + cCondVar waitCondition; + cMyMutex mutex; + bool active; + bool running; }; //*************************************************************************** |