format.c 22.5 KB
Newer Older
1
/* format.c - attempts to emulate excel's number formatting ability.
Arturo Espinosa's avatar
Arturo Espinosa committed
2
 * Copyright (C) 1998 Chris Lahey, Miguel de Icaza
3
 *
4
5
 * Redid the format parsing routine to make it accept more of the Excel
 * formats.  The number rendeing code from Chris has not been touched,
Arturo Espinosa's avatar
Arturo Espinosa committed
6
 * that routine is pretty good.
7
 *
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

Chris Lahey's avatar
Chris Lahey committed
23
#include <config.h>
24
#include <gnome.h>
25
26
#include <stdio.h>
#include <string.h>
27
#include <time.h>
28
#include <math.h>
29
#include <locale.h>
Arturo Espinosa's avatar
Compile    
Arturo Espinosa committed
30
#include <ctype.h>
31
32
33
#ifdef HAVE_IEEEFP_H
#    include <ieeefp.h>
#endif
34
35
36
#include "gnumeric.h"
#include "format.h"
#include "dates.h"
37
#include "utils.h"
38

39
40
41
/* Points to the locale information for number display */
static struct lconv *lc;

42
static void style_entry_free (gpointer data, gpointer user_data);
43
44


Chris Lahey's avatar
Chris Lahey committed
45
/*
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 * The returned string is newly allocated.
 *
 * Current format is an optional date specification followed by an
 * optional number specification.
 *
 * A date specification is an arbitrary sequence of characters (other
 * than '#', '0', '?', or '.') which is copied to the output.  The
 * standard date fields are substituted for.  If it ever finds an a or
 * a p it lists dates in 12 hour time, otherwise, it lists dates in 24
 * hour time.
 *
 * A number specification is as described in the relavent portions of
 * the excel formatting information.  Commas can currently only appear
 * at the end of the number specification.  Fractions are not yet
Arturo Espinosa's avatar
Arturo Espinosa committed
60
 * supported.
61
 */
62

63

64
65
66
67
/*
 * Parses the year field at the beginning of the format.  Returns the
 * number of characters used.
 */
