/* Ofront+ 1.0 -swC -48 */
#include "SYSTEM.oh"
#include "WinApi.oh"

typedef
  struct bbKernel_Type__rec *bbKernel_Type;

struct bbKernel__1 {
  INTEGER id;
  bbKernel_Type struct_;
};

typedef
  void (*bbKernel_Command)(void);

typedef
  struct bbKernel_Directory__rec *bbKernel_Directory;

typedef
  struct bbKernel_ObjDesc {
    INTEGER fprint, offs, id;
    bbKernel_Type struct_;
  } bbKernel_ObjDesc;

typedef
  struct bbKernel_Directory__rec {
    INTEGER num;
    bbKernel_ObjDesc obj[1000000];
  } bbKernel_Directory__rec;

typedef
  void (*bbKernel_Handler)(void);

typedef
  struct bbKernel_Module__rec *bbKernel_Module;

typedef
  CHAR bbKernel_Utf8Name[256];

typedef
  struct bbKernel_Module__rec {
    bbKernel_Module next;
    SET opts;
    INTEGER refcnt;
    SHORTINT compTime[6], loadTime[6];
    INTEGER ext;
    bbKernel_Command term;
    INTEGER nofimps, nofptrs, csize, dsize, rsize, code, data, refs, procBase, varBase;
    CHAR *names;
    INTEGER *ptrs;
    bbKernel_Module *imports;
    bbKernel_Directory export_;
    bbKernel_Utf8Name name;
  } bbKernel_Module__rec;

typedef
  LONGCHAR bbKernel_Name[256];

typedef
  bbKernel_ObjDesc *bbKernel_Object;

typedef
  struct bbKernel_Signature__rec *bbKernel_Signature;

typedef
  struct bbKernel_Signature__rec {
    bbKernel_Type retStruct;
    INTEGER num;
    struct bbKernel__1 par[1000000];
  } bbKernel_Signature__rec;

typedef
  struct bbKernel_Type__rec {
    INTEGER size;
    bbKernel_Module mod;
    INTEGER id;
    bbKernel_Type base[16];
    bbKernel_Directory fields;
    INTEGER ptroffs[1000000];
  } bbKernel_Type__rec;


static INTEGER bbKernel_told, bbKernel_shift;


export void bbKernel_Beep (void);
export BOOLEAN bbKernel_IsAlpha (LONGCHAR ch);
export BOOLEAN bbKernel_IsLower (LONGCHAR ch);
export BOOLEAN bbKernel_IsUpper (LONGCHAR ch);
export LONGCHAR bbKernel_Lower (LONGCHAR ch);
export void bbKernel_MakeFileName (LONGCHAR *name, INTEGER name__len, LONGCHAR *type, INTEGER type__len);
export void bbKernel_SplitName (LONGCHAR *name, INTEGER name__len, LONGCHAR *head, INTEGER head__len, LONGCHAR *tail, INTEGER tail__len);
export void bbKernel_StringToUtf8 (LONGCHAR *in, INTEGER in__len, CHAR *out, INTEGER out__len, INTEGER *res);
export LONGINT bbKernel_Time (void);
export LONGCHAR bbKernel_Upper (LONGCHAR ch);
export void bbKernel_Utf8ToString (CHAR *in, INTEGER in__len, LONGCHAR *out, INTEGER out__len, INTEGER *res);


/*============================================================================*/

BOOLEAN bbKernel_IsAlpha (LONGCHAR ch)
{
  SHORTINT type;
  INTEGER res;
  res = GetStringTypeW(0x01, (WinApi_PtrWSTR)((SYSTEM_ADRINT)&ch), 1, &type);
  return (0x0100 & (SET)type) != 0x0;
}

/*----------------------------------------------------------------------------*/
LONGCHAR bbKernel_Upper (LONGCHAR ch)
{
  INTEGER ord;
  if ('a' <= ch && ch <= 'z') {
    return __CAP(ch);
  } else if (ch > 0x7f) {
    ord = (INTEGER)ch;
    return (LONGCHAR)((INTEGER)CharUpperW((WinApi_PtrWSTR)ord));
  } else {
    return ch;
  }
  __RETCHK((CHAR*)"bbKernel", 124159);
}

/*----------------------------------------------------------------------------*/
BOOLEAN bbKernel_IsUpper (LONGCHAR ch)
{
  if ('A' <= ch && ch <= 'Z') {
    return 1;
  } else if (ch > 0x7f) {
    return IsCharUpperW(ch) != 0;
  } else {
    return 0;
  }
  __RETCHK((CHAR*)"bbKernel", 126207);
}

/*----------------------------------------------------------------------------*/
LONGCHAR bbKernel_Lower (LONGCHAR ch)
{
  INTEGER ord;
  if ('A' <= ch && ch <= 'Z') {
    return (LONGCHAR)((INTEGER)ch + 32);
  } else if (ch > 0x7f) {
    ord = (INTEGER)ch;
    return (LONGCHAR)((INTEGER)CharLowerW((WinApi_PtrWSTR)ord));
  } else {
    return ch;
  }
  __RETCHK((CHAR*)"bbKernel", 129023);
}

