Imported Upstream version 2.9.0
[debian/cc1111] / 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   char *msg= NULL;
120   vasprintf(&msg, format, ap);
121   return(msg);
122 #elif defined HAVE_VSNPRINTF
123   char *msg= (char*)malloc(80*25);
124   vsnprintf(msg, 80*25, format, ap);
125   return(msg);
126 #elif defined HAVE__VSNPRINTF
127   char *msg= (char*)malloc(80*25);
128   _vsnprintf(msg, 80*25, format, ap);
129   return(msg);
130 #else
131 #error No vasprintf or vsnprintf
132 #endif
133   va_end(ap);
134   return(msg);
135 }
136
137
138 const char *
139 object_name(class cl_base *o)
140 {
141   const char *name= 0;
142
143   if (o)
144     name= o->get_name();
145   if (name && *name)
146     return(name);
147   return("(unkown)");
148 }
149
150
151 char *
152 case_string(enum letter_case lcase, const char *str)
153 {
154   char *p= strdup(str);
155   char *s= p;
156
157   switch (lcase)
158     {
159     case case_upper:
160       while (p && *p)
161         {
162           *p= toupper(*p);
163           p++;
164        }
165       break;
166     case case_lower:
167       while (p && *p)
168         {
169           *p= tolower(*p);
170           p++;
171         }
172       break;
173     case case_case:
174       if (!p || *p == '\0')
175         break;
176       while (isspace(*p))
177         p++;
178       if (*p)
179         *p= toupper(*p);
180       break;
181     }
182   return(s);
183 }
184
185 /*const char *
186 case_string(enum letter_case lcase, const char *str)
187 {
188   char *p= NIL;
189
190   if (!str ||
191       !*str)
192     return(NIL);
193   p= strdup(str);
194   return case_string(lcase, p);
195 }*/
196
197
198 /* End of utils.cc */