Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Brief:
- * This example using Gtk+-2.0 to create Find/Search for R3D files
- * in the given directory on Linux
- *
- * The output filename will be truncated to zero (if exist).
- * MD5 program on Linux is called 'md5sum', while on Mac OS X this program
- * called just 'md5'.
- *
- * It turns out that the output text file is not really needed, because
- * you can easily copy/paste the output text from the TextView, and save it
- * manually.
- *
- * To compile the program, use the following command:
- * gcc -o r3dsums r3dsums.c `pkg-config gtk+-2.0 --cflags --libs` -lobjc
- *
- * This example using Objective-C (without GNUstep/Cocoa)
- * But you can easily make it plain-C by altering the source.
- *
- */
- #import <objc/runtime.h>
- #import <gtk/gtk.h>
- #import <stdio.h>
- #import <stdlib.h>
- #import <string.h>
- @interface UITest {
- Class isa;
- @private
- GtkWidget* window;
- GtkWidget* table;
- GtkWidget* textOutput;
- GtkWidget* textSource;
- GtkWidget* statusView;
- }
- @end
- static void start_search(UITest* p);
- @implementation UITest
- -(id)initWithTitle:(const char*) pszTitle {
- window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
- if (window) {
- GtkWindow* w = GTK_WINDOW(window);
- gtk_window_set_title(w, pszTitle);
- gtk_window_set_default_size(w, 640, 350);
- table = gtk_table_new(10, 2, FALSE);
- gtk_container_add(GTK_CONTAINER(window), table);
- GtkWidget* label = gtk_label_new("Source R3D Directory:");
- gtk_misc_set_alignment((GtkMisc*)label, 0.0, 0.5);
- gtk_table_attach((GtkTable*)table, label,
- 0, 1,
- 0, 1,
- GTK_EXPAND|GTK_FILL,
- GTK_SHRINK,
- 5, 1);
- textSource = gtk_entry_new();
- gtk_table_attach((GtkTable*)table, textSource,
- 0, 1,
- 1, 2,
- GTK_EXPAND|GTK_FILL,
- GTK_SHRINK,
- 5, 5);
- GtkWidget* btn = gtk_button_new_with_label("Browse");
- gtk_table_attach((GtkTable*)table, btn,
- 1, 2,
- 1, 2,
- GTK_FILL,
- GTK_SHRINK,
- 5, 5);
- textOutput = gtk_entry_new();
- label = gtk_label_new("Output file name:");
- gtk_misc_set_alignment((GtkMisc*)label, 0.0, 0.5);
- gtk_table_attach((GtkTable*)table, label,
- 0, 1,
- 2, 3,
- GTK_EXPAND|GTK_FILL,
- GTK_SHRINK,
- 5, 1);
- gtk_table_attach((GtkTable*)table, textOutput,
- 0, 1,
- 3, 4,
- GTK_EXPAND|GTK_FILL,
- GTK_SHRINK,
- 5, 5);
- GtkWidget* sc = gtk_scrolled_window_new(NULL, NULL);
- statusView = gtk_text_view_new();
- gtk_container_add(GTK_CONTAINER(sc), statusView);
- gtk_table_attach((GtkTable*)table, sc,
- 0, 2,
- 4, 8,
- GTK_EXPAND|GTK_FILL,
- GTK_EXPAND|GTK_FILL,
- 5, 5);
- GtkWidget* hbox = gtk_hbox_new(FALSE, 5);
- gtk_table_attach((GtkTable*)table, hbox,
- 0, 2,
- 8, 9,
- GTK_EXPAND|GTK_FILL,
- GTK_SHRINK,
- 10, 10);
- btn = gtk_button_new_with_label("Start");
- gtk_box_pack_start((GtkBox*)hbox, btn, FALSE, TRUE, 1);
- g_signal_connect_swapped(btn, "clicked", G_CALLBACK(start_search), self);
- btn = gtk_button_new_with_label("Quit");
- gtk_box_pack_end((GtkBox*)hbox, btn, FALSE, TRUE, 1);
- g_signal_connect_swapped(btn, "clicked", G_CALLBACK(gtk_widget_destroy), window);
- gtk_widget_show_all(window);
- g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
- }
- return self;
- }
- +(id)testWithTitle:(const char*) pszTitle {
- UITest* p = (UITest*)class_createInstance(self, 0);
- if (p) return [p initWithTitle:pszTitle];
- return NULL;
- }
- -(GtkTextBuffer*)bufferForOutput {
- return gtk_text_view_get_buffer(GTK_TEXT_VIEW(statusView));
- }
- -(const char*)pathForR3DSource {
- return gtk_entry_get_text(GTK_ENTRY(textSource));
- }
- -(const char*)outputFileName {
- return gtk_entry_get_text(GTK_ENTRY(textOutput));
- }
- @end
- int main(int argc, char** argv) {
- gdk_threads_init();
- gtk_init(&argc, &argv);
- UITest* pObj = [UITest testWithTitle:"Object Test"];
- if (pObj) {
- gdk_threads_enter();
- gtk_main();
- gdk_threads_leave();
- free(pObj);
- }
- return 0;
- }
- static void* doSearch(UITest* p);
- GThread* _searchThread = NULL;
- static void start_search(UITest* p) {
- _searchThread = g_thread_new("__search_thread__",
- (GThreadFunc)doSearch,
- p);
- }
- static void* doSearch(UITest* p) {
- int pid;
- int fd[3];
- gdk_threads_enter();
- pipe(fd);
- pid = fork();
- if (pid < 0) {
- GtkTextBuffer* buffer = [p bufferForOutput];
- gtk_text_buffer_set_text(buffer, "fork error", -1);
- return (void*)0;
- }
- else if (pid == 0) {
- dup2(fd[1], STDOUT_FILENO);
- dup2(fd[2], STDERR_FILENO);
- close(fd[0]);
- close(fd[1]);
- execlp("/bin/find",
- "find",
- [p pathForR3DSource],
- "-type", "f",
- "-name", "*.R3D",
- "-exec", "md5sum", "{}", ";",
- NULL);
- }
- else {
- int status;
- GtkTextBuffer* buffer = [p bufferForOutput];
- GtkTextIter iter;
- char szBuff[256];
- size_t reads;
- close(fd[1]);
- while (waitpid(-1, &status, WNOHANG) != pid) {
- while (reads = read(fd[0], szBuff, 256)) {
- gtk_text_buffer_get_end_iter(buffer, &iter);
- gtk_text_buffer_insert(buffer, &iter, szBuff, reads);
- usleep(1);
- }
- usleep(1);
- }
- close(fd[0]);
- }
- gdk_threads_leave();
- return (void*)0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement