diff options
author | apnadkarni <apnmbx-wits@yahoo.com> | 2023-02-26 06:26:01 (GMT) |
---|---|---|
committer | apnadkarni <apnmbx-wits@yahoo.com> | 2023-02-26 06:26:01 (GMT) |
commit | 2fb6b99620807fff819ce6c0c7ac60ae4774fc73 (patch) | |
tree | 8dec6f717bf9726a00157ed029948bd9d7366c0f /generic/tclIO.c | |
parent | 7b0b2ebd37ef0e7ea2e38a394ee476fed9829a35 (diff) | |
download | tcl-2fb6b99620807fff819ce6c0c7ac60ae4774fc73.zip tcl-2fb6b99620807fff819ce6c0c7ac60ae4774fc73.tar.gz tcl-2fb6b99620807fff819ce6c0c7ac60ae4774fc73.tar.bz2 |
Minor refactor, add tests
Diffstat (limited to 'generic/tclIO.c')
-rw-r--r-- | generic/tclIO.c | 59 |
1 files changed, 25 insertions, 34 deletions
diff --git a/generic/tclIO.c b/generic/tclIO.c index 82887d9..ff0e7fb 100644 --- a/generic/tclIO.c +++ b/generic/tclIO.c @@ -4238,47 +4238,38 @@ Tcl_WriteObj( if (CheckChannelErrors(statePtr, TCL_WRITABLE) != 0) { return TCL_INDEX_NONE; } - if (statePtr->encoding == NULL) { - size_t srcLen; - size_t totalWritten = 0; + size_t srcLen; + if (statePtr->encoding == NULL) { src = (char *) Tcl_GetByteArrayFromObj(objPtr, &srcLen); - /* TODO - refactor common code below */ if (src == NULL) { Tcl_SetErrno(EILSEQ); - totalWritten = TCL_INDEX_NONE; - } else { - int chunkSize = srcLen > INT_MAX ? INT_MAX : srcLen; - int written; - written = WriteBytes(chanPtr, src, chunkSize); - if (written < 0) { - return TCL_INDEX_NONE; - } - totalWritten += written; - srcLen -= chunkSize; - } while (srcLen); - - return totalWritten; + return TCL_INDEX_NONE; + } } else { - size_t srcLen; - size_t totalWritten = 0; src = Tcl_GetStringFromObj(objPtr, &srcLen); - /* - * Note original code always called WriteChars even if srcLen 0 - * so we will too. - */ - do { - int chunkSize = srcLen > INT_MAX ? INT_MAX : srcLen; - int written; - written = WriteChars(chanPtr, src, chunkSize); - if (written < 0) { - return TCL_INDEX_NONE; - } - totalWritten += written; - srcLen -= chunkSize; - } while (srcLen); - return totalWritten; } + + size_t totalWritten = 0; + /* + * Note original code always called WriteChars even if srcLen 0 + * so we will too. + */ + do { + int chunkSize = srcLen > INT_MAX ? INT_MAX : srcLen; + int written; + if (statePtr->encoding == NULL) { + written = WriteBytes(chanPtr, src, chunkSize); + } else { + written = WriteChars(chanPtr, src, chunkSize); + } + if (written < 0) { + return TCL_INDEX_NONE; + } + totalWritten += written; + srcLen -= chunkSize; + } while (srcLen); + return totalWritten; } static void |