/*
 *  MBFUNC.C -  Utility functions.
 */

#include "mbtool.h"

#define RECSIZE 128

/*
 *  Parse trailing ssid from call.
 */

pcall(c, p)
char *c;
char *p;
{
  register short i;

/*
 *  Blank fill the target buffer.
 */

  fill (c, ' ', ln_call);

/*
 *  Ignore leading spaces.
 */

  while (*p and (*p is ' ')) p++;

/*
 *  Copy the call from the string into the call buffer.
 */

  for (i = ln_call; i and *p; i--)
  {
    if (*p <= ' ') return 0;
    if (*p is '-') return atoi(++p);
    *c++ = *p++;
  }
  if (*p++ is '-') return atoi(p); else return 0;
}

/*
 *  Is the string a number?
 */

num(p)
char *p;
{
  for (; *p; p++) if (!isdigit(*p)) return false;
  return true;
}

/*
 *  "Output, no blanks".
 */

outnb(p, n)
char *p;
short n;
{
  while (n--) switch (*p)
  {
    case ' ' :
    case '\n' :
    case '\0' : return;
    default: printf("%c", *p++);
  }
}

/*
 *  Read random record.
 */

read_rec(fid, rec, buffer)
int fid;
int rec;
char buffer[];
{
  long lseek();
  long offs;

  offs = (long)rec * (long)RECSIZE;
  lseek(fid, offs, 0);
  return (read(fid, buffer, RECSIZE) is RECSIZE);
}

/*
 *  Copy LJSF string to C string.
 */

unbl(to, from, size)
char *to, *from;
int size;
{
  while (size--)
  {
    if (*from <= ' ') { *to = '\0'; return; }
    *to++ = *from++;
  }
  *to = '\0';
}

fill(adr, ch, len)
char *adr;
char ch;
int len;
{
  while (len--) *adr++ = ch;
}



