xref: /haiku/src/libs/mapm/mapmistr.c (revision 7749d0bb0c358a3279b1b9cc76d8376e900130a5)
1 
2 /*
3  *  M_APM  -  mapmistr.c
4  *
5  *  Copyright (C) 1999 - 2007   Michael C. Ring
6  *
7  *  Permission to use, copy, and distribute this software and its
8  *  documentation for any purpose with or without fee is hereby granted,
9  *  provided that the above copyright notice appear in all copies and
10  *  that both that copyright notice and this permission notice appear
11  *  in supporting documentation.
12  *
13  *  Permission to modify the software is granted. Permission to distribute
14  *  the modified code is granted. Modifications are to be distributed by
15  *  using the file 'license.txt' as a template to modify the file header.
16  *  'license.txt' is available in the official MAPM distribution.
17  *
18  *  This software is provided "as is" without express or implied warranty.
19  */
20 
21 /*
22  *      $Id: mapmistr.c,v 1.9 2007/12/03 01:55:27 mike Exp $
23  *
24  *      This file contains M_APM -> integer string function
25  *
26  *      $Log: mapmistr.c,v $
27  *      Revision 1.9  2007/12/03 01:55:27  mike
28  *      Update license
29  *
30  *      Revision 1.8  2003/07/21 20:37:09  mike
31  *      Modify error messages to be in a consistent format.
32  *
33  *      Revision 1.7  2003/03/31 21:52:07  mike
34  *      call generic error handling function
35  *
36  *      Revision 1.6  2002/11/03 22:28:02  mike
37  *      Updated function parameters to use the modern style
38  *
39  *      Revision 1.5  2001/08/06 16:58:20  mike
40  *      improve the new function
41  *
42  *      Revision 1.4  2001/08/05 23:18:48  mike
43  *      fix function when input is not an integer but the
44  *      number is close to rounding upwards (NNN.9999999999....)
45  *
46  *      Revision 1.3  2000/02/03 22:48:38  mike
47  *      use MAPM_* generic memory function
48  *
49  *      Revision 1.2  1999/07/18 01:33:04  mike
50  *      minor tweak to code alignment
51  *
52  *      Revision 1.1  1999/07/12 02:06:08  mike
53  *      Initial revision
54  */
55 
56 #include "m_apm_lc.h"
57 
58 /****************************************************************************/
59 void	m_apm_to_integer_string(char *s, M_APM mtmp)
60 {
61 void    *vp;
62 UCHAR	*ucp, numdiv, numrem;
63 char	*cp, *p, sbuf[128];
64 int	ct, dl, numb, ii;
65 
66 vp = NULL;
67 ct = mtmp->m_apm_exponent;
68 dl = mtmp->m_apm_datalength;
69 
70 /*
71  *  if |input| < 1, result is "0"
72  */
73 
74 if (ct <= 0 || mtmp->m_apm_sign == 0)
75   {
76    s[0] = '0';
77    s[1] = '\0';
78    return;
79   }
80 
81 if (ct > 112)
82   {
83    if ((vp = (void *)MAPM_MALLOC((ct + 32) * sizeof(char))) == NULL)
84      {
85       /* fatal, this does not return */
86 
87       M_apm_log_error_msg(M_APM_FATAL,
88                           "\'m_apm_to_integer_string\', Out of memory");
89      }
90 
91    cp = (char *)vp;
92   }
93 else
94   {
95    cp = sbuf;
96   }
97 
98 p  = cp;
99 ii = 0;
100 
101 /* handle a negative number */
102 
103 if (mtmp->m_apm_sign == -1)
104   {
105    ii = 1;
106    *p++ = '-';
107   }
108 
109 /* get num-bytes of data (#digits / 2) to use in the string */
110 
111 if (ct > dl)
112   numb = (dl + 1) >> 1;
113 else
114   numb = (ct + 1) >> 1;
115 
116 ucp = mtmp->m_apm_data;
117 
118 while (TRUE)
119   {
120    M_get_div_rem_10((int)(*ucp++), &numdiv, &numrem);
121 
122    *p++ = numdiv + '0';
123    *p++ = numrem + '0';
124 
125    if (--numb == 0)
126      break;
127   }
128 
129 /* pad with trailing zeros if the exponent > datalength */
130 
131 if (ct > dl)
132   memset(p, '0', (ct + 1 - dl));
133 
134 cp[ct + ii] = '\0';
135 strcpy(s, cp);
136 
137 if (vp != NULL)
138   MAPM_FREE(vp);
139 }
140 /****************************************************************************/
141