Research portable Memory game | Исследовать портируемую игру Память
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.1KB

  1. #include <stdlib.h> /* malloc */
  2. #include <stdio.h> /* printf */
  3. #include <string.h>
  4. #include "uthash.h"
  5. #include "utlist.h"
  6. #include "utstring.h"
  7. typedef struct example_user_t {
  8. int id;
  9. int cookie;
  10. UT_hash_handle hh;
  11. } example_user_t;
  12. #define BUFLEN 20
  13. typedef struct el {
  14. char bname[BUFLEN];
  15. struct el *next, *prev;
  16. } el;
  17. static int namecmp(void *_a, void *_b)
  18. {
  19. el *a = (el*)_a;
  20. el *b = (el*)_b;
  21. return strcmp(a->bname,b->bname);
  22. }
  23. int main()
  24. {
  25. el *name, *elt, *tmp, etmp;
  26. int i;
  27. example_user_t *user, *users=NULL;
  28. el *head = NULL; /* important- initialize to NULL! */
  29. char linebuf[BUFLEN];
  30. FILE *file;
  31. UT_string *s;
  32. char binary[] = "\xff\xff";
  33. file = fopen( "test11.dat", "r" );
  34. if (file == NULL) {
  35. perror("can't open: ");
  36. exit(-1);
  37. }
  38. while (fgets(linebuf,BUFLEN,file) != NULL) {
  39. name = (el*)malloc(sizeof(el));
  40. if (name == NULL) {
  41. exit(-1);
  42. }
  43. strcpy(name->bname, linebuf);
  44. DL_APPEND(head, name);
  45. }
  46. DL_SORT(head, namecmp);
  47. DL_FOREACH(head,elt) {
  48. printf("%s", elt->bname);
  49. }
  50. memcpy(etmp.bname, "WES\n", 5UL);
  51. DL_SEARCH(head,elt,&etmp,namecmp);
  52. if (elt != NULL) {
  53. printf("found %s\n", elt->bname);
  54. }
  55. /* now delete each element, use the safe iterator */
  56. DL_FOREACH_SAFE(head,elt,tmp) {
  57. DL_DELETE(head,elt);
  58. }
  59. fclose(file);
  60. /* create elements */
  61. for(i=0; i<10; i++) {
  62. user = (example_user_t*)malloc(sizeof(example_user_t));
  63. if (user == NULL) {
  64. exit(-1);
  65. }
  66. user->id = i;
  67. user->cookie = i*i;
  68. HASH_ADD_INT(users,id,user);
  69. }
  70. for(user=users; user != NULL; user=(example_user_t*)(user->hh.next)) {
  71. printf("user %d, cookie %d\n", user->id, user->cookie);
  72. }
  73. utstring_new(s);
  74. utstring_bincpy(s, binary, sizeof(binary));
  75. printf("length is %u\n", (unsigned)utstring_len(s));
  76. utstring_clear(s);
  77. utstring_printf(s,"number %d", 10);
  78. printf("%s\n", utstring_body(s));
  79. utstring_free(s);
  80. return 0;
  81. }