static void child_watch_cb (GPid pid, gint status, gpointer user_data) { SpawnData *data = user_data; gchar *buf; gsize buf_size; if (g_io_channel_read_to_end (data->child_stdout_channel, &buf, &buf_size, NULL) == G_IO_STATUS_NORMAL) { g_string_append_len (data->child_stdout, buf, buf_size); g_free (buf); } if (g_io_channel_read_to_end (data->child_stderr_channel, &buf, &buf_size, NULL) == G_IO_STATUS_NORMAL) { g_string_append_len (data->child_stderr, buf, buf_size); g_free (buf); } data->exit_status = status; /* ok, child watch is history, make sure we don't free it in spawn_data_free() */ data->child_pid = 0; data->child_watch_source = NULL; /* we're done */ g_simple_async_result_complete_in_idle (data->simple); g_object_unref (data->simple); }
static char * dclock_get_timezones(DClockPlugin * dc) { if (!dc->timezones) { FILE * fpipe; char * command = "grep -v '^#' /usr/share/zoneinfo/zone.tab | awk '{print $3}' | sort"; if ( !(fpipe = popen(command,"r")) ) { return NULL; } GIOChannel * channel = g_io_channel_unix_new(fileno(fpipe)); if (channel) { gchar * data; gsize data_size; GIOStatus status = g_io_channel_read_to_end(channel, &data, &data_size, NULL); if (status == G_IO_STATUS_NORMAL) { dc->timezones = g_strdup_printf("\n%s", data); g_free(data); } g_io_channel_unref(channel); } pclose(fpipe); } return dc->timezones; }
void xg_io_channel_read_to_end (GIOChannel *channel, gchar **str_return, gsize *length) { GIOStatus s; GError *e = NULL; const char *nm = "(no message)"; s = g_io_channel_read_to_end (channel, str_return, length, &e); switch (s) { case G_IO_STATUS_ERROR: g_critical ("g_io_channel_read_to_end returned ERROR: %s", (e && e->message) ? e->message : nm); break; case G_IO_STATUS_EOF: g_critical ("g_io_channel_read_to_end returned EOF: %s", (e && e->message) ? e->message : nm); break; case G_IO_STATUS_AGAIN: case G_IO_STATUS_NORMAL: // g_debug ("g_io_channel_read_to_end returned %d bytes", *length); break; } }
/* Read count_bytes from the channel and write them to the loader. * Returns number of bytes written. * count_bytes = G_MAXSIZE means read as many bytes as possible. */ static gsize loader_write_from_channel (GdkPixbufLoader *loader, GIOChannel *channel, gsize count_bytes) { guchar* buffer; gsize bytes_read; GIOStatus read_status; if(count_bytes < G_MAXSIZE) { /* read no more than 'count_bytes' bytes */ buffer = g_malloc(count_bytes); read_status = g_io_channel_read_chars (channel, (gchar*) buffer, count_bytes, &bytes_read, NULL); } else { /*read up to end */ read_status = g_io_channel_read_to_end (channel, (gchar**) &buffer, &bytes_read, NULL); } if ((read_status != G_IO_STATUS_NORMAL) && (read_status != G_IO_STATUS_EOF)) g_assert_not_reached (); if (!gdk_pixbuf_loader_write(loader, buffer, bytes_read, NULL)) g_assert_not_reached (); g_free (buffer); return bytes_read; }
//--------------------------------------------------------------------------- string text_string_from_file( char const *filename ) { string out_str = NULL; GError* err = NULL; boolean processing = True; while( processing == True ) { GIOChannel* in_file = g_io_channel_new_file( filename , "r" , &err ); if( !in_file ) { fprintf( stderr , "failed to open file '%s'. \n)", filename ); processing = False; break; } if( g_io_channel_read_to_end( in_file , &out_str , NULL , &err ) != G_IO_STATUS_NORMAL ) { fprintf( stderr , "found file '%s' but could not read it.\n)", filename ); processing = False; break; } processing = False; } return out_str; }
const char* nsRetrievalContextWayland::GetClipboardData(const char* aMimeType, int32_t aWhichClipboard, uint32_t* aContentLength) { NS_ASSERTION(mDataOffer, "Requested data without valid data offer!"); if (!mDataOffer) { // TODO // Something went wrong. We're requested to provide clipboard data // but we haven't got any from wayland. Looks like rhbz#1455915. return nullptr; } int pipe_fd[2]; if (pipe(pipe_fd) == -1) return nullptr; wl_data_offer_receive(mDataOffer, aMimeType, pipe_fd[1]); close(pipe_fd[1]); wl_display_flush(mDisplay); struct pollfd fds; fds.fd = pipe_fd[0]; fds.events = POLLIN; // Choose some reasonable timeout here int ret = poll(&fds, 1, kClipboardTimeout / 1000); if (!ret || ret == -1) { close(pipe_fd[0]); return nullptr; } GIOChannel *channel = g_io_channel_unix_new(pipe_fd[0]); GError* error = nullptr; gchar *clipboardData = nullptr; gsize dataLength = 0; g_io_channel_set_encoding(channel, nullptr, &error); if (!error) { g_io_channel_read_to_end(channel, &clipboardData, &dataLength, &error); } if (error) { NS_WARNING( nsPrintfCString("Unexpected error when reading clipboard data: %s", error->message).get()); g_error_free(error); } g_io_channel_unref(channel); close(pipe_fd[0]); *aContentLength = dataLength; return reinterpret_cast<const char*>(clipboardData); }
static gboolean handle_request(GIOChannel* gio, GIOCondition condition, ClientInfo* info) { LXTermWindow * lxtermwin = info->lxtermwin; int fd = info->fd; /* Read message. */ gchar * msg = NULL; gsize len = 0; GError * err = NULL; GIOStatus ret = g_io_channel_read_to_end(gio, &msg, &len, &err); if (ret == G_IO_STATUS_ERROR) { g_warning("Error reading socket: %s\n", err->message); } /* Process message. */ if (len > 0) { /* Skip the the first (cur_dir) and last '\0' for argument count */ gint argc = -1; gsize i; for (i = 0; i < len; i ++) { if (msg[i] == '\0') { argc ++; } } gchar * cur_dir = msg; gchar * * argv = g_malloc(argc * sizeof(char *)); gint nul_count = 0; for (i = 0; i < len; i ++) { if (msg[i] == '\0' && nul_count < argc) { argv[nul_count] = &msg[i + 1]; nul_count ++; } } /* Parse arguments. * Initialize a new LXTerminal and create a new window. */ CommandArguments arguments; lxterminal_process_arguments(argc, argv, &arguments); g_free(argv); /* Make sure working directory matches that of the client process */ if (arguments.working_directory == NULL) { arguments.working_directory = g_strdup(cur_dir); } lxterminal_initialize(lxtermwin, &arguments); } if (condition & G_IO_HUP) { g_free(msg); g_free(info); close(fd); return FALSE; } return TRUE; }
gboolean ForkExecParent::outputReady(GIOChannel *source, GIOCondition condition, gpointer data) throw () { bool cont = true; try { ForkExecParent *me = static_cast<ForkExecParent *>(data); gchar *buffer = NULL; gsize length = 0; GErrorCXX error; // Try reading, even if the condition wasn't G_IO_IN. GIOStatus status = g_io_channel_read_to_end(source, &buffer, &length, error); if (buffer && length) { if (source == me->m_out) { me->m_onStdout(buffer, length); } else if (me->m_mergedStdoutStderr) { me->m_onOutput(buffer, length); } else { me->m_onStderr(buffer, length); } } if (status == G_IO_STATUS_EOF || (condition & (G_IO_HUP|G_IO_ERR)) || error) { SE_LOG_DEBUG(NULL, NULL, "reading helper %s done: %s", source == me->m_out ? "stdout" : me->m_mergedStdoutStderr ? "combined stdout/stderr" : "stderr", (const char *)error); // Will remove event source from main loop. cont = false; // Free channel. if (source == me->m_out) { me->m_out = NULL; } else { me->m_err = NULL; } g_io_channel_unref(source); // Send delayed OnQuit signal now? me->checkCompletion(); } // If an exception skips this, we are going to die, in // which case we don't care about the leak. g_free(buffer); } catch (...) { Exception::handle(HANDLE_EXCEPTION_FATAL); } return cont; }
static gchar *ReaderThread(GIOChannel *channel) { gchar *result = NULL; gsize result_len = 0; GError *error = NULL; GIOStatus status = g_io_channel_read_to_end(channel, &result, &result_len, &error); g_assert_no_error(error); g_assert_cmpint(G_IO_STATUS_NORMAL, ==, status); g_assert_cmpint(result_len, ==, strlen(result)); g_io_channel_shutdown(channel, FALSE, NULL); return result; }
static void child_watch_cb (GPid pid, gint status, gpointer user_data) { UlSpawnedJob *self = UL_SPAWNED_JOB (user_data); gchar *buf; gsize buf_size; gboolean ret; if (g_io_channel_read_to_end (self->child_stdout_channel, &buf, &buf_size, NULL) == G_IO_STATUS_NORMAL) { g_string_append_len (self->child_stdout, buf, buf_size); g_free (buf); } if (g_io_channel_read_to_end (self->child_stderr_channel, &buf, &buf_size, NULL) == G_IO_STATUS_NORMAL) { g_string_append_len (self->child_stderr, buf, buf_size); g_free (buf); } /* take a reference so it's safe for a signal-handler to release the last one */ g_object_ref (self); g_signal_emit (self, signals[SPAWNED_JOB_COMPLETED_SIGNAL], 0, NULL, /* GError */ status, self->child_stdout, self->child_stderr, &ret); self->child_pid = 0; self->child_watch_source = NULL; udisks_spawned_job_release_resources (self); g_object_unref (self); }
static void menu_about (void) { GError *error = NULL; GIOChannel *channel = NULL; channel = g_io_channel_new_file ("licence.txt", "r", &error); if (channel != NULL) { char *text = NULL; if (g_io_channel_read_to_end (channel, &text, 0, &error) == G_IO_STATUS_NORMAL) { GtkWidget *p_dialog = gtk_about_dialog_new (); gtk_about_dialog_set_name (GTK_ABOUT_DIALOG (p_dialog), "Exemple d'utilisation de GtkUIManager"); gtk_about_dialog_set_copyright (GTK_ABOUT_DIALOG (p_dialog), "Nicolas JOSEPH"); gtk_about_dialog_set_license (GTK_ABOUT_DIALOG (p_dialog), text); gtk_about_dialog_set_website (GTK_ABOUT_DIALOG (p_dialog), "http://nicolasj.developpez.com"); gtk_dialog_run (GTK_DIALOG (p_dialog)); g_free (text); if (g_io_channel_shutdown (channel, FALSE, &error) == G_IO_STATUS_ERROR) { GtkWidget *p_dialog = NULL; gtk_dialog_run (GTK_DIALOG (p_dialog)); gtk_widget_destroy (p_dialog); } } } if (error != NULL) { GtkWidget *p_dialog = NULL; gtk_dialog_run (GTK_DIALOG (p_dialog)); gtk_widget_destroy (p_dialog); } return; }
static gboolean attr_changed (GIOChannel *channel, GIOCondition cond, gpointer user_data) { UDisksLinuxMDRaidObject *object = UDISKS_LINUX_MDRAID_OBJECT (user_data); gboolean bail = FALSE; GError *error = NULL; gchar *str = NULL; gsize len = 0; if (cond & ~G_IO_ERR) goto out; if (g_io_channel_seek_position (channel, 0, G_SEEK_SET, &error) != G_IO_STATUS_NORMAL) { udisks_debug ("Error seeking in channel (uuid %s): %s (%s, %d)", object->uuid, error->message, g_quark_to_string (error->domain), error->code); g_clear_error (&error); bail = TRUE; goto out; } if (g_io_channel_read_to_end (channel, &str, &len, &error) != G_IO_STATUS_NORMAL) { udisks_debug ("Error reading (uuid %s): %s (%s, %d)", object->uuid, error->message, g_quark_to_string (error->domain), error->code); g_clear_error (&error); bail = TRUE; goto out; } g_free (str); /* synthesize uevent */ if (object->raid_device != NULL) udisks_linux_mdraid_object_uevent (object, "change", object->raid_device, FALSE); out: if (bail) remove_watches (object); return TRUE; /* keep event source around */ }
char * jb_read_file (const char *filename, GError **err) { GIOChannel *channel; char *contents = NULL; gsize length; g_return_val_if_fail(filename != NULL, FALSE); channel = g_io_channel_new_file(filename, "r", err); if (channel == NULL) return NULL; g_io_channel_read_to_end(channel, &contents, &length, err); g_io_channel_shutdown(channel, FALSE, NULL); g_io_channel_unref(channel); return contents; }
static gboolean terminateProcessIO (GIOChannel *channel, GIOCondition condition, gpointer data) { Client *c; char *str; gsize len; GError *err; c = (Client *) data; g_return_val_if_fail (c != NULL, FALSE); str = NULL; len = 0; err = NULL; if (condition & G_IO_IN) { g_io_channel_read_to_end (channel, &str, &len, &err); if (err) { g_warning (_("Error reading data from child process: %s\n"), err->message); g_error_free (err); } if (len > 0) { if (!g_ascii_strncasecmp (str, "yes", 3)) { clientTerminate (c); } } g_free (str); } terminateCloseDialog (c); return FALSE; }
gboolean HttpClient::on_io_in_event(GIOChannel *ch, GIOCondition cond, gpointer user_data) { HttpClient *http_client = static_cast<HttpClient *>(user_data); if (cond & G_IO_ERR) { on_error_.emit(http_client, "Http client error!"); return FALSE; } if (cond & G_IO_STATUS_EOF) { on_response_.emit(http_client); return FALSE; } GIOStatus res; gchar *str_return = NULL; gsize length; GError *err = NULL; res = g_io_channel_read_to_end(http_client->channel_, &str_return, &length, &err); if (err) { gchar *str = g_strdup_printf("Error while reading reply from server: %s", err->message); on_error_.emit(http_client, str); g_free(str); g_error_free(err); return FALSE; } if (str_return) { http_client->buffer = (char *)g_realloc(http_client->buffer, http_client->buffer_len + length); memcpy(http_client->buffer+http_client->buffer_len, str_return, length); http_client->buffer_len += length; g_free(str_return); } if (res == G_IO_STATUS_NORMAL) { http_client->buffer = (char *)g_realloc(http_client->buffer, http_client->buffer_len + 1); http_client->buffer[http_client->buffer_len] = '\0'; // So the text is end by a extra '\0'. on_response_.emit(http_client); return FALSE; } return TRUE; }
/* Result must be freed */ X264_Gtk * x264_gtk_load (void) { X264_Gtk *x264_gtk; GIOChannel *file; GError *error = NULL; gchar *filename; x264_gtk = (X264_Gtk *)g_malloc0 (sizeof (X264_Gtk)); if (!x264_gtk) return NULL; filename = x264_gtk_path ("x264.cfg"); file = g_io_channel_new_file (filename, "r", &error); if (error) { g_print (_("x264.cfg: %s\n"), error->message); g_print (_("Loading default configuration\n")); _default_set (x264_gtk); } else { GIOStatus status; gchar *data = NULL; gsize length; g_print (_("Loading configuration from %s\n"), filename); g_io_channel_set_encoding (file, NULL, NULL); status = g_io_channel_read_to_end (file, &data, &length, &error); if ((status == G_IO_STATUS_NORMAL) && (length == sizeof (X264_Gtk))) { memcpy (x264_gtk, data, length); } g_io_channel_shutdown (file, TRUE, NULL); g_io_channel_unref (file); } g_free (filename); return x264_gtk; }
static void tooltip_set() { gchar *argv[] = { LIBEXECDIR "/fbxkb/options.sh", NULL }; gchar *text; gint standard_output; gsize len; GIOChannel *gio; ENTER; if (!g_spawn_async_with_pipes(NULL, argv, NULL, 0, NULL, NULL, NULL, NULL, &standard_output, NULL, NULL)) RET(); gio = g_io_channel_unix_new (standard_output); if (g_io_channel_read_to_end(gio, &text, &len, NULL) == G_IO_STATUS_NORMAL) { g_strchomp(text); gtk_status_icon_set_tooltip_markup(icon, text); } g_io_channel_shutdown(gio, FALSE, NULL); g_free(text); RET(); }
static void variant_reader_watch_child (GPid pid, gint status, gpointer user_data) { struct VariantReaderData *data = user_data; guint8 *buf; gsize buf_size; GVariant *result; GError *error = NULL; data->pid = 0; if (!g_spawn_check_exit_status (status, &error)) { data->callback (pid, NULL, error, data->user_data); g_error_free (error); g_byte_array_free (data->output, TRUE); } else { if (g_io_channel_read_to_end (data->output_channel, (gchar **)&buf, &buf_size, NULL) == G_IO_STATUS_NORMAL) { g_byte_array_append (data->output, buf, buf_size); g_free (buf); } result = g_variant_new_from_data (data->type, data->output->data, data->output->len, TRUE, g_free, NULL); g_byte_array_free (data->output, FALSE); data->callback (pid, result, NULL, data->user_data); g_variant_unref (result); } }
static VALUE rg_read(gint argc, VALUE *argv, VALUE self) { VALUE rbcount; gsize count; gchar *buffer; gsize bytes_read; GIOChannel *channel = _SELF(self); GError *error = NULL; GIOStatus status; rb_scan_args(argc, argv, "01", &rbcount); if (NIL_P(rbcount)) { status = g_io_channel_read_to_end(channel, &buffer, &bytes_read, &error); ioc_error(status, error); return buffer != NULL ? CSTR2RVAL_LEN_FREE(buffer, bytes_read) : CSTR2RVAL(""); } count = NUM2UINT(rbcount); buffer = g_new(gchar, count); memset(buffer, '\0', count); status = g_io_channel_read_chars(channel, buffer, count, &bytes_read, &error); if (status == G_IO_STATUS_NORMAL) return CSTR2RVAL_LEN_FREE(buffer, bytes_read); else if (status == G_IO_STATUS_EOF) return CSTR2RVAL(""); ioc_error(status, error); /* Not reached. */ return Qnil; }
void menu_open_activate_cb (GtkWidget *widget, gpointer data) { queue_t *list; GtkWidget *fs; GIOChannel *channel; GError *error = NULL; GIOStatus status; gchar *filename, *buffer, *tmp, *tmp2, *title; fs = gtk_file_chooser_dialog_new ("Open File", GTK_WINDOW (window_main->window), GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL); if (gtk_dialog_run (GTK_DIALOG (fs)) != GTK_RESPONSE_OK) { gtk_widget_destroy (fs); return; } filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (fs)); gtk_widget_destroy (fs); channel = g_io_channel_new_file (filename, "r", NULL); status = g_io_channel_read_to_end (channel, &buffer, NULL, &error); if (status != G_IO_STATUS_NORMAL) { tmp = g_path_get_basename (filename); fs = gtk_message_dialog_new (GTK_WINDOW (window_main->window), GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s: %s", tmp, error->message); gtk_dialog_run (GTK_DIALOG (fs)); gtk_widget_destroy (fs); g_free (tmp); g_error_free (error); } else { list = read_rbfsum2 (buffer); g_free (buffer); if (!list) { fs = gtk_message_dialog_new (GTK_WINDOW (window_main->window), GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "Format file tidak valid"); gtk_dialog_run (GTK_DIALOG (fs)); gtk_widget_destroy (fs); return; } set_treeview_station (list); station_list = list; tmp = filename; if ((tmp2 = strrchr (filename, '/'))) tmp = ++tmp2; title = g_strdup_printf ("%s - RBFSUM Utility", tmp); gtk_window_set_title (GTK_WINDOW (window_main->window), title); g_free (title); } g_free (filename); g_io_channel_shutdown (channel, FALSE, NULL); g_io_channel_unref (channel); }
gchar * __pkey_retrieve_from_file (gchar **fn, gchar *cert_pem) { gsize file_length = 0; GError *error = NULL; gboolean cancel = FALSE; gboolean save_new_filename = FALSE; gchar *file_name = g_strdup(* fn); gchar *file_contents = NULL; gchar *pem_pkey = NULL; gint tls_error = 0; gchar *password = NULL; TlsCert *cert = tls_parse_cert_pem (cert_pem); do { if (g_file_test(file_name, G_FILE_TEST_EXISTS)) { GIOChannel *gc = g_io_channel_new_file (file_name, "r", &error); if (gc) { g_io_channel_read_to_end (gc, &file_contents, &file_length, &error); g_io_channel_shutdown (gc, TRUE, NULL); do { pem_pkey = tls_load_pkcs8_private_key (file_contents, password, cert->key_id, &tls_error); if (tls_error == TLS_INVALID_PASSWORD) { if (password) dialog_error (_("The given password doesn't match with the one used while crypting the file.")); // We ask for a password password = __pkey_manage_ask_external_file_password (cert->dn); if (! password) cancel = TRUE; } } while (tls_error == TLS_INVALID_PASSWORD && ! cancel); g_free (password); if (! pem_pkey) { if (tls_error == TLS_NON_MATCHING_PRIVATE_KEY) { // The file could be opened, but it didn't contain any recognized private key dialog_error (_("The file designated in database contains a private key, but it " "is not the private key corresponding to the certificate.")); } else { // The file could be opened, but it didn't contain any recognized private key dialog_error (_("The file designated in database doesn't contain any recognized private key.")); } } } else { // The file cannot be opened dialog_error (_("The file designated in database couldn't be opened.")); } } else { // The file doesn't exist dialog_error (_("The file designated in database doesn't exist.")); } if (! pem_pkey && ! cancel) { #ifndef GNOMINTCLI // Show file open dialog GObject * widget = NULL, * filepath_widget = NULL, *remember_filepath_widget = NULL; GtkBuilder * dialog_gtkb = NULL; gint response = 0; dialog_gtkb = gtk_builder_new(); gtk_builder_add_from_file (dialog_gtkb, g_build_filename (PACKAGE_DATA_DIR, "gnomint", "get_pkey_dialog.ui", NULL), NULL); gtk_builder_connect_signals (dialog_gtkb, NULL); filepath_widget = gtk_builder_get_object (dialog_gtkb, "pkey_filechooser"); remember_filepath_widget = gtk_builder_get_object (dialog_gtkb, "save_filename_checkbutton"); g_object_set (G_OBJECT(remember_filepath_widget), "visible", FALSE, NULL); gtk_widget_grab_focus (GTK_WIDGET(filepath_widget)); gtk_file_chooser_set_filename (GTK_FILE_CHOOSER(filepath_widget), file_name); g_object_set_data (G_OBJECT(filepath_widget), "save_filename_checkbutton", remember_filepath_widget); widget = gtk_builder_get_object (dialog_gtkb, "cert_dn_label"); gtk_label_set_text (GTK_LABEL(widget), cert->dn); widget = gtk_builder_get_object (dialog_gtkb, "get_pkey_dialog"); response = gtk_dialog_run(GTK_DIALOG(widget)); if (! response) { cancel = TRUE; } else { g_free (file_name); file_name = g_strdup ((gchar *) gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(filepath_widget))); save_new_filename = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(remember_filepath_widget)); } widget = gtk_builder_get_object (dialog_gtkb, "get_pkey_dialog"); gtk_widget_destroy (GTK_WIDGET(widget)); g_object_unref (G_OBJECT(dialog_gtkb)); #else cancel = TRUE; #endif } } while (! pem_pkey && ! cancel); tls_cert_free (cert); g_free (file_contents); if (error) g_error_free (error); if (cancel) { g_free (file_name); return NULL; } if (save_new_filename) { g_free (*fn); (* fn) = file_name; } return pem_pkey; }
/*****--- Callbacks ---*****/ static void log_file_changed_callback(GFileMonitor *mon,GFile *file,GFile *other,GFileMonitorEvent event,gpointer data) { FacqLogWindow *log_window = FACQ_LOG_WINDOW(data); gchar *log_content = NULL, **read_lines = NULL; gsize log_size; GError *local_err = NULL; guint n_written_lines = 0, n_read_lines = 0, n_prev_lines = 0, n_empty_lines = 0; GtkTextBuffer *text_buffer = NULL; GtkTextIter endIter; if( g_io_channel_read_to_end(log_window->priv->log, &log_content, &log_size,&local_err) != G_IO_STATUS_NORMAL){ if(local_err){ g_printerr("%s\n",local_err->message); g_clear_error(&local_err); } if(log_content) g_free(log_content); return; } text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(log_window->priv->text_view)); n_written_lines = gtk_text_buffer_get_line_count(text_buffer); read_lines = g_strsplit(log_content,"\n",0); n_read_lines = g_strv_length(read_lines); n_empty_lines = log_window->priv->lines - n_written_lines; if(n_empty_lines >= n_read_lines){ /* append the read_lines lines to the end */ facq_log_file_append_to_text_buffer(text_buffer,log_content); } else { if(n_read_lines >= log_window->priv->lines){ /* update the buffer with the last priv->lines lines no * need to store previous lines. */ facq_log_file_replace_text_buffer(text_buffer, read_lines, n_read_lines, log_window->priv->lines); } else { /* store the last previous lines from the buffer */ n_prev_lines = log_window->priv->lines - n_read_lines; /* mix the last lines with the new lines and update the * buffer */ facq_log_file_update_text_buffer(text_buffer,log_content,n_prev_lines); } } /* scroll to end */ gtk_text_buffer_get_end_iter(text_buffer,&endIter); gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(log_window->priv->text_view), &endIter, 0.0,FALSE,0.0,0.0); g_free(log_content); g_strfreev(read_lines); }
static void facq_log_window_constructed(GObject *self) { gchar *window_title = NULL; GtkWidget *window = NULL; GError *local_err = NULL; gchar *log_content = NULL; gsize log_size = 0; FacqLogWindow *log_window = FACQ_LOG_WINDOW(self); /* create a new #GIOChannel for reading the log file */ log_window->priv->log = g_io_channel_new_file(log_window->priv->filename,"r",&local_err); if(local_err) goto error; /* read the file to the end */ if( g_io_channel_read_to_end(log_window->priv->log, &log_content, &log_size, &local_err) != G_IO_STATUS_NORMAL ){ g_io_channel_unref(log_window->priv->log); log_window->priv->log = NULL; goto error; } /* set \n as line term */ g_io_channel_set_line_term(log_window->priv->log,"\n",-1); /* create the file monitor, first we create a #GFile. */ log_window->priv->file = g_file_new_for_path(log_window->priv->filename); log_window->priv->mon = g_file_monitor(log_window->priv->file, G_FILE_MONITOR_NONE, NULL, &local_err); /* create the text view and put the last lines, lines, in buffer */ log_window->priv->text_view = gtk_text_view_new(); gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(log_window->priv->text_view), FALSE); gtk_text_view_set_justification(GTK_TEXT_VIEW(log_window->priv->text_view), GTK_JUSTIFY_LEFT); gtk_text_view_set_editable(GTK_TEXT_VIEW(log_window->priv->text_view), FALSE); facq_log_window_from_log_to_text_buffer(log_window,log_content); /* connect the file monitor with the callback function, every time the * file changes the callback will be called*/ g_signal_connect(log_window->priv->mon,"changed", G_CALLBACK(log_file_changed_callback),log_window); /* create a scrolled window and append the text view to it*/ window = gtk_scrolled_window_new(NULL,NULL); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(window),GTK_SHADOW_NONE); log_window->priv->scrolled_window = window; gtk_container_add(GTK_CONTAINER(window),log_window->priv->text_view); /* Main log window, this window contains all the other components * and will have a title according to your main application window title */ window = gtk_window_new(GTK_WINDOW_TOPLEVEL); if(gtk_window_get_title(GTK_WINDOW(log_window->priv->top_window))) window_title = g_strdup_printf("%s Log", gtk_window_get_title(GTK_WINDOW(log_window->priv->top_window))); else window_title = g_strdup_printf("Log Window"); gtk_window_set_title(GTK_WINDOW(window),window_title); g_free(window_title); gtk_window_set_destroy_with_parent(GTK_WINDOW(window),TRUE); gtk_window_set_transient_for(GTK_WINDOW(window),GTK_WINDOW(log_window->priv->top_window)); gtk_container_add(GTK_CONTAINER(window),log_window->priv->scrolled_window); g_signal_connect(window,"delete-event", G_CALLBACK(delete_event),log_window); /* ready to go */ gtk_widget_show_all(window); log_window->priv->window = window; return; error: if(local_err) g_propagate_error(&log_window->priv->construct_error,local_err); return; }
void ref_meth_counts_add_path (RefMethCounts *counts, SeqDB *ref, const char *path, GError **error) { GIOChannel *channel; SeqDBElement *elem = NULL; GError *tmp_error = NULL; char *buffer = NULL; gsize length = 0; gsize j = 0; gsize i; /* Open */ channel = g_io_channel_new_file (path, "r", &tmp_error); if (tmp_error) { g_propagate_error (error, tmp_error); return; } g_io_channel_read_to_end (channel, &buffer, &length, &tmp_error); if (tmp_error) { g_propagate_error (error, tmp_error); if (buffer) g_free (buffer); return; } for (i = 0; i < length; ) { if (buffer[i] == '>') { for (j = i + 1; j < length && buffer[j] != '\n'; j++); buffer[j] = '\0'; elem = g_hash_table_lookup (ref->index, buffer + i + 1); if (!elem) g_printerr ("[WARNING] Reference `%s' not found\n", buffer + i + 1); } else if (elem) { unsigned long offset; gsize starts[3] = {0, 0, 0}; int field_idx = 0; for (j = i; j < length && buffer[j] != '\n'; j++) { if (buffer[j] == '\t') { buffer[j] = '\0'; if (field_idx < 3) starts[field_idx] = j + 1; ++field_idx; } } buffer[j] = '\0'; if (field_idx == 2) { offset = g_ascii_strtoll (buffer + i, NULL, 10); counts->meth_index[elem->offset + offset]->n_meth += g_ascii_strtoll (buffer + starts[0], NULL, 10); counts->meth_index[elem->offset + offset]->n_unmeth += g_ascii_strtoll (buffer + starts[1], NULL, 10); } else if (field_idx == 3) { offset = g_ascii_strtoll (buffer + starts[0], NULL, 10); counts->meth_index[elem->offset + offset]->n_meth += g_ascii_strtoll (buffer + starts[1], NULL, 10); counts->meth_index[elem->offset + offset]->n_unmeth += g_ascii_strtoll (buffer + starts[2], NULL, 10); } else if (field_idx != 0) g_printerr ("[WARNING] Could not parse meth count line\n"); } i = j + 1; } if (buffer) g_free (buffer); /* Close */ g_io_channel_shutdown (channel, TRUE, &tmp_error); if (tmp_error) { g_printerr ("[WARNING] Closing meth count file `%s' failed: %s\n", path, tmp_error->message); g_error_free (tmp_error); } g_io_channel_unref (channel); }
#include <string.h> #include <stdio.h> #include "irc.h" #include "testsuite.h" START_TEST(test_connect) GIOChannel *ch1, *ch2; irc_t *irc; char *raw; fail_unless(g_io_channel_pair(&ch1, &ch2)); irc = irc_new(g_io_channel_unix_get_fd(ch1)); irc_free(irc); fail_unless(g_io_channel_read_to_end(ch2, &raw, NULL, NULL) == G_IO_STATUS_NORMAL); fail_if(strcmp(raw, "") != 0); g_free(raw); END_TEST START_TEST(test_login) GIOChannel *ch1, *ch2; irc_t *irc; GError *error = NULL; char *raw; fail_unless(g_io_channel_pair(&ch1, &ch2)); g_io_channel_set_flags(ch1, G_IO_FLAG_NONBLOCK, NULL); g_io_channel_set_flags(ch2, G_IO_FLAG_NONBLOCK, NULL);
/** * midgard_blob_read_content: * @self: MidgardBlob self instance * @bytes_read: number of bytes read * * Returned content should be freed when no longer needed. * @bytes_read holds size of returned content. * * This function should be used to get content of small files. * For large and huge ones midgard_blob_get_handler should be used * to get file handle. * * Returns: content of the file, or %NULL on failure */ gchar *midgard_blob_read_content(MidgardBlob *self, gsize *bytes_read) { g_assert(self != NULL); MidgardConnection *mgd = self->priv->mgd; MIDGARD_ERRNO_SET(mgd, MGD_ERR_OK); __get_filepath(self); if(!self->priv->filepath) { midgard_set_error(mgd, MGD_GENERIC_ERROR, MGD_ERR_USER_DATA, "Invalid attachment. " "Can not read file from empty location"); return NULL; } __get_channel(self, "r"); if(!self->priv->channel) return NULL; GIOChannel *channel = self->priv->channel; gchar *content = NULL; GError *err = NULL; GIOStatus status; const gchar *err_msg = ""; /* Rewind. Channel could be already used for writing. */ status = g_io_channel_seek_position(channel, 0, G_SEEK_SET, &err); if(status != G_IO_STATUS_NORMAL) { if(err != NULL) err_msg = err->message; midgard_set_error(self->priv->mgd, MGD_GENERIC_ERROR, MGD_ERR_INTERNAL, " %s ", err_msg); return NULL; } g_clear_error(&err); g_io_channel_set_encoding (channel, NULL, NULL); status = g_io_channel_read_to_end(channel, &content, bytes_read, &err); /* FIXME, I have no idea how to determine file encoding */ /* Let's set UTF-8 and try again */ if(status == G_IO_STATUS_ERROR) { if(err && err->domain == G_CONVERT_ERROR){ g_io_channel_set_encoding (channel, "UTF-8", NULL); g_clear_error(&err); status = g_io_channel_read_to_end(channel, &content, bytes_read, &err); } } if(err) g_clear_error(&err); err_msg = ""; if(status == G_IO_STATUS_NORMAL && *bytes_read == 0){ if(err != NULL) err_msg = err->message; midgard_set_error(self->priv->mgd, MGD_GENERIC_ERROR, MGD_ERR_INTERNAL, " %s ", err_msg); return NULL; } if(err) g_clear_error(&err); return content; }
/** \brief Show a text file in the gpredict system directory * \param filename The basic file name * * This function is intended to display files like NEWS, COPYING, etc. * Note that on windows these files have .txt suffix, while on Unix they * do not. * */ void gpredict_help_show_txt (const gchar *filename) { GtkWidget *dialog; GtkWidget *swin; GtkWidget *view; GtkTextBuffer *txtbuf; GIOChannel *chan; GError *error = NULL; gchar *fname; gchar *buff; gsize length; /* get system data directory */ #ifdef G_OS_UNIX fname = g_strconcat (PACKAGE_DATA_DIR, G_DIR_SEPARATOR_S, filename, NULL); #else # ifdef G_OS_WIN32 buff = g_win32_get_package_installation_directory (NULL, NULL); fname = g_strconcat (buff, G_DIR_SEPARATOR_S, "doc", G_DIR_SEPARATOR_S, filename, ".txt", NULL); g_free (buff); # endif #endif /* load file into buffer */ chan = g_io_channel_new_file (fname, "r", &error); if (error != NULL) { sat_log_log (SAT_LOG_LEVEL_ERROR, _("%s: Failed to load %s (%s)"), __FUNCTION__, fname, error->message); g_free (fname); g_clear_error (&error); return; } g_io_channel_read_to_end (chan, &buff, &length, &error); if (error != NULL) { sat_log_log (SAT_LOG_LEVEL_ERROR, _("%s: Error reading %s (%s)"), __FUNCTION__, fname, error->message); g_free (buff); g_clear_error (&error); g_io_channel_shutdown (chan, TRUE, NULL); g_io_channel_unref (chan); return; } g_free (fname); /* create text view and text buffer widgets */ view = gtk_text_view_new (); gtk_text_view_set_editable (GTK_TEXT_VIEW (view), FALSE); gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (view), FALSE); txtbuf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)); gtk_text_buffer_set_text (txtbuf, buff, -1); g_free (buff); /* scrolled window */ swin = gtk_scrolled_window_new (NULL, NULL); gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (swin), GTK_SHADOW_ETCHED_IN); gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); gtk_container_add (GTK_CONTAINER (swin), view); /* create and show dialogue with textbuffer */ dialog = gtk_dialog_new_with_buttons (_("Gpredict Info"), NULL, 0, GTK_STOCK_CLOSE, GTK_RESPONSE_ACCEPT, NULL); gtk_widget_set_size_request (dialog, -1, 450); buff = icon_file_name ("gpredict-icon.png"); gtk_window_set_icon_from_file (GTK_WINDOW (dialog), buff, NULL); g_free (buff); g_signal_connect_swapped (dialog, "response", G_CALLBACK (gtk_widget_destroy), dialog); gtk_container_add (GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(dialog))), swin); gtk_widget_show_all (dialog); }
static gpointer manage_backups (gpointer user_data) { gchar date [100]; gchar *original_path; gchar *original_contents; gchar *copy_path; int original_fd; int copy_fd; time_t now_t; struct tm now_tm; gboolean fail; GIOChannel *original_stream; GIOChannel *copy_stream; GError *error; original_path = user_data; remove_old_copies (original_path); original_fd = open (original_path, O_RDONLY); if (original_fd == -1) { g_warning ("Unable to open original file: %s", strerror (errno)); g_free (original_path); return NULL; } original_stream = g_io_channel_unix_new (original_fd); error = NULL; fail = FALSE; if (g_io_channel_read_to_end (original_stream, &original_contents, NULL, &error) == G_IO_STATUS_ERROR) { g_warning ("Unable to read original file: %s", error->message); g_error_free (error); fail = TRUE; } g_io_channel_unref (original_stream); if (fail == TRUE) { g_free (original_path); return NULL; } now_t = time (NULL); localtime_r (&now_t, &now_tm); strftime (date, 100, "%Y%m%d%H%M%S", &now_tm); copy_path = g_strdup_printf ("%s.%s.bkp", original_path, date); g_free (original_path); copy_fd = open (copy_path, O_WRONLY | O_CREAT, 0644); if (copy_fd == -1) { g_warning ("Unable to open copy file: %s", strerror (errno)); g_free (copy_path); g_free (original_contents); return NULL; } copy_stream = g_io_channel_unix_new (copy_fd); g_io_channel_write_chars (copy_stream, original_contents, -1, NULL, NULL); g_io_channel_unref (copy_stream); g_free (copy_path); g_free (original_contents); return NULL; }
void GwSpawn::run() { describe(); // Working directory. const gchar *workingdirectory = NULL; if (!myworkingdirectory.empty()) workingdirectory = myworkingdirectory.c_str(); // Store arguments in argv. char *argv[myarguments.size() + 2]; // I know these casts are ugly. To do: figure out a better way. argv[0] = (char *)myprogram.c_str(); for (unsigned int i = 0; i < myarguments.size(); i++) { argv[i + 1] = (char *)myarguments[i].c_str(); } // Terminate argv. argv[myarguments.size() + 1] = NULL; // Spawn flags. int flags = G_SPAWN_SEARCH_PATH; if (mydevnull) { flags |= (G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL); } // Possible pipes. gint standard_input_filedescriptor = 0; gint standard_output_filedescriptor; gint standard_error_filedescriptor; gint *standard_input_filedescriptor_pointer = NULL; gint *standard_output_filedescriptor_pointer = NULL; gint *standard_error_filedescriptor_pointer = NULL; gchar *standard_output = NULL; gchar *standard_error = NULL; gchar **standard_output_pointer = NULL; gchar **standard_error_pointer = NULL; if (myread) { standard_output_filedescriptor_pointer = &standard_output_filedescriptor; standard_error_filedescriptor_pointer = &standard_error_filedescriptor; standard_output_pointer = &standard_output; standard_error_pointer = &standard_error; } if (!mywrite.empty()) { standard_input_filedescriptor_pointer = &standard_input_filedescriptor; } // Spawn process. if (myasync) { result = g_spawn_async_with_pipes(workingdirectory, argv, NULL, (GSpawnFlags) flags, NULL, NULL, &pid, standard_input_filedescriptor_pointer, standard_output_filedescriptor_pointer, standard_error_filedescriptor_pointer, NULL); // Handle writing to stdin. if (standard_input_filedescriptor) { tiny_spawn_write(standard_input_filedescriptor, mywrite); close(standard_input_filedescriptor); } } else { result = g_spawn_sync(workingdirectory, argv, NULL, (GSpawnFlags) flags, NULL, NULL, standard_output_pointer, standard_error_pointer, &exitstatus, NULL); } // Handle case we didn't spawn the process. if (!result) { exitstatus = -1; ustring message = myprogram; message.append(_(" didn't spawn")); gw_critical(message); return; } // Handle progress function. if (myprogress || standard_input_filedescriptor) { ProgressWindow *progresswindow = NULL; if (myprogress) progresswindow = new ProgressWindow(mytext, myallowcancel); ustring filename = gw_build_filename("/proc", convert_to_string(pid)); while (g_file_test(filename.c_str(), G_FILE_TEST_EXISTS)) { if (progresswindow) { progresswindow->pulse(); if (progresswindow->cancel) { unix_kill(pid); cancelled = true; } } g_usleep(500000); } // Close pid. g_spawn_close_pid(pid); if (progresswindow) { delete progresswindow; } } // Handle reading the output. if (myread) { // In async mode we've got file descriptors, and in sync mode we have // gchar * output. // If async mode, read the output and close the descriptors. if (myasync) { GIOChannel *channel_out = g_io_channel_unix_new(standard_output_filedescriptor); g_io_channel_read_to_end(channel_out, &standard_output, NULL, NULL); g_io_channel_shutdown(channel_out, false, NULL); GIOChannel *channel_err = g_io_channel_unix_new(standard_error_filedescriptor); g_io_channel_read_to_end(channel_err, &standard_error, NULL, NULL); g_io_channel_shutdown(channel_err, false, NULL); } ParseLine parse_out(standard_output); standardout = parse_out.lines; ParseLine parse_err(standard_error); standarderr = parse_err.lines; // Free data. if (standard_output) g_free(standard_output); if (standard_error) g_free(standard_error); } }