int main(int argc, char **argv) { printf ("*** testing search_by_name\n"); struct record records[] = { {"Alice",18,6}, {"Bob",17,7},{"Claudia",18,49}, {"bobby", 0, 0} }; int rv = search_by_name(records, 4, "alice", 0); assert(rv == 0); rv = search_by_name(records, 4, "audi", 1); assert(rv == 2); rv = search_by_name(records, 4, "alice", 1); assert(rv == -1); printf ("*** testing get_matches\n"); int *matches = NULL; matches = get_matches(records, 4, "bob"); assert(matches[0] == 2); assert(matches[1] == 1); assert(matches[2] == 3); free(matches); // this shouldn't crash :-) matches = NULL; matches = get_matches(records, 4, "robert"); assert(matches[0] == 0); free(matches); printf ("*** if you got here, then the tests passed\n"); return 0; }
int main(int argc, char *argv[]) { /* * We save 2 hash_table: phone_hash_table and name_hash_table, * every time user input new entry, we save it in either * hash_table, so that we can search it by name, or search it * by phone_num. * */ struct phone_hash_table *phone_table = init_phone_hash_table(); struct name_hash_table *name_table = init_name_hash_table(); struct data * entry; while (1) { entry = user_input(); if (entry == NULL) { break; } insert_into_phone_hash_table(phone_table, entry); insert_into_name_hash_table(name_table, entry); } int option; while (1) { struct data *entry; printf("option:1 for exit, 2 for search by phone, 3 for search by name\n"); scanf("%d",&option); switch (option) { case 1 : exit(0); case 2 : entry = search_by_phone(phone_table); break; case 3 : entry = search_by_name(name_table); break; } if (entry != NULL) { print_data(entry); } else { printf("Not found!\n"); } } }
static void read_afms(FILE *f_c, FILE *f_h) { DIR *d = opendir("."); struct dirent *de; fputs( "/*\n" " * Built-in font metrics\n" " */\n" "\n" "const AFM *const PSDRV_BuiltinAFMs[] =\n" "{\n", f_c); if (d == NULL) { fprintf(stderr, "Error opening current directory\n"); exit(__LINE__); } while ((de = readdir(d)) != NULL) { FILE *f; char *cp, linebuf[256], font_family[128]; int i, num_metrics; cp = strrchr(de->d_name, '.'); /* Does it end in */ if (cp == NULL || strcasecmp(cp, ".afm") != 0) /* .afm or .AFM? */ continue; f = fopen(de->d_name, "r"); if (f == NULL) { fprintf(stderr, "Error opening %s\n", de->d_name); exit(__LINE__); } while (1) { if (fgets(linebuf, sizeof(linebuf), f) == NULL) { fprintf(stderr, "FontName not found in %s\n", de->d_name); exit(__LINE__); } if (strncmp(linebuf, "FontName ", 9) == 0) break; } sscanf(linebuf, "FontName %[^\r\n]", font_family); for (i = 0; font_family[i] != '\0'; ++i) if (font_family[i] == '-') font_family[i] = '_'; fprintf(f_h, "extern const AFM PSDRV_%s;\n", font_family); fprintf(f_c, " &PSDRV_%s,\n", font_family); while (1) { if (fgets(linebuf, sizeof(linebuf), f) == NULL) { fprintf(stderr, "FamilyName not found in %s\n", de->d_name); exit(__LINE__); } if (strncmp(linebuf, "FamilyName ", 11) == 0) break; } sscanf(linebuf, "FamilyName %[^\r\n]", font_family); while (1) { if (fgets(linebuf, sizeof(linebuf), f) == NULL) { fprintf(stderr, "StartCharMetrics not found in %s\n", de->d_name); exit(__LINE__); } if (strncmp(linebuf, "StartCharMetrics ", 17) == 0) break; } sscanf(linebuf, "StartCharMetrics %i", &num_metrics); for (i = 0; i < num_metrics; ++i) { char namebuf[128]; if (fgets(linebuf, sizeof(linebuf), f) == NULL) { fprintf(stderr, "Unexpected EOF after %i glyphs in %s\n", i, de->d_name); exit(__LINE__); } cp = strchr(linebuf, 'N'); if (cp == NULL || strlen(cp) < 3) { fprintf(stderr, "Parse error after %i glyphs in %s\n", i, de->d_name); exit(__LINE__); } sscanf(cp, "N %s", namebuf); if (search_by_name(namebuf) != NULL) continue; sprintf(linebuf, "FONT FAMILY;%s", font_family); glyphs[num_glyphs].UV = -1; glyphs[num_glyphs].name = strdup(namebuf); glyphs[num_glyphs].comment = strdup(linebuf); if (glyphs[num_glyphs].name == NULL || glyphs[num_glyphs].comment == NULL) { fprintf(stderr, "Memory allocation failure\n"); exit(__LINE__); } ++num_glyphs; sort_by_name(); } fclose(f); } closedir(d); fputs(" NULL\n};\n", f_c); }
GtkWidget * do_search_entry (GtkWidget *do_widget) { GtkWidget *vbox; GtkWidget *hbox; GtkWidget *label; GtkWidget *entry; GtkWidget *find_button; GtkWidget *cancel_button; if (!window) { window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_screen (GTK_WINDOW (window), gtk_widget_get_screen (do_widget)); gtk_window_set_title (GTK_WINDOW (window), "Search Entry"); gtk_window_set_resizable (GTK_WINDOW (window), FALSE); g_signal_connect (window, "destroy", G_CALLBACK (search_entry_destroyed), &window); vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5); gtk_container_add (GTK_CONTAINER (window), vbox); gtk_container_set_border_width (GTK_CONTAINER (vbox), 5); label = gtk_label_new (NULL); gtk_label_set_markup (GTK_LABEL (label), "Search entry demo"); gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0); hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 10); gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0); gtk_container_set_border_width (GTK_CONTAINER (hbox), 0); /* Create our entry */ entry = gtk_search_entry_new (); gtk_box_pack_start (GTK_BOX (hbox), entry, FALSE, FALSE, 0); /* Create the find and cancel buttons */ notebook = gtk_notebook_new (); gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook), FALSE); gtk_notebook_set_show_border (GTK_NOTEBOOK (notebook), FALSE); gtk_box_pack_start (GTK_BOX (hbox), notebook, FALSE, FALSE, 0); find_button = gtk_button_new_with_label ("Find"); g_signal_connect (find_button, "clicked", G_CALLBACK (start_search), entry); gtk_notebook_append_page (GTK_NOTEBOOK (notebook), find_button, NULL); gtk_widget_show (find_button); cancel_button = gtk_button_new_with_label ("Cancel"); g_signal_connect (cancel_button, "clicked", G_CALLBACK (stop_search), NULL); gtk_notebook_append_page (GTK_NOTEBOOK (notebook), cancel_button, NULL); gtk_widget_show (cancel_button); /* Set up the search icon */ search_by_name (NULL, GTK_ENTRY (entry)); /* Set up the clear icon */ g_signal_connect (entry, "icon-press", G_CALLBACK (icon_press_cb), NULL); g_signal_connect (entry, "activate", G_CALLBACK (activate_cb), NULL); /* Create the menu */ menu = create_search_menu (entry); gtk_menu_attach_to_widget (GTK_MENU (menu), entry, NULL); /* add accessible alternatives for icon functionality */ g_object_set (entry, "populate-all", TRUE, NULL); g_signal_connect (entry, "populate-popup", G_CALLBACK (entry_populate_popup), NULL); } if (!gtk_widget_get_visible (window)) gtk_widget_show_all (window); else { gtk_widget_destroy (menu); gtk_widget_destroy (window); } return window; }
GtkWidget * do_search_entry (GtkWidget *do_widget) { GtkWidget *vbox; GtkWidget *hbox; GtkWidget *label; GtkWidget *entry; GtkWidget *find_button; GtkWidget *cancel_button; if (!window) { window = gtk_dialog_new_with_buttons ("Search Entry", GTK_WINDOW (do_widget), 0, GTK_STOCK_CLOSE, GTK_RESPONSE_NONE, NULL); gtk_window_set_resizable (GTK_WINDOW (window), FALSE); g_signal_connect (window, "response", G_CALLBACK (gtk_widget_destroy), NULL); g_signal_connect (window, "destroy", G_CALLBACK (search_entry_destroyed), &window); vbox = gtk_vbox_new (FALSE, 5); gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (window))), vbox, TRUE, TRUE, 0); gtk_container_set_border_width (GTK_CONTAINER (vbox), 5); label = gtk_label_new (NULL); gtk_label_set_markup (GTK_LABEL (label), "Search entry demo"); gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0); hbox = gtk_hbox_new (FALSE, 10); gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0); gtk_container_set_border_width (GTK_CONTAINER (hbox), 0); /* Create our entry */ entry = gtk_entry_new (); gtk_box_pack_start (GTK_BOX (hbox), entry, FALSE, FALSE, 0); /* Create the find and cancel buttons */ notebook = gtk_notebook_new (); gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook), FALSE); gtk_notebook_set_show_border (GTK_NOTEBOOK (notebook), FALSE); gtk_box_pack_start (GTK_BOX (hbox), notebook, FALSE, FALSE, 0); find_button = gtk_button_new_with_label ("Find"); g_signal_connect (find_button, "clicked", G_CALLBACK (start_search), entry); gtk_notebook_append_page (GTK_NOTEBOOK (notebook), find_button, NULL); gtk_widget_show (find_button); cancel_button = gtk_button_new_with_label ("Cancel"); g_signal_connect (cancel_button, "clicked", G_CALLBACK (stop_search), NULL); gtk_notebook_append_page (GTK_NOTEBOOK (notebook), cancel_button, NULL); gtk_widget_show (cancel_button); /* Set up the search icon */ search_by_name (NULL, GTK_ENTRY (entry)); /* Set up the clear icon */ gtk_entry_set_icon_from_stock (GTK_ENTRY (entry), GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR); text_changed_cb (GTK_ENTRY (entry), NULL, find_button); g_signal_connect (entry, "icon-press", G_CALLBACK (icon_press_cb), NULL); g_signal_connect (entry, "notify::text", G_CALLBACK (text_changed_cb), find_button); g_signal_connect (entry, "activate", G_CALLBACK (activate_cb), NULL); /* Create the menu */ menu = create_search_menu (entry); gtk_menu_attach_to_widget (GTK_MENU (menu), entry, NULL); /* add accessible alternatives for icon functionality */ g_signal_connect (entry, "populate-popup", G_CALLBACK (entry_populate_popup), NULL); } if (!gtk_widget_get_visible (window)) gtk_widget_show_all (window); else { gtk_widget_destroy (menu); gtk_widget_destroy (window); window = NULL; } return window; }
int main(void) { int sfd, n; char buf[1024]; usr_t *p; socklen_t len; struct sockaddr_in cli_addr; get_usr_info("usr_info.txt"); sfd = sock_init(8000, "127.0.0.1"); while (1) { len = sizeof(struct sockaddr_in); n = recvfrom(sfd, buf, 1024, 0, (struct sockaddr *)&cli_addr, &len); buf[n] = '\0'; if (strncmp(buf, "syn", 3) == 0) { /* login */ char *name; name = strstr(buf, " "); /* 客户端数据格式 "syn usr_name"*/ name++; p = search_by_name(name); if (p == NULL) sendto(sfd, "fail", strlen("fail"), 0, (struct sockaddr *)&cli_addr, sizeof(cli_addr)); else { p->is_online = 1; p->addr = cli_addr; sendto(sfd, "OK", strlen("OK"), 0, (struct sockaddr *)&cli_addr, sizeof(cli_addr)); } #ifdef DEBUG print(); #endif } else if (strncmp(buf, "fin", 3) == 0) {/* logout */ char *name; name = strstr(buf, " "); name++; p = search_by_name(name); if (p == NULL) sendto(sfd, "fail", strlen("fail"), 0, (struct sockaddr *)&cli_addr, sizeof(cli_addr)); else { p->is_online = 0; memset(&p->addr, 0, sizeof(p->addr)); sendto(sfd, "OK", strlen("OK"), 0, (struct sockaddr *)&cli_addr, sizeof(cli_addr)); } #ifdef DEBUG print(); #endif } else { /* somebody talk */ char msg[32]; p = search_by_addr(&cli_addr); if (p == NULL) continue; sprintf(msg, "%s say: ", p->usr_name); for (p = head; p != NULL; p = p->next) { /* 广播 */ if (p->is_online) { n = sendto(sfd, msg, strlen(msg), 0, (struct sockaddr *)&p->addr, sizeof(p->addr)); n = sendto(sfd, buf, strlen(buf), 0, (struct sockaddr *)&p->addr, sizeof(p->addr)); } } } printf("one data over...\n"); } destroy(); return 0; }