shithub: orca

ref: c4982a97396bc85953177c51eccfa00e3900889a
dir: /plan9.c/

View raw version
#include "plan9.h"
#include "field.h"
#include "gbuffer.h"
#include "sim.h"

Usz orca_round_up_power2(Usz x) {
  assert(x <= SIZE_MAX / 2 + 1);
  x -= 1;
  x |= x >> 1;
  x |= x >> 2;
  x |= x >> 4;
  x |= x >> 8;
  x |= x >> 16;
#if SIZE_MAX > UINT32_MAX
  x |= x >> 32;
#endif
  return x + 1;
}

bool orca_is_valid_glyph(Glyph c) {
  if (c >= '0' && c <= '9')
    return true;
  if (c >= 'A' && c <= 'Z')
    return true;
  if (c >= 'a' && c <= 'z')
    return true;
  switch (c) {
  case '!':
  case '#':
  case '%':
  case '*':
  case '.':
  case ':':
  case ';':
  case '=':
  case '?':
    return true;
  }
  return false;
}

static void
usage(void)
{
  print("usage: orca [-t ticks] [-f file]\n");
  exits("usage");
}

void
main(int argc, char **argv)
{
  const char *input_file = "/fd/0";
  int ticks = 1;
  bool print_output = true;
  Field field;

  ARGBEGIN{
  case 't':
    ticks = atoi(EARGF(usage()));
    break;
  case 'f':
    input_file = EARGF(usage());
    break;
  }ARGEND;

  field_init(&field);
  Field_load_error fle = field_load_file(input_file, &field);
  if (fle != Field_load_error_ok) {
    field_deinit(&field);
    char const *errstr = "Unknown";
    switch (fle) {
    case Field_load_error_ok:
      break;
    case Field_load_error_cant_open_file:
      errstr = "Unable to open file";
      break;
    case Field_load_error_too_many_columns:
      errstr = "Grid file has too many columns";
      break;
    case Field_load_error_too_many_rows:
      errstr = "Grid file has too many rows";
      break;
    case Field_load_error_no_rows_read:
      errstr = "Grid file has no rows";
      break;
    case Field_load_error_not_a_rectangle:
      errstr = "Grid file is not a rectangle";
      break;
    }
    exits(errstr);
  }
  Mbuf_reusable mbuf_r;
  mbuf_reusable_init(&mbuf_r);
  mbuf_reusable_ensure_size(&mbuf_r, field.height, field.width);
  Oevent_list oevent_list;
  oevent_list_init(&oevent_list);
  Usz max_ticks = (Usz)ticks;
  for (Usz i = 0; i < max_ticks; ++i) {
    mbuffer_clear(mbuf_r.buffer, field.height, field.width);
    oevent_list_clear(&oevent_list);
    orca_run(field.buffer, mbuf_r.buffer, field.height, field.width, i,
             &oevent_list, 0);
  }
  mbuf_reusable_deinit(&mbuf_r);
  oevent_list_deinit(&oevent_list);
  if (print_output)
    field_fput(&field, stdout);
  field_deinit(&field);

  exits(nil);
}