/* WARNING: This file was generated by dkct. Changes you make here will be lost if dkct is run again! You should modify the original source and run dkct on it. Original source: dk4strmo32.ctr */ /* Copyright (C) 2015-2017, Dirk Krause Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above opyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``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 COPYRIGHT OWNER OR CONTRIBUTORS 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. */ /** @file dk4strmo32.c The dk4strmo32 module. */ #line 95 "dk4strmo32.ctr" #include "dk4strmo32.h" #include "dk4mem.h" #include "dk4ansi.h" #include "dk4utf8.h" #include "dk4utf16.h" #include "dk4enc.h" #line 106 "dk4strmo32.ctr" void dk4stream_set_output_encoding(dk4_stream_t *strm, int oe) { #line 113 "dk4strmo32.ctr" if (NULL != strm) { switch (oe) { case DK4_FILE_ENCODING_ASCII: case DK4_FILE_ENCODING_ANSI: case DK4_FILE_ENCODING_UTF8: case DK4_FILE_ENCODING_UTF16_LE: case DK4_FILE_ENCODING_UTF16_BE: case DK4_FILE_ENCODING_32_LE: case DK4_FILE_ENCODING_32_BE: { strm->oenc = oe; } break; } } #line 127 "dk4strmo32.ctr" } int dk4stream_c32_putc(dk4_stream_t *strm, dk4_c32_t c32, dk4_er_t *erp) { unsigned char buf[16]; dk4_c16_t b16[4]; size_t sz; int back = 0; #line 139 "dk4strmo32.ctr" if (NULL != strm) { #line 140 "dk4strmo32.ctr" if (0 != ((strm->fl) & DK4_STREAM_WRITE)) { #line 141 "dk4strmo32.ctr" switch (strm->oenc) { case DK4_FILE_ENCODING_ASCII: { #line 143 "dk4strmo32.ctr" if (dkC32(0x00000100) > c32) { back = dk4stream_write_byte(strm, (unsigned char)c32, erp); } else { dk4error_set_simple_error_code(erp, DK4_E_ENCODING_FAILED); } } break; case DK4_FILE_ENCODING_ANSI: { #line 150 "dk4strmo32.ctr" if (0 != dk4ansi_encode(buf, c32)) { back = dk4stream_write_byte(strm, buf[0], erp); } else { dk4error_set_simple_error_code(erp, DK4_E_ENCODING_FAILED); } } break; case DK4_FILE_ENCODING_UTF8: { #line 157 "dk4strmo32.ctr" sz = sizeof(buf); if (0 != dk4utf8_encode(buf, &sz, c32, NULL)) { back = dk4stream_write(strm, (void *)buf, sz, erp); } else { dk4error_set_simple_error_code(erp, DK4_E_ENCODING_FAILED); } } break; case DK4_FILE_ENCODING_UTF16_LE: { #line 165 "dk4strmo32.ctr" sz = DK4_SIZEOF(b16,dk4_c16_t); if (0 != dk4utf16_encode(b16, &sz, c32, erp)) { #line 167 "dk4strmo32.ctr" if ((1 == sz) || (2 == sz)) { buf[0] = (unsigned char)( b16[0] & 0x00FF); buf[1] = (unsigned char)((b16[0] >> 8) & 0x00FF); if (2 == sz) { #line 171 "dk4strmo32.ctr" buf[2] = (unsigned char)( b16[1] & 0x00FF); buf[3] = (unsigned char)((b16[1] >> 8) & 0x00FF); #line 174 "dk4strmo32.ctr" back = dk4stream_write(strm, (void *)buf, 4, erp); } else { #line 176 "dk4strmo32.ctr" #line 177 "dk4strmo32.ctr" back = dk4stream_write(strm, (void *)buf, 2, erp); } } else { dk4error_set_simple_error_code(erp, DK4_E_ENCODING_FAILED); } } else { #line 183 "dk4strmo32.ctr" dk4error_set_simple_error_code(erp, DK4_E_ENCODING_FAILED); } } break; case DK4_FILE_ENCODING_UTF16_BE: { #line 187 "dk4strmo32.ctr" sz = DK4_SIZEOF(b16,dk4_c16_t); if (0 != dk4utf16_encode(b16, &sz, c32, erp)) { #line 189 "dk4strmo32.ctr" if ((1 == sz) || (2 == sz)) { buf[1] = (unsigned char)( b16[0] & 0x00FF); buf[0] = (unsigned char)((b16[0] >> 8) & 0x00FF); if (2 == sz) { #line 193 "dk4strmo32.ctr" buf[3] = (unsigned char)( b16[1] & 0x00FF); buf[2] = (unsigned char)((b16[1] >> 8) & 0x00FF); back = dk4stream_write(strm, (void *)buf, 4, erp); } else { #line 197 "dk4strmo32.ctr" back = dk4stream_write(strm, (void *)buf, 2, erp); } } else { #line 200 "dk4strmo32.ctr" dk4error_set_simple_error_code(erp, DK4_E_ENCODING_FAILED); } } else { #line 203 "dk4strmo32.ctr" dk4error_set_simple_error_code(erp, DK4_E_ENCODING_FAILED); } } break; case DK4_FILE_ENCODING_32_LE: { #line 207 "dk4strmo32.ctr" buf[0] = (unsigned char)( c32 & dkC32(0x000000FF)); buf[1] = (unsigned char)((c32 >> 8) & dkC32(0x000000FF)); buf[2] = (unsigned char)((c32 >> 16) & dkC32(0x000000FF)); buf[3] = (unsigned char)((c32 >> 24) & dkC32(0x000000FF)); back = dk4stream_write(strm, (void *)buf, 4, erp); } break; case DK4_FILE_ENCODING_32_BE: { #line 214 "dk4strmo32.ctr" buf[3] = (unsigned char)( c32 & dkC32(0x000000FF)); buf[2] = (unsigned char)((c32 >> 8) & dkC32(0x000000FF)); buf[1] = (unsigned char)((c32 >> 16) & dkC32(0x000000FF)); buf[0] = (unsigned char)((c32 >> 24) & dkC32(0x000000FF)); back = dk4stream_write(strm, (void *)buf, 4, erp); } break; default: { dk4error_set_simple_error_code(erp, DK4_E_BUG); } break; } } else{ dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } #line 230 "dk4strmo32.ctr" return back; } int dk4stream_c32_puts(dk4_stream_t *strm, const dk4_c32_t *txt, dk4_er_t *erp) { int back = 0; if ((NULL != strm) && (NULL != txt)) { if (0 != ((strm->fl) & DK4_STREAM_WRITE)) { back = 1; while ((dkC32(0) != *txt) && (1 == back)) { if (0 == dk4stream_c32_putc(strm, *(txt++), erp)) { back = 0; } } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } return back; } int dk4stream_write_bom_if_necessary(dk4_stream_t *strm, dk4_er_t *erp) { int back = 0; #line 263 "dk4strmo32.ctr" if (NULL != strm) { if (0 != ((strm->fl) & DK4_STREAM_WRITE)) { switch (strm->oenc) { #if VERSION_BEFORE_20141218 /* For UTF-8 files most text editors do not handle the BOM correctly, they show a glyph for unrepresentable characters. So I decided to write a BOM only for 16 bit and 32 bit character output. */ case DK4_FILE_ENCODING_UTF8: #endif case DK4_FILE_ENCODING_UTF16_LE: case DK4_FILE_ENCODING_UTF16_BE: case DK4_FILE_ENCODING_32_LE: case DK4_FILE_ENCODING_32_BE: { #line 280 "dk4strmo32.ctr" back = dk4stream_c32_putc(strm, dkC32(0x0000FEFF), erp); } break; default: { back = 1; } break; } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } #line 292 "dk4strmo32.ctr" return back; }