Advertisement
adilima

Search and create MD5 for R3D using C

Dec 18th, 2014
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Brief:
  2.  * This example using Gtk+-2.0 to create Find/Search for R3D files
  3.  * in the given directory on Linux
  4.  *
  5.  * The output filename will be truncated to zero (if exist).
  6.  * MD5 program on Linux is called 'md5sum', while on Mac OS X this program
  7.  * called just 'md5'.
  8.  *
  9.  * It turns out that the output text file is not really needed, because
  10.  * you can easily copy/paste the output text from the TextView, and save it
  11.  * manually.
  12.  *
  13.  * To compile the program, use the following command:
  14.  * gcc -o r3dsums r3dsums.c `pkg-config gtk+-2.0 --cflags --libs` -lobjc
  15.  *
  16.  * This example using Objective-C (without GNUstep/Cocoa)
  17.  * But you can easily make it plain-C by altering the source.
  18.  *
  19.  */
  20.  
  21. #import <objc/runtime.h>
  22. #import <gtk/gtk.h>
  23. #import <stdio.h>
  24. #import <stdlib.h>
  25. #import <string.h>
  26.  
  27. @interface UITest {
  28.   Class isa;
  29. @private
  30.   GtkWidget* window;
  31.   GtkWidget* table;
  32.   GtkWidget* textOutput;
  33.   GtkWidget* textSource;
  34.   GtkWidget* statusView;
  35. }
  36. @end
  37.  
  38. static void start_search(UITest* p);
  39.  
  40. @implementation UITest
  41.  
  42. -(id)initWithTitle:(const char*) pszTitle {
  43.   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  44.   if (window) {
  45.     GtkWindow* w = GTK_WINDOW(window);
  46.     gtk_window_set_title(w, pszTitle);
  47.     gtk_window_set_default_size(w, 640, 350);
  48.    
  49.     table = gtk_table_new(10, 2, FALSE);
  50.     gtk_container_add(GTK_CONTAINER(window), table);
  51.  
  52.     GtkWidget* label = gtk_label_new("Source R3D Directory:");
  53.     gtk_misc_set_alignment((GtkMisc*)label, 0.0, 0.5);
  54.     gtk_table_attach((GtkTable*)table, label,
  55.              0, 1,
  56.              0, 1,
  57.              GTK_EXPAND|GTK_FILL,
  58.              GTK_SHRINK,
  59.              5, 1);
  60.     textSource = gtk_entry_new();
  61.     gtk_table_attach((GtkTable*)table, textSource,
  62.              0, 1,
  63.              1, 2,
  64.              GTK_EXPAND|GTK_FILL,
  65.              GTK_SHRINK,
  66.              5, 5);
  67.     GtkWidget* btn = gtk_button_new_with_label("Browse");
  68.     gtk_table_attach((GtkTable*)table, btn,
  69.              1, 2,
  70.              1, 2,
  71.              GTK_FILL,
  72.              GTK_SHRINK,
  73.              5, 5);
  74.     textOutput = gtk_entry_new();
  75.     label = gtk_label_new("Output file name:");
  76.     gtk_misc_set_alignment((GtkMisc*)label, 0.0, 0.5);
  77.     gtk_table_attach((GtkTable*)table, label,
  78.              0, 1,
  79.              2, 3,
  80.              GTK_EXPAND|GTK_FILL,
  81.              GTK_SHRINK,
  82.              5, 1);
  83.     gtk_table_attach((GtkTable*)table, textOutput,
  84.              0, 1,
  85.              3, 4,
  86.              GTK_EXPAND|GTK_FILL,
  87.              GTK_SHRINK,
  88.              5, 5);
  89.  
  90.     GtkWidget* sc = gtk_scrolled_window_new(NULL, NULL);
  91.     statusView = gtk_text_view_new();
  92.     gtk_container_add(GTK_CONTAINER(sc), statusView);
  93.     gtk_table_attach((GtkTable*)table, sc,
  94.              0, 2,
  95.              4, 8,
  96.              GTK_EXPAND|GTK_FILL,
  97.              GTK_EXPAND|GTK_FILL,
  98.              5, 5);
  99.  
  100.     GtkWidget* hbox = gtk_hbox_new(FALSE, 5);
  101.     gtk_table_attach((GtkTable*)table, hbox,
  102.              0, 2,
  103.              8, 9,
  104.              GTK_EXPAND|GTK_FILL,
  105.              GTK_SHRINK,
  106.              10, 10);
  107.  
  108.     btn = gtk_button_new_with_label("Start");
  109.     gtk_box_pack_start((GtkBox*)hbox, btn, FALSE, TRUE, 1);
  110.     g_signal_connect_swapped(btn, "clicked", G_CALLBACK(start_search), self);
  111.  
  112.     btn = gtk_button_new_with_label("Quit");
  113.     gtk_box_pack_end((GtkBox*)hbox, btn, FALSE, TRUE, 1);
  114.     g_signal_connect_swapped(btn, "clicked", G_CALLBACK(gtk_widget_destroy), window);
  115.  
  116.     gtk_widget_show_all(window);
  117.     g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
  118.   }
  119.   return self;
  120. }
  121.  
  122. +(id)testWithTitle:(const char*) pszTitle {
  123.   UITest* p = (UITest*)class_createInstance(self, 0);
  124.   if (p) return [p initWithTitle:pszTitle];
  125.   return NULL;
  126. }
  127.  
  128. -(GtkTextBuffer*)bufferForOutput {
  129.   return gtk_text_view_get_buffer(GTK_TEXT_VIEW(statusView));
  130. }
  131.  
  132. -(const char*)pathForR3DSource {
  133.   return gtk_entry_get_text(GTK_ENTRY(textSource));
  134. }
  135.  
  136. -(const char*)outputFileName {
  137.   return gtk_entry_get_text(GTK_ENTRY(textOutput));
  138. }
  139.  
  140. @end
  141.  
  142. int main(int argc, char** argv) {
  143.   gdk_threads_init();
  144.   gtk_init(&argc, &argv);
  145.   UITest* pObj = [UITest testWithTitle:"Object Test"];
  146.   if (pObj) {
  147.     gdk_threads_enter();
  148.     gtk_main();
  149.     gdk_threads_leave();
  150.     free(pObj);
  151.   }
  152.   return 0;
  153. }
  154.  
  155. static void* doSearch(UITest* p);
  156.  
  157. GThread* _searchThread = NULL;
  158. static void start_search(UITest* p) {
  159.   _searchThread = g_thread_new("__search_thread__",
  160.                    (GThreadFunc)doSearch,
  161.                    p);
  162. }
  163.  
  164. static void* doSearch(UITest* p) {
  165.   int pid;
  166.   int fd[3];
  167.  
  168.   gdk_threads_enter();
  169.  
  170.   pipe(fd);
  171.   pid = fork();
  172.   if (pid < 0) {
  173.     GtkTextBuffer* buffer = [p bufferForOutput];
  174.     gtk_text_buffer_set_text(buffer, "fork error", -1);
  175.     return (void*)0;
  176.   }
  177.   else if (pid == 0) {
  178.     dup2(fd[1], STDOUT_FILENO);
  179.     dup2(fd[2], STDERR_FILENO);
  180.     close(fd[0]);
  181.     close(fd[1]);
  182.     execlp("/bin/find",
  183.        "find",
  184.        [p pathForR3DSource],
  185.        "-type", "f",
  186.        "-name", "*.R3D",
  187.        "-exec", "md5sum", "{}", ";",
  188.        NULL);
  189.   }
  190.   else {
  191.     int status;
  192.     GtkTextBuffer* buffer = [p bufferForOutput];
  193.     GtkTextIter iter;
  194.     char szBuff[256];
  195.     size_t reads;
  196.     close(fd[1]);
  197.     while (waitpid(-1, &status, WNOHANG) != pid) {
  198.       while (reads = read(fd[0], szBuff, 256)) {
  199.     gtk_text_buffer_get_end_iter(buffer, &iter);
  200.     gtk_text_buffer_insert(buffer, &iter, szBuff, reads);
  201.     usleep(1);
  202.       }
  203.       usleep(1);
  204.     }
  205.  
  206.     close(fd[0]);
  207.   }
  208.   gdk_threads_leave();
  209.   return (void*)0;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement