Simplified development of cc functions

This commit is contained in:
Moritz Lipp 2010-07-04 10:35:15 +02:00
parent a35da19b6e
commit eaed702e2d

284
zathura.c
View File

@ -356,6 +356,12 @@ void set_page(int);
void switch_view(GtkWidget*); void switch_view(GtkWidget*);
GtkEventBox* createCompletionRow(GtkBox*, char*, char*, gboolean); GtkEventBox* createCompletionRow(GtkBox*, char*, char*, gboolean);
Completion* completion_init();
CompletionGroup* completion_group_create(char*);
void completion_add_group(Completion*, CompletionGroup*);
void completion_free(Completion*);
void completion_group_add_element(CompletionGroup*, char*, char*);
/* thread declaration */ /* thread declaration */
void* search(void*); void* search(void*);
@ -1526,6 +1532,89 @@ switch_view(GtkWidget* widget)
gtk_container_add(GTK_CONTAINER(Zathura.UI.viewport), GTK_WIDGET(widget)); gtk_container_add(GTK_CONTAINER(Zathura.UI.viewport), GTK_WIDGET(widget));
} }
Completion*
completion_init()
{
Completion *completion = malloc(sizeof(Completion));
if(!completion)
out_of_memory();
completion->groups = NULL;
return completion;
}
CompletionGroup*
completion_group_create(char* name)
{
CompletionGroup* group = malloc(sizeof(CompletionGroup));
if(!group)
out_of_memory();
group->value = name;
group->elements = NULL;
group->next = NULL;
return group;
}
void
completion_add_group(Completion* completion, CompletionGroup* group)
{
CompletionGroup* cg = completion->groups;
while(cg && cg->next)
cg = cg->next;
if(cg)
cg->next = group;
else
completion->groups = group;
}
void completion_free(Completion* completion)
{
CompletionGroup* group = completion->groups;
CompletionElement *element;
while(group)
{
element = group->elements;
while(element)
{
CompletionElement* ne = element->next;
free(element);
element = ne;
}
CompletionGroup *ng = group->next;
free(group);
group = ng;
}
}
void completion_group_add_element(CompletionGroup* group, char* name, char* description)
{
CompletionElement* el = group->elements;
while(el && el->next)
el = el->next;
CompletionElement* new_element = malloc(sizeof(CompletionElement));
if(!new_element)
out_of_memory();
new_element->value = name;
new_element->description = description;
new_element->next = NULL;
if(el)
el->next = new_element;
else
group->elements = new_element;
}
/* thread implementation */ /* thread implementation */
void* void*
search(void* parameter) search(void* parameter)
@ -2390,22 +2479,7 @@ isc_completion(Argument* argument)
} }
/* clean up */ /* clean up */
group = result->groups; completion_free(result);
while(group)
{
element = group->elements;
while(element)
{
CompletionElement* ne = element->next;
free(element);
element = ne;
}
CompletionGroup *ng = group->next;
free(group);
group = ng;
}
} }
/* create list based on commands */ /* create list based on commands */
else else
@ -3223,22 +3297,11 @@ cmd_save(int argc, char** argv)
Completion* Completion*
cc_bookmark(char* input) cc_bookmark(char* input)
{ {
/* init completion group */ Completion* completion = completion_init();
Completion *completion = malloc(sizeof(Completion)); CompletionGroup* group = completion_group_create(NULL);
if(!completion)
out_of_memory();
CompletionGroup* group = malloc(sizeof(CompletionGroup)); completion_add_group(completion, group);
if(!group)
out_of_memory();
group->value = NULL;
group->next = NULL;
group->elements = NULL;
completion->groups = group;
CompletionElement *last_element = NULL;
int element_counter = 0;
int i = 0; int i = 0;
int input_length = input ? strlen(input) : 0; int input_length = input ? strlen(input) : 0;
@ -3247,20 +3310,7 @@ cc_bookmark(char* input)
if( (input_length <= strlen(Zathura.Bookmarks.bookmarks[i].id)) && if( (input_length <= strlen(Zathura.Bookmarks.bookmarks[i].id)) &&
!strncmp(input, Zathura.Bookmarks.bookmarks[i].id, input_length) ) !strncmp(input, Zathura.Bookmarks.bookmarks[i].id, input_length) )
{ {
CompletionElement* el = malloc(sizeof(CompletionElement)); completion_group_add_element(group, Zathura.Bookmarks.bookmarks[i].id, g_strdup_printf("Page %d", Zathura.Bookmarks.bookmarks[i].page));
if(!el)
out_of_memory();
el->value = Zathura.Bookmarks.bookmarks[i].id;
el->description = g_strdup_printf("Page: %d", Zathura.Bookmarks.bookmarks[i].page);
el->next = NULL;
if(element_counter++ != 0)
last_element->next = el;
else
group->elements = el;
last_element = el;
} }
} }
@ -3270,42 +3320,13 @@ cc_bookmark(char* input)
Completion* Completion*
cc_export(char* input) cc_export(char* input)
{ {
/* init completion group */ Completion* completion = completion_init();
Completion *completion = malloc(sizeof(Completion)); CompletionGroup* group = completion_group_create(NULL);
if(!completion)
out_of_memory();
CompletionGroup* group = malloc(sizeof(CompletionGroup)); completion_add_group(completion, group);
if(!group)
out_of_memory();
group->value = NULL; completion_group_add_element(group, "images", "Export images");
group->next = NULL; completion_group_add_element(group, "attachments", "Export attachments");
group->elements = NULL;
completion->groups = group;
/* export images */
CompletionElement *export_images = malloc(sizeof(CompletionElement));
if(!export_images)
out_of_memory();
export_images->value = "images";
export_images->description = "Export images";
export_images->next = NULL;
/* export attachmants */
CompletionElement *export_attachments = malloc(sizeof(CompletionElement));
if(!export_attachments)
out_of_memory();
export_attachments->value = "attachments";
export_attachments->description = "Export attachments";
export_attachments->next = NULL;
/* connect */
group->elements = export_attachments;
export_attachments->next = export_images;
return completion; return completion;
} }
@ -3313,20 +3334,10 @@ cc_export(char* input)
Completion* Completion*
cc_open(char* input) cc_open(char* input)
{ {
/* init completion group */ Completion* completion = completion_init();
Completion *completion = malloc(sizeof(Completion)); CompletionGroup* group = completion_group_create(NULL);
if(!completion)
out_of_memory();
CompletionGroup* group = malloc(sizeof(CompletionGroup)); completion_add_group(completion, group);
if(!group)
out_of_memory();
group->value = NULL;
group->next = NULL;
group->elements = NULL;
completion->groups = group;
/* read dir */ /* read dir */
char* path = "/"; char* path = "/";
@ -3372,9 +3383,7 @@ cc_open(char* input)
return NULL; return NULL;
/* create element list */ /* create element list */
CompletionElement *last_element = NULL;
char* name = NULL; char* name = NULL;
int element_counter = 0;
while((name = (char*) g_dir_read_name(dir)) != NULL) while((name = (char*) g_dir_read_name(dir)) != NULL)
{ {
@ -3384,26 +3393,14 @@ cc_open(char* input)
if( ((file_length <= d_length) && !strncmp(file, d_name, file_length)) || if( ((file_length <= d_length) && !strncmp(file, d_name, file_length)) ||
(file_length == 0) ) (file_length == 0) )
{ {
CompletionElement* el = malloc(sizeof(CompletionElement)); char* d = g_strdup_printf("%s%s", path, d_name);
if(!el) if(g_file_test(d, G_FILE_TEST_IS_DIR))
out_of_memory();
el->value = g_strdup_printf("%s%s", path, d_name);
if(g_file_test(el->value, G_FILE_TEST_IS_DIR))
{ {
gchar *subdir = el->value; gchar *subdir = d;
el->value = g_strdup_printf("%s/", subdir); d = g_strdup_printf("%s/", subdir);
g_free(subdir); g_free(subdir);
} }
el->description = NULL; completion_group_add_element(group, d, NULL);
el->next = NULL;
if(element_counter++ != 0)
last_element->next = el;
else
group->elements = el;
last_element = el;
} }
} }
@ -3415,22 +3412,11 @@ cc_open(char* input)
Completion* Completion*
cc_print(char* input) cc_print(char* input)
{ {
/* init completion group */ Completion* completion = completion_init();
Completion *completion = malloc(sizeof(Completion)); CompletionGroup* group = completion_group_create(NULL);
if(!completion)
out_of_memory();
CompletionGroup* group = malloc(sizeof(CompletionGroup)); completion_add_group(completion, group);
if(!group)
out_of_memory();
group->value = NULL;
group->next = NULL;
group->elements = NULL;
completion->groups = group;
CompletionElement *last_element = NULL;
int element_counter = 0;
int input_length = input ? strlen(input) : 0; int input_length = input ? strlen(input) : 0;
/* read printers */ /* read printers */
@ -3442,8 +3428,7 @@ cc_print(char* input)
if(!fp) if(!fp)
{ {
free(completion); completion_free(completion);
free(group);
return NULL; return NULL;
} }
@ -3466,20 +3451,7 @@ cc_print(char* input)
if( (input_length <= line_length) || if( (input_length <= line_length) ||
(!strncmp(input, current_line, input_length)) ) (!strncmp(input, current_line, input_length)) )
{ {
CompletionElement* el = malloc(sizeof(CompletionElement)); completion_group_add_element(group, g_strdup(current_line), NULL);
if(!el)
out_of_memory();
el->value = g_strdup(current_line);
el->description = NULL;
el->next = NULL;
if(element_counter++ != 0)
last_element->next = el;
else
group->elements = el;
last_element = el;
} }
free(current_line); free(current_line);
@ -3496,22 +3468,11 @@ cc_print(char* input)
Completion* Completion*
cc_set(char* input) cc_set(char* input)
{ {
/* init completion group */ Completion* completion = completion_init();
Completion *completion = malloc(sizeof(Completion)); CompletionGroup* group = completion_group_create(NULL);
if(!completion)
out_of_memory();
CompletionGroup* group = malloc(sizeof(CompletionGroup)); completion_add_group(completion, group);
if(!group)
out_of_memory();
group->value = NULL;
group->next = NULL;
group->elements = NULL;
completion->groups = group;
CompletionElement *last_element = NULL;
int element_counter = 0;
int i = 0; int i = 0;
int input_length = input ? strlen(input) : 0; int input_length = input ? strlen(input) : 0;
@ -3520,20 +3481,7 @@ cc_set(char* input)
if( (input_length <= strlen(settings[i].name)) && if( (input_length <= strlen(settings[i].name)) &&
!strncmp(input, settings[i].name, input_length) ) !strncmp(input, settings[i].name, input_length) )
{ {
CompletionElement* el = malloc(sizeof(CompletionElement)); completion_group_add_element(group, settings[i].name, settings[i].description);
if(!el)
out_of_memory();
el->value = settings[i].name;
el->description = settings[i].description;
el->next = NULL;
if(element_counter++ != 0)
last_element->next = el;
else
group->elements = el;
last_element = el;
} }
} }