1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** GUIX Component                                                        */
16 /**                                                                       */
17 /**   Utility (Utility)                                                   */
18 /**                                                                       */
19 /**************************************************************************/
20 
21 #define GX_SOURCE_CODE
22 
23 
24 /* Include necessary system files.  */
25 
26 #include "gx_api.h"
27 #include "gx_utility.h"
28 
29 
30 /**************************************************************************/
31 /*                                                                        */
32 /*  FUNCTION                                               RELEASE        */
33 /*                                                                        */
34 /*    _gx_utility_ltoa                                    PORTABLE C      */
35 /*                                                           6.1          */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    Kenneth Maxwell, Microsoft Corporation                              */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This service converts a long integer value into an ASCII string.    */
43 /*                                                                        */
44 /*  INPUT                                                                 */
45 /*                                                                        */
46 /*    value                                 Long integer value to convert */
47 /*    return_buffer                         Destination buffer for ASCII  */
48 /*                                            string                      */
49 /*    return_buffer_size                    Size of destination buffer    */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    None                                                                */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    None                                                                */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    Application Code                                                    */
62 /*    GUIX Internal Code                                                  */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  05-19-2020     Kenneth Maxwell          Initial Version 6.0           */
69 /*  09-30-2020     Kenneth Maxwell          Modified comment(s),          */
70 /*                                            resulting in version 6.1    */
71 /*                                                                        */
72 /**************************************************************************/
_gx_utility_ltoa(LONG value,GX_CHAR * return_buffer,UINT return_buffer_size)73 UINT _gx_utility_ltoa(LONG value, GX_CHAR *return_buffer, UINT return_buffer_size)
74 {
75 LONG     temp_val;
76 GX_CHAR  temp_buf[20];
77 GX_CHAR *mirror;
78 GX_CHAR *putchar;
79 INT      quotient;
80 INT      sign = 0;
81 
82     temp_val = value;
83 
84     if (value < 0)
85     {
86         sign = -1;
87         temp_val = -value;
88     }
89 
90     /* build the string in our temp_buf. The string will
91        be in reverse (mirrored) order, we will fix that
92        when we copy into the output buffer
93      */
94 
95     mirror = temp_buf;
96 
97     do
98     {
99         quotient = temp_val % 10;
100         temp_val = temp_val / 10;
101         quotient = quotient | 0x30;
102         *mirror++ = (GX_CHAR)quotient;
103     } while (temp_val != 0);
104 
105     /* put a negative sign at the end if needed */
106 
107     if (sign != 0)
108     {
109         *mirror++ = '-';
110     }
111 
112     /* now copy the result back into the caller's buffer,
113        being careful not to exceed the buffer size
114      */
115 
116     mirror--;
117     putchar = return_buffer;
118 
119     while (mirror >= temp_buf && return_buffer_size > 1)
120     {
121         *putchar++ = *mirror--;
122         return_buffer_size--;
123     }
124 
125     /* termniate the return string */
126     *putchar = 0;
127     return GX_SUCCESS;
128 }
129 
130