* debugger/mcs51/break.c, debugger/mcs51/cmd.c,
[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 const char *
71 get_id_string(struct id_element *ids, int id)
72 {
73   int i= 0;
74
75   while (ids[i].id_string && id != ids[i].id)
76     i++;
77   return(ids[i].id_string);
78 }
79
80 const char *
81 get_id_string(struct id_element *ids, int id, const char *def)
82 {
83   const char *s= get_id_string(ids, id);
84
85   return(s?s:def);
86 }
87
88 int
89 get_string_id(struct id_element *ids, const char *str)
90 {
91   int i= 0;
92
93   while (ids[i].id_string && strcmp(ids[i].id_string, str) != 0)
94     i++;
95   return(ids[i].id);
96 }
97
98 int
99 get_string_id(struct id_element *ids, const char *str, int def)
100 {
101   int i= 0;
102
103   while (ids[i].id_string && strcmp(ids[i].id_string, str) != 0)
104     i++;
105   return(ids[i].id_string?ids[i].id:def);
106 }
107
108
109 extern "C" int vasprintf(char **strp, const  char *format, va_list ap);
110 extern "C" int vsnprintf(char *str, size_t size,const char *format,va_list ap);
111
112 char *
113 format_string(const char *format, ...)
114 {
115   va_list ap;
116
117   va_start(ap, format);
118 #ifdef HAVE_VASPRINTF
119   int res;
120   char *msg= NULL;
121   res = vasprintf(&msg, format, ap);
122   return(msg);
123 #elif defined HAVE_VSNPRINTF
124   int res;
125   char *msg= (char*)malloc(80*25);
126   res = vsnprintf(msg, 80*25, format, ap);
127   return(msg);
128 #elif defined HAVE__VSNPRINTF
129   char *msg= (char*)malloc(80*25);
130   _vsnprintf(msg, 80*25, format, ap);
131   return(msg);
132 #else
133 #error No vasprintf or vsnprintf
134 #endif
135   va_end(ap);
136   return(msg);
137 }
138
139
140 const char *
141 object_name(class cl_base *o)
142 {
143   const char *name= 0;
144
145   if (o)
146     name= o->get_name();
147   if (name && *name)
148     return(name);
149   return("(unkown)");
150 }
151
152
153 char *
154 case_string(enum letter_case lcase, const char *str)
155 {
156   char *p= strdup(str);
157   char *s= p;
158
159   switch (lcase)
160     {
161     case case_upper:
162       while (p && *p)
163         {
164           *p= toupper(*p);
165           p++;
166        }
167       break;
168     case case_lower:
169       while (p && *p)
170         {
171           *p= tolower(*p);
172           p++;
173         }
174       break;
175     case case_case:
176       if (!p || *p == '\0')
177         break;
178       while (isspace(*p))
179         p++;
180       if (*p)
181         *p= toupper(*p);
182       break;
183     }
184   return(s);
185 }
186
187 /*const char *
188 case_string(enum letter_case lcase, const char *str)
189 {
190   char *p= NIL;
191
192   if (!str ||
193       !*str)
194     return(NIL);
195   p= strdup(str);
196   return case_string(lcase, p);
197 }*/
198
199
200 /* End of utils.cc */