示例#1
0
/*!

\brief Converts Config to dmz::Float64.
\details Defined in dmzRuntimeConfigToTypesBase.h.
This function uses dmz::config_to_sring to retrieve the value and then uses
dmz::string_to_float64 to convert the returned String to a dmz::Float64.
\param[in] Name String containing name of the attribute in the config context to convert.
\param[in] Source Config containing config context to convert.
\param[in] DefaultValue dmz::Float64 containing default value that is returned if
the config context is not found.
\return Returns dmz::Float64 containing value.
\sa dmz::config_to_string(const String &Name, const Config &Source, const String &DefaultValue)

*/
dmz::Float64
dmz::config_to_float64 (
      const String &Name,
      const Config &Source,
      const Float64 DefaultValue) {

   Float64 result (DefaultValue);
   String str;

   if (local_config_to_string (Name, Source, str)) {

      result = string_to_float64 (str);
   }

   return result;
}
示例#2
0
float64_t
stream_read_float64(stream_t* stream) {
	float64_t value = 0;
	if (stream_is_binary(stream)) {
		stream_read(stream, &value, 8);
		if (stream->swap) {
			float64_cast_t cast;
			cast.fval = value;
			cast.uival = byteorder_swap64(cast.uival);
			value = cast.fval;
		}
	}
	else {
		char buffer[64] = {0};
		string_t str = stream_read_string_buffer(stream, buffer, 64);
		value = string_to_float64(str.str, str.length);
	}
	return value;
}
示例#3
0
	int MysqlForLua::_Query(lua_State* L){
		//// prepare args
		if(lua_gettop(L) < 2){
			lua_pushnil(L);
			lua_pushstring(L, "missing arg");
			return 2;
		}
		// self
		MysqlForLua* self =0;
		if(!get_object_from_lua< MysqlForLua >(L, 1, self) || !self){
			lua_pushnil(L);
			lua_pushstring(L, "not a valid MysqlForLua Object");
			return 2;
		}
		// what
		if(0 == lua_isstring(L, 2)){
			lua_pushnil(L);
			lua_pushstring(L, "invalid arg what");
			return 2;
		}
		const char* what =lua_tostring(L, 2);
		ASSERT(what);
		//// check
		if(!self->m_mysql){
			lua_pushnil(L);
			lua_pushstring(L, "MysqlForLua not connected");
			return 2;
		}
		//// query
		DEBUG(what);
		if(mysql_query(self->m_mysql, what)){
			lua_pushnil(L);
			lua_pushfstring(L, "%s", self->_mysql_error());
			return 2;
		}
		//// process result
		MYSQL_RES *result =mysql_store_result(self->m_mysql);
		if(!result){
			lua_pushnil(L);
			lua_pushfstring(L, "%s", self->_mysql_error());
			return 2;
		}
		const int64_t num_fields =mysql_num_fields(result);
		MYSQL_FIELD* fields =mysql_fetch_fields(result);
		if(!fields || num_fields<=0){
			mysql_free_result(result);
			lua_pushnil(L);
			lua_pushfstring(L, "%s", self->_mysql_error());
			return 2;
		}
		const int64_t num_rows =mysql_num_rows(result);
		lua_createtable(L, (int)num_rows, 0);
		int index =1;
		while(MYSQL_ROW row =mysql_fetch_row(result)){
			lua_createtable(L, 0, num_fields);
			for(int64_t i=0; i<num_fields; ++i){
				const int type =fields[i].type;
				switch(type){
				case FIELD_TYPE_TINY:
				case FIELD_TYPE_SHORT:
				case FIELD_TYPE_LONG:
				case FIELD_TYPE_INT24:
				case FIELD_TYPE_LONGLONG:
				case FIELD_TYPE_FLOAT:
				case FIELD_TYPE_DOUBLE:
					if(row[i]){
						float64_t v =0;
						if(string_to_float64(row[i], v)){
							lua_pushnumber(L, v);
							lua_setfield(L, -2, fields[i].name);
						}
						else{
							mysql_free_result(result);
							lua_pop(L, 2);
							lua_pushnil(L);
							lua_pushfstring(L, "the value of mysql field %s is not a number, value is %s", fields[i].name, row[i]);
							return 2;
						}
					}
					break;
				case FIELD_TYPE_STRING:
				case FIELD_TYPE_VAR_STRING:
					if(row[i]){
						lua_pushstring(L, row[i]);
						lua_setfield(L, -2, fields[i].name);
					}
					break;
				default:
					mysql_free_result(result);
					lua_pop(L, 2);
					lua_pushnil(L);
					lua_pushfstring(L, "the type of mysql field %s is not support, type is %d", fields[i].name, type);
					return 2;
				}
			}
			lua_rawseti(L, -2, index);
			index +=1;
		}
		mysql_free_result(result);
		return 1;
	}