diff options
author | antirez <antirez> | 2005-04-12 08:34:41 +0000 |
---|---|---|
committer | antirez <antirez> | 2005-04-12 08:34:41 +0000 |
commit | a3e9a74fcc15c97d273fbee0758dc6e8ae206e2e (patch) | |
tree | 85f8289051a6733058e7b227d3cbb1f145bd30f3 | |
parent | 116e3c15ce7fd800aab2af6b4e7d0fa854b66875 (diff) | |
download | jimtcl-a3e9a74fcc15c97d273fbee0758dc6e8ae206e2e.zip jimtcl-a3e9a74fcc15c97d273fbee0758dc6e8ae206e2e.tar.gz jimtcl-a3e9a74fcc15c97d273fbee0758dc6e8ae206e2e.tar.bz2 |
new methods for AIO files, 'read' and 'eof'. Documentation
still no longer in sync.
-rw-r--r-- | ChangeLog | 24 | ||||
-rw-r--r-- | jim-aio.c | 81 |
2 files changed, 102 insertions, 3 deletions
@@ -1,3 +1,27 @@ +2005-04-11 19:25 chi + + * jim.c: - Remove the '%lu' scan format specification. The largest + possible type in the Jim core is jim_wide. That type is a + signed one. A unsigned variante of jim_wide is not forseen. So + a '%lu' specification seems not to make any sense. + + - Some extra castings necessary to let the Jim core interpreter + compile and run under Tru64U on Dec Alpha. + + - Fix a bug in UpdateStringOfList(). If a list was created via + the Jim command [list] w/o any argument, the internal + representation of that list pointed to ZERO and the len + attribute was equally set to ZERO. If now UpdateStringOfList + was called to create the string representation of the list, + Jim_Alloc for quotingType got a ZERO size for number of bytes + to allocate. That let to SIGSEGV under Tru64U, so I changed that + to allocate at least 1 byte then. + +2005-04-11 16:34 antirez + + * ChangeLog, TODO, jim.c: RHS/Nem vision about [tailcall] + implementation/behaviour is now Jim's way to go. + 2005-04-11 13:17 antirez * ChangeLog, jim.c: [tailcall] command added @@ -1,7 +1,7 @@ /* Jim - ANSI I/O extension * Copyright 2005 Salvatore Sanfilippo <antirez@invece.org> * - * $Id: jim-aio.c,v 1.7 2005/03/31 12:20:21 antirez Exp $ + * $Id: jim-aio.c,v 1.8 2005/04/12 08:34:41 antirez Exp $ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -57,9 +57,10 @@ static int JimAioHandlerCommand(Jim_Interp *interp, int argc, AioFile *af = Jim_CmdPrivData(interp); int option; const char *options[] = { - "close", "seek", "tell", "gets", "puts", "flush", NULL + "close", "seek", "tell", "gets", "read", "puts", "flush", "eof", NULL }; - enum {OPT_CLOSE, OPT_SEEK, OPT_TELL, OPT_GETS, OPT_PUTS, OPT_FLUSH}; + enum {OPT_CLOSE, OPT_SEEK, OPT_TELL, OPT_GETS, OPT_READ, OPT_PUTS, + OPT_FLUSH, OPT_EOF}; if (argc < 2) { Jim_WrongNumArgs(interp, 1, argv, "method ?args ...?"); @@ -173,6 +174,72 @@ static int JimAioHandlerCommand(Jim_Interp *interp, int argc, Jim_SetResult(interp, objPtr); } return JIM_OK; + } else if (option == OPT_READ) { + /* READ */ + char buf[AIO_BUF_LEN]; + Jim_Obj *objPtr; + int nonewline = 0; + int neededLen = -1; /* -1 is "read as much as possible" */ + + if (argc != 2 && argc != 3) { + Jim_WrongNumArgs(interp, 2, argv, "?-nonewline? ?len?"); + return JIM_ERR; + } + if (argc == 3 && + Jim_CompareStringImmediate(interp, argv[2], "-nonewline")) + { + nonewline = 1; + argv++; + argc--; + } + if (argc == 3) { + jim_wide wideValue; + if (Jim_GetWide(interp, argv[2], &wideValue) != JIM_OK) + return JIM_ERR; + if (wideValue < 0) { + Jim_SetResultString(interp, "invalid parameter: negative len", + -1); + return JIM_ERR; + } + neededLen = (int) wideValue; + } + objPtr = Jim_NewStringObj(interp, NULL, 0); + while (neededLen != 0) { + int retval; + int readlen; + + if (neededLen == -1) { + readlen = AIO_BUF_LEN; + } else { + readlen = (neededLen > AIO_BUF_LEN ? AIO_BUF_LEN : neededLen); + } + retval = fread(buf, 1, readlen, af->fp); + if (retval > 0) { + Jim_AppendString(interp, objPtr, buf, retval); + if (neededLen != -1) { + neededLen -= retval; + } + } + if (retval != readlen) break; + } + /* Check for error conditions */ + if (ferror(af->fp)) { + /* I/O error */ + Jim_FreeNewObj(interp, objPtr); + JimAioSetError(interp); + return JIM_ERR; + } + if (nonewline) { + int len; + const char *s = Jim_GetString(objPtr, &len); + + if (len > 0 && s[len-1] == '\n') { + objPtr->length--; + objPtr->bytes[objPtr->length] = '\0'; + } + } + Jim_SetResult(interp, objPtr); + return JIM_OK; } else if (option == OPT_PUTS) { /* PUTS */ unsigned int wlen; @@ -201,6 +268,14 @@ static int JimAioHandlerCommand(Jim_Interp *interp, int argc, return JIM_ERR; } return JIM_OK; + } else if (option == OPT_EOF) { + /* EOF */ + if (argc != 2) { + Jim_WrongNumArgs(interp, 2, argv, ""); + return JIM_ERR; + } + Jim_SetResult(interp, Jim_NewIntObj(interp, feof(af->fp))); + return JIM_OK; } return JIM_OK; } |