2016-11-18 18:11:13 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
2018-02-21 10:11:39 +00:00
|
|
|
Copyright (c) 2014-2018 Chung, Hyung-Hwan. All rights reserved.
|
2016-11-18 18:11:13 +00:00
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-12-31 15:35:19 +00:00
|
|
|
#include "_stdio.h"
|
2017-01-09 09:54:49 +00:00
|
|
|
#include <moo-utl.h>
|
2016-11-18 18:11:13 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2016-12-26 18:44:47 +00:00
|
|
|
#include <errno.h>
|
2016-11-18 18:11:13 +00:00
|
|
|
#include <limits.h>
|
|
|
|
|
2017-01-09 14:52:15 +00:00
|
|
|
#if !defined(PATH_MAX)
|
|
|
|
# define PATH_MAX 256
|
|
|
|
#endif
|
|
|
|
|
2016-11-27 15:37:14 +00:00
|
|
|
typedef struct stdio_t stdio_t;
|
|
|
|
struct stdio_t
|
|
|
|
{
|
|
|
|
FILE* fp;
|
|
|
|
};
|
|
|
|
|
2018-05-07 16:53:16 +00:00
|
|
|
static moo_pfrc_t pf_open (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
|
2016-11-18 18:11:13 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oop_char_t name;
|
|
|
|
moo_oop_char_t mode;
|
2017-02-14 08:29:30 +00:00
|
|
|
stdio_t* stdio;
|
2016-11-18 18:11:13 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
#if defined(MOO_OOCH_IS_UCH)
|
|
|
|
moo_oow_t ucslen, bcslen;
|
|
|
|
moo_bch_t namebuf[PATH_MAX];
|
2017-02-14 08:29:30 +00:00
|
|
|
moo_bch_t modebuf[32]; /* TODO: mode should not be long but use dynamic-sized conversion?? */
|
2016-11-18 18:11:13 +00:00
|
|
|
#endif
|
|
|
|
|
2017-02-14 08:29:30 +00:00
|
|
|
stdio = (stdio_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
|
2017-01-09 09:54:49 +00:00
|
|
|
name = (moo_oop_char_t)MOO_STACK_GETARG(moo, nargs, 0);
|
|
|
|
mode = (moo_oop_char_t)MOO_STACK_GETARG(moo, nargs, 1);
|
2016-11-18 18:11:13 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
#if defined(MOO_OOCH_IS_UCH)
|
|
|
|
ucslen = MOO_OBJ_GET_SIZE(name);
|
|
|
|
bcslen = MOO_COUNTOF(namebuf) - 1;
|
2018-12-19 15:47:52 +00:00
|
|
|
if (moo_convootobchars(moo, MOO_OBJ_GET_CHAR_SLOT(name), &ucslen, namebuf, &bcslen) <= -1) goto softfail;
|
2016-11-18 18:11:13 +00:00
|
|
|
namebuf[bcslen] = '\0';
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
ucslen = MOO_OBJ_GET_SIZE(mode);
|
|
|
|
bcslen = MOO_COUNTOF(modebuf) - 1;
|
2018-12-19 15:47:52 +00:00
|
|
|
if (moo_convootobchars(moo, MOO_OBJ_GET_CHAR_SLOT(mode), &ucslen, modebuf, &bcslen) <= -1) goto softfail;
|
2016-11-18 18:11:13 +00:00
|
|
|
modebuf[bcslen] = '\0';
|
|
|
|
|
2017-02-14 08:29:30 +00:00
|
|
|
stdio->fp = fopen (namebuf, modebuf);
|
2016-11-18 18:11:13 +00:00
|
|
|
#else
|
2018-12-19 15:47:52 +00:00
|
|
|
stdio->fp = fopen (MOO_OBJ_GET_CHAR_SLOT(name), MOO_OBJ_GET_CHAR_SLOT(mode));
|
2016-11-18 18:11:13 +00:00
|
|
|
#endif
|
2017-02-14 08:29:30 +00:00
|
|
|
if (!stdio->fp)
|
2016-11-18 18:11:13 +00:00
|
|
|
{
|
2018-11-02 05:51:22 +00:00
|
|
|
moo_seterrwithsyserr (moo, 0, errno);
|
2017-01-12 17:48:04 +00:00
|
|
|
goto softfail;
|
2016-11-18 18:11:13 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_STACK_SETRETTORCV (moo, nargs);
|
|
|
|
return MOO_PF_SUCCESS;
|
2016-12-27 18:15:35 +00:00
|
|
|
|
2017-01-12 17:48:04 +00:00
|
|
|
softfail:
|
2017-04-03 05:43:50 +00:00
|
|
|
MOO_STACK_SETRETTOERRNUM (moo, nargs);
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_PF_SUCCESS;
|
2016-11-18 18:11:13 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 16:53:16 +00:00
|
|
|
static moo_pfrc_t pf_close (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
|
2016-11-18 18:11:13 +00:00
|
|
|
{
|
2017-02-14 08:29:30 +00:00
|
|
|
stdio_t* stdio;
|
2016-11-18 18:11:13 +00:00
|
|
|
|
2017-02-14 08:29:30 +00:00
|
|
|
stdio = (stdio_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
|
|
|
|
if (stdio->fp)
|
2016-11-18 18:11:13 +00:00
|
|
|
{
|
2017-02-14 08:29:30 +00:00
|
|
|
fclose (stdio->fp);
|
|
|
|
stdio->fp = NULL;
|
2016-11-18 18:11:13 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_STACK_SETRETTORCV (moo, nargs);
|
|
|
|
return MOO_PF_SUCCESS;
|
2016-11-18 18:11:13 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 16:53:16 +00:00
|
|
|
static moo_pfrc_t pf_gets (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
|
2016-11-18 18:11:13 +00:00
|
|
|
{
|
2017-02-14 08:29:30 +00:00
|
|
|
/* TODO: ...*/
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_STACK_SETRETTORCV (moo, nargs);
|
|
|
|
return MOO_PF_SUCCESS;
|
2016-11-18 18:11:13 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
static moo_pfrc_t __pf_puts (moo_t* moo, moo_ooi_t nargs, moo_oow_t limit)
|
2016-11-27 15:37:14 +00:00
|
|
|
{
|
2017-02-14 08:29:30 +00:00
|
|
|
stdio_t* stdio;
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_ooi_t i;
|
2016-12-14 09:40:41 +00:00
|
|
|
|
2017-02-14 08:29:30 +00:00
|
|
|
stdio = (stdio_t*)moo_getobjtrailer(moo, MOO_STACK_GETRCV(moo, nargs), MOO_NULL);
|
2016-12-14 09:40:41 +00:00
|
|
|
|
|
|
|
for (i = 0; i < nargs; i++)
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oop_char_t x;
|
|
|
|
moo_obj_char_t tmpc;
|
2016-12-14 09:40:41 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
x = (moo_oop_char_t)MOO_STACK_GETARG(moo, nargs, i);
|
|
|
|
if (MOO_OOP_IS_CHAR(x))
|
2016-12-14 09:40:41 +00:00
|
|
|
{
|
|
|
|
/* do some faking. */
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_ASSERT (moo, MOO_SIZEOF(tmpc) >= MOO_SIZEOF(moo_obj_t) + MOO_SIZEOF(moo_ooch_t));
|
2016-12-14 09:40:41 +00:00
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
tmpc.slot[0] = MOO_OOP_TO_CHAR(x);
|
|
|
|
x = (moo_oop_char_t)&tmpc;
|
|
|
|
MOO_OBJ_SET_SIZE(x, 1);
|
2016-12-14 09:40:41 +00:00
|
|
|
goto puts_string;
|
|
|
|
}
|
2017-01-09 09:54:49 +00:00
|
|
|
else if (MOO_OOP_IS_POINTER(x) && MOO_OBJ_GET_FLAGS_TYPE(x) == MOO_OBJ_TYPE_CHAR)
|
2016-12-14 09:40:41 +00:00
|
|
|
{
|
2017-02-03 06:28:12 +00:00
|
|
|
#if defined(MOO_OOCH_IS_UCH)
|
2016-12-14 09:40:41 +00:00
|
|
|
int n;
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_oow_t ucspos, ucsrem, ucslen, bcslen;
|
|
|
|
moo_bch_t bcs[1024]; /* TODO: choose a better buffer size */
|
2016-12-14 09:40:41 +00:00
|
|
|
|
|
|
|
puts_string:
|
|
|
|
ucspos = 0;
|
2017-01-09 09:54:49 +00:00
|
|
|
ucsrem = MOO_OBJ_GET_SIZE(x);
|
2016-12-27 18:15:35 +00:00
|
|
|
if (ucsrem > limit) ucsrem = limit;
|
|
|
|
|
2016-12-14 09:40:41 +00:00
|
|
|
while (ucsrem > 0)
|
|
|
|
{
|
|
|
|
ucslen = ucsrem;
|
2017-01-09 09:54:49 +00:00
|
|
|
bcslen = MOO_COUNTOF(bcs);
|
2016-12-27 18:15:35 +00:00
|
|
|
|
|
|
|
/* TODO: implement character conversion into stdio and use it instead of vm's conversion facility. */
|
2018-12-19 15:47:52 +00:00
|
|
|
if ((n = moo_convootobchars (moo, MOO_OBJ_GET_CHAR_PTR(x, ucspos), &ucslen, bcs, &bcslen)) <= -1)
|
2016-12-14 09:40:41 +00:00
|
|
|
{
|
2017-01-12 17:48:04 +00:00
|
|
|
if (n != -2 || ucslen <= 0) goto softfail;
|
2016-12-14 09:40:41 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 08:29:30 +00:00
|
|
|
if (fwrite (bcs, 1, bcslen, stdio->fp) < bcslen)
|
2016-12-27 18:15:35 +00:00
|
|
|
{
|
2018-11-02 05:51:22 +00:00
|
|
|
moo_seterrwithsyserr (moo, 0, errno);
|
2017-01-12 17:48:04 +00:00
|
|
|
goto softfail;
|
2016-12-27 18:15:35 +00:00
|
|
|
}
|
|
|
|
|
2016-12-14 09:40:41 +00:00
|
|
|
/* TODO: abort looping for async processing???? */
|
|
|
|
ucspos += ucslen;
|
|
|
|
ucsrem -= ucslen;
|
|
|
|
}
|
2017-02-03 06:28:12 +00:00
|
|
|
#else
|
|
|
|
puts_string:
|
2018-12-19 15:47:52 +00:00
|
|
|
if (fwrite (MOO_OBJ_GET_CHAR_SLOT(x), 1, MOO_OBJ_GET_SIZE(x), stdio->fp) < MOO_OBJ_GET_SIZE(x))
|
2017-02-03 06:28:12 +00:00
|
|
|
{
|
2018-11-02 05:51:22 +00:00
|
|
|
moo_seterrwithsyserr (moo, 0, errno);
|
2017-02-03 06:28:12 +00:00
|
|
|
goto softfail;
|
|
|
|
}
|
|
|
|
#endif
|
2016-12-14 09:40:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
moo_seterrnum (moo, MOO_EINVAL);
|
2017-01-12 17:48:04 +00:00
|
|
|
goto softfail;
|
2016-12-14 09:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
MOO_STACK_SETRETTORCV (moo, nargs);
|
|
|
|
return MOO_PF_SUCCESS;
|
2016-12-27 18:15:35 +00:00
|
|
|
|
2017-01-12 17:48:04 +00:00
|
|
|
softfail:
|
2017-04-03 05:43:50 +00:00
|
|
|
MOO_STACK_SETRETTOERRNUM (moo, nargs);
|
2017-01-09 09:54:49 +00:00
|
|
|
return MOO_PF_SUCCESS;
|
2016-11-27 15:37:14 +00:00
|
|
|
}
|
2016-12-06 17:21:47 +00:00
|
|
|
|
2018-05-07 16:53:16 +00:00
|
|
|
static moo_pfrc_t pf_putc (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
|
2016-12-27 18:15:35 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
return __pf_puts (moo, nargs, 1);
|
2016-12-27 18:15:35 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 16:53:16 +00:00
|
|
|
static moo_pfrc_t pf_puts (moo_t* moo, moo_mod_t* mod, moo_ooi_t nargs)
|
2016-12-27 18:15:35 +00:00
|
|
|
{
|
2017-01-09 09:54:49 +00:00
|
|
|
return __pf_puts (moo, nargs, MOO_TYPE_MAX(moo_oow_t));
|
2016-12-27 18:15:35 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 13:42:12 +00:00
|
|
|
/*TODO: add print function that can accept ByteArray
|
|
|
|
* add format function for formatted output */
|
|
|
|
|
|
|
|
|
2016-11-18 18:11:13 +00:00
|
|
|
/* ------------------------------------------------------------------------ */
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
#define C MOO_METHOD_CLASS
|
|
|
|
#define I MOO_METHOD_INSTANCE
|
2017-03-30 14:59:55 +00:00
|
|
|
#define MA MOO_TYPE_MAX(moo_oow_t)
|
2016-12-24 18:35:23 +00:00
|
|
|
|
2017-02-14 10:25:26 +00:00
|
|
|
static moo_pfinfo_t pfinfos[] =
|
2016-11-18 18:11:13 +00:00
|
|
|
{
|
2017-03-30 14:59:55 +00:00
|
|
|
{ I, { 'c','l','o','s','e','\0' }, 0, { pf_close, 0, 0 } },
|
|
|
|
{ I, { 'g','e','t','s','\0' }, 0, { pf_gets, 0, 0 } },
|
|
|
|
{ I, { 'o','p','e','n','\0' }, 0, { pf_open, 2, 2 } },
|
|
|
|
{ I, { 'p','u','t','c','\0' }, 1, { pf_putc, 0, MA } },
|
|
|
|
{ I, { 'p','u','t','s','\0' }, 1, { pf_puts, 0, MA } }
|
2016-11-18 18:11:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------ */
|
2017-02-15 11:57:24 +00:00
|
|
|
static int import (moo_t* moo, moo_mod_t* mod, moo_oop_class_t _class)
|
2016-11-29 05:25:08 +00:00
|
|
|
{
|
2017-06-16 09:45:22 +00:00
|
|
|
if (moo_setclasstrsize (moo, _class, MOO_SIZEOF(stdio_t), MOO_NULL) <= -1) return -1;
|
2017-03-30 14:59:55 +00:00
|
|
|
return 0;
|
2016-11-29 05:25:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-31 14:21:22 +00:00
|
|
|
static moo_pfbase_t* query (moo_t* moo, moo_mod_t* mod, const moo_ooch_t* name, moo_oow_t namelen)
|
2016-11-18 18:11:13 +00:00
|
|
|
{
|
2017-03-31 14:21:22 +00:00
|
|
|
return moo_findpfbase (moo, pfinfos, MOO_COUNTOF(pfinfos), name, namelen);
|
2016-11-18 18:11:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* TOOD: concept of a argument_check fucntion?
|
|
|
|
* check if receiver is a certain object?
|
|
|
|
* check if receiver is at a certain size?
|
|
|
|
* etc...
|
|
|
|
*/
|
2017-01-09 09:54:49 +00:00
|
|
|
static int sanity_check (moo_t* moo)
|
2016-11-18 18:11:13 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
static void unload (moo_t* moo, moo_mod_t* mod)
|
2016-11-18 18:11:13 +00:00
|
|
|
{
|
|
|
|
/* TODO: close all open handle?? */
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:54:49 +00:00
|
|
|
int moo_mod_stdio (moo_t* moo, moo_mod_t* mod)
|
2016-11-18 18:11:13 +00:00
|
|
|
{
|
2016-11-29 05:25:08 +00:00
|
|
|
mod->import = import;
|
2016-11-18 18:11:13 +00:00
|
|
|
mod->query = query;
|
|
|
|
mod->unload = unload;
|
2017-01-09 09:54:49 +00:00
|
|
|
mod->ctx = MOO_NULL;
|
2016-11-18 18:11:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|