/* * Filter is used to decide which properties to load: NULL loads all keys, * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match. */ static void load_properties_from_file(const char* filename, const char* filter) { Timer t; std::string data; if (read_file(filename, &data)) { load_properties(&data[0], filter); } NOTICE("(Loading properties from %s took %.2fs.)\n", filename, t.duration()); }
/* * Filter is used to decide which properties to load: NULL loads all keys, * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match. */ static void load_properties_from_file(const char* filename, const char* filter) { Timer t; std::string data; if (read_file(filename, &data)) { data.push_back('\n'); load_properties(&data[0], filter); } LOG(VERBOSE) << "(Loading properties from " << filename << " took " << t.duration() << "s.)"; }
void load_file(hashmap *map, char *filename) { FILE *file = 0; file = fopen(filename, "r"); if (file == NULL) { fprintf(stderr, "Error: Could open file: %s\n", filename); exit(EXIT_FAILURE); } load_properties(map, file); fclose(file); }
/* * Filter is used to decide which properties to load: NULL loads all keys, * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match. */ static void load_properties_from_file(const char *fn, const char *filter) { char *data; unsigned sz; data = read_file(fn, &sz); if(data != 0) { load_properties(data, filter); free(data); } }
static void load_properties_from_file(const char *fn) { char *data; unsigned sz; data = (char *)load_file(fn, &sz); if (data) { load_properties(data); free(data); } }
/* * Filter is used to decide which properties to load: NULL loads all keys, * "ro.foo.*" is a prefix match, and "ro.foo.bar" is an exact match. */ static void load_properties_from_file(const char *filename, const char *filter) { std::chrono::system_clock::time_point start = std::chrono::system_clock::now(); std::vector<unsigned char> data; if (mb::util::file_read_all(filename, &data)) { data.push_back('\n'); data.push_back('\0'); load_properties((char *) data.data(), filter); } std::chrono::system_clock::time_point end = std::chrono::system_clock::now(); std::chrono::duration<double> elapsed = end - start; LOGI("(Loading properties from %s took %.2fs.)", filename, elapsed.count()); }
/** * main * @argc: number of arguments * @argv: arguments * * Description: * main * * Returns: * exit code **/ int main (int argc, char *argv[]) { GtkWidget *errordialog; GtkWidget *vbox, *menubar, *toolbar, *statusbar, *gridframe; GtkUIManager *ui_manager; GOptionContext *context; struct timeval tv; gint i; gchar *config; gboolean retval; GError *error = NULL; setlocale (LC_ALL, ""); bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); games_scores_startup (); gettimeofday (&tv, NULL); srand (tv.tv_usec); context = g_option_context_new (NULL); g_option_context_set_translation_domain (context, GETTEXT_PACKAGE); g_option_context_add_group (context, gtk_get_option_group (TRUE)); g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE); retval = g_option_context_parse (context, &argc, &argv, &error); g_option_context_free (context); if (!retval) { g_print ("%s", error->message); g_error_free (error); exit (1); } g_set_application_name (_("Robots")); highscores = games_scores_new ("gnobots2", scorecats, G_N_ELEMENTS (scorecats), NULL, NULL, 0 /* default category */, GAMES_SCORES_STYLE_PLAIN_DESCENDING); settings = g_settings_new ("org.gnome.gnobots2"); gtk_window_set_default_icon_name ("gnobots2"); app = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_title (GTK_WINDOW (app), _("Robots")); g_signal_connect (GTK_WINDOW (app), "configure-event", G_CALLBACK (window_configure_event_cb), NULL); g_signal_connect (GTK_WINDOW (app), "window-state-event", G_CALLBACK (window_state_event_cb), NULL); gtk_window_set_default_size (GTK_WINDOW (app), g_settings_get_int (settings, "window-width"), g_settings_get_int (settings, "window-height")); if (g_settings_get_boolean (settings, "window-is-fullscreen")) gtk_window_fullscreen (GTK_WINDOW (app)); if (g_settings_get_boolean (settings, "window-is-maximized")) gtk_window_maximize (GTK_WINDOW (app)); g_signal_connect (G_OBJECT (app), "delete_event", G_CALLBACK (quit_game), NULL); statusbar = gnobots_statusbar_new (); ui_manager = gtk_ui_manager_new (); games_stock_prepare_for_statusbar_tooltips (ui_manager, statusbar); create_game_menus (ui_manager); gtk_window_add_accel_group (GTK_WINDOW (app), gtk_ui_manager_get_accel_group (ui_manager)); menubar = gtk_ui_manager_get_widget (ui_manager, "/MainMenu"); toolbar = gtk_ui_manager_get_widget (ui_manager, "/Toolbar"); gtk_style_context_add_class (gtk_widget_get_style_context (toolbar), GTK_STYLE_CLASS_PRIMARY_TOOLBAR); make_cursors (); game_area = gtk_drawing_area_new (); gtk_widget_add_events (game_area, GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK); g_signal_connect (G_OBJECT (game_area), "button-press-event", G_CALLBACK (mouse_cb), NULL); g_signal_connect (G_OBJECT (game_area), "motion-notify-event", G_CALLBACK (move_cb), NULL); g_signal_connect (G_OBJECT (game_area), "configure-event", G_CALLBACK (resize_cb), NULL); g_signal_connect (G_OBJECT (game_area), "draw", G_CALLBACK (draw_cb), NULL); gridframe = games_grid_frame_new (GAME_WIDTH, GAME_HEIGHT); gtk_container_add (GTK_CONTAINER (gridframe), game_area); vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); gtk_box_pack_start (GTK_BOX (vbox), menubar, FALSE, FALSE, 0); gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0); gtk_box_pack_start (GTK_BOX (vbox), gridframe, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (vbox), statusbar, FALSE, FALSE, 0); gtk_container_add (GTK_CONTAINER (app), vbox); gtk_widget_set_size_request (GTK_WIDGET (game_area), MINIMUM_TILE_WIDTH * GAME_WIDTH, MINIMUM_TILE_HEIGHT * GAME_HEIGHT); /* Set the window position if it was set by the session manager */ if (session_xpos >= 0 && session_ypos >= 0) { gtk_window_move (GTK_WINDOW (app), session_xpos, session_ypos); } gtk_widget_show_all (app); if (!load_game_configs ()) { /* Oops, no configs, we probably haven't been installed properly. */ errordialog = gtk_message_dialog_new_with_markup (NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "<b>%s</b>\n\n%s", _("No game data could be found."), _ ("The program Robots was unable to find any valid game configuration files. Please check that the program is installed correctly.")); gtk_window_set_resizable (GTK_WINDOW (errordialog), FALSE); gtk_dialog_run (GTK_DIALOG (errordialog)); exit (1); } load_properties (); if (!load_game_graphics ()) { /* Oops, no graphics, we probably haven't been installed properly. */ errordialog = gtk_message_dialog_new_with_markup (GTK_WINDOW (app), GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "<b>%s</b>\n\n%s", _ ("Some graphics files are missing or corrupt."), _ ("The program Robots was unable to load all the necessary graphics files. Please check that the program is installed correctly.")); gtk_dialog_run (GTK_DIALOG (errordialog)); exit (1); } connect_toolbar_toggle (toolbar); init_sound (); init_game (); if (cmdline_scenario) { set_game_graphics (cmdline_scenario); } if (cmdline_config) { for (i = 0; i < num_game_configs (); ++i) { config = game_config_name (i); if (!strcmp (cmdline_config, config)) { properties_set_config (i); g_free (config); break; } g_free (config); } } gtk_main (); g_settings_sync(); return 0; }
//! Every line should be one of the following: //! - \<property\> = \<value\> with optional comments at the end preceded by '#'. //! - blank //! - A commented line starting with a '#'. //! \param is An input stream (std::ifstream, std::iostream, or similar). //! \throws Genetic_AI_Creation_Error If there is an invalid line or an unexpected property void Gene::read_from(std::istream& is) { auto properties = list_properties(); std::string line; while(std::getline(is, line)) { line = String::strip_comments(line, "#"); if(line.empty()) { break; } auto split_line = String::split(line, ":"); if(split_line.size() != 2) { throw_on_invalid_line(line, "There should be exactly one colon per gene property line."); } auto property_name = String::trim_outer_whitespace(split_line[0]); auto property_data = String::trim_outer_whitespace(split_line[1]); if(property_name == "Name") { if(property_data == name()) { continue; } else { throw_on_invalid_line(line, "Reading data for wrong gene. Gene is " + name() + ", data is for " + property_data + "."); } } try { properties.at(property_name) = std::stod(property_data); } catch(const std::out_of_range&) { throw_on_invalid_line(line, "Unrecognized parameter."); } catch(const std::invalid_argument&) { throw_on_invalid_line(line, "Bad parameter value."); } } try { load_properties(properties); } catch(const std::out_of_range& err) { auto parameters = std::accumulate(properties.begin(), properties.end(), std::string{}, [](const auto& so_far, const auto& next) { return so_far + next.first + "\n"; }); throw Genetic_AI_Creation_Error("Bad parameter input for " + name() + "\n" + parameters + "\n" + err.what()); } }