68
69
static int
append_year (GString *string, gchar *format, struct tm *time_split)
70
{
71
	char temp [5];
Arturo Espinosa's avatar
Arturo Espinosa committed
72

73
	if (tolower (format [1]) != 'y'){
74
75
76
		g_string_append_c (string, 'y');
		return 1;
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
77

Arturo Espinosa's avatar
Compile    
Arturo Espinosa committed
78
	if (tolower (format [2]) != 'y' || tolower (format [3]) != 'y'){
Miguel de Icaza's avatar
Miguel de Icaza committed
79
		sprintf (temp, "%02d", time_split->tm_year % 100);
80
81
82
83
84
85
86
87
		g_string_append (string, temp);
		return 2;
	}

	sprintf (temp, "%04d", time_split->tm_year + 1900);
	g_string_append (string, temp);

	return 4;
88
89
}

90
91
92
93
94
95
/*
 * Parses the month field at the beginning of the format.  Returns the
 * number of characters used.
 */
static int
append_month (GString *string, gchar *format, struct tm *time_split)
96
{
97
	char temp [3];
Arturo Espinosa's avatar
Arturo Espinosa committed
98

99
	if (tolower (format [1]) != 'm'){
Miguel de Icaza's avatar
New:    
Miguel de Icaza committed
100
		sprintf (temp, "%d", time_split->tm_mon+1);
101
102
103
		g_string_append( string, temp);
		return 1;
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
104

105
	if (tolower (format [2]) != 'm')
106
	{
Miguel de Icaza's avatar
New:    
Miguel de Icaza committed
107
		sprintf (temp, "%02d", time_split->tm_mon+1);
108
109
110
		g_string_append (string, temp);
		return 2;
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
111

112
	if (tolower (format [3]) != 'm'){
Miguel de Icaza's avatar
New:    
Miguel de Icaza committed
113
		g_string_append (string, _(month_short [time_split->tm_mon])+1);
114
115
116
117
118
		return 3;
	}

	g_string_append (string, _(month_long [time_split->tm_mon]));
	return 4;
119
120
}

121
122
123
124
125
126
/*
 * Parses the hour field at the beginning of the format.  Returns the
 * number of characters used.
 */
static int
append_hour (GString *string, gchar *format, struct tm *time_split, int timeformat)
127
{
128
129
	char temp[3];

130
	if (tolower (format [1]) != 'h'){
131
132
133
134
135
136
137
	    sprintf (temp, "%d", timeformat ? (time_split->tm_hour % 12) : time_split->tm_hour);
	    g_string_append (string, temp);
	    return 1;
	}
	sprintf (temp, "%02d", timeformat ? (time_split->tm_hour % 12) : time_split->tm_hour);
	g_string_append (string, temp);
	return 2;
138
139
}

140
141
142
143
144
145
/*
 * Parses the day field at the beginning of the format.  Returns the
 * number of characters used.
 */
static int
append_day (GString *string, gchar *format, struct tm *time_split)
146
{
147
148
	char temp[3];

149
	if (tolower (format [1]) != 'd'){
150
151
152
153
154
		sprintf (temp, "%d", time_split->tm_mday);
		g_string_append (string, temp);
		return 1;
	}

155
	if (tolower (format [2]) != 'd'){
156
157
158
159
160
		sprintf (temp, "%02d", time_split->tm_mday);
		g_string_append (string, temp);
		return 2;
	}

161
	if (tolower (format [3]) != 'd'){
Miguel de Icaza's avatar
New:    
Miguel de Icaza committed
162
		g_string_append (string, _(day_short[time_split->tm_wday])+1);
163
164
165
166
167
168
		return 3;
	}

	g_string_append (string, _(day_long[time_split->tm_wday]));

	return 4;
169
170
}

171
172
173
174
175
176
/*
 * Parses the minute field at the beginning of the format.  Returns the
 * number of characters used.
 */
static int
append_minute (GString *string, gchar *format, struct tm *time_split)
177
{
178
179
	char temp [3];

180
	if (tolower (format [1]) != 'm'){
181
182
183
184
185
186
187
188
189
		sprintf (temp, "%d", time_split->tm_min);
		g_string_append (string, temp);
		return 1;
	}

	sprintf (temp, "%02d", time_split->tm_min);
	g_string_append (string, temp);

	return 2;
190
191
}

192
193
194
195
196
197
/*
 * Parses the second field at the beginning of the format.  Returns the
 * number of characters used.
 */
static int
append_second (GString *string, gchar *format, struct tm *time_split)
198
{
199
	char temp[3];
Arturo Espinosa's avatar
Arturo Espinosa committed
200

201
	if (tolower (format [1]) != 's'){
202
203
204
205
206
207
208
209
210
		sprintf (temp, "%d", time_split->tm_sec);
		g_string_append (string, temp);
		return 1;
	}

	sprintf (temp, "%02d", time_split->tm_sec);
	g_string_append (string, temp);

	return 2;
211
212
}

213
214
215
216
217
218
/*
 * Parses the day part field at the beginning of the format.  Returns
 * the number of characters used.
 */
static int
append_half (GString *string, gchar *format, struct tm *time_split)
Chris Lahey's avatar
Chris Lahey committed
219
{
220
	if (time_split->tm_hour <= 11){
221
		if (tolower (format [0]) == 'a' || tolower (format [0]) == 'p')
222
223
224
225
226
			g_string_append_c (string, 'a');
		else
			g_string_append_c (string, 'A');
	}
	else {
227
		if (tolower (format [0]) == 'a' || tolower (format [0]) == 'p')
228
229
230
231
			g_string_append_c (string, 'p');
		else
			g_string_append_c (string, 'P');
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
232

233
	if (tolower (format [1]) == 'm'){
234
235
236
237
		g_string_append_c (string, format [1]);
		return 2;
	} else
		return 1;
Chris Lahey's avatar
Chris Lahey committed
238
239
}

240
/*
Miguel de Icaza's avatar
Miguel de Icaza committed
241
242
 * Since the Excel formating codes contain a number of ambiguities,
 * this routine does some analisis on the format first.
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
 */
static void
pre_parse_format (StyleFormatEntry *style)
{
	char *format;

	style->want_am_pm = 0;
	for (format = style->format; *format; format++){
		switch (*format){
		case '"':
			for (format++; *format && *format != '"'; format++)
				;
			if (*format)
				format++;
			break;
Arturo Espinosa's avatar
Arturo Espinosa committed
258

259
260
261
262
263
264
265
266
267
268
269
		case '\\':
			if (*(format+1))
				format++;
			else
				return;
			break;

		case 'a':
		case 'p':
		case 'A':
		case 'P':
270
			if (tolower (*(format+1) == 'm'))
271
272
273
274
275
276
				style->want_am_pm = 1;
			break;
		}
	}
}

Chris Lahey's avatar
Chris Lahey committed
277
278
typedef struct
{
279
280
281
	int decimal;
	int timeformat;
	int hasnumbers;
282
} xformat_info;
Chris Lahey's avatar
Chris Lahey committed
283

284
/*
Miguel de Icaza's avatar
Miguel de Icaza committed
285
286
 * This routine should always return, it can not fail, in the worst
 * case it should just downgrade to simplistic formatting
Chris Lahey's avatar
Chris Lahey committed
287
 */
288
289
290
void
format_compile (StyleFormat *format)
{
291
292
293
294
295
296
	GString *string = g_string_new ("");
	int i;
	int which = 0;
	int length = strlen (format->format);
	StyleFormatEntry standard_entries[4];
	StyleFormatEntry *temp;
Arturo Espinosa's avatar
Arturo Espinosa committed
297

298
299
	g_list_free (format->format_list);
	format->format_list = 0;
Arturo Espinosa's avatar
Arturo Espinosa committed
300

301
	/* g_string_maybe_expand (string, length); */
Arturo Espinosa's avatar
Arturo Espinosa committed
302

303
	for (i = 0; i < length; i++){
Chris Lahey's avatar
Chris Lahey committed
304

Miguel de Icaza's avatar
Miguel de Icaza committed
305
306
		switch (format->format[i]){

307
		case ';':
Miguel de Icaza's avatar
Miguel de Icaza committed
308
			if (which < 4){
309
310
311
312
313
314
315
316
317
318
319
320
321
				standard_entries [which].format = g_malloc0 (string->len + 1);
				strncpy (standard_entries[which].format, string->str, string->len);
				standard_entries [which].format[string->len] = 0;
				standard_entries [which].restriction_type = '*';
				which++;
			}
			string = g_string_truncate (string, 0);
			break;
		default:
			string = g_string_append_c (string, format->format [i]);
			break;
		}
	}
Chris Lahey's avatar
Chris Lahey committed
322

323
324
325
326
327
328
329
	if (which < 4){
		standard_entries[which].format = g_malloc0 (string->len + 1);
		strncpy (standard_entries[which].format, string->str, string->len);
		standard_entries[which].format[string->len] = 0;
		standard_entries[which].restriction_type = '*';
		which++;
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
330

331
332
333
	/* Set up restriction types. */
	standard_entries[1].restriction_type = '<';
	standard_entries[1].restriction_value = 0;
Miguel de Icaza's avatar
Miguel de Icaza committed
334
335
	switch (which){

336
337
338
339
340
341
342
343
344
345
346
347
348
	case 4:
		standard_entries[3].restriction_type = '@';
		/* Fall through. */
	case 3:
		standard_entries[2].restriction_type = '=';
		standard_entries[2].restriction_value = 0;
		standard_entries[0].restriction_type = '>';
		standard_entries[0].restriction_value = 0;
		break;
	case 2:
		standard_entries[0].restriction_type = '.';  /* . >= */
		standard_entries[0].restriction_value = 0;
		break;
Chris Lahey's avatar
Chris Lahey committed
349
	}
Miguel de Icaza's avatar
Miguel de Icaza committed
350
351

	for (i = 0; i < which; i++){
352
353
354
355
356
		temp = g_new (StyleFormatEntry, 1);
		*temp = standard_entries[i];
		pre_parse_format (temp);
		format->format_list = g_list_append (format->format_list, temp);
	}
Miguel de Icaza's avatar
Miguel de Icaza committed
357

358
	g_string_free (string, TRUE);
Chris Lahey's avatar
Chris Lahey committed
359
360
361
}

static void
362
style_entry_free(gpointer data, gpointer user_data)
Chris Lahey's avatar
Chris Lahey committed
363
{
364
	StyleFormatEntry *entry = data;
Arturo Espinosa's avatar
Arturo Espinosa committed
365

366
	g_free (entry->format);
367
	g_free (entry);
368
369
}

Miguel de Icaza's avatar
Miguel de Icaza committed
370
371
372
373
374
375
376
/*
 * This routine is invoked when the last user of the
 * format is gone (ie, refcount has reached zero) just
 * before the StyleFormat structure is actually released.
 *
 * resources allocated in format_compile should be disposed here
 */
377
378
379
void
format_destroy (StyleFormat *format)
{
380
381
	g_list_foreach (format->format_list, style_entry_free, NULL);
	g_list_free (format->format_list);
Arturo Espinosa's avatar
Arturo Espinosa committed
382
	format->format_list = NULL;
383
384
}

385
386
387
388
389
390
391
392
393
394
395
396
397
static struct {
	char       *name;
	StyleColor *color;
} format_colors [] = {
	{ N_("black")   },
	{ N_("blue")    },
	{ N_("cyan")    },
	{ N_("green")   },
	{ N_("magenta") },
	{ N_("red")     },
	{ N_("white")   },
	{ N_("yellow")  },
	{ NULL, NULL }
398
399
};

400
401
402
403
404
405
406
407
void
format_color_init (void)
{
	int i;

	for (i = 0; format_colors [i].name; i++){
		StyleColor *sc;
		GdkColor c;
Arturo Espinosa's avatar
Arturo Espinosa committed
408

409
410
		gdk_color_parse (format_colors [i].name, &c);
		sc = style_color_new (c.red, c.green, c.blue);
Arturo Espinosa's avatar
Arturo Espinosa committed
411

412
413
414
415
416
417
418
419
420
421
422
423
424
425
		format_colors [i].color = sc;
	}
}

void
format_color_shutdown (void)
{
	int i;

	for (i = 0; format_colors [i].name; i++)
		style_color_unref (format_colors [i].color);
}

static StyleColor *
426
427
428
429
lookup_color (char *str, char *end)
{
	int i;

430
431
	for (i = 0; format_colors [i].name; i++){
		int len = strlen (format_colors [i].name);
432

433
		if ((strncasecmp (format_colors [i].name, str, len) == 0) ||
Miguel de Icaza's avatar
Miguel de Icaza committed
434
		    (strncasecmp (_(format_colors [i].name), str, len) == 0)){
435
436
			style_color_ref (format_colors [i].color);
			return format_colors [i].color;
437
438
439
440
441
442
443
444
445
446
447
448
		}
	}
	return NULL;
}

static GString *
render_number (gdouble number,
	       int left_req,
	       int right_req,
	       int left_spaces,
	       int right_spaces,
	       int right_allowed,
449
	       int use_thousand_sep,
450
451
452
453
454
455
	       int negative,
	       int supress_minus,
	       int decimal,
	       char *show_decimal)
{
	GString *number_string = g_string_new ("");
Arturo Espinosa's avatar
Arturo Espinosa committed
456
	gint zero_count;
457
458
459
	gdouble temp;
	int group = 0;

Arturo Espinosa's avatar
Arturo Espinosa committed
460
461
462
463
464
465
466
467
468
469
470
	if (right_allowed >= 0) {
		/* Change "rounding" into "truncating".  */
		gdouble delta = 0.5;
		int i;
		for (i = 0; i < right_allowed; i++)
			delta /= 10.0;
		/* Note, that we assume number >= 0 here.  */
		number += delta;
	}

	for (temp = number; temp >= 1.0; temp /= 10.0){
471
472
		double r = floor (temp);
		int digit;
Arturo Espinosa's avatar
Arturo Espinosa committed
473

474
		if (use_thousand_sep){
475
476
			group++;
			if (group == 4){
477
				int c;
Arturo Espinosa's avatar
Arturo Espinosa committed
478

479
				group = 1;
480
481
482
483
				if (lc->thousands_sep [0] == 0)
					c = ',';
				else
					c = lc->thousands_sep [0];
Arturo Espinosa's avatar
Arturo Espinosa committed
484

485
				g_string_prepend_c (number_string, c);
486
487
			}
		}
Arturo Espinosa's avatar
Arturo Espinosa committed
488

489
490
		digit = r - floor (r / 10) * 10;
		g_string_prepend_c (number_string, (digit) + '0');
491
492
493
494
		if (left_req > 0)
			left_req --;
		if (left_spaces > 0)
			left_spaces --;
Arturo Espinosa's avatar
Arturo Espinosa committed
495

496
	}
Arturo Espinosa's avatar
Arturo Espinosa committed
497

498
499
500
501
502
503
504
505
506
507
	for (; left_req > 0; left_req--, left_spaces--)
		g_string_prepend_c (number_string, '0');

	for (; left_spaces > 0; left_spaces--)
		g_string_prepend_c (number_string, ' ');

	if (negative && !supress_minus)
		g_string_prepend_c (number_string, '-');

	if (decimal > 0)
508
		g_string_append (number_string, lc->decimal_point);
509
510
511
	else
		g_string_append (number_string, show_decimal);

Arturo Espinosa's avatar
Arturo Espinosa committed
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
	temp = number - floor (number);

	for (; right_req > 0; right_req --, right_allowed --, right_spaces --)
	{
		gint digit;
		temp *= 10.0;
		digit = (gint)temp;
		temp -= digit;
		g_string_append_c (number_string, digit + '0');
	}

	zero_count = 0;

	for (; right_allowed > 0; right_allowed --)
	{
		gint digit;
		temp *= 10.0;
		digit = (gint)temp;
		temp -= digit;

		if (digit == 0)
			zero_count ++;
		else
		{
			right_spaces -= zero_count + 1;
			zero_count = 0;
		}

		g_string_append_c (number_string, digit + '0');
	}

	g_string_truncate (number_string, number_string->len - zero_count);

	for (; right_spaces > 0; right_spaces--)
	{
		g_string_append_c (number_string, ' ');
	}

	return number_string;
551
552
}

553
554
555
556
557
558
559
560
561
562
563
564
565
typedef struct {
	char *decimal_point, *append_after_number;
	int  right_optional, right_spaces, right_req, right_allowed;
	int  left_spaces, left_req;
	int  scientific;
	int  scientific_shows_plus;
	int  scientific_exp;
	int  rendered;
	int  negative;
	int  decimal_separator_seen;
	int  supress_minus;
	int  comma_separator_seen;
} format_info_t;
Chris Lahey's avatar
Chris Lahey committed
566

567
568
569
570
571
static char *
do_render_number (gdouble number, format_info_t *info)
{
	GString *res;
	char *result;
572
	char decimal_point [2];
Arturo Espinosa's avatar
Arturo Espinosa committed
573

574
	info->rendered = 1;
Chris Lahey's avatar
Chris Lahey committed
575

576
577
578
579
580
	/*
	 * If the format contains only "#"s to the left of the decimal
	 * point, number in the [0.0,1.0] range are prefixed with a
	 * decimal point
	 */
581
582
583
584
585
	if (number > 0.0 && number < 1.0 && info->right_allowed == 0 && info->right_optional > 0){
		decimal_point [0] = lc->decimal_point [0];
		decimal_point [1] = 0;
	} else
		decimal_point [0] = 0;
Chris Lahey's avatar
Chris Lahey committed
586

Arturo Espinosa's avatar
Arturo Espinosa committed
587
#if 0
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
	printf ("Rendering: %g with:\n", number);
	printf ("left_req:    %d\n"
		"right_req:   %d\n"
		"left_spaces: %d\n"
		"right_spaces:%d\n"
		"right_allow: %d\n"
		"negative:    %d\n"
		"supress:     %d\n"
		"decimalseen: %d\n"
		"decimalp:    %s\n",
		info->left_req,
		info->right_req,
		info->left_spaces,
		info->right_spaces,
		info->right_allowed + info->right_optional,
		info->negative,
		info->supress_minus,
		info->decimal_separator_seen,
		decimal_point);
#endif
Arturo Espinosa's avatar
Arturo Espinosa committed
608

609
610
611
612
613
614
615
616
617
618
619
620
	res = render_number (
		number,
		info->left_req,
		info->right_req,
		info->left_spaces,
		info->right_spaces,
		info->right_allowed + info->right_optional,
		info->comma_separator_seen,
		info->negative,
		info->supress_minus,
		info->decimal_separator_seen,
		decimal_point);
Chris Lahey's avatar
Chris Lahey committed
621

622
623
	if (info->append_after_number)
		g_string_append (res, info->append_after_number);
Arturo Espinosa's avatar
Arturo Espinosa committed
624

625
626
	result = g_strdup (res->str);
	g_string_free (res, TRUE);
Arturo Espinosa's avatar
Arturo Espinosa committed
627

628
629
630
	return result;
}

631
632
633
634
635
636
637
/*
 * Microsoft Excel has a bug in the handling of year 1900,
 * I quote from http://catless.ncl.ac.uk/Risks/19.64.html#subj9.1
 *
 * > Microsoft EXCEL version 6.0 ("Office 95 version") and version 7.0 ("Office
 * > 97 version") believe that year 1900 is a leap year.  The extra February 29
 * > cause the following problems.
Arturo Espinosa's avatar
Arturo Espinosa committed
638
 * >
639
640
641
 * > 1)  All day-of-week before March 1, 1900 are incorrect;
 * > 2)  All date sequence (serial number) on and after March 1, 1900 are incorrect.
 * > 3)  Calculations of number of days across March 1, 1900 are incorrect.
Arturo Espinosa's avatar
Arturo Espinosa committed
642
 * >
643
644
645
646
 * > The risk of the error will cause must be little.  Especially case 1.
 * > However, import or export date using serial date number will be a problem.
 * > If no one noticed anything wrong, it must be that no one did it that way.
 */
647
648
649
650
static struct tm *
split_time (gdouble number)
{
	static struct tm tm;
Miguel de Icaza's avatar
New:    
Miguel de Icaza committed
651
	double secs;
652

653
	GDate* date = g_date_new_serial (number);
Arturo Espinosa's avatar
Arturo Espinosa committed
654

655
	g_date_to_struct_tm (date, &tm);
656

Miguel de Icaza's avatar
New:    
Miguel de Icaza committed
657
	secs = (number - floor (number)) * 86400.0;
658
	tm.tm_hour = secs / 3600;
Miguel de Icaza's avatar
New:    
Miguel de Icaza committed
659
660
661
662
	secs -= tm.tm_hour * 3600;
	tm.tm_min  = secs / 60;
	secs -= tm.tm_min * 60;
	tm.tm_sec  = floor (secs);
663

Morten Welinder's avatar
Morten Welinder committed
664
665
	g_date_free (date);

666
667
668
	return &tm;
}

669
670
671
672
673
674
675
676
static gchar *
format_number (gdouble number, StyleFormatEntry *style_format_entry)
{
	GString *result = g_string_new ("");
	char *format = style_format_entry->format;
	format_info_t info;
	int can_render_number = 0;
	int hour_seen = 0;
677
	struct tm *time_split = 0;
678
	char *res;
Arturo Espinosa's avatar
Arturo Espinosa committed
679

680
681
682
683
	memset (&info, 0, sizeof (info));
	if (number < 0.0){
		info.negative = TRUE;
		number = -number;
Chris Lahey's avatar
Chris Lahey committed
684
	}
685
686
687

	if (!lc)
		lc = localeconv ();
Arturo Espinosa's avatar
Arturo Espinosa committed
688

689
690
691
692
693
694
695
	while (*format){
		switch (*format){
		case '#':
			can_render_number = 1;
			if (info.decimal_separator_seen)
				info.right_optional++;
			break;
Arturo Espinosa's avatar
Arturo Espinosa committed
696

697
698
699
700
701
702
703
		case '?':
			can_render_number = 1;
			if (info.decimal_separator_seen)
				info.right_spaces++;
			else
				info.left_spaces++;
			break;
Arturo Espinosa's avatar
Arturo Espinosa committed
704

705
706
707
708
709
710
711
712
713
714
715
		case '0':
			can_render_number = 1;
			if (info.decimal_separator_seen){
				info.right_req++;
				info.right_allowed++;
				info.right_spaces++;
			} else {
				info.left_spaces++;
				info.left_req++;
			}
			break;
Arturo Espinosa's avatar
Arturo Espinosa committed
716

717
718
719
		case ',': case '.': {
			if (*format == lc->decimal_point [0]){
				int c = *(format+1);
Arturo Espinosa's avatar
Arturo Espinosa committed
720

721
722
723
724
725
726
727
728
729
730
				can_render_number = 1;
				if (c && (c != '0' && c != '#' && c != '?'))
					number /= 1000;
				else
					info.decimal_separator_seen = TRUE;
				break;
			} else {
				info.comma_separator_seen = TRUE;
				break;
			}
731
		}
Arturo Espinosa's avatar
Arturo Espinosa committed
732

733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
		case 'E': case 'e':
			can_render_number = 1;
			info.scientific = TRUE;
			format++;
			for (format++; *format;){
				if (*format == '+'){
					info.scientific_shows_plus = TRUE;
					format++;
				} else if (*format == '-')
					format++;
				else if (*format == '0'){
					info.scientific_exp++;
					format++;
				} else
					break;
			}
			/* FIXME: this is a gross hack */
			{
				char buffer [40];
				sprintf (buffer, "%g", number);
Arturo Espinosa's avatar
Arturo Espinosa committed
753

754
755
756
				g_string_append (result, buffer);
				goto finish;
			}
Arturo Espinosa's avatar
Arturo Espinosa committed
757

758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
		/* percent */
		case '%':
			can_render_number = 1;
			number *= 100;
			info.append_after_number = "%";
			break;

		case '\\':
			if (*(format+1)){
				if (can_render_number && !info.rendered)
					g_string_append (result, do_render_number (number, &info));
				g_string_append_c (result, *format);
			}
			break;

		case '"': {
			if (can_render_number && !info.rendered)
				g_string_append (result, do_render_number (number, &info));

			for (format++; *format && *format != '"'; format++)
				g_string_append_c (result, *format);
			break;
Chris Lahey's avatar
Chris Lahey committed
780
781
782

		}

783
784
785
786
787
788
789
790
		case '-':
		case '/':
		case '(':
		case '+':
		case ' ':
		case ':':
			info.supress_minus = TRUE;
			/* fall down */
Arturo Espinosa's avatar
Arturo Espinosa committed
791

792
793
794
		case '$':
			g_string_append_c (result, *format);
			break;
Arturo Espinosa's avatar
Arturo Espinosa committed
795

796
797
798
		case '£':
			g_string_append_c (result, *format);
			break;
Arturo Espinosa's avatar
Arturo Espinosa committed
799

800
801
802
803
804
		case ')':
			if (can_render_number && !info.rendered)
				g_string_append (result, do_render_number (number, &info));
			g_string_append_c (result, *format);
			break;
Chris Lahey's avatar
Chris Lahey committed
805

806
807
808
809
810
		case '_':
			if (*(format+1))
				format++;
			g_string_append_c (result, ' ');
			break;
811

812
		case 'M':
813
		case 'm':
814
815
			if (!time_split)
				time_split = split_time (number);
816
817
818
819
820
821
			if (hour_seen)
				format += append_minute (result, format, time_split) - 1;
			else
				format += append_month (result, format, time_split) - 1;
			break;

822
		case 'D':
823
		case 'd':
824
825
			if (!time_split)
				time_split = split_time (number);
826
827
828
			format += append_day (result, format, time_split) -1;
			break;

829
		case 'Y':
830
		case 'y':
831
832
			if (!time_split)
				time_split = split_time (number);
833
834
835
			format += append_year (result, format, time_split) - 1;
			break;

836
		case 'S':
837
		case 's':
838
839
			if (!time_split)
				time_split = split_time (number);
840
841
842
843
844
845
846
			format += append_second (result, format, time_split) - 1;
			break;

		case '*':
			g_warning ("REPEAT FORMAT NOT YET SUPPORTED\n");
			break;

847
		case 'H':
848
		case 'h':
849
850
			if (!time_split)
				time_split = split_time (number);
851
			format += append_hour (result, format, time_split, style_format_entry->want_am_pm) - 1;
Miguel de Icaza's avatar
New:    
Miguel de Icaza committed
852
			hour_seen = TRUE;
853
			break;
Arturo Espinosa's avatar
Arturo Espinosa committed
854

855
856
		case 'A':
		case 'a':
857
858
			if (!time_split)
				time_split = split_time (number);
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
			if (time_split->tm_hour < 12){
				g_string_append_c (result, *format);
				format++;
				if (*format == 'm' || *format == 'M'){
					g_string_append_c (result, *format);
					if (*(format+1) == '/')
						format++;
				}
			} else {
				if (*(format+1) == 'm' || *(format+1) == 'M')
					format++;
				if (*(format+1) == '/')
					format++;
			}
			break;
Arturo Espinosa's avatar
Arturo Espinosa committed
874

875
		case 'P': case 'p':
876
877
			if (!time_split)
				time_split = split_time (number);
878
879
880
881
882
883
884
885
886
887
888
			if (time_split->tm_hour >= 12){
				g_string_append_c (result, *format);
				if (*(format+1) == 'm' || *(format+1) == 'M'){
					format++;
					g_string_append_c (result, *format);
				}
			} else {
				if (*(format+1) == 'm' || *(format+1) == 'M')
					format++;
			}
			break;
Arturo Espinosa's avatar
Arturo Espinosa committed
889

890
891
892
893
894
		default:
			break;
		}
		format++;
	}
Miguel de Icaza's avatar
Miguel de Icaza committed
895
896
	if (!info.rendered && can_render_number){
		char *rendered_string = do_render_number (number, &info);
Arturo Espinosa's avatar
Arturo Espinosa committed
897

Miguel de Icaza's avatar
Miguel de Icaza committed
898
899
900
		g_string_append (result, rendered_string);
		g_free (rendered_string);
	}
901
902
903
904
 finish:
	res = g_strdup (result->str);
	g_string_free (result, TRUE);
	return res;
905
}
Chris Lahey's avatar
Chris Lahey committed
906

907
static gboolean
Chris Lahey's avatar
Chris Lahey committed
908
check_valid (StyleFormatEntry *entry, Value *value)
909
{
Miguel de Icaza's avatar
Miguel de Icaza committed
910
911
912
913
914
915
916
917
	switch (value->type){

	case VALUE_STRING:
		return entry->restriction_type == '@';

	case VALUE_FLOAT:
		switch (entry->restriction_type){

Arturo Espinosa's avatar
Arturo Espinosa committed
918
		case '*':
Miguel de Icaza's avatar
Miguel de Icaza committed
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
			return TRUE;
		case '<':
			return value->v.v_float < entry->restriction_value;
		case '>':
			return value->v.v_float > entry->restriction_value;
		case '=':
			return value->v.v_float == entry->restriction_value;
		case ',':
			return value->v.v_float <= entry->restriction_value;
		case '.':
			return value->v.v_float >= entry->restriction_value;
		case '+':
			return value->v.v_float != entry->restriction_value;
		default:
			return FALSE;
		}
Arturo Espinosa's avatar
Arturo Espinosa committed
935

Miguel de Icaza's avatar
Miguel de Icaza committed
936
937
938
	case VALUE_INTEGER:
		switch (entry->restriction_type){

Arturo Espinosa's avatar
Arturo Espinosa committed
939
		case '*':
Miguel de Icaza's avatar
Miguel de Icaza committed
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
			return TRUE;
		case '<':
			return value->v.v_int < entry->restriction_value;
		case '>':
			return value->v.v_int > entry->restriction_value;
		case '=':
			return value->v.v_int == entry->restriction_value;
		case ',':
			return value->v.v_int <= entry->restriction_value;
		case '.':
			return value->v.v_int >= entry->restriction_value;
		case '+':
			return value->v.v_int != entry->restriction_value;
		default:
			return FALSE;
		}
Arturo Espinosa's avatar
Arturo Espinosa committed
956

957
	default:
Miguel de Icaza's avatar
Miguel de Icaza committed
958
		return FALSE;
959
	}
Chris Lahey's avatar
Chris Lahey committed
960
961
962
}

gchar *
963
format_value (StyleFormat *format, Value *value, StyleColor **color)
Chris Lahey's avatar
Chris Lahey committed
964
{
965
966
967
968
	char *v = NULL;
	StyleFormatEntry entry;
	GList *list;
	int is_general = 0;
Arturo Espinosa's avatar
Arturo Espinosa committed
969

970
971
	if (color)
		*color = NULL;
Arturo Espinosa's avatar
Arturo Espinosa committed
972

973
974
975
976
	/* get format */
	for (list = format->format_list; list; list = g_list_next (list))
		if (check_valid (list->data, value))
			break;
Arturo Espinosa's avatar
Arturo Espinosa committed
977

978
979
980
981
	if (list)
		entry = *(StyleFormatEntry *)(list->data);
	else
		entry.format = format->format;
982

983
984
985
	/* Try to parse a color specification */
	if (entry.format [0] == '['){
		char *end = strchr (entry.format, ']');
Arturo Espinosa's avatar
Arturo Espinosa committed
986

987
		if (end){
988
989
			if (color)
				*color = lookup_color (&entry.format [1], end);
990
991
992
993
			entry.format = end+1;
		}
	}

Miguel de Icaza's avatar
Miguel de Icaza committed
994
995
	if (entry.format [0] == 0)
		is_general = 1;
Arturo Espinosa's avatar
Arturo Espinosa committed
996

997
998
999
	if (strcmp (entry.format, "General") == 0){
		entry.format += 7;
		is_general = 1;
Arturo Espinosa's avatar
Arturo Espinosa committed
1000
	}