/*----------------------------------------------------------------------------*/
BOOLEAN bbKernel_IsLower (LONGCHAR ch)
{
  if ('a' <= ch && ch <= 'z') {
    return 1;
  } else if (ch > 0x7f) {
    return IsCharLowerW(ch) != 0;
  } else {
    return 0;
  }
  __RETCHK((CHAR*)"bbKernel", 131071);
}

/*----------------------------------------------------------------------------*/
static struct Utf8ToString__11 {
  CHAR *in;
  INTEGER in__len;
  LONGCHAR *out;
  INTEGER out__len;
  INTEGER *res;
  struct Utf8ToString__11 *lnk;
} *Utf8ToString__11_s;

static void __12 (void);

static void __12 (void)
{
  __STRCOPYCL(Utf8ToString__11_s->in, Utf8ToString__11_s->out, Utf8ToString__11_s->out__len, (CHAR*)"bbKernel", 133138);
  *Utf8ToString__11_s->res = 2;
}

void bbKernel_Utf8ToString (CHAR *in, INTEGER in__len, LONGCHAR *out, INTEGER out__len, INTEGER *res)
{
  INTEGER i, j, val, max;
  CHAR ch;
  struct Utf8ToString__11 _s;
  _s.in = (void*)in; _s.in__len = in__len;
  _s.out = (void*)out; _s.out__len = out__len;
  _s.res = res;
  _s.lnk = Utf8ToString__11_s;
  Utf8ToString__11_s = &_s;
  ch = in[0];
  i = 1;
  j = 0;
  max = out__len - 1;
  while (ch != 0x00 && j < max) {
    if (ch < 0x80) {
      out[__X(j, out__len, (CHAR*)"bbKernel", 134928)] = ch;
      j += 1;
    } else if (ch < 0xe0) {
      val = (INTEGER)ch - 192;
      if (val < 0) {
        __12();
        Utf8ToString__11_s = _s.lnk;
        return;
      }
      ch = in[__X(i, in__len, (CHAR*)"bbKernel", 135951)];
      i += 1;
      val = (__ASHL(val, 6, INTEGER) + (INTEGER)ch) - 128;
      if (ch < 0x80 || ch >= 0xe0) {
        __12();
        Utf8ToString__11_s = _s.lnk;
        return;
      }
      out[__X(j, out__len, (CHAR*)"bbKernel", 136470)] = (LONGCHAR)val;
      j += 1;
    } else if (ch < 0xf0) {
      val = (INTEGER)ch - 224;
      ch = in[__X(i, in__len, (CHAR*)"bbKernel", 137231)];
      i += 1;
      val = (__ASHL(val, 6, INTEGER) + (INTEGER)ch) - 128;
      if (ch < 0x80 || ch >= 0xe0) {
        __12();
        Utf8ToString__11_s = _s.lnk;
        return;
      }
      ch = in[__X(i, in__len, (CHAR*)"bbKernel", 137743)];
      i += 1;
      val = (__ASHL(val, 6, INTEGER) + (INTEGER)ch) - 128;
      if (ch < 0x80 || ch >= 0xe0) {
        __12();
        Utf8ToString__11_s = _s.lnk;
        return;
      }
      out[__X(j, out__len, (CHAR*)"bbKernel", 138262)] = (LONGCHAR)val;
      j += 1;
    } else {
      __12();
      Utf8ToString__11_s = _s.lnk;
      return;
    }
    ch = in[__X(i, in__len, (CHAR*)"bbKernel", 139278)];
    i += 1;
  }
  out[__X(j, out__len, (CHAR*)"bbKernel", 139790)] = 0x00;
  if (ch == 0x00) {
    *res = 0;
  } else {
    *res = 1;
  }
  Utf8ToString__11_s = _s.lnk;
}

/*----------------------------------------------------------------------------*/
void bbKernel_StringToUtf8 (LONGCHAR *in, INTEGER in__len, CHAR *out, INTEGER out__len, INTEGER *res)
{
  INTEGER i, j, val, max;
  i = 0;
  j = 0;
  max = out__len - 3;
  while (in[__X(i, in__len, (CHAR*)"bbKernel", 142112)] != 0x00 && j < max) {
    val = (INTEGER)in[__X(i, in__len, (CHAR*)"bbKernel", 142356)];
    i += 1;
    if (val < 128) {
      out[__X(j, out__len, (CHAR*)"bbKernel", 142877)] = (CHAR)val;
      j += 1;
    } else if (val < 2048) {
      out[__X(j, out__len, (CHAR*)"bbKernel", 143402)] = (CHAR)(__ASHR(val, 6, INTEGER) + 192);
      j += 1;
      out[__X(j, out__len, (CHAR*)"bbKernel", 143658)] = (CHAR)(__MASK(val, -64) + 128);
      j += 1;
    } else {
      out[__X(j, out__len, (CHAR*)"bbKernel", 144172)] = (CHAR)(__ASHR(val, 12, INTEGER) + 224);
      j += 1;
      out[__X(j, out__len, (CHAR*)"bbKernel", 144433)] = (CHAR)(__MASK(__ASHR(val, 6, INTEGER), -64) + 128);
      j += 1;
      out[__X(j, out__len, (CHAR*)"bbKernel", 144682)] = (CHAR)(__MASK(val, -64) + 128);
      j += 1;
    }
  }
  out[__X(j, out__len, (CHAR*)"bbKernel", 145422)] = 0x00;
  if (in[__X(i, in__len, (CHAR*)"bbKernel", 145919)] == 0x00) {
    *res = 0;
  } else {
    *res = 1;
  }
}

