Tom 发表于 2010-9-17 10:00:48

GTK+写的MD5计算工具

利用GLib库的GChecksum,对文件进行MD5计算。

所用知识:
GChecksum的简单使用,FILE读取文件,stat获取文件大小,简单GTK+布局,信号绑定。

界面如下:


代码如下:
/*
    Creator by Tom
    E-mail:Tom@awaysoft.com
    gtk.awaysoft.com
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <gtk/gtk.h>
#include <sys/stat.h>

GtkWidget *choose_button = NULL;

static void calc_Md5 (GtkWidget *wid, GtkWidget *win)
{
  GtkWidget *dialog = NULL;
  gchar *filename = NULL;
  const gchar *result = NULL;
  gchar mess;
  filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(choose_button));
  if (filename){
    GChecksum * checksum;
    guchar * buffer = NULL;
    FILE *fp = NULL;

    if ((fp = fopen(filename, "rb"))!=NULL){
        /* get file size */
        struct stat filesize;
        stat(filename, &filesize);
        int file_no = filesize.st_size;
        if (file_no != -1){
            /* read buffer */
            buffer = (guchar *)g_malloc(sizeof(guchar)*file_no);
            fread(buffer, file_no, 1, fp);

            /* checksum */
            checksum = g_checksum_new(G_CHECKSUM_MD5);
            g_checksum_update(checksum, (const guchar *)buffer, file_no);
            result = g_checksum_get_string(checksum);
            g_free(buffer);
        }else{
            strcpy ((gchar *)result, "文件打开失败!");
        }
        fclose(fp);
    }else{
        strcpy ((gchar *)result, "文件打开失败!");
    }
    strcpy(mess, result);
    g_free((gchar *)result);
    g_free(filename);
  }else{
    strcpy(mess, "请选择或者拖动一个文件!");
  }
  dialog = gtk_message_dialog_new (GTK_WINDOW (win), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, mess);
  gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
  gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (dialog);
}

int main (int argc, char *argv[])
{
  GtkWidget *button = NULL;
  GtkWidget *win = NULL;
  GtkWidget *vbox = NULL;
  GtkWidget *hbox = NULL;
  GtkWidget *label = NULL;

  /* Initialize GTK+ */
  g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false, NULL);
  gtk_init (&argc, &argv);
  g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL);

  /* Create the main window */
  win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_container_set_border_width (GTK_CONTAINER (win), 8);
  gtk_window_set_title (GTK_WINDOW (win), "MD5 计算");
  gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_CENTER);
  gtk_widget_realize (win);
  gtk_widget_set_size_request(win, 200,100);
  g_signal_connect (win, "destroy", gtk_main_quit, NULL);

  /* Create a vertical box with buttons */
  vbox = gtk_vbox_new (TRUE, 6);
  gtk_container_add (GTK_CONTAINER (win), vbox);

  hbox = gtk_hbox_new (FALSE, 6);
  gtk_box_pack_start(GTK_BOX (vbox), hbox, TRUE, TRUE, 0);

  label = gtk_label_new("文件:");
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);

  choose_button = gtk_file_chooser_button_new ("选择一个文件",
                                        GTK_FILE_CHOOSER_ACTION_OPEN);
  gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (choose_button),
                                       "$HOME");
  gtk_box_pack_start (GTK_BOX (hbox), choose_button, TRUE, TRUE, 0);

  hbox = gtk_hbox_new (TRUE, 6);
  gtk_box_pack_start(GTK_BOX (vbox), hbox, TRUE, TRUE, 0);

  button = gtk_button_new_with_label("计算");
  gtk_box_pack_start(GTK_BOX (hbox), button, TRUE, TRUE, 0);
  g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (calc_Md5), (gpointer) win);


  button = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
  g_signal_connect (button, "clicked", gtk_main_quit, NULL);
  gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);

  /* Enter the main loop */
  gtk_widget_show_all (win);
  gtk_main ();
  return 0;
}

firebat 发表于 2010-9-17 16:14:01

嘿,也可以直接调用md5sum……

Tom 发表于 2010-9-17 18:44:41

嗯,不过,貌似那就要用管道了。。。。更麻烦啊。。。。

firebat 发表于 2010-9-17 22:45:02

用exec*啊

Tom 发表于 2010-9-18 02:43:10

exec 没用过,不过要获得返回值,也必须要管道吧?

pp245610587 发表于 2010-10-29 05:00:03

gtk的文件选择窗口不怎么好用啊

gtk-inkscape 发表于 2010-10-30 07:05:06

收藏先。。。对小菜来说一个学习的阶梯。。。

Tom 发表于 2010-11-1 05:56:25

引用第5楼pp245610587于2010-10-28 21:00发表的:
gtk的文件选择窗口不怎么好用啊 images/back.gif

呵呵,习惯了就好。。。

ekd123 发表于 2011-1-27 16:05:22

popen一个md5sum岂不是更简单?read出来返回值就可以……

hellosimple 发表于 2011-2-15 22:58:07

很不错,学习了。
页: [1] 2
查看完整版本: GTK+写的MD5计算工具