TA的每日心情 | 奋斗 2021-11-19 13:15 |
---|
签到天数: 20 天 连续签到: 1 天 [LV.4]偶尔看看III
|
利用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[33];
- 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;
- }
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?马上加入
x
|