1 /* Hey EMACS -*- linux-c -*- */
2 /* $Id$ */
3 
4 /*  libticalcs - Ti Calculator library, a part of the TiLP project
5  *  Copyright (C) 1999-2005  Romain Li�vin
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software Foundation,
19  *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <stdio.h>
23 #include <string.h>
24 
25 #include "ticalcs.h"
26 #include "gettext.h"
27 #include "internal.h"
28 #include "logging.h"
29 #include "error.h"
30 
31 /**
32  * ticalcs_model_to_string:
33  * @model: a calculator model.
34  *
35  * Do an integer to string conversion.
36  *
37  * Return value: a string like "TI92+".
38  **/
ticalcs_model_to_string(CalcModel model)39 TIEXPORT3 const char *TICALL ticalcs_model_to_string(CalcModel model)
40 {
41 	return tifiles_model_to_string(model);
42 }
43 
44 /**
45  * ticalcs_string_to_model:
46  * @str: a calculator model as string like "TI92".
47  *
48  * Do a string to integer conversion.
49  *
50  * Return value: a calculator model.
51  **/
ticalcs_string_to_model(const char * str)52 TIEXPORT3 CalcModel TICALL ticalcs_string_to_model(const char *str)
53 {
54 	return tifiles_string_to_model(str);
55 }
56 
57 
ticalcs_scrfmt_to_string(CalcScreenFormat format)58 TIEXPORT3 const char *TICALL ticalcs_scrfmt_to_string(CalcScreenFormat format)
59 {
60 	switch (format)
61 	{
62 	case SCREEN_FULL: return _("full");
63 	case SCREEN_CLIPPED: return _("clipped");
64 	default: return _("unknown");
65 	}
66 }
67 
ticalcs_string_to_scrfmt(const char * str)68 TIEXPORT3 CalcScreenFormat TICALL ticalcs_string_to_scrfmt(const char *str)
69 {
70 	if (str != NULL)
71 	{
72 		if (!strcmp(str, _("full")))
73 		{
74 			return SCREEN_FULL;
75 		}
76 		else if (!strcmp(str, _("clipped")))
77 		{
78 			return SCREEN_CLIPPED;
79 		}
80 	}
81 	else
82 	{
83 		ticalcs_critical("ticalcs_string_to_scrfmt(NULL)");
84 	}
85 
86 	return SCREEN_CLIPPED;
87 }
88 
89 
ticalcs_pathtype_to_string(CalcPathType type)90 TIEXPORT3 const char *TICALL ticalcs_pathtype_to_string(CalcPathType type)
91 {
92 	switch (type)
93 	{
94 	case PATH_FULL: return _("full");
95 	case PATH_LOCAL: return _("local");
96 	default: return _("unknown");
97 	}
98 }
99 
ticalcs_string_to_pathtype(const char * str)100 TIEXPORT3 CalcPathType TICALL ticalcs_string_to_pathtype(const char *str)
101 {
102 	if (str != NULL)
103 	{
104 		if (!strcmp(str, _("full")))
105 		{
106 			return PATH_FULL;
107 		}
108 		else if (!strcmp(str, _("local")))
109 		{
110 			return PATH_LOCAL;
111 		}
112 	}
113 	else
114 	{
115 		ticalcs_critical("ticalcs_string_to_pathtype(NULL)");
116 	}
117 
118 	return PATH_FULL;
119 }
120 
121 
ticalcs_memtype_to_string(CalcMemType type)122 TIEXPORT3 const char *TICALL ticalcs_memtype_to_string(CalcMemType type)
123 {
124 	switch (type)
125 	{
126 	case MEMORY_FREE: return _("free");
127 	case MEMORY_USED: return _("used");
128 	default: return _("unknown");
129 	}
130 }
131 
ticalcs_string_to_memtype(const char * str)132 TIEXPORT3 CalcMemType TICALL ticalcs_string_to_memtype(const char *str)
133 {
134 	if (str != NULL)
135 	{
136 		if (!strcmp(str, _("free")))
137 		{
138 			return MEMORY_FREE;
139 		}
140 		else if (!strcmp(str, _("used")))
141 		{
142 			return MEMORY_USED;
143 		}
144 	}
145 
146 	return MEMORY_NONE;
147 }
148 
ticalcs_infos_to_string(CalcInfos * infos,char * str,uint32_t maxlen)149 TIEXPORT3 int TICALL ticalcs_infos_to_string(CalcInfos *infos, char *str, uint32_t maxlen)
150 {
151 	if (infos != NULL)
152 	{
153 		char language_ids[25];
154 		char device_type[3];
155 		char hw_version[11];
156 		char clock_speed[15];
157 		char lcd_width[20];
158 		char lcd_height[20];
159 		char bpp[11];
160 		char color_screen[11];
161 		char ram_phys[30];
162 		char ram_user[30];
163 		char ram_free[30];
164 		char flash_phys[30];
165 		char flash_user[30];
166 		char flash_free[30];
167 
168 		language_ids[0] = 0;
169 		if (infos->mask & INFOS_LANG_ID)
170 		{
171 			sprintf(language_ids, "%d %d", infos->language_id, infos->sub_lang_id);
172 		}
173 
174 		device_type[0] = 0;
175 		if (infos->mask & INFOS_DEVICE_TYPE)
176 		{
177 			sprintf(device_type, "%02X", infos->device_type);
178 		}
179 		hw_version[0] = 0;
180 		if (infos->mask & INFOS_HW_VERSION)
181 		{
182 			sprintf(hw_version, "%d", infos->hw_version);
183 		}
184 
185 		clock_speed[0] = 0;
186 		if (infos->mask & INFOS_CLOCK_SPEED)
187 		{
188 			sprintf(clock_speed, "%d MHz", infos->clock_speed);
189 		}
190 
191 		lcd_width[0] = 0;
192 		if (infos->mask & INFOS_LCD_WIDTH)
193 		{
194 			sprintf(lcd_width, "%d pixels", infos->lcd_width);
195 		}
196 		lcd_height[0] = 0;
197 		if (infos->mask & INFOS_LCD_HEIGHT)
198 		{
199 			sprintf(lcd_height, "%d pixels", infos->lcd_height);
200 		}
201 		bpp[0] = 0;
202 		if (infos->mask & INFOS_BPP)
203 		{
204 			sprintf(bpp, "%d pixels", infos->bits_per_pixel);
205 		}
206 		color_screen[0] = 0;
207 		if (infos->mask & INFOS_COLOR_SCREEN)
208 		{
209 			sprintf(color_screen, "%d", infos->color_screen);
210 		}
211 
212 		ram_phys[0] = 0; // The casts below aren't truncating in practice until one of the models grows more than 4 GB of RAM or Flash.
213 		if (infos->mask & INFOS_RAM_PHYS)
214 		{
215 			sprintf(ram_phys, "%lu B (%lu KB)", (unsigned long)infos->ram_phys, (unsigned long)((infos->ram_phys + 512) >> 10));
216 		}
217 		ram_user[0] = 0;
218 		if (infos->mask & INFOS_RAM_USER)
219 		{
220 			sprintf(ram_user, "%lu B (%lu KB)", (unsigned long)infos->ram_user, (unsigned long)((infos->ram_user + 512) >> 10));
221 		}
222 		ram_free[0] = 0;
223 		if (infos->mask & INFOS_RAM_FREE)
224 		{
225 			sprintf(ram_free, "%lu B (%lu KB)", (unsigned long)infos->ram_free, (unsigned long)((infos->ram_free + 512) >> 10));
226 		}
227 		flash_phys[0] = 0;
228 		if (infos->mask & INFOS_FLASH_PHYS)
229 		{
230 			sprintf(flash_phys, "%lu B (%lu KB)", (unsigned long)infos->flash_phys, (unsigned long)((infos->flash_phys + 512) >> 10));
231 		}
232 		flash_user[0] = 0;
233 		if (infos->mask & INFOS_FLASH_USER)
234 		{
235 			sprintf(flash_user, "%lu B (%lu KB)", (unsigned long)infos->flash_user, (unsigned long)((infos->flash_user + 512) >> 10));
236 		}
237 		flash_free[0] = 0;
238 		if (infos->mask & INFOS_FLASH_FREE)
239 		{
240 			sprintf(flash_free, "%lu B (%lu KB)", (unsigned long)infos->flash_free, (unsigned long)((infos->flash_free + 512) >> 10));
241 		}
242 
243 		ticalcs_slprintf(str, maxlen,
244 			"%s"
245 			"%s\n"
246 			"%s"
247 			"%s\n"
248 			"%s"
249 			"%s\n"
250 			"\n"
251 			"%s"
252 			"%s\n"
253 			"%s"
254 			"%s\n"
255 			"%s"
256 			"%s\n"
257 			"%s"
258 			"%s\n"
259 			"%s"
260 			"%s\n"
261 			"%s"
262 			"%s\n"
263 			"%s"
264 			"%s\n"
265 			"%s"
266 			"%s\n"
267 			"\n"
268 			"%s"
269 			"%s\n"
270 			"%s"
271 			"%s\n"
272 			"%s"
273 			"%s\n"
274 			"%s"
275 			"%s\n"
276 			"\n"
277 			"%s"
278 			"%s\n"
279 			"%s"
280 			"%s\n"
281 			"%s"
282 			"%s\n"
283 			"%s"
284 			"%s\n"
285 			"%s"
286 			"%s\n"
287 			"%s"
288 			"%s\n"
289 			"\n"
290 			"%s"
291 			"%s\n",
292 
293 			(infos->mask & INFOS_PRODUCT_NAME) ? _("Product Name: ") : "",
294 			(infos->mask & INFOS_PRODUCT_NAME) ? infos->product_name : "",
295 			(infos->mask & INFOS_PRODUCT_ID) ? _("Product ID: ") : "",
296 			(infos->mask & INFOS_PRODUCT_ID) ? infos->product_id : "",
297 			(infos->mask & INFOS_LANG_ID) ? _("Language ID: ") : "",
298 			language_ids,
299 
300 			(infos->mask & INFOS_DEVICE_TYPE) ? _("Device Type: ") : "",
301 			device_type,
302 			(infos->mask & INFOS_HW_VERSION) ? _("Hardware Version: ") : "",
303 			hw_version,
304 			(infos->mask & INFOS_BOOT_VERSION) ? _("Boot Version: ") : "",
305 			(infos->mask & INFOS_BOOT_VERSION) ? infos->boot_version : "",
306 			(infos->mask & INFOS_BOOT2_VERSION) ? _("Boot2 Version: ") : "",
307 			(infos->mask & INFOS_BOOT2_VERSION) ? infos->boot2_version : "",
308 			(infos->mask & INFOS_OS_VERSION) ? _("OS Version: ") : "",
309 			(infos->mask & INFOS_OS_VERSION) ? infos->os_version : "",
310 			(infos->mask & INFOS_RUN_LEVEL) ? _("Run level: ") : "",
311 			(infos->mask & INFOS_RUN_LEVEL) ? ((infos->run_level == 2) ? "OS" : "boot") : "",
312 			(infos->mask & INFOS_CLOCK_SPEED) ? _("Clock speed: ") : "",
313 			clock_speed,
314 			(infos->mask & INFOS_EXACT_MATH) ? _("Exact math engine: ") : "",
315 			(infos->mask & INFOS_EXACT_MATH) ? ((infos->exact_math) ? _("Yes") : _("No")) : "",
316 
317 			(infos->mask & INFOS_LCD_WIDTH) ? _("LCD width: ") : "",
318 			lcd_width,
319 			(infos->mask & INFOS_LCD_HEIGHT) ? _("LCD height: ") : "",
320 			lcd_height,
321 			(infos->mask & INFOS_BPP) ? _("Bits per pixel: ") : "",
322 			bpp,
323 			(infos->mask & INFOS_COLOR_SCREEN) ? _("Color screen: ") : "",
324 			color_screen,
325 
326 			(infos->mask & INFOS_RAM_PHYS) ? _("Physical RAM: ") : "",
327 			ram_phys,
328 			(infos->mask & INFOS_RAM_USER) ? _("User RAM: ") : "",
329 			ram_user,
330 			(infos->mask & INFOS_RAM_FREE) ? _("Free RAM: ") : "",
331 			ram_free,
332 			(infos->mask & INFOS_FLASH_PHYS) ? _("Physical Flash: ") : "",
333 			flash_phys,
334 			(infos->mask & INFOS_FLASH_USER) ? _("User Flash: ") : "",
335 			flash_user,
336 			(infos->mask & INFOS_FLASH_FREE) ? _("Free Flash: ") : "",
337 			flash_free,
338 
339 			(infos->mask & INFOS_BATTERY) ? _("Battery: ") : "",
340 			(infos->mask & INFOS_BATTERY) ? (infos->battery ? _("good") : _("low")) : "");
341 		return 0;
342 	}
343 	else
344 	{
345 		ticalcs_critical("%s: infos is NULL", __FUNCTION__);
346 		return ERR_INVALID_PARAMETER;
347 	}
348 }
349