1 /*************************************************************************\
2  *
3  * Package:        MyLib
4  * File:           addstr.c
5  * Environment:    ANSI C
6  *
7  * Copyright (c) 2002 Pierre L'Ecuyer, DIRO, Université de Montréal.
8  * e-mail: lecuyer@iro.umontreal.ca
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted without a fee for private, research,
13  * academic, or other non-commercial purposes.
14  * Any use of this software in a commercial environment requires a
15  * written licence from the copyright owner.
16  *
17  * Any changes made to this package must be clearly identified as such.
18  *
19  * In scientific publications which used this software, a reference to it
20  * would be appreciated.
21  *
22  * Redistributions of source code must retain this copyright notice
23  * and the following disclaimer.
24  *
25  * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
26  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
27  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28  *
29 \*************************************************************************/
30 
31 #include "addstr.h"
32 
33 #include <stdio.h>
34 #include <string.h>
35 
36 
37 #define LEN1 63
38 
39 
40 
41 /* ========================== functions  ================================= */
42 
addstr_Long(char * to,const char * add,long n)43 void  addstr_Long (char *to, const char *add, long n)
44 {
45    char str[LEN1 + 1];
46    strcat (to, add);
47    sprintf (str, "%1ld", n);
48    strcat (to, str);
49 }
50 
addstr_Uint(char * to,const char * add,unsigned int n)51 void  addstr_Uint (char *to, const char *add, unsigned int n)
52 {
53    char str[LEN1 + 1];
54    strcat (to, add);
55    sprintf (str, "%1u", n);
56    strcat (to, str);
57 }
58 
addstr_Int(char * to,const char * add,int n)59 void  addstr_Int (char *to, const char *add, int n)
60 {
61    char str[LEN1 + 1];
62    strcat (to, add);
63    sprintf (str, "%1d", n);
64    strcat (to, str);
65 }
66 
addstr_Ulong(char * to,const char * add,unsigned long n)67 void  addstr_Ulong (char *to, const char *add, unsigned long n)
68 {
69    char str[LEN1 + 1];
70    strcat (to, add);
71    sprintf (str, "%1lu", n);
72    strcat (to, str);
73 }
74 
75 #ifdef USE_LONGLONG
addstr_LONG(char * to,const char * add,longlong n)76 void  addstr_LONG (char *to, const char *add, longlong n)
77 {
78    char str[LEN1 + 1];
79    strcat (to, add);
80    sprintf (str, "%1" PRIdLEAST64, n);
81    strcat (to, str);
82 }
83 
addstr_ULONG(char * to,const char * add,ulonglong n)84 void  addstr_ULONG (char *to, const char *add, ulonglong n)
85 {
86    char str[LEN1 + 1];
87    strcat (to, add);
88    sprintf (str, "%1" PRIuLEAST64, n);
89    strcat (to, str);
90 }
91 #endif
92 
addstr_Char(char * to,const char * add,char c)93 void  addstr_Char (char *to, const char *add, char c)
94 {
95    char str[LEN1 + 1];
96    strcat (to, add);
97    sprintf (str, "%c", c);
98    strcat (to, str);
99 }
100 
addstr_Double(char * to,const char * add,double x)101 void  addstr_Double (char *to, const char *add, double x)
102 {
103    char str[LEN1 + 1];
104    strcat (to, add);
105    sprintf (str, "%.16G", x);
106    strcat (to, str);
107 }
108 
addstr_Bool(char * to,const char * add,int b)109 void  addstr_Bool (char *to, const char *add, int b)
110 {
111    strcat (to, add);
112    if (b)
113       strcat (to, "TRUE");
114    else
115       strcat (to, "FALSE");
116 }
117 
addstr_ArrayLong(char * to,const char * add,int high,long val[])118 void  addstr_ArrayLong (char *to, const char *add, int high, long val[])
119 {
120    int j;
121    strcat (to, add);
122    addstr_Long (to, "(", val[0]);
123    for (j = 1; (j < high) && (j < 5); j++)
124       addstr_Long (to, ", ", val[j]);
125    if (high > 5)
126       strcat (to, ", ... )");
127    else
128       strcat (to, ")");
129 }
130 
addstr_ArrayUlong(char * to,const char * add,int high,unsigned long val[])131 void  addstr_ArrayUlong (char *to, const char *add,
132                          int high, unsigned long val[])
133 {
134    int j;
135    strcat (to, add);
136    addstr_Ulong (to, "(", val[0]);
137    for (j = 1; (j < high) && (j < 5); j++)
138       addstr_Ulong (to, ", ", val[j]);
139    if (high > 5)
140       strcat (to, ", ... )");
141    else
142       strcat (to, ")");
143 }
144 
addstr_ArrayUint(char * to,const char * add,int high,unsigned int val[])145 void  addstr_ArrayUint (char *to, const char *add, int high,
146 		        unsigned int val[])
147 {
148    int j;
149    strcat (to, add);
150    addstr_Uint (to, "(", val[0]);
151    for (j = 1; (j < high) && (j < 5); j++)
152       addstr_Uint (to, ", ", val[j]);
153    if (high > 5)
154       strcat (to, ", ... )");
155    else
156       strcat (to, ")");
157 }
158 
addstr_ArrayInt(char * to,const char * add,int high,int val[])159 void  addstr_ArrayInt (char *to, const char *add, int high, int val[])
160 {
161    int j;
162    strcat (to, add);
163    addstr_Int (to, "(", val[0]);
164    for (j = 1; (j < high) && (j < 5); j++)
165       addstr_Int (to, ", ", val[j]);
166    if (high > 5)
167       strcat (to, ", ... )");
168    else
169       strcat (to, ")");
170 }
171 
addstr_ArrayDouble(char * to,const char * add,int high,double val[])172 void  addstr_ArrayDouble (char *to, const char *add,
173                           int high, double val[])
174 {
175    int j;
176    strcat (to, add);
177    addstr_Double (to, "(", val[0]);
178    for (j = 1; (j < high) && (j < 5); j++)
179       addstr_Double (to, ", ", val[j]);
180    if (high > 5)
181       strcat (to, ", ... )");
182    else
183       strcat (to, ")");
184 }
185