1 /*
2 Copyright (C) 2014-2021, Dirk Krause
3 SPDX-License-Identifier: BSD-3-Clause
4 */
5
6 /*
7 WARNING: This file was generated by the dkct program (see
8 http://dktools.sourceforge.net/ for details).
9 Changes you make here will be lost if dkct is run again!
10 You should modify the original source and run dkct on it.
11 Original source: dk3madso.ctr
12 */
13
14 /** @file dk3madso.c The dk3madso module.
15 */
16
17
18
19
20 #include <libdk3c/dk3ma.h>
21 #if DK3_HAVE_STDLIB_H
22 #include <stdlib.h>
23 #endif
24 #if DK3_HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27 #if DK3_HAVE_STRING_H
28 #include <string.h>
29 #endif
30 #if DK3_HAVE_ERRNO_H
31 #include <errno.h>
32 #endif
33
34 #include <libdk3c/dk3str.h>
35
36
37
38
39
40
41
42 int
dk3ma_d_to_c8_string(char * rb,size_t sz,double va)43 dk3ma_d_to_c8_string(char *rb, size_t sz, double va)
44 {
45 int back = 0;
46 #if DK3_HAVE_SNPRINTF || DK3_HAVE__SNPRINTF || DK3_ON_WINDOWS
47 int res;
48 if ((NULL != rb) && (0 < sz)) {
49 #if DK3_HAVE_SNPRINTF
50 res = snprintf(rb, sz, "%g", va);
51 #else
52 res = _snprintf(rb, sz, "%g", va);
53 #endif
54 if (0 < res) { /* Anything written? */
55 if ((size_t)res < sz) { /* Buffer large enough? */
56 rb[res] = '\0';
57 rb[sz - 1] = '\0';
58 back = 1;
59 } else { /* Buffer too small! */
60 rb[sz - 1] = '\0';
61 }
62 } else { /* Nothing written! */
63 rb[0] = '\0';
64 }
65 }
66 #else
67 char buf[128];
68 if ((NULL != rb) && (0 < sz)) {
69 sprintf(buf, "%g", va);
70 if (dk3str_c8_len(buf) < sz) {
71 dk3str_c8_cpy(rb, buf);
72 back = 1;
73 } else {
74 rb[0] = '\0';
75 }
76 }
77 #endif
78 return back;
79 }
80
81
82
83 int
dk3ma_d_to_string(dkChar * rb,size_t sz,double va)84 dk3ma_d_to_string(dkChar *rb, size_t sz, double va)
85 {
86 #if DK3_CHAR_SIZE > 1
87 char buf[128];
88 char *cptr;
89 dkChar *dptr;
90 int back = 0;
91 if ((NULL != rb) && (0 < sz)) {
92 if (dk3ma_d_to_c8_string(buf, sizeof(buf), va)) {
93 if (dk3str_c8_len(buf) < sz) {
94 back = 1;
95 dptr = rb; cptr = buf;
96 while(*cptr) { *(dptr++) = (dkChar)(*(cptr++)); }
97 *dptr = dkT('\0');
98 } else {
99 rb[0] = dkT('\0');
100 }
101 } else {
102 rb[0] = dkT('\0');
103 }
104 }
105 return back;
106 #else
107 return (dk3ma_d_to_c8_string(rb, sz, va));
108 #endif
109 }
110
111
112