}
}
+static int
+compare_int(const void *a_, const void *b_)
+{
+ const int *a = a_;
+ const int *b = b_;
+ return *a < *b ? -1 : *a > *b;
+}
+
+static void
+check_permutation(int *a, int n, const char *name)
+{
+ int b[n];
+ memcpy(b, a, n * sizeof *a);
+ qsort(b, n, sizeof *b, compare_int);
+ for (int i = 0; i < n; i++)
+ if (b[i] != i)
+ {
+ fprintf(stderr, "bad %s permutation:", name);
+ for (int i = 0; i < n; i++)
+ fprintf(stderr, " %d", a[i]);
+ putc('\n', stderr);
+ exit(1);
+ }
+}
+
static void
-dump_category(int level)
+dump_category(int level, int *indexes, int *n_indexes)
{
dump_value__ (level, true);
match_byte(0);
int indx = get_u32();
int n_categories = get_u32();
- if (indx != -1 && n_categories != 0)
+ if (indx != -1)
{
- fprintf(stderr, "index not -1 but subcategories\n");
- exit(1);
+ if (n_categories != 0)
+ {
+ fprintf(stderr, "index not -1 but subcategories\n");
+ exit(1);
+ }
+ indexes[(*n_indexes)++] = indx;
}
if (n_categories > 0)
printf (", %d subcategories:", n_categories);
+ else
+ printf (", index %d", indx);
printf("\n");
for (int i = 0; i < n_categories; i++)
- dump_category (level + 1);
+ dump_category (level + 1, indexes, n_indexes);
}
static void
match_byte(0);
n_categories = get_u32();
printf("%d nested categories\n", n_categories);
+
+ int indexes[1024];
+ int n_indexes = 0;
for (int i = 0; i < n_categories; i++)
- dump_category (0);
+ dump_category (0, indexes, &n_indexes);
+ check_permutation(indexes, n_indexes, "categories");
}
int n_dims;
}
}
-static int
-compare_int(const void *a_, const void *b_)
-{
- const int *a = a_;
- const int *b = b_;
- return *a < *b ? -1 : *a > *b;
-}
-
static void
dump_data(void)
{
match_u32_assert(n_dims - t);
/* The next n_dims numbers are a permutation of the dimension numbers. */
- int a[n_dims], b[n_dims];
- for (int i = 0; i < n_dims; i++)
- a[i] = b[i] = get_u32();
- qsort(b, n_dims, sizeof *b, compare_int);
+ int a[n_dims];
for (int i = 0; i < n_dims; i++)
- if (b[i] != i)
- {
- fprintf(stderr, "bad dimension permutation:");
- for (int i = 0; i < n_dims; i++)
- fprintf(stderr, " %d", a[i]);
- putc('\n', stderr);
- exit(1);
- }
+ a[i] = get_u32();
+ check_permutation(a, n_dims, "dimensions");
int x = get_u32();
printf ("%d data values, starting at %08x\n", x, pos);