#include "cdefs.h"
#include "mem.h"
extern cptr object_methods[meth_max];
extern cptr string_methods[meth_max];
extern cptr strpool_methods[meth_max];
#include "cint.h"
#include "cintarray.h"
extern cptr clib_methods[meth_max];
#include "cfile.h"
#include "cstring.h"

extern cint strpool_str_5;
extern cint strpool_str_6;
extern cint strpool_str_10;
extern cint strpool_str_11;
extern cint strpool_str_12;
extern cint clib_stdo;

cint stdout_Curr_item;
cint stdout_Curr_col;
cint stdout_Col_start;
cint stdout_Col_end;
cint stdout_Indent;

cptr stdout_methods[meth_max];

cint stdout_init(){
  cint t1;
  stdout_Col_start = stdout_zeroed_cintarray();
  stdout_Col_end = stdout_zeroed_cintarray();
  t1_label: return t1;
}

cint stdout_fini(){
  cint t2;
  cintarray_mem_free((cint)stdout_Col_start);
  cintarray_mem_free((cint)stdout_Col_end);
  t2_label: return t2;
}

cint stdout_zeroed_cintarray(){
  cint stdout_result, stdout_i;
  cint t3;
  stdout_result = cintarray_mem_malloc((cint)4096);
  for (stdout_i = 0; stdout_i <= 1023; stdout_i++){
    ((cptr)(stdout_result))[stdout_i] = 0;
  };t4:;
  t3 = stdout_result;
  goto t3_label;
  t3_label: return t3;
}

cint stdout_Put(cint stdout_ob){
  cint stdout_str;
  cint t5, t6, t7, t8, t9, t10, t11, t12, t13;
  stdout_str = stdout_ob;
  t6 = ((cptr)stdout_ob)[4] /* class_of*/ != strpool_str_6;
  if (t6){
    t7 = ((cptr)(((cptr)stdout_ob)[0]))[88];
    t8 = ((f0)t7)((cint)stdout_ob); /* Mkstr*/
    stdout_str = t8;
  };
  t10 = (cptr)(((cptr)(((cptr)stdout_str)[0]))[128]); /* put_on*/
  ((f0)t10)((cint)stdout_str, (cint)clib_stdo, (cint)1, (cint)((cptr)stdout_str)[3] /* len*/);
  ((cptr)(stdout_Col_start))[stdout_Curr_item] = stdout_Curr_col;
  t11 = stdout_Curr_col + ((cptr)stdout_str)[3] /* len*/;
  stdout_Curr_col = t11;
  ((cptr)(stdout_Col_end))[stdout_Curr_item] = stdout_Curr_col;
  t12 = stdout_Curr_item < 1024;
  if (t12){
    t13 = stdout_Curr_item + 1;
    stdout_Curr_item = t13;
  };
  object_Dec_ref(stdout_str);
  t5_label: return t5;
}

cint stdout_Put_comma(){
  cint t14, t15, t16;
  t15 = stdout_Curr_col == 1;
  if (t15){
    goto t14_label;
  };
  cfile_fprintf((cint)clib_stdo, (cint)((cptr)strpool_str_10)[8] /* buf*/);
  t16 = stdout_Curr_col + 1;
  stdout_Curr_col = t16;
  t14_label: return t14;
}

cint stdout_Put_cstring(cint stdout_str){
  cint t17, t18;
  cfile_fprintf((cint)clib_stdo, (cint)stdout_str);
  t18 = stdout_Curr_col + cstring_strlen((cint)stdout_str);
  stdout_Curr_col = t18;
  t17_label: return t17;
}

cint stdout_Put_eol(){
  cint t19;
  cfile_fprintf((cint)clib_stdo, (cint)((cptr)strpool_str_11)[8] /* buf*/);
  stdout_Curr_col = 1;
  stdout_Curr_item = 0;
  t19_label: return t19;
}

cint stdout_Put_space(){
  cint t20, t21, t22;
  t21 = stdout_Curr_col == 1;
  if (t21){
    goto t20_label;
  };
  cfile_fprintf((cint)clib_stdo, (cint)((cptr)strpool_str_12)[8] /* buf*/);
  t22 = stdout_Curr_col + 1;
  stdout_Curr_col = t22;
  t20_label: return t20;
}

