コード例 #1
0
static void
_verve_history_cache_write (void)
{
  if (verve_history_is_empty ())
    return;

  const gchar *basename = _verve_history_cache_get_filename ();
  gchar *filename = xfce_resource_save_location (XFCE_RESOURCE_CONFIG, basename, TRUE);

  if (G_UNLIKELY (filename == NULL))
    return;

  GError *error = NULL;
  GIOChannel *handle = g_io_channel_new_file (filename, "w+", &error);

  if (error)
    g_error_free (error);

  if (G_LIKELY (handle != NULL))
  {
    GList *current = verve_history_begin();
    
    GIOStatus status;
    gsize bytes;
    
    int i;
    for (i=0; i<25, current != NULL; i++) /* Cache the last 25 commands */
    {
      g_io_channel_write_chars (handle, g_strconcat ("", current->data, "\n", NULL), -1, &bytes, &error);
      
      if (error)
        break;
      
      current = verve_history_get_next (current);
    }

    g_io_channel_shutdown (handle, TRUE, &error);

    if (error)
      g_error_free (error);

    g_io_channel_unref (handle);
  }
  
  g_free (filename);
}
コード例 #2
0
static void
verve_history_cache_write (void)
{
  /* Do not write history if it is empty */
  if (verve_history_is_empty ())
    return;

  const gchar *basename = verve_history_cache_get_filename ();

  /* Search for history file, create if it does not exist yet */
  gchar *filename = xfce_resource_save_location (XFCE_RESOURCE_CONFIG, basename, TRUE);

  if (G_LIKELY (filename != NULL))
    {
      GError     *error = NULL;
      GIOChannel *handle;
     
      /* Open output stream */
      handle = g_io_channel_new_file (filename, "w+", &error);

      /* Ignore and free error messages */
      if (G_UNLIKELY (error != NULL))
        g_error_free (error);

      /* Only write contents if stream could be opened */
      if (G_LIKELY (handle != NULL))
      {
        GList    *current;
        gsize     bytes;
        int       i;

        /* Get first history entry */
        current = verve_history_begin();

        /* Save the last 25 commands */
        for (i = 0; i < history_length && current != NULL; i++)
          {
            /* Build output line */
            gchar *line = g_strconcat ("", current->data, "\n", NULL);

            /* Write line */
            g_io_channel_write_chars (handle, line, -1, &bytes, &error);

            /* Free line string */
            g_free (line);
            
            /* Do not write more records if there was an error (e.g. no space left on device) */
            if (G_UNLIKELY (error != NULL))
              {
                g_error_free (error);
                break;
              }
            
            /* Step over to next entry */
            current = verve_history_get_next (current);
          }

        /* Close file handle */
        g_io_channel_shutdown (handle, TRUE, &error);

        /* Free error message */
        if (G_UNLIKELY (error != NULL))
          g_error_free (error);

        /* Destroy stream object */
        g_io_channel_unref (handle);
      }
    }
  
  /* Free filename string */
  g_free (filename);
}