view help2html/support.c @ 21:1c9dac05d040

Add lint-style FALLTHROUGH annotations to fallthrough cases. (in the parse engine and thus the output code) Document this, because the old output causes warnings with gcc10.
author David A. Holland
date Mon, 13 Jun 2022 00:04:38 -0400
parents 60d3ca9d3f6b
children
line wrap: on
line source

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <err.h>

#include "must.h"
#include "stringdict.h"
#include "support.h"

////////////////////////////////////////////////////////////

void intstack_init(struct intstack *is) {
  is->pos = 0;
}

void intstack_push(struct intstack *is, int val) {
  if (is->pos >= INTSTACKSIZE) {
    errx(1, "Paragraph stack overflow - increase INTSTACKSIZE and recompile");
  }
  is->vals[is->pos++] = val;
}

int intstack_pop(struct intstack *is) {
  assert(is->pos > 0);
  return is->vals[--is->pos];
}

int intstack_top(const struct intstack *is) {
  assert(is->pos > 0);
  return is->vals[is->pos-1];
}

////////////////////////////////////////////////////////////

static const struct stringdict *sortdict;
static int sorter(const void *av, const void *bv) {
  const char *as = stringdict_getbynum(sortdict, *(const unsigned *)av);
  const char *bs = stringdict_getbynum(sortdict, *(const unsigned *)bv);
  return strcmp(as, bs);
}

struct permutation *mySort(const struct stringdict *sd) {
  sortdict = sd;

  /* establish permutation vector */
  int i, n = stringdict_count(sortdict);
  int *pv = must_malloc(n*sizeof(int));
  for (i=0; i<n; i++) {
    pv[i] = i;
  }

  /* sort it */
  qsort(pv, n, sizeof(int), sorter);

  sortdict = NULL;

  /* return it */
  struct permutation *p = must_malloc(sizeof(*p));
  p->v = pv;
  p->num = n;
  return p;
}

void permutation_destroy(struct permutation *p) {
  free(p->v);
  free(p);
}