cint stdout_Putcol(cint stdout_ob){
  cint stdout_str, stdout_i;
  cint t23, t24, t25, t26, t27, t30, t31, t32, t35, t36, t37, t39, t40, t41, t42, t43;
  stdout_str = stdout_ob;
  t24 = ((cptr)stdout_ob)[4] /* class_of*/ != strpool_str_6;
  if (t24){
    t25 = ((cptr)(((cptr)stdout_ob)[0]))[88];
    t26 = ((f0)t25)((cint)stdout_ob); /* Mkstr*/
    stdout_str = t26;
  };
  t28:
  t30 = stdout_Curr_col < ((cptr)(stdout_Col_start))[stdout_Curr_item];
  if (!t30) goto t29;
    cfile_fprintf((cint)clib_stdo, (cint)((cptr)strpool_str_12)[8] /* buf*/);
    t31 = stdout_Curr_col + 1;
    stdout_Curr_col = t31;
  goto t28;
  t29:;
  t32 = ((cptr)stdout_ob)[4] /* class_of*/ == strpool_str_5;
  if (t32){
    t33:
    t35 = stdout_Curr_col + ((cptr)stdout_str)[3] /* len*/;
    t36 = t35 < ((cptr)(stdout_Col_end))[stdout_Curr_item];
    if (!t36) goto t34;
      cfile_fprintf((cint)clib_stdo, (cint)((cptr)strpool_str_12)[8] /* buf*/);
      t37 = stdout_Curr_col + 1;
      stdout_Curr_col = t37;
    goto t33;
    t34:;
  };
  for (stdout_i = 1; stdout_i <= ((cptr)stdout_str)[3] /* len*/; stdout_i++){
    t39 = ((cptr)(((cptr)stdout_str)[0]))[63];
    t40 = ((f0)t39)((cint)stdout_str, (cint)stdout_i); /* lookup_as_cint*/
    cfile_fputc((cint)t40, (cint)clib_stdo);
    t41 = stdout_Curr_col + 1;
    stdout_Curr_col = t41;
  };t38:;
  t42 = stdout_Curr_item < 1024;
  if (t42){
    t43 = stdout_Curr_item + 1;
    stdout_Curr_item = t43;
  };
  object_Dec_ref(stdout_str);
  t23_label: return t23;
}

cint stdout_Put_indent(){
  cint stdout_i;
  cint t44;
  for (stdout_i = 1; stdout_i <= stdout_Indent; stdout_i++){
    cfile_fputc((cint)32, (cint)clib_stdo);
  };t45:;
  t44_label: return t44;
}

cint stdout_Inc_indent(){
  cint t46, t47;
  cfile_fflush((cint)clib_stdo);
  t47 = stdout_Indent + 1;
  stdout_Indent = t47;
  t46_label: return t46;
}

cint stdout_Dec_indent(){
  cint t48, t49;
  cfile_fflush((cint)clib_stdo);
  t49 = stdout_Indent - 1;
  stdout_Indent = t49;
  t48_label: return t48;
}

void init_stdout(){
  copy_meth_table(stdout_methods, entity_methods);
  stdout_methods[0] = 0;
  stdout_methods[1] = stdout_init;
  stdout_methods[2] = stdout_fini;
  stdout_methods[144] = stdout_zeroed_cintarray;
  stdout_methods[145] = stdout_Put;
  stdout_methods[146] = stdout_Put_comma;
  stdout_methods[372] = stdout_Put_cstring;
  stdout_methods[147] = stdout_Put_eol;
  stdout_methods[148] = stdout_Put_space;
  stdout_methods[149] = stdout_Putcol;
  stdout_methods[150] = stdout_Put_indent;
  stdout_methods[151] = stdout_Inc_indent;
  stdout_methods[152] = stdout_Dec_indent;
  stdout_Curr_item = 0;
  stdout_Curr_col = 1;
  stdout_Indent = 0;
}
