1 /*
2 Copyright (C) 2017-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: dk4mao8dns.ctr
12 */
13 
14 /**	@file dk4mao8dns.c The dk4mao8dns module.
15 */
16 
17 
18 
19 #include "dk4conf.h"
20 
21 #ifndef	DK4MAO8DNS_H_INCLUDED
22 #include <libdk4c/dk4mao8dns.h>
23 #endif
24 
25 #if	DK4_HAVE_STRING_H
26 #ifndef	STRING_H_INCLUDED
27 #include <string.h>
28 #define	STRING_H_INCLUDED	1
29 #endif
30 #endif
31 
32 #if	DK4_HAVE_MATH_H
33 #ifndef	MATH_H_INCLUDED
34 #if	DK4_ON_WINDOWS
35 #ifndef	_USE_MATH_DEFINES
36 #define	_USE_MATH_DEFINES 1
37 #endif
38 #endif
39 #include <math.h>
40 #define	MATH_H_INCLUDED 1
41 #endif
42 #endif
43 
44 #ifndef	DK4MAO8DBL_H_INCLUDED
45 #include <libdk4maio8d/dk4mao8dbl.h>
46 #endif
47 
48 #ifndef	DK4MAI8DDI_H_INCLUDED
49 #include <libdk4maio8d/dk4mai8ddi.h>
50 #endif
51 
52 #ifndef	DK4STR8_H_INCLUDED
53 #include <libdk4base/dk4str8.h>
54 #endif
55 
56 #if DK4_HAVE_ASSERT_H
57 #ifndef	ASSERT_H_INCLUDED
58 #include <assert.h>
59 #define	ASSERT_H_INCLUDED 1
60 #endif
61 #endif
62 
63 
64 
65 
66 
67 
68 int
dk4ma_write_c8_double_string_no_sci_to_stream(dk4_stream_t * strm,char * buf,dk4_er_t * erp)69 dk4ma_write_c8_double_string_no_sci_to_stream(
70 	dk4_stream_t	*strm,
71 	char			*buf,
72 	dk4_er_t		*erp
73 )
74 {
75 	char		*ptrex	= NULL;			/* Start of exponent */
76 	char		*ptrdd	= NULL;			/* Decimal dot */
77 	char		*ptrout	= NULL;			/* Character to output */
78 	const char	*endptr	= NULL;			/* First unusable exponent character */
79 	dk4_im_t	 dotpos	= (dk4_im_t)0L;	/* Dot position */
80 	dk4_im_t	 expon	= (dk4_im_t)0L;	/* Exponent */
81 	dk4_im_t	 i		= (dk4_im_t)0L;	/* Running index */
82 	int			 isneg	= 0;			/* Flag: Negative value */
83 	int			 res	= 0;			/* Conversion result */
84 	int			 back	= 0;			/* Result */
85 
86 #if	DK4_USE_ASSERT
87   assert(NULL != strm);
88   assert(NULL != buf);
89 #endif
90 	if ((NULL != strm) && (NULL != buf)) {
91 		back = 1;
92 		if ('-' == *buf) {
93 			isneg = 1;
94 			buf++;
95 		}
96 #if TRACE_DEBUG
97 		else {
98 		}
99 #endif
100 		/*
101 			Find exponent
102 		*/
103 		ptrex = strchr(buf, 'e');
104 		if (NULL == ptrex) { ptrex = strchr(buf, 'E'); }
105 		if (NULL != ptrex) {
106 			*(ptrex++) = '\0';
107 			if ('+' == *ptrex) { ptrex++; }
108 			res = dk4ma_input_c8_dec_dk4_im_t(&expon, ptrex, &endptr, 1, erp);
109 			if (0 == res) { expon = (dk4_im_t)0L; back = 0; }
110 
111 		}
112 #if TRACE_DEBUG
113 		else {
114 		}
115 #endif
116 		/*	Find decimal dot
117 		*/
118 		ptrdd = strchr(buf, '.');
119 		if (NULL != ptrdd) {
120 			*ptrdd = '\0';
121 			dotpos = (dk4_im_t)strlen(buf);
122 			dk4str8_cpy_to_left(ptrdd, &(ptrdd[1]));
123 		}
124 		else {
125 			dotpos = (dk4_im_t)strlen(buf);
126 		}
127 		/*	Calculate dot position from original position and exponent
128 		*/
129 		dotpos += expon;
130 		/*
131 			Write leading zero(s)
132 		*/
133 		if ((dk4_im_t)0L >= dotpos) {
134 			if (0 != isneg) {
135 				if (0 == dk4stream_write_byte(strm, '-', erp)) { back = 0; }
136 				isneg = 0;
137 			}
138 			if (0 == dk4stream_write_byte(strm, '0', erp)) { back = 0; }
139 			if (0 == dk4stream_write_byte(strm, '.', erp)) { back = 0; }
140 			i = dotpos;
141 			while ((dk4_im_t)0L > i++) {
142 				if (0 == dk4stream_write_byte(strm, '0', erp)) { back = 0; }
143 			}
144 		}
145 #if TRACE_DEBUG
146 		else {
147 		}
148 #endif
149 		/*	Write digits from buffer
150 		*/
151 		if (0 != isneg) {
152 			if (0 == dk4stream_write_byte(strm, '-', erp)) { back = 0; }
153 		}
154 #if TRACE_DEBUG
155 		else {
156 		}
157 #endif
158 		i = (dk4_im_t)0L;
159 		ptrout = buf;
160 		while ('\0' != *ptrout) {
161 
162 
163 			if ((dotpos == i) && ((dk4_im_t)0L != dotpos)) {
164 
165 				if (0 == dk4stream_write_byte(strm, '.', erp)) {
166 					back = 0;
167 				}
168 			}
169 
170 			if (0 == dk4stream_write_byte(strm, *(ptrout++), erp)) {
171 				back = 0;
172 			}
173 
174 			i++;
175 		}
176 		/*	Append trailing zero(s)
177 		*/
178 
179 		while (i++ < dotpos) {
180 			if (0 == dk4stream_write_byte(strm, '0', erp)) {
181 				back = 0;
182 			}
183 		}
184 	}
185 	else {
186 		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
187 	}
188 
189 	return  back;
190 }
191 
192 
193 
194 int
dk4ma_write_c8_double_no_sci_to_stream(dk4_stream_t * strm,double v,double eps,int ex,int min,dk4_er_t * erp)195 dk4ma_write_c8_double_no_sci_to_stream(
196 	dk4_stream_t	*strm,
197 	double			 v,
198 	double			 eps,
199 	int				 ex,
200 	int				 min,
201 	dk4_er_t		*erp
202 )
203 {
204 	char	 buf[16*sizeof(double)];
205 	size_t	 szbuf	= sizeof(buf);
206 	int		 back	= 0;
207 
208 #if	DK4_USE_ASSERT
209   assert(NULL != strm);
210 #endif
211 	if (NULL != strm) {
212 		if (fabs(v) < eps) {
213 			back = dk4stream_write_byte(strm, '0', erp);
214 		}
215 		else {
216 			if (0 != dk4ma_write_c8_double(buf, szbuf, v, ex, min, erp)) {
217 
218 				back = dk4ma_write_c8_double_string_no_sci_to_stream(
219 					strm, buf, erp
220 				);
221 			}
222 #if TRACE_DEBUG
223 			else {
224 			}
225 #endif
226 		}
227 	}
228 	else {
229 		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
230 	}
231 
232 	return back;
233 }
234 
235 
236 /* vim: set ai sw=4 ts=4 : */
237 
238