boolean FontFamily::font( int size, const char* style, const char*& name, float& scale ) const { int weight = name_value(style, weight_names, 5); int slant = name_value(style, slant_names, 2); int width = name_value(style, width_names, 5); int best_match = -1; int least_badness = 1000; FontFamilyRep* r = rep(Session::instance()->default_display()); for (int i = 0; i < r->count_; ++i) { int badness = 0; badness += Math::abs(r->weights_[i] - weight); badness += Math::abs(r->widths_[i] - width); badness += Math::abs(r->slants_[i] - slant); badness += Math::abs(r->sizes_[i] - size); if (badness < least_badness) { least_badness = badness; best_match = i; } } if (best_match == -1) { return false; } int best_size = r->sizes_[best_match]; name = r->names_[best_match]; scale = (size == best_size) ? 1.0 : float(size)/float(best_size); return true; }
void redirect_to(const std::string& url) { headers.push_back(name_value("Refresh", "0; url=" + url)); content = " "; content_extension = "html"; }
void set_cookie(const std::string& name, const std::string& value) { std::vector<name_value>::iterator found; found = std::find_if(cookies.begin(), cookies.end(), boost::bind(&name_value::compare_name, _1, name)); if (found == cookies.end()) { cookies.push_back(name_value(name, value)); return; } found->value = value; }
gcache::GCache::Params::Params (gu::Config& cfg, const std::string& data_dir) throw (gu::Exception) : rb_name (name_value (cfg, data_dir)), dir_name (cfg.get(GCACHE_PARAMS_DIR)), mem_size (size_value (cfg, GCACHE_PARAMS_MEM_SIZE, GCACHE_DEFAULT_MEM_SIZE)), rb_size (size_value (cfg, GCACHE_PARAMS_RB_SIZE, GCACHE_DEFAULT_RB_SIZE)), page_size (size_value (cfg, GCACHE_PARAMS_PAGE_SIZE, GCACHE_DEFAULT_PAGE_SIZE)), keep_pages_size (size_value (cfg, GCACHE_PARAMS_KEEP_PAGES_SIZE, GCACHE_DEFAULT_KEEP_PAGES_SIZE)) {}
int main( int argc, char** argv ) { boost::scoped_ptr< comma::Multiplay > multiplay; try { const boost::array< comma::signal_flag::signals, 2 > signals = { { comma::signal_flag::sigint, comma::signal_flag::sigterm } }; comma::signal_flag shutdown_flag( signals ); comma::command_line_options options( argc, argv ); if( options.exists( "--help,-h" ) ) { usage(); } options.assert_mutually_exclusive( "--speed,--slow,--slowdown" ); double speed = options.value( "--speed", 1.0 / options.value< double >( "--slow,--slowdown", 1.0 ) ); double resolution = options.value< double >( "--resolution", 0.01 ); std::string from = options.value< std::string>( "--from", "" ); std::string to = options.value< std::string>( "--to", "" ); bool quiet = options.exists( "--quiet" ); bool flush = !options.exists( "--no-flush" ); std::vector< std::string > configstrings = options.unnamed("--interactive,-i,--paused,--paused-at-start,--quiet,--flush,--no-flush","--slow,--slowdown,--speed,--resolution,--binary,--fields,--clients,--from,--to"); if( configstrings.empty() ) { configstrings.push_back( "-;-" ); } comma::csv::options csvoptions( argc, argv ); comma::name_value::parser name_value("filename,output", ';', '=', false ); std::vector< comma::Multiplay::SourceConfig > sourceConfigs( configstrings.size() ); comma::Multiplay::SourceConfig defaultConfig( "-", options.value( "--clients", 0 ), csvoptions ); for( unsigned int i = 0U; i < configstrings.size(); ++i ) { sourceConfigs[i] = name_value.get< comma::Multiplay::SourceConfig >( configstrings[i], defaultConfig ); } boost::posix_time::ptime fromtime; if( !from.empty() ) { fromtime = boost::posix_time::from_iso_string( from ); } boost::posix_time::ptime totime; if( !to.empty() ) { totime = boost::posix_time::from_iso_string( to ); } multiplay.reset( new comma::Multiplay( sourceConfigs, 1.0 / speed, quiet, boost::posix_time::microseconds( static_cast<unsigned int> (resolution * 1000000) ), fromtime, totime, flush ) ); key_press_handler_t key_press_handler( options.exists( "--interactive,-i" ), options.exists( "--paused,--paused-at-start" ) ); while( !shutdown_flag && std::cout.good() && !std::cout.bad() && !std::cout.eof() ) { key_press_handler.update( multiplay->now() ); if( key_press_handler.state() == key_press_handler_t::paused ) { boost::this_thread::sleep( boost::posix_time::millisec( 200 ) ); continue; } if( !multiplay->read() ) { break; } key_press_handler.has_read_once(); } multiplay->close(); multiplay.reset(); if( shutdown_flag ) { std::cerr << "csv-play: interrupted by signal" << std::endl; return -1; } return 0; } catch( std::exception& ex ) { std::cerr << "csv-play: " << ex.what() << std::endl; } catch( ... ) { std::cerr << "csv-play: unknown exception" << std::endl; } try { if( multiplay ) { multiplay->close(); } } catch ( ... ) {} // windows thing return 1; }
static GdkGC * _getUIStyle_gc (const gchar * name, const gchar * state, GtkStyle * style) { GdkGC *gc; gint n, m; g_return_val_if_fail (state != NULL, NULL); g_return_val_if_fail (name != NULL, NULL); g_return_val_if_fail (style != NULL, NULL); g_return_val_if_fail (GTK_IS_STYLE(style), NULL); n = state_value (state); m = name_value (name); switch (m) { case GTKSTYLE_FG: gc = style->fg_gc[n]; break; case GTKSTYLE_BG: gc = style->bg_gc[n]; break; case GTKSTYLE_TEXT: gc = style->text_gc[n]; break; case GTKSTYLE_BASE: gc = style->base_gc[n]; break; case GTKSTYLE_LIGHT: gc = style->light_gc[n]; break; case GTKSTYLE_DARK: gc = style->dark_gc[n]; break; default: case GTKSTYLE_MID: gc = style->mid_gc[n]; break; } return (gc); }
int main() { const char file[] = "names.txt"; std::ifstream ifs(file); char buf[MAX_BUF]; std::vector<std::string> names; while (ifs.getline(buf, MAX_BUF, ',')) { buf[strlen(buf) - 1] = 0; names.push_back(std::string(buf + 1)); } std::sort(names.begin(), names.end()); std::size_t sum = 0, i = 0; for (const auto &name : names) sum += ++i * name_value(name); std::cout << "Answer: " << sum << std::endl; }
static gchar * print_rc_style (GtkWidget * win, const gchar * name, const gchar * state, GtkStyle * style) { gchar *s; gint n, m; g_return_val_if_fail (state != NULL, NULL); g_return_val_if_fail (name != NULL, NULL); n = state_value (state); m = name_value (name); switch (m) { case GTKSTYLE_FG: s = print_colors (win, style->fg, n); break; case GTKSTYLE_BG: s = print_colors (win, style->bg, n); break; case GTKSTYLE_TEXT: s = print_colors (win, style->text, n); break; case GTKSTYLE_BASE: s = print_colors (win, style->base, n); break; case GTKSTYLE_LIGHT: s = print_colors (win, style->light, n); break; case GTKSTYLE_DARK: s = print_colors (win, style->dark, n); break; default: case GTKSTYLE_MID: s = print_colors (win, style->mid, n); break; } return (s); }
void request_parser::parse_cookie(request& req, const std::string& data) { std::string::size_type pos = data.find("=", 0); if (pos == std::string::npos) { return; } std::string::size_type space_count = 0; std::string::const_iterator data_iter; for (data_iter = data.begin(); data_iter != data.end(); ++data_iter, ++space_count) { if (std::isspace(*data_iter) == 0) { break; } } std::string name = data.substr(space_count, pos - space_count); std::string value = data.substr(++pos); req.cookies.push_back(name_value(name, value)); }
void request_parser::parse_multipart_content(request& req, const std::string& data) { std::string end = "\r\n\r\n"; std::string::size_type head_limit = data.find(end, 0); if (head_limit == std::string::npos) { throw std::runtime_error("invalid part header"); } std::string::size_type value_start = head_limit + end.size(); std::string value = data.substr(value_start, data.size() - value_start - 2); multipart_header head = parse_multipart_header(req, data.substr(0, value_start)); if (head.filename.empty()) { req.params.push_back(name_value(head.name, value)); } else { req.uploads.push_back(multipart_content(head.name, head.filename, head.filetype, value)); } }
void CRowInfo_SP_SQL_Server::Initialize(void) const { impl::CConnection& conn = GetCConnection(); unsigned int step = 0; CDBConnParams::EServerType server_type = conn.GetServerType(); if (server_type == CDBConnParams::eUnknown) { server_type = conn.CalculateServerType(server_type); ++step; } while (step++ < 3) { if (server_type == CDBConnParams::eSybaseSQLServer || server_type == CDBConnParams::eMSSqlServer) { string sql; string db_name; string db_owner; string sp_name; auto_ptr<CDB_LangCmd> cmd; { vector<string> arr_param; NStr::Tokenize(GetSPName(), ".", arr_param); size_t pos = 0; switch (arr_param.size()) { case 3: db_name = arr_param[pos++]; case 2: db_owner = arr_param[pos++]; case 1: sp_name = arr_param[pos++]; break; default: DATABASE_DRIVER_ERROR("Invalid format of stored procedure's name: " + GetSPName(), 1); } } if (db_name.empty()) { sql = "SELECT '' from sysobjects WHERE name = @name \n" "UNION \n" "SELECT 'master' from master..sysobjects WHERE name = @name \n" ; if (server_type == CDBConnParams::eSybaseSQLServer) { sql += "UNION \n" "SELECT 'sybsystemprocs' from sybsystemprocs..sysobjects WHERE name = @name \n" "UNION \n" "SELECT 'sybsystemdb' from sybsystemdb..sysobjects WHERE name = @name" ; } CMsgHandlerGuard guard(conn); cmd.reset(conn.LangCmd(sql)); CDB_VarChar sp_name_value(sp_name); try { cmd->GetBindParams().Bind("@name", &sp_name_value); cmd->Send(); while (cmd->HasMoreResults()) { auto_ptr<CDB_Result> res(cmd->Result()); if (res.get() != NULL && res->ResultType() == eDB_RowResult ) { CDB_VarChar db_name_value; while (res->Fetch()) { res->GetItem(&db_name_value); if (!db_name_value.IsNULL()) { db_name = db_name_value.Value(); } } } } } catch (const CDB_Exception&) { // Something is wrong. Probably we do not have enough permissios. // We assume that the object is located in the current database. What // else can we do? // Probably, this method was supplied with a wrong // server_type value; if (step < 2) { server_type = conn.CalculateServerType(CDBConnParams::eUnknown); ++step; continue; } } } // auto_ptr<CDB_RPCCmd> sp(conn.RPC("sp_sproc_columns")); // We cannot use CDB_RPCCmd here because of recursion ... sql = "exec " + db_name + "." + db_owner + ".sp_sproc_columns @procedure_name"; cmd.reset(conn.LangCmd(sql)); CDB_VarChar name_value(sp_name); try { cmd->GetBindParams().Bind("@procedure_name", &name_value); cmd->Send(); while (cmd->HasMoreResults()) { auto_ptr<CDB_Result> res(cmd->Result()); if (res.get() != NULL && res->ResultType() == eDB_RowResult ) { CDB_VarChar column_name; CDB_SmallInt column_type; CDB_SmallInt data_type; CDB_Int data_len = 0; while (res->Fetch()) { res->SkipItem(); res->SkipItem(); res->SkipItem(); res->GetItem(&column_name); res->GetItem(&column_type); res->GetItem(&data_type); res->SkipItem(); res->SkipItem(); res->GetItem(&data_len); // Decode data_type EDB_Type edb_data_type(eDB_UnsupportedType); switch (data_type.Value()) { case SQL_LONGVARCHAR: edb_data_type = eDB_VarChar; break; case SQL_BINARY: edb_data_type = eDB_Binary; break; case SQL_VARBINARY: edb_data_type = eDB_VarBinary; break; case SQL_LONGVARBINARY: edb_data_type = eDB_Binary; break; case SQL_BIGINT: edb_data_type = eDB_BigInt; break; case SQL_TINYINT: edb_data_type = eDB_TinyInt; break; case SQL_BIT: edb_data_type = eDB_Bit; break; // case SQL_GUID: case -9: edb_data_type = eDB_VarChar; break; case SQL_CHAR: edb_data_type = eDB_Char; break; case SQL_NUMERIC: case SQL_DECIMAL: edb_data_type = eDB_Numeric; break; case SQL_INTEGER: edb_data_type = eDB_Int; break; case SQL_SMALLINT: edb_data_type = eDB_SmallInt; break; case SQL_FLOAT: case SQL_REAL: edb_data_type = eDB_Float; break; case SQL_DOUBLE: edb_data_type = eDB_Double; break; case SQL_DATETIME: case SQL_TIME: case SQL_TIMESTAMP: edb_data_type = eDB_DateTime; break; case SQL_VARCHAR: edb_data_type = eDB_VarChar; break; // case SQL_TYPE_DATE: // case SQL_TYPE_TIME: // case SQL_TYPE_TIMESTAMP: default: edb_data_type = eDB_UnsupportedType; } EDirection direction = CDBParams::eIn; if (column_type.Value() == 2 /*SQL_PARAM_TYPE_OUTPUT*/ || column_type.Value() == 4 /*SQL_PARAM_OUTPUT*/ || column_type.Value() == 5 /*SQL_RETURN_VALUE*/ ) { direction = CDBParams::eOut; } Add(column_name.Value(), size_t(data_len.Value()), edb_data_type, direction ); } } // if ... } // while HasMoreresults ... // Break the loop, Everything seems to be fine. ... break; } catch (const CDB_Exception&) { // Something is wrong ... // We may not have permissions to run stored procedures ... // Probably, this method was supplied with a wrong // server_type value; if (step < 2) { server_type = conn.CalculateServerType(CDBConnParams::eUnknown); ++step; } } } // if server_type } SetInitialized(); }
FontFamilyRep* FontFamily::create(Display* d) const { register FontFamilyRep* r = new FontFamilyRep; char buffer[256]; sprintf(buffer, "*-*-%s-*-*-*-*-75-75-*-*-*-*", impl_->name); char** fonts = XListFonts( d->rep()->display_, buffer, 100, &r->count_ ); r->display_ = d; r->names_ = new char*[r->count_]; r->weights_ = new int[r->count_]; r->slants_ = new int[r->count_]; r->widths_ = new int[r->count_]; r->sizes_ = new int[r->count_]; r->min_weight_ = 1000; r->max_weight_ = 0; r->min_slant_ = 1000; r->max_slant_ = 0; r->min_width_ = 1000; r->max_width_ = 0; r->min_size_ = 1000; r->max_size_ = 0; for (unsigned int i = 0; i < r->count_; ++i) { r->names_[i] = new char[strlen(fonts[i]) + 1]; strcpy(r->names_[i], fonts[i]); char weight[100]; char slant[100]; char width[100]; int size; sscanf( r->names_[i], "-%*[^-]-%*[^-]-%[^-]-%[^-]-%[^-]--%*[^-]-%d", weight, slant, width, &size ); r->weights_[i] = name_value(weight, weight_names, 5); String sl(slant); if (sl == "o" || sl == "i") { r->slants_[i] = 3; } else if (sl == "r") { r->slants_[i] = 2; } else if (sl == "ro" || sl == "ri") { r->slants_[i] = 1; } else { r->slants_[i] = 2; } r->widths_[i] = name_value(width, width_names, 5); r->sizes_[i] = size/10; r->min_width_ = Math::min(r->widths_[i], r->min_width_); r->max_width_ = Math::max(r->widths_[i], r->max_width_); r->min_weight_ = Math::min(r->weights_[i], r->min_weight_); r->max_weight_ = Math::max(r->weights_[i], r->max_weight_); r->min_slant_ = Math::min(r->slants_[i], r->min_slant_); r->max_slant_ = Math::max(r->slants_[i], r->max_slant_); r->min_size_ = Math::min(r->sizes_[i], r->min_size_); r->max_size_ = Math::max(r->sizes_[i], r->max_size_); } XFreeFontNames(fonts); return r; }
string conf_gen::gen_conf() { string spec_line = "# COMPUTE NODES"; string ctlmachine_line = "ControlMachine=##"; string ctladdr_line = "ControlAddr=##"; bool spec_line_hit = false; string conf_line; ifstream ifs(_conf_file); while (getline(ifs, conf_line)) { // string line = replace_at_tok(conf_line); string line = conf_line; if (line == spec_line) { //"# COMPUTE NODES"; spec_line_hit = true; _conf_list.push_back(line); } else if (line == ctlmachine_line) { //ControlMachine=## char buf[100]; sprintf(buf, "ControlMachine=%s", _ctlmachine.c_str()); string tmp(buf); _conf_list.push_back(tmp); } else if (line == ctladdr_line) { //ControlAddr=## char buf[100]; sprintf(buf, "ControlAddr=%s", _ctladdr.c_str()); string tmp(buf); _conf_list.push_back(tmp); } else { if (spec_line_hit) { _store_list.push_back(line); //spec_line_hit = false; } else { _conf_list.push_back(line); } } } /*NodeName=node-[3-4] Nodeaddr=node-[3-4].xiaobing.usrc CPUs=2 Sockets=2 CoresPerSocket=1 ThreadsPerCore=1 State=UNKNOWN*/ for (IT it = _store_list.begin(); it != _store_list.end(); it++) { str_tok attrs(*it); // " ", delimiter bool nodes_hit = false; bool nodename_hit = false; bool nodeaddr_hit = false; string nodes = "Nodes"; //Nodes=node-[2-101] string nodename = "NodeName"; //NodeName=node-[2-101] string nodeaddr = "Nodeaddr"; //Nodeaddr=node-[3-4].xiaobing.usrc while (attrs.has_more_tokens()) { string attr = trim(attrs.next_token()); //Nodeaddr=node-[3-4].xiaobing.usrc str_tok name_value(attr, "="); string name = name_value.next_token(); //e.g. Nodeaddr if (name == nodes) { //"Nodes" nodes_hit = true; } else if (name == nodename) { //"NodeName" nodename_hit = true; } else if (name == nodeaddr) { //"Nodeaddr" nodeaddr_hit = true; } else ; if (nodes_hit) { char buf[100]; sprintf(buf, "Nodes=node-[%d-%d]", _start, _end); string tmp(buf); _spec_list.push_back(tmp); nodes_hit = false; } else if (nodename_hit) { char buf[100]; sprintf(buf, "NodeName=node-[%d-%d]", _start, _end); string tmp(buf); _spec_list.push_back(tmp); nodename_hit = false; } else if (nodeaddr_hit) { string node_addr_list = gen_node_arr_list(); char buf[10000]; sprintf(buf, "Nodeaddr=%s", node_addr_list.c_str()); string tmp(buf); _spec_list.push_back(tmp); nodeaddr_hit = false; } else { _spec_list.push_back(attr); } } _spec_list.push_back("\n"); } return to_string(); }
void request_parser::parse_content(request& req) { if (req.content.empty()) { return; } std::string standard_type = "application/x-www-form-urlencoded"; std::string multipart_type = "multipart/form-data"; std::string content_type = req.header("Content-Type"); if (content_type.empty() || strings_are_equal(content_type, standard_type, standard_type.size())) { std::string name, value; std::string::size_type pos; std::string::size_type old_pos = 0; while (true) { pos = req.content.find_first_of("&=", old_pos); if (pos == std::string::npos) { break; } if (req.content.at(pos) == '&') { const char* c_str_ = req.content.c_str() + old_pos; while (*c_str_ == '&') { ++c_str_; ++old_pos; } if (old_pos >= pos) { old_pos = ++pos; continue; } name = percent_decode(req.content.substr(old_pos, pos - old_pos)); req.params.push_back(name_value(name, std::string())); old_pos = ++pos; continue; } name = percent_decode(req.content.substr(old_pos, pos - old_pos)); old_pos = ++pos; pos = req.content.find_first_of(";&", old_pos); value = percent_decode(req.content.substr(old_pos, pos - old_pos)); req.params.push_back(name_value(name, value)); if (pos == std::string::npos) { break; } old_pos = ++pos; } } else if (strings_are_equal(multipart_type, content_type, multipart_type.size())) { std::string b_type = "boundary="; std::string::size_type pos = content_type.find(b_type); std::string sep1 = content_type.substr(pos + b_type.size()); if (sep1.find(";") != std::string::npos) { sep1 = sep1.substr(0, sep1.find(";")); } sep1.append("\r\n"); sep1.insert(0, "--"); std::string sep2 = content_type.substr(pos + b_type.size()); if (sep2.find(";") != std::string::npos) { sep2 = sep2.substr(0, sep2.find(";")); } sep2.append("--\r\n"); sep2.insert(0, "--"); std::string::size_type start = req.content.find(sep1); std::string::size_type sep_size = sep1.size(); std::string::size_type old_pos = start + sep_size; while (true) { pos = req.content.find(sep1, old_pos); if (pos == std::string::npos) { break; } parse_multipart_content(req, req.content.substr(old_pos, pos - old_pos)); old_pos = pos + sep_size; } pos = req.content.find(sep2, old_pos); if (pos != std::string::npos) { parse_multipart_content(req, req.content.substr(old_pos, pos - old_pos)); } } }
void name_pair_(){ if(look_ahead == ',') { match(',');name_value();name_pair_(); }else ; }
void name_pair() { name_value();name_pair_(); }