7c-dynarray.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2017 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. *
  5. * This file is part of GNU Mes.
  6. *
  7. * GNU Mes is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * GNU Mes is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <mes/lib.h>
  21. #include <mes/lib.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. void
  26. add (void *ptab, int *nb_ptr, void *data)
  27. {
  28. int nb, nb_alloc;
  29. int **pp;
  30. nb = *nb_ptr;
  31. pp = *(void ***) ptab;
  32. /* every power of two we double array size */
  33. if ((nb & (nb - 1)) == 0)
  34. {
  35. if (!nb)
  36. nb_alloc = 1;
  37. else
  38. nb_alloc = nb * 2;
  39. pp = realloc (pp, nb_alloc * sizeof (void *));
  40. *(void ***) ptab = pp;
  41. }
  42. pp[nb++] = data;
  43. *nb_ptr = nb;
  44. }
  45. typedef struct file4
  46. {
  47. char name[4];
  48. } file4_struct;
  49. typedef struct file12
  50. {
  51. int foo;
  52. int bar;
  53. char name[4];
  54. } file12_struct;
  55. //#define file file4
  56. #define file file12
  57. struct state
  58. {
  59. int bla;
  60. char **paths;
  61. int path_count;
  62. struct file **files;
  63. //file_struct **files;
  64. int file_count;
  65. };
  66. struct state g_s;
  67. int
  68. main ()
  69. {
  70. struct state *s = &g_s;
  71. char *file_name = "file-name";
  72. struct file *file;
  73. file = malloc (sizeof (struct file) + strlen (file_name));
  74. strcpy (file->name, file_name);
  75. add (&s->files, &s->file_count, file);
  76. char *path_name = "foo:bar:baz";
  77. add (&s->paths, &s->path_count, path_name);
  78. if (strcmp (*s->paths, path_name))
  79. return 1;
  80. eputs ("&PATHS=");
  81. eputs (itoa (&s->paths));
  82. eputs ("\n");
  83. eputs ("&FILES=");
  84. eputs (itoa (&s->files));
  85. eputs ("\n");
  86. // struct file *fs;
  87. // eputs ("foo\n");
  88. // fs = s->files[0];
  89. struct file *fs = s->files[0];
  90. eputs ("add s= ");
  91. eputs (itoa (s));
  92. eputs ("\n");
  93. eputs ("add fs= ");
  94. eputs (itoa (fs));
  95. eputs ("\n");
  96. eputs ("&fs->[0]=");
  97. eputs (itoa (fs->name));
  98. eputs ("\n");
  99. eputs ("fs->name=");
  100. eputs (fs->name);
  101. eputs ("\n");
  102. eputs ("ps= ");
  103. eputs (itoa (s->paths));
  104. eputs ("\n");
  105. eputs ("*ps ");
  106. eputs (*s->paths);
  107. eputs ("\n");
  108. if (strcmp (fs->name, file_name))
  109. return 2;
  110. eputs ("&fs->[0]=");
  111. eputs (itoa (fs->name));
  112. eputs ("\n");
  113. eputs ("fs->name=");
  114. eputs (fs->name);
  115. eputs ("\n");
  116. eputs ("ps= ");
  117. eputs (itoa (s->paths));
  118. eputs ("\n");
  119. eputs ("*ps ");
  120. eputs (*s->paths);
  121. eputs ("\n");
  122. file = malloc (sizeof (struct file) + strlen (file_name));
  123. file_name = "hallo";
  124. strcpy (file->name, file_name);
  125. add (&s->files, &s->file_count, file);
  126. struct file **pf = s->files;
  127. fs = pf[0];
  128. eputs ("\n");
  129. eputs ("&fs0*= ");
  130. eputs (itoa (&pf[0]));
  131. eputs ("\n");
  132. eputs ("fs0*= ");
  133. eputs (itoa (fs));
  134. eputs ("\n");
  135. fs = s->files[0];
  136. eputs ("fs0*= ");
  137. eputs (itoa (fs));
  138. eputs ("\n");
  139. eputs ("\n");
  140. pf = s->files;
  141. fs = pf[1];
  142. eputs ("&fs1*= ");
  143. eputs (itoa (&pf[1]));
  144. eputs ("\n");
  145. eputs ("fs1*= ");
  146. eputs (itoa (fs));
  147. eputs ("\n");
  148. fs = s->files[1];
  149. eputs ("fs1*= ");
  150. eputs (itoa (fs));
  151. eputs ("\n");
  152. eputs ("\n");
  153. if (strcmp (fs->name, file_name))
  154. return 3;
  155. fs = g_s.files[0];
  156. eputs ("gfs0*= ");
  157. eputs (itoa (fs));
  158. eputs ("\n");
  159. fs = g_s.files[1];
  160. eputs ("gfs1*= ");
  161. eputs (itoa (fs));
  162. eputs ("\n");
  163. eputs ("\n");
  164. if (strcmp (fs->name, file_name))
  165. return 3;
  166. return 0;
  167. }