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 /*
23 	Clock support for AMS >= 2.08.
24 */
25 
26 #include <string.h>
27 
28 #include "ticalcs.h"
29 #include "logging.h"
30 
31 #ifdef __WIN32__
32 #define strcasecmp _stricmp
33 #endif
34 
35 #define MAX_FORMAT_89	8
36 #define MAX_FORMAT_84	3
37 
38 static const char *TI_CLOCK_89[] =
39 {
40 	"",
41 	"MM/DD/YY",
42 	"DD/MM/YY",
43 	"MM.DD.YY",
44 	"DD.MM.YY",
45 	"YY.MM.DD",
46 	"MM-DD-YY",
47 	"DD-MM-YY",
48 	"YY-MM-DD",
49 	""
50 };
51 
52 static const char *TI_CLOCK_84[] =
53 {
54 	"",
55 	"M/D/Y",
56 	"D/M/Y",
57 	"Y/M/D",
58 	""
59 };
60 
61 /**
62  * ticalcs_clock_format2date:
63  * @model: a calculator model
64  * @value: a format type
65  *
66  * Convert a format type into a format string.
67  * Example: 1 -> "MM/DD/YY"
68  *
69  * Return value: a format string.
70  **/
ticalcs_clock_format2date(CalcModel model,int value)71 TIEXPORT3 const char *TICALL ticalcs_clock_format2date(CalcModel model, int value)
72 {
73 	int v;
74 
75 	if (tifiles_calc_is_ti9x(model))
76 	{
77 		if (value < 1)
78 		{
79 			v = 1;
80 		}
81 		else if (value > MAX_FORMAT_89)
82 		{
83 			v = MAX_FORMAT_89;
84 		}
85 		else
86 		{
87 			v = value;
88 		}
89 
90 		return TI_CLOCK_89[v];
91 	}
92 	else if (tifiles_calc_is_ti8x(model))
93 	{
94 		if (value < 1)
95 		{
96 			v = 1;
97 		}
98 		else if (value > MAX_FORMAT_84)
99 		{
100 			v = MAX_FORMAT_84;
101 		}
102 		else
103 		{
104 			v = value;
105 		}
106 
107 		return TI_CLOCK_84[v];
108 	}
109 
110 	return "";
111 }
112 
113 /**
114  * ticalcs_clock_date2format:
115  * @model: a calculator model
116  * @format: a format string
117  *
118  * Convert a format string into a format type.
119  * Example: "MM/DD/YY" -> 1
120  *
121  * Return value: a format string.
122  **/
ticalcs_clock_date2format(CalcModel model,const char * format)123 TIEXPORT3 int TICALL ticalcs_clock_date2format(CalcModel model, const char *format)
124 {
125 	int i = 1;
126 
127 	if (format == NULL)
128 	{
129 		ticalcs_critical("ticalcs_clock_date2format: format is NULL");
130 		return 0;
131 	}
132 
133 	if (tifiles_calc_is_ti9x(model))
134 	{
135 		for (i = 1; i <= MAX_FORMAT_89; i++)
136 		{
137 			if (!strcasecmp(TI_CLOCK_89[i], format))
138 			{
139 				break;
140 			}
141 		}
142 		if (i > MAX_FORMAT_89)
143 		{
144 			return 1;
145 		}
146 	}
147 	else if (tifiles_calc_is_ti8x(model))
148 	{
149 		for (i = 1; i <= MAX_FORMAT_84; i++)
150 		{
151 			if (!strcasecmp(TI_CLOCK_84[i], format))
152 			{
153 				break;
154 			}
155 		}
156 		if (i > MAX_FORMAT_84)
157 		{
158 			return 1;
159 		}
160 	}
161 
162 	return i;
163 }
164 
165 /**
166  * ticalcs_clock_show:
167  * @model: calc model
168  * @clock: a #CalcClock structure
169  *
170  * Display to stdout the content of the structure.
171  *
172  * Return value: always 0.
173  **/
ticalcs_clock_show(CalcModel model,CalcClock * s)174 TIEXPORT3 int TICALL ticalcs_clock_show(CalcModel model, CalcClock* s)
175 {
176 	if (s != NULL)
177 	{
178 		ticalcs_info("Date: %02i/%02i/%02i", s->day, s->month, s->year);
179 		ticalcs_info("Time: %02i/%02i/%02i", s->hours, s->minutes, s->seconds);
180 		ticalcs_info("Time format: %02i", s->time_format);
181 		ticalcs_info("Date format: %s", ticalcs_clock_format2date(model, s->date_format));
182 	}
183 
184 	return 0;
185 }
186