/*
    This file is part of libtermui.

    libtermui is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    libtermui is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with libtermui.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2006, Alexandre Becoulet <alexandre.becoulet@free.fr>

*/

#ifndef TERMUI_TERMIO_PV_H_
#define TERMUI_TERMIO_PV_H_

#include <termui/term.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>

static inline termui_ssize_t
termui_io_nprintf(struct termui_term_s * fd, termui_size_t n, const char *fmt, ...)
{
  char buf[n];
  termui_ssize_t res;
  va_list list;

  va_start(list, fmt);
  res = vsnprintf(buf, n, fmt, list);
  fd->s_write(fd, buf, res > n ? n : res);
  va_end(list);

  return res;
}

static inline termui_ssize_t
termui_io_puts(const char *str, struct termui_term_s * fd)
{
  termui_size_t len = strlen(str);
  fd->s_write(fd, str, len);
  return len;
}

static inline termui_ssize_t
termui_io_put(const char *str, termui_size_t len, struct termui_term_s * fd)
{
  fd->s_write(fd, str, len);
  return len;
}

static inline void
termui_io_putc(char c, struct termui_term_s * fd)
{
  fd->s_write(fd, &c, 1);
}

static inline int_fast16_t
termui_io_getc(struct termui_term_s * fd)
{
  return fd->s_read(fd);
}

static inline termui_ssize_t
termui_io_geti(struct termui_term_s * fd, termui_cursor_pos_t *result)
{
  termui_cursor_pos_t c, res;

  for (res = 0; (c = termui_io_getc(fd)) >= '0' && c <= '9'; 
       res = res * 10 + c - '0')
    ;

  *result = res;
  return c;
}

#endif

