void FiltersDialog::on_combobox_filters() { // Clear rules buffer. gtk_text_buffer_set_text(rulesbuffer, "", -1); // Get the name of the script. ustring filter = combobox_get_active_string(combobox_filters); // Set the type of the script. ScriptType scripttype; script_get_path(filter, &scripttype); gtk_label_set_text(GTK_LABEL(label_type), script_get_named_type(scripttype).c_str()); // Set sensitivity of widgets. bool editable = true; if (filter == scripts_straight_through()) editable = false; gtk_widget_set_sensitive(textview_rules, editable); gtk_widget_set_sensitive(button_delete, editable); // If not sensitive, bail out. if (!editable) return; // Load the script rules or code. ustring filename = script_get_path(filter, NULL); gchar *contents; g_file_get_contents(filename.c_str(), &contents, NULL, NULL); if (contents) { gtk_text_buffer_set_text(rulesbuffer, contents, -1); g_free(contents); } gtk_text_buffer_set_modified(rulesbuffer, false); }
void FiltersDialog::on_button_try() { // Iterators. GtkTextIter startiter, enditer; // Input text. GtkTextBuffer *inputbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview_input)); gtk_text_buffer_get_start_iter(inputbuffer, &startiter); gtk_text_buffer_get_end_iter(inputbuffer, &enditer); ustring inputfile = script_temporal_input_file(); gchar *txt = gtk_text_buffer_get_text(inputbuffer, &startiter, &enditer, false); g_file_set_contents(inputfile.c_str(), txt, -1, NULL); g_free(txt); // Postiff: plug memory leak // Filter. ustring scriptname = combobox_get_active_string(combobox_filters); bool straightthrough = scriptname == scripts_straight_through(); // Output file. ustring outputfile = script_temporal_output_file(); // Run filter. ustring error = script_filter(scriptname, straightthrough, inputfile, outputfile); // Show output in textview. gchar *outputtext; g_file_get_contents(outputfile.c_str(), &outputtext, NULL, NULL); GtkTextBuffer *outputbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview_output)); if (outputtext) { gtk_text_buffer_set_text(outputbuffer, outputtext, -1); g_free(outputtext); } else { gtk_text_buffer_set_text(outputbuffer, "", -1); } // If script failed, give the error for debugging purposes. if (!error.empty()) { gtk_text_buffer_set_text(outputbuffer, error.c_str(), -1); } // If there were compile errors before, show these. if (!compile_errors.empty()) { gtk_text_buffer_set_text(outputbuffer, "", -1); for (unsigned int i = 0; i < compile_errors.size(); i++) { gtk_text_buffer_insert_at_cursor(outputbuffer, compile_errors[i].c_str(), -1); gtk_text_buffer_insert_at_cursor(outputbuffer, "\n", -1); } } }
vector < ustring > scripts_get_all() // Gets a list of available scripts. { // Read available scripts and clean them up. ReadFiles rf(Directories->get_scripts(), script_prefix(stEnd), ""); for (unsigned int i = 0; i < rf.files.size(); i++) { for (int scripttype = stSed; scripttype < stEnd; scripttype++) { ustring prefix = script_prefix((ScriptType) scripttype); ustring source_suffix = script_suffix((ScriptType) scripttype, false); ustring binary_suffix = script_suffix((ScriptType) scripttype, true); if (g_str_has_prefix(rf.files[i].c_str(), prefix.c_str())) { rf.files[i].erase(0, prefix.length()); } if (!source_suffix.empty()) { if (g_str_has_suffix(rf.files[i].c_str(), source_suffix.c_str())) { rf.files[i].erase(rf.files[i].length() - source_suffix.length(), source_suffix.length()); } } if (!binary_suffix.empty()) { if (g_str_has_suffix(rf.files[i].c_str(), binary_suffix.c_str())) { rf.files[i].erase(rf.files[i].length() - binary_suffix.length(), binary_suffix.length()); } } } } // In cases of TECkit, there is the .map script and the compiled .tec script. // These both have the same name, so we have two scripts of the same name. // Remove any double names. set < ustring > scriptset(rf.files.begin(), rf.files.end()); vector < ustring > scripts(scriptset.begin(), scriptset.end()); // Sort them. sort(scripts.begin(), scripts.end()); // Add the "straight through" script at the start. scripts.insert(scripts.begin(), scripts_straight_through()); // Return them. return scripts; }
void FiltersDialog::on_rulesbuffer_changed_execute() { // Bail out if there's no change in the rules buffer. if (!gtk_text_buffer_get_modified(rulesbuffer)) return; // Get the name of the script. Bail out if straight throug. ustring scriptname = combobox_get_active_string(combobox_filters); if (scriptname == scripts_straight_through()) return; // Get the filename and type of the script. ScriptType scripttype; ustring scriptfile = script_get_path(scriptname, &scripttype); // Save the rules to the script file. GtkTextIter startiter, enditer; gtk_text_buffer_get_start_iter(rulesbuffer, &startiter); gtk_text_buffer_get_end_iter(rulesbuffer, &enditer); gchar *txt = gtk_text_buffer_get_text(rulesbuffer, &startiter, &enditer, false); g_file_set_contents(scriptfile.c_str(), txt, -1, NULL); g_free(txt); // Postiff: plug memory leak // If it is a TECkit mapping, compile it. if (scripttype == stTECkit) { compile_errors.clear(); GwSpawn spawn("teckit_compile"); spawn.workingdirectory(Directories->get_scripts()); spawn.arg(scriptfile); // To compile UTF-8 source that lacks an encoding signature, the -u flag must be specified on the compiler command line. spawn.arg("-u"); spawn.run(); if (spawn.exitstatus != 0) { ustring tecfile = script_get_path(scriptname, scripttype, true); unix_unlink(tecfile.c_str()); spawn.read(); spawn.run(); compile_errors = spawn.standarderr; } } }