view help2html/support.c @ 8:ec2b657edf13

Add explicit lint-comment-style fallthrough annotations. GCC now assumes that if you don't have these you're making a mistake, which is annoying. XXX: This changeset updates the AG output files only (by hand) and is XXX: abusive - rebuilding them will erase the change. However, I need XXX: to get things to build before I can try to get AG to issue the XXX: annotations itself, so this seems like a reasonable expedient.
author David A. Holland
date Mon, 30 May 2022 23:51:43 -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);
}