Example #1
0
static void FlushBuf(TCmpStruct * pWork)
{
    unsigned char save_ch1;
    unsigned char save_ch2;
    unsigned int size = 0x800;

    pWork->write_buf(pWork->out_buff, &size, pWork->param);

    save_ch1 = pWork->out_buff[0x800];
    save_ch2 = pWork->out_buff[pWork->out_bytes];
    pWork->out_bytes -= 0x800;

    lmemset(pWork->out_buff, 0, 0x802);

    if(pWork->out_bytes != 0)
        pWork->out_buff[0] = save_ch1;
    if(pWork->out_bits != 0)
        pWork->out_buff[pWork->out_bytes] = save_ch2;
}
Example #2
0
LPITCB FAR PASCAL G23A05toIni (LPITCB lpITC, LPITCI lpIni)
{
    lmemset (lpITC, 0, sizeof (*lpITC));
    g72x_init_state (&lpITC->gbG2X);
    return (lpITC);                     /* Return Init/Term/Cont block  */
}
Example #3
0
/*
 * Program entry point
 */
uint main(uint argc, uchar* argv[])
{
  uchar* title_info = "L:     F1:Save ESC:Exit"; /* const */
  uchar  line_ibcd[4]; /* To store line number digits */
  uint   ibcdt;

  uint i = 0;
  uint n = 0;
  uint result = 0;

  /* buff is fixed size and allocated in far memory so it
   * can be big enough.
   * buff_size is the size in bytes actually used in buff
   * buff_cursor_offset is the linear offset of current
   * cursor position inside buff
   */
  lp_t buff = 0;
  ul_t buff_size = 0;
  ul_t buff_cursor_offset = 0;

  /* First line number to display in the editor area */
  uint current_line = 0;

  /* Var to get key presses */
  uint k = 0;

  struct FS_ENTRY entry;

  /* Chck usage */
  if(argc != 2) {
    putstr("Usage: %s <file>\n\r\n\r", argv[0]);
    putstr("<file> can be:\n\r");
    putstr("-an existing file path: opens existing file to edit\n\r");
    putstr("-a new file path: opens empty editor. File will be created on save\n\r");
    putstr("\n\r");
    return 1;
  }

  /* Allocate fixed size text buffer */
  buff = lmalloc(0xFFFFL);
  if(buff == 0) {
    putstr("Error: can't allocate memory\n\r");
    return 1;
  }

  /* Find file */
  n = get_entry(&entry, argv[1], UNKNOWN_VALUE, UNKNOWN_VALUE);

  /* Load file or show error */
  if(n<ERROR_ANY && (entry.flags & FST_FILE)) {
    ul_t offset = 0;
    uchar cbuff[512];
    setlc(buff, entry.size, 0);
    buff_size = entry.size;
    while(result = read_file(cbuff, argv[1], (uint)offset, sizeof(cbuff))) {
      if(result >= ERROR_ANY) {
        lmfree(buff);
        putstr("Can't read file %s (error=%x)\n\r", argv[1], result);
        return 1;
      }
      lmemcpy(buff + offset, lp(cbuff), (ul_t)result);
      offset += result;
    }
    if(offset != entry.size) {
      lmfree(buff);
      putstr("Can't read file (readed %d bytes, expected %d)\n\r",
        (uint)offset, entry.size);
      return 1;
    }
    /* Buffer must finish with a 0 and */
    /* must fit at least this 0, so buff_size can't be 0 */
    if(buff_size == 0 || getlc(buff + buff_size-1L) != 0) {
      buff_size++;
    }
  }

  /* Create 1 byte buffer if this is a new file */
  /* This byte is for the final 0 */
  if(buff_size == 0) {
    buff_size = 1;
    lmemset(buff, 0, buff_size);
  }

  /* Get screen size */
  get_screen_size(SSM_CHARS, &SCREEN_WIDTH, &SCREEN_HEIGHT);

  /* Allocate screen buffer */
  screen_buff = lmalloc((ul_t)(SCREEN_WIDTH*(SCREEN_HEIGHT-1)));
  if(screen_buff == 0) {
    putstr("Error: can't allocate memory\n\r");
    lmfree(buff);
    return 1;
  }

  /* Clear screen buffer */
  lmemset(screen_buff, 0, (ul_t)(SCREEN_WIDTH*(SCREEN_HEIGHT-1)));

  /* Write title */
  for(i=0; i<strlen(argv[1]); i++) {
    putchar_attr(i, 0, argv[1][i], TITLE_ATTRIBUTES);
  }
  for(; i<SCREEN_WIDTH-strlen(title_info); i++) {
    putchar_attr(i, 0, ' ', TITLE_ATTRIBUTES);
  }
  for(; i<SCREEN_WIDTH; i++) {
    putchar_attr(i, 0,
      title_info[i+strlen(title_info)-SCREEN_WIDTH], TITLE_ATTRIBUTES);
  }

  /* Show buffer and set cursor at start */
  set_show_cursor(HIDE_CURSOR);
  show_buffer_at_line(buff, current_line);
  set_cursor_position(0, 1);
  set_show_cursor(SHOW_CURSOR);

  /* Main loop */
  while(k != KEY_ESC) {
    uint col, line;

    /* Getmouse state */
    get_mouse_state(SSM_CHARS, &mouse_x, &mouse_y, &mouse_buttons);

    /* Process buttons */
    if(mouse_buttons & MOUSE_LEFT_BUTTON) {
      buff_cursor_offset = linecol_to_buffer_offset(buff, mouse_x, mouse_y-1);
    }

    /* Get key press */
    k = getkey(KM_NO_WAIT);

    /* Process key actions */

    /* Keys to ignore */
    if((k>KEY_F1 && k<=KEY_F10) ||
      k==KEY_F11 || k==KEY_F12 ||
      k==KEY_PRT_SC || k==KEY_INS ||
      k==0) {
      continue;

    /* Key F1: Save */
    } else if(k == KEY_F1) {
      ul_t offset = 0;
      uchar cbuff[512];
      result = 0;
      while(offset<buff_size && result<ERROR_ANY) {
        ul_t to_copy = min(sizeof(cbuff), buff_size-offset);
        lmemcpy(lp(cbuff), buff + offset, to_copy);
        result = write_file(cbuff, argv[1], (uint)offset, (uint)to_copy, FWF_CREATE | FWF_TRUNCATE);
        offset += to_copy;
      }

      /* Update state indicator */
      if(result < ERROR_ANY) {
        putchar_attr(strlen(argv[1]), 0, ' ', TITLE_ATTRIBUTES);
      } else {
        putchar_attr(strlen(argv[1]), 0, '*', (TITLE_ATTRIBUTES&0xF0)|AT_T_RED);
      }
      /* This opperation takes some time, so clear keyboard buffer */
      getkey(KM_CLEAR_BUFFER);

    /* Cursor keys: Move cursor */
    } else if(k == KEY_UP) {
      buffer_offset_to_linecol(buff, buff_cursor_offset, &col, &line);
      if(line > 0) {
        line -= 1;
        buff_cursor_offset = linecol_to_buffer_offset(buff, col, line);
      }

    } else if(k == KEY_DOWN) {
      buffer_offset_to_linecol(buff, buff_cursor_offset, &col, &line);
      line += 1;
      buff_cursor_offset = linecol_to_buffer_offset(buff, col, line);

    } else if(k == KEY_LEFT) {
      if(buff_cursor_offset > 0) {
        buff_cursor_offset--;
      }

    } else if(k == KEY_RIGHT) {
      if(buff_cursor_offset < buff_size - 1) {
        buff_cursor_offset++;
      }

    /* HOME, END, PG_UP and PG_DN keys */
    } else if(k == KEY_HOME) {
      buffer_offset_to_linecol(buff, buff_cursor_offset, &col, &line);
      col = 0;
      buff_cursor_offset = linecol_to_buffer_offset(buff, col, line);

    } else if(k == KEY_END) {
      buffer_offset_to_linecol(buff, buff_cursor_offset, &col, &line);
      col = 0xFFFF;
      buff_cursor_offset = linecol_to_buffer_offset(buff, col, line);

    } else if(k == KEY_PG_DN) {
      buffer_offset_to_linecol(buff, buff_cursor_offset, &col, &line);
      line += SCREEN_HEIGHT-1;
      buff_cursor_offset = linecol_to_buffer_offset(buff, col, line);

    } else if(k == KEY_PG_UP) {
      buffer_offset_to_linecol(buff, buff_cursor_offset, &col, &line);
      line -= min(line, SCREEN_HEIGHT-1);
      buff_cursor_offset = linecol_to_buffer_offset(buff, col, line);


    /* Backspace key: delete char before cursor and move cursor there */
    } else if(k == KEY_BACKSPACE) {
      if(buff_cursor_offset > 0) {
        lmemcpy(buff+buff_cursor_offset-1L, buff+buff_cursor_offset, buff_size-buff_cursor_offset);
        buff_size--;
        putchar_attr(strlen(argv[1]), 0, '*', TITLE_ATTRIBUTES);
        buff_cursor_offset--;
      }

    /* Del key: delete char at cursor */
    } else if(k == KEY_DEL) {
      if(buff_cursor_offset < buff_size-1) {
        lmemcpy(buff+buff_cursor_offset, buff+buff_cursor_offset+1, buff_size-buff_cursor_offset-1);
        buff_size--;
        putchar_attr(strlen(argv[1]), 0, '*', TITLE_ATTRIBUTES);
      }

    /* Any other key but esc: insert char at cursor */
    } else if(k != KEY_ESC && k != 0) {

      if(k == KEY_RETURN) {
        k = '\n';
      }
      if(k == KEY_TAB) {
        k = '\t';
      }
      lmemcpy(buff+buff_cursor_offset+1, buff+buff_cursor_offset, buff_size-buff_cursor_offset);
      setlc(buff, buff_cursor_offset++, k);
      buff_size++;
      putchar_attr(strlen(argv[1]), 0, '*', TITLE_ATTRIBUTES);
    }

    /* Update cursor position and display */
    buffer_offset_to_linecol(buff, buff_cursor_offset, &col, &line);
    if(line < current_line) {
      current_line = line;
    } else if(line > current_line + SCREEN_HEIGHT - 2) {
      current_line = line - SCREEN_HEIGHT + 2;
    }

    /* Update line number in title */
    /* Compute bcd value (reversed) */
    ibcdt = min(9999, buffer_offset_to_fileline(buff, buff_cursor_offset)+1);
    n = SCREEN_WIDTH-strlen(title_info)+2;
    for(i=0; i<4; i++) {
      line_ibcd[i] = ibcdt%10;
      ibcdt /= 10;
      if(ibcdt==0) {
        ibcdt = i;
        break;
      }
    }
    /* Display it */
    for(i=0; i<4; i++) {
      uchar c = i<=ibcdt?line_ibcd[ibcdt-i]+'0':' ';
      putchar_attr(n+i, 0, c, TITLE_ATTRIBUTES);
    }

    set_show_cursor(HIDE_CURSOR);
    show_buffer_at_line(buff, current_line);
    line -= current_line;
    line += 1;
    set_cursor_position(col, line);
    set_show_cursor(SHOW_CURSOR);
  }

  /* Free screen buffer */
  lmfree(screen_buff);

  /* Free buffer */
  lmfree(buff);

  /* Reset screen */
  clear_screen();
  set_cursor_position(0, 0);

  return 0;
}