cf_query.c

Go to the documentation of this file.
00001 #include "cf_query.h"
00002 
00003 #include "ac_app_context.h"
00004 #include "er_errors.h"
00005 #include "lua_cl2.h"
00006 
00007 #include <string.h>
00008 
00009 // --------------------------------------------------
00010 // general
00011 // --------------------------------------------------
00012 
00013 // Evaluates the Lua expression "luaStr". If it evaluates to a nil
00014 // value, sets "L" to NULL. If there is an actual error, returns it.
00015 // Otherwise leaves the result to the Lua state "L", and the caller
00016 // takes ownership of "L".
00017 static gboolean eval_lua_str(const gchar* luaStr, lua_State** pL, GError** error)
00018 {
00019   {
00020     lua_State* L = cl_lua_new_libs();
00021     if (!L) {
00022       if (error) *error = gx_error_no_memory;
00023       return FALSE;
00024     }
00025     //logg("will load string '%s'", luaStr);
00026 
00027     int res = (luaL_loadstring(L, luaStr) || lua_pcall(L, 0, 1, 0));
00028     if (res != 0) {
00029       lua_set_gerror(L, error);
00030       lua_close(L);
00031       return FALSE;
00032     }
00033     //logt("lua string evaluated ok");
00034 
00035     if (lua_isnil(L, -1)) {
00036       lua_close(L);
00037       *pL = NULL;
00038       return TRUE;
00039     }
00040 
00041     *pL = L;
00042     return TRUE;
00043   }
00044 }
00045 
00046 // Like get_lua_value, but uses a default Lua expression when not
00047 // found from the database. "With default", that is. defStr may be
00048 // NULL, and it will not be freed.
00049 static gboolean get_lua_value_wd(const gchar* name,
00050          const gchar* defStr,
00051          lua_State** pL, GError** error)
00052 {
00053   gchar* luaStr = NULL;
00054 
00055   {
00056     GError* localError = NULL;
00057     luaStr = ac_DYNAMIC_GET_ERR(name, &localError);
00058     if (!luaStr) {
00059       if (is_not_found_error(localError)) {
00060   g_error_free(localError);
00061   if (!defStr) {
00062     *pL = NULL;
00063     return TRUE;
00064   }
00065       } else {
00066   gx_propagate_error(error, localError);
00067   return FALSE;
00068       }
00069     }
00070   }
00071 
00072   gboolean ok = eval_lua_str(luaStr ? luaStr : defStr, pL, error);
00073   if (luaStr) g_free(luaStr);
00074   return ok;
00075 }
00076 
00077 // Gets a Lua expression from configuration by "name". If there is no
00078 // value by "name", or if it evaluates to a nil value, sets "L" to
00079 // NULL. If there is an actual error, returns it. Otherwise leaves the
00080 // result to the Lua state "L", and the caller takes ownership of "L".
00081 #define get_lua_value(name,L,err) get_lua_value_wd(name,NULL,L,err)
00082 
00083 // --------------------------------------------------
00084 // integer
00085 // --------------------------------------------------
00086 
00087 static gboolean get_int_from_lua(lua_State* L, int* value, GError** error)
00088 {
00089   if (!lua_isnumber(L, -1)) {
00090     if (error)
00091       *error = gx_error_new(domain_cl2app, code_type_error, 
00092           "integer type Lua value expected");
00093     return FALSE;
00094   }
00095   //logt("lua value is a number");
00096     
00097   lua_Number num = lua_tonumber(L, -1); // double by default
00098   *value = (int)num; // not checking for loss of precision
00099   return TRUE;
00100 }
00101 
00102 gboolean try_get_ConfigDb_int(const gchar* name, int* value, 
00103             gboolean* found, GError** error)
00104 {
00105   lua_State* L = NULL;
00106   if (!get_lua_value(name, &L, error)) {
00107     return FALSE;
00108   }
00109   
00110   if (L) {
00111     gboolean typeOk = get_int_from_lua(L, value, error);
00112     lua_close(L);
00113     if (!typeOk) return FALSE;
00114   }
00115 
00116   if (found) *found = (L != NULL);
00117   return TRUE;
00118 }
00119 
00120 gboolean get_ConfigDb_int(const gchar* name, int* value, 
00121         int default_value, GError** error)
00122 {
00123   gboolean found = FALSE;
00124   if (!try_get_ConfigDb_int(name, value, &found, error))
00125     return FALSE;
00126   if (!found)
00127     *value = default_value;
00128   return TRUE;
00129 }
00130 
00131 int force_get_ConfigDb_int(const gchar* name, int default_value)
00132 {
00133   int value = default_value;
00134   try_get_ConfigDb_int(name, &value, NULL, NULL);
00135   return value;
00136 }
00137 
00138 int force_lua_eval_int(const gchar* luaStr, int default_value)
00139 {
00140   lua_State* L = NULL;
00141   if (eval_lua_str(luaStr, &L, NULL)) {
00142     int value;
00143     gboolean typeOk = get_int_from_lua(L, &value, NULL);
00144     lua_close(L);
00145     if (typeOk) return value;
00146   }
00147   return default_value;
00148 }
00149 
00150 // --------------------------------------------------
00151 // boolean
00152 // --------------------------------------------------
00153 
00154 static gboolean get_bool_from_lua(lua_State* L, gboolean* value, GError** error)
00155 {
00156   if (!lua_isboolean(L, -1)) {
00157     if (error)
00158       *error = gx_error_new(domain_cl2app, code_type_error, 
00159           "boolean type Lua value expected");
00160     return FALSE;
00161   }
00162   //logt("lua value is a boolean");
00163     
00164   int num = lua_toboolean(L, -1);
00165   *value = (num ? TRUE : FALSE);
00166   return TRUE;
00167 }
00168 
00169 gboolean try_get_ConfigDb_bool(const gchar* name, gboolean* value, 
00170              gboolean* found, GError** error)
00171 {
00172   lua_State* L = NULL;
00173   if (!get_lua_value(name, &L, error)) {
00174     return FALSE;
00175   }
00176   
00177   if (L) {
00178     gboolean typeOk = get_bool_from_lua(L, value, error);
00179     lua_close(L);
00180     if (!typeOk) return FALSE;
00181   }
00182 
00183   if (found) *found = (L != NULL);
00184   return TRUE;
00185 }
00186 
00187 gboolean get_ConfigDb_bool(const gchar* name, gboolean* value, 
00188          gboolean default_value, GError** error)
00189 {
00190   gboolean found = FALSE;
00191   if (!try_get_ConfigDb_bool(name, value, &found, error))
00192     return FALSE;
00193   if (!found)
00194     *value = default_value;
00195   return TRUE;
00196 }
00197 
00198 gboolean force_get_ConfigDb_bool(const gchar* name, gboolean default_value)
00199 {
00200   gboolean value = default_value;
00201   try_get_ConfigDb_bool(name, &value, NULL, NULL);
00202   return value;
00203 }
00204 
00205 gboolean force_lua_eval_bool(const gchar* luaStr, gboolean default_value)
00206 {
00207   lua_State* L = NULL;
00208   if (eval_lua_str(luaStr, &L, NULL)) {
00209     gboolean value;
00210     gboolean typeOk = get_bool_from_lua(L, &value, NULL);
00211     lua_close(L);
00212     if (typeOk) return value;
00213   }
00214   return default_value;
00215 }
00216 
00217 // --------------------------------------------------
00218 // string
00219 // --------------------------------------------------
00220 
00221 gboolean try_get_ConfigDb_str(const gchar* name, gchar** s, GError** error)
00222 {
00223   lua_State* L = NULL;
00224   if (!get_lua_value(name, &L, error)) {
00225     return FALSE;
00226   }
00227   
00228   gchar* gotStr = NULL;
00229 
00230   if (L) {
00231     if (!lua_isstring(L, -1)) {
00232       if (error)
00233   *error = gx_error_new(domain_cl2app, code_type_error, "code for '%s' did not yield a string", name);
00234       lua_close(L);
00235       return FALSE;
00236     }
00237     //logt("lua value is a string");
00238 
00239     const char* luaOwned = lua_tostring(L, -1);
00240     gotStr = strdup(luaOwned);
00241     lua_close(L);
00242 
00243     if (!gotStr) {
00244       if (error) *error = gx_error_no_memory;
00245       return FALSE;
00246     }
00247   }
00248 
00249   *s = gotStr;
00250   return TRUE;
00251 }
00252 
00253 gboolean get_ConfigDb_str(const gchar* name, gchar** s, 
00254         const gchar* default_s, GError** error)
00255 {
00256   if (!try_get_ConfigDb_str(name, s, error)) {
00257     return FALSE;
00258   }
00259   if (!*s && default_s) {
00260     *s = strdup(default_s);
00261     if (G_UNLIKELY(!*s)) {
00262       if (error) *error = gx_error_no_memory;
00263       return FALSE;
00264     }
00265   }
00266   return TRUE;
00267 }
00268 
00269 int get_config_iap_id()
00270 {
00271   // Get from dynamic config.
00272   int value = -1;
00273   gboolean found = FALSE;
00274   try_get_ConfigDb_int("iap", &value, &found, NULL);
00275   if (found) return value;
00276   // Get from static config.
00277   return ac_STATIC_GET(iap);
00278 }
00279 
00280 const gchar* get_config_username()
00281 {
00282   return ac_STATIC_GET(username);
00283 }
00284 
00285 /**
00286 
00287 cf_query.c
00288 
00289 Copyright 2009 Helsinki Institute for Information Technology (HIIT)
00290 and the authors. All rights reserved.
00291 
00292 Authors: Tero Hasu <tero.hasu@hut.fi>
00293 
00294 Permission is hereby granted, free of charge, to any person
00295 obtaining a copy of this software and associated documentation files
00296 (the "Software"), to deal in the Software without restriction,
00297 including without limitation the rights to use, copy, modify, merge,
00298 publish, distribute, sublicense, and/or sell copies of the Software,
00299 and to permit persons to whom the Software is furnished to do so,
00300 subject to the following conditions:
00301 
00302 The above copyright notice and this permission notice shall be
00303 included in all copies or substantial portions of the Software.
00304 
00305 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00306 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00307 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00308 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
00309 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00310 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00311 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00312 SOFTWARE.
00313 
00314  **/

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:52 2011 by Doxygen 1.6.1