1%% options 2 3copyright owner = Dirk Krause 4copyright year = 2015-xxxx 5SPDX-License-Identifier: BSD-3-Clause 6 7 8%% header 9 10/** @file 11 String recoding from ASCII to ANSI. 12 13 CRT on Windows: Not used. 14*/ 15 16#ifndef DK4CONF_H_INCLUDED 17#if DK4_BUILDING_DKTOOLS4 18#include "dk4conf.h" 19#else 20#include <dktools-4/dk4conf.h> 21#endif 22#endif 23 24#ifndef DK4TYPES_H_INCLUDED 25#if DK4_BUILDING_DKTOOLS4 26#include <libdk4base/dk4types.h> 27#else 28#include <dktools-4/dk4types.h> 29#endif 30#endif 31 32#ifndef DK4ERROR_H_INCLUDED 33#if DK4_BUILDING_DKTOOLS4 34#include <libdk4base/dk4error.h> 35#else 36#include <dktools-4/dk4error.h> 37#endif 38#endif 39 40#ifdef __cplusplus 41extern "C" { 42#endif 43 44/** Recode string from ASCII to ANSI. 45 @param dstb Destination buffer. 46 @param szdstb Size of destination buffer (number of bytes). 47 @param src Source string to convert. 48 @param erp Error report, may be NULL. 49 @return 1 on success, 0 on error. 50 51 Error codes: 52 - DK4_E_INVALID_ARGUMENTS<br> 53 if src or dstb is NULL or szdstb is 0, 54 - DK4_E_BUFFER_TOO_SMALL<br> 55 if dstb is too small, 56 - DK4_E_SYNTAX<br> 57 with the number of successfully recoded characters in nelem if a 58 non-recodable character was found. 59*/ 60int 61dk4recode_ascii_to_ansi( 62 char *dstb, size_t szdstb, const char *src, dk4_er_t *erp 63); 64 65#ifdef __cplusplus 66} 67#endif 68 69%% module 70 71#include "dk4conf.h" 72#include <libdk4c/dk4rec01.h> 73#include <libdk4c/dk4ansi.h> 74 75#if DK4_HAVE_ASSERT_H 76#ifndef ASSERT_H_INCLUDED 77#include <assert.h> 78#define ASSERT_H_INCLUDED 1 79#endif 80#endif 81 82 83int 84dk4recode_ascii_to_ansi( 85 char *dstb, size_t szdstb, const char *src, dk4_er_t *erp 86) 87{ 88 size_t used = 0; 89 dk4_c32_t c32 = dkC32(0); 90 int back = 0; 91 unsigned char uc = '\0'; 92#if DK4_USE_ASSERT 93 assert(NULL != dstb); 94 assert(0 < szdstb); 95 assert(NULL != src); 96#endif 97 if ((NULL != dstb) && (0 < szdstb) && (NULL != src)) { 98 back = 1; 99 while(('\0' != *src) && (1 == back) && (used < szdstb)) { 100 uc = (unsigned char)(*(src++)); 101 c32 = (dk4_c32_t)uc; 102 if (0 != dk4ansi_encode(&uc, c32)) { 103 dstb[used++] = (char)uc; 104 } else { 105 back = 0; 106 dk4error_set_elsize_nelem(erp, DK4_E_SYNTAX, 1, used); 107 } 108 } 109 if (used < szdstb) { 110 dstb[used] = '\0'; 111 } else { 112 dstb[szdstb - 1] = '\0'; 113 back = 0; 114 dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL); 115 } 116 } else { 117 dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); 118 } 119 return back; 120} 121 122 123