/*----------------------------------------------------------------------------*/
void bbKernel_SplitName (LONGCHAR *name, INTEGER name__len, LONGCHAR *head, INTEGER head__len, LONGCHAR *tail, INTEGER tail__len)
{
  INTEGER i, j;
  LONGCHAR ch, lch;
  __DUP(name, name__len);
  i = 0;
  ch = name[0];
  if (ch != 0x00) {
    do {
      head[__X(i, head__len, (CHAR*)"bbKernel", 148241)] = ch;
      lch = ch;
      i += 1;
      ch = name[__X(i, name__len, (CHAR*)"bbKernel", 148479)];
    } while (!((ch == 0x00 || ch == '.') || (bbKernel_IsUpper(ch) && !bbKernel_IsUpper(lch))));
    if (ch == '.') {
      i = 0;
      ch = name[0];
    }
    head[__X(i, head__len, (CHAR*)"bbKernel", 149008)] = 0x00;
    j = 0;
    while (ch != 0x00) {
      tail[__X(j, tail__len, (CHAR*)"bbKernel", 149281)] = ch;
      i += 1;
      j += 1;
      ch = name[__X(i, name__len, (CHAR*)"bbKernel", 149312)];
    }
    tail[__X(j, tail__len, (CHAR*)"bbKernel", 149520)] = 0x00;
    if (tail[0] == 0x00) {
      __STRCOPYLL(head, tail, __MIN(tail__len, head__len), (CHAR*)"bbKernel", 149794);
      head[0] = 0x00;
    }
  } else {
    head[0] = 0x00;
    tail[0] = 0x00;
  }
}

/*----------------------------------------------------------------------------*/
void bbKernel_MakeFileName (LONGCHAR *name, INTEGER name__len, LONGCHAR *type, INTEGER type__len)
{
  INTEGER i, j;
  LONGCHAR ext[8];
  LONGCHAR ch;
  __DUP(type, type__len);
  i = 0;
  while (name[__X(i, name__len, (CHAR*)"bbKernel", 152104)] != 0x00 && name[__X(i, name__len, (CHAR*)"bbKernel", 152104)] != '.') {
    i += 1;
  }
  if (name[__X(i, name__len, (CHAR*)"bbKernel", 155391)] == '.') {
    if (name[__X(i + 1, name__len, (CHAR*)"bbKernel", 152831)] == 0x00) {
      name[__X(i, name__len, (CHAR*)"bbKernel", 152617)] = 0x00;
    }
  } else {
    if (type[0] == 0x00) {
      __MOVE(((LONGCHAR[]){111, 100, 99, 0}), ext, 8);
    } else {
      __STRCOPYLL(type, ext, __MIN(8, type__len), (CHAR*)"bbKernel", 153141);
    }
    if (i < (name__len - __STRLENL(ext, 8, (CHAR*)"bbKernel", 155135)) - 1) {
      name[__X(i, name__len, (CHAR*)"bbKernel", 153618)] = '.';
      i += 1;
      j = 0;
      ch = ext[0];
      while (ch != 0x00) {
        name[__X(i, name__len, (CHAR*)"bbKernel", 154137)] = bbKernel_Lower(ch);
        i += 1;
        j += 1;
        ch = ext[__X(j, 8, (CHAR*)"bbKernel", 154367)];
      }
      name[__X(i, name__len, (CHAR*)"bbKernel", 154879)] = 0x00;
    }
  }
}

/*----------------------------------------------------------------------------*/
LONGINT bbKernel_Time (void)
{
  INTEGER t;
  t = GetTickCount();
  if (t < bbKernel_told) {
    bbKernel_shift += 1;
  }
  bbKernel_told = t;
  return __ASHL((LONGINT)bbKernel_shift, 32, LONGINT) + (LONGINT)t;
}

/*----------------------------------------------------------------------------*/
void bbKernel_Beep (void)
{
  INTEGER res;
  res = MessageBeep(0xffffffff);
}

/*----------------------------------------------------------------------------*/

export void *bbKernel__init (void)
{
  __DEFMOD;
  __REGMOD("bbKernel", 0);
  __REGCMD("Beep", bbKernel_Beep);
/* BEGIN */
  __ENDMOD;
}
