Fix of typecast malloc
[fw/sdcc] / sim / ucsim / utils.cc
1 /*
2  * Simulator of microcontrollers (utils.cc)
3  *
4  * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
5  * 
6  * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
7  *
8  */
9
10 /* This file is part of microcontroller simulator: ucsim.
11
12 UCSIM is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 UCSIM is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with UCSIM; see the file COPYING.  If not, write to the Free
24 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 02111-1307, USA. */
26 /*@1@*/
27
28 #include "ddconfig.h"
29
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <stdarg.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include "i_string.h"
36
37   // prj
38 #include "stypes.h"
39 #include "pobjcl.h"
40
41
42 int
43 get_sub_opt(char **option, const char * const *tokens, char **valuep)
44 {
45   char *end, *equ;
46   int i;
47
48   if (!(end= strchr(*option, ',')))
49     end= *option + strlen(*option);
50   else
51     *end++= '\0';
52   if ((equ= strchr(*option, '=')))
53     {
54       *valuep= equ+1;
55       *equ= '\0';
56     }
57   else
58     *valuep= 0;
59   i= 0;
60   while (tokens[i] &&
61          strcmp(*option, tokens[i]))
62     i++;
63   if (!tokens[i])
64     *valuep= *option;
65   *option= end;
66   return tokens[i]?i:-1;
67 }
68
69
70 char *
71 get_id_string(struct id_element *ids, int id)
72 {
73   int i= 0;
74
75   while (ids[i].id_string &&
76          id != ids[i].id)
77     i++;
78   return(ids[i].id_string);
79 }
80
81 char *
82 get_id_string(struct id_element *ids, int id, char *def)
83 {
84   char *s= get_id_string(ids, id);
85
86   return(s?s:def);
87 }
88
89 int
90 get_string_id(struct id_element *ids, char *str)
91 {
92   int i= 0;
93
94   while (ids[i].id_string &&
95          strcmp(ids[i].id_string, str) != 0)
96     i++;
97   return(ids[i].id);
98 }
99
100 int
101 get_string_id(struct id_element *ids, char *str, int def)
102 {
103   int i= 0;
104
105   while (ids[i].id_string &&
106          strcmp(ids[i].id_string, str) != 0)
107     i++;
108   return(ids[i].id_string?ids[i].id:def);
109 }
110
111
112 extern "C" int vasprintf(char **strp, const  char *format, va_list ap);
113 extern "C" int vsnprintf(char *str, size_t size,const char *format,va_list ap);
114
115 static char *
116 vformat_string(char *format, va_list ap)
117 {
118 #ifdef HAVE_VASPRINTF
119   char *msg= NULL;
120   vasprintf(&msg, format, ap);
121   return(msg);
122 #else
123 #  ifdef HAVE_VSNPRINTF
124   char *msg= (char*)malloc(80*25);
125   vsnprintf(msg, 80*25, format, ap);
126   return(msg);
127 #  else
128 #    ifdef HAVE_VPRINTF
129   char *msg= (char*)malloc(80*25);
130   vsprintf(msg, format, ap); /* Dangerous */
131   return(msg);
132 #    endif
133 #  endif
134 #endif
135 }
136
137 char *
138 format_string(char *format, ...)
139 {
140   va_list ap;
141
142   va_start(ap, format);
143   char *s= vformat_string(format, ap);
144   va_end(ap);
145   return(s);
146 }
147
148
149 void
150 print_char_octal(char c, FILE *f)
151 {
152   if (strchr("\a\b\f\n\r\t\v\"", c))
153     switch (c)
154       {
155       case '\a': fprintf(f, "\a"); break;
156       case '\b': fprintf(f, "\b"); break;
157       case '\f': fprintf(f, "\f"); break;
158       case '\n': fprintf(f, "\n"); break;
159       case '\r': fprintf(f, "\r"); break;
160       case '\t': fprintf(f, "\t"); break;
161       case '\v': fprintf(f, "\v"); break;
162       case '\"': fprintf(f, "\""); break;
163       }
164   else if (isprint(c))
165     fprintf(f, "%c", c);
166   else
167     fprintf(f, "\\%03hho", c);
168 }
169
170
171 char *
172 object_name(class cl_base *o)
173 {
174   char *name= 0;
175
176   if (o)
177     name= o->get_name();
178   if (name &&
179       *name)
180     return(name);
181   return("(unkown)");
182 }
183
184
185 /* End of utils.cc */