format.c 23.7 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 6 7
 * Redid the format parsing routine to make it accept more of the Excel
 * formats.  The number rendeing code from Chris has not been touched,
 * that routine is pretty good. 
 *
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.
 */

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 "gnumeric.h"
30
#include "format.h"
Chris Lahey's avatar
Chris Lahey committed
31
#include <glib.h>
32
#include <libgnome/lib_date.h>
33

34 35 36
/* Points to the locale information for number display */
static struct lconv *lc;

37
static void style_entry_free (gpointer data, gpointer user_data);
38 39


40
/*
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
 * 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
 * supported.  
 */
57

58

59
static char *day_short [] =
60
{
61 62 63 64 65 66 67
	N_("Sun"),
	N_("Mon"),
	N_("Tue"),
	N_("Wed"),
	N_("Thu"),
	N_("Fri"),
	N_("Sat")
68 69
};

70
static char *day_long [] =
71
{
72 73 74 75 76 77 78
	N_("Sunday"),
	N_("Monday"),
	N_("Tuesday"),
	N_("Wednesday"),
	N_("Thursday"),
	N_("Friday"),
	N_("Saturday")
79 80
};

81
static char *month_short [] =
82
{
83 84 85 86 87 88 89 90 91 92 93 94
	N_("Jan"),
	N_("Feb"),
	N_("Mar"),
	N_("Apr"),
	N_("May"),
	N_("Jun"),
	N_("Jul"),
	N_("Aug"),
	N_("Sep"),
	N_("Oct"),
	N_("Nov"),
	N_("Dec")
95 96
};

97
static char *month_long [] =
98
{
99 100 101 102 103 104 105 106 107 108 109 110
	N_("January"),
	N_("Februrary"),
	N_("March"),
	N_("April"),
	N_("May"),
	N_("June"),
	N_("July"),
	N_("August"),
	N_("September"),
	N_("October"),
	N_("November"),
	N_("December")
111
};
112

113 114
static void
do_roundup (GString *string)
115
{
116
	int i;
117
  
118 119 120 121 122 123 124 125 126 127 128
	for (i = string->len - 1; string->str [i] == '9'; i--)
		string->str[i] = '0';

	if (string->str [i] == '.')
	{
		/* FIXME */
	}
	else
	{
		string->str [i]++;
	}
129 130
}

131 132 133 134
/*
 * Parses the year field at the beginning of the format.  Returns the
 * number of characters used.
 */
135
static
136
int append_year (GString *string, gchar *format, struct tm *time_split)
137
{
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	char temp [5];
	
	if (format [1] != 'y'){
		g_string_append_c (string, 'y');
		return 1;
	}
	
	if (format [2] != 'y' || format [3] != 'y'){
		sprintf (temp, "%02d", time_split->tm_year);
		g_string_append (string, temp);
		return 2;
	}

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

	return 4;
155 156
}

157 158 159 160 161 162
/*
 * 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)
163
{
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	char temp [3];
	
	if (format [1] != 'm'){
		sprintf( temp, "%d", time_split->tm_mon);
		g_string_append( string, temp);
		return 1;
	}
	
	if (format [2] != 'm')
	{
		sprintf (temp, "%02d", time_split->tm_mon);
		g_string_append (string, temp);
		return 2;
	}
	
	if (format [3] != 'm'){
		g_string_append (string, _(month_short [time_split->tm_mon]));
		return 3;
	}

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

188 189 190 191 192 193
/*
 * 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)
194
{
195 196 197 198 199 200 201 202 203 204
	char temp[3];

	if (format [1] != 'h'){
	    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;
205 206
}

207 208 209 210 211 212
/*
 * 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)
213
{
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	char temp[3];

	if (format [1] != 'd'){
		sprintf (temp, "%d", time_split->tm_mday);
		g_string_append (string, temp);
		return 1;
	}

	if (format [2] != 'd'){
		sprintf (temp, "%02d", time_split->tm_mday);
		g_string_append (string, temp);
		return 2;
	}

	if (format [3] != 'd'){
		g_string_append (string, _(day_short[time_split->tm_wday]));
		return 3;
	}

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

	return 4;
236 237
}

238 239 240 241 242 243
/*
 * 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)
244
{
245 246 247 248 249 250 251 252 253 254 255 256
	char temp [3];

	if (format [1] != 'm'){
		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;
257 258
}

259 260 261 262 263 264
/*
 * 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)
265
{
266 267 268 269 270 271 272 273 274 275 276 277
	char temp[3];
	
	if (format [1] != 's'){
		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;
278 279
}

280 281 282 283 284 285
/*
 * 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)
286
{
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
	if (time_split->tm_hour <= 11){
		if (format [0] == 'a' || format [0] == 'p')
			g_string_append_c (string, 'a');
		else
			g_string_append_c (string, 'A');
	}
	else {
		if (format [0] == 'a' || format [0] == 'p')
			g_string_append_c (string, 'p');
		else
			g_string_append_c (string, 'P');
	}
	
	if (format [1] == 'm' || format [1] == 'M'){
		g_string_append_c (string, format [1]);
		return 2;
	} else
		return 1;
305 306
}

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
/*
 * Does some analisis before hand on the format
 */
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;
			
		case '\\':
			if (*(format+1))
				format++;
			else
				return;
			break;

		case 'a':
		case 'p':
		case 'A':
		case 'P':
			if ((*(format+1) == 'm') ||
337
			   (*(format+1) == 'M'))
338 339 340 341 342 343
				style->want_am_pm = 1;
			break;
		}
	}
}

344 345
typedef struct
{
346 347 348
	int decimal;
	int timeformat;
	int hasnumbers;
349
} xformat_info;
350

351 352
/*
 * This routine should always return, it cant fail, in the worst
Chris Lahey's avatar
Chris Lahey committed
353 354
 * case it should just downgrade to stupid formatting
 */
355 356 357
void
format_compile (StyleFormat *format)
{
358 359 360 361 362 363 364 365 366 367 368 369 370
	GString *string = g_string_new ("");
	int i;
	int which = 0;
	int length = strlen (format->format);
	StyleFormatEntry standard_entries[4];
	StyleFormatEntry *temp;
	
	g_list_free (format->format_list);
	format->format_list = 0;
	
	/* g_string_maybe_expand (string, length); */
	
	for (i = 0; i < length; i++){
Chris Lahey's avatar
Chris Lahey committed
371

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
		switch (format->format[i])
		{
		case ';':
			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++;
			}
			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
390

391 392 393 394 395 396 397 398 399 400 401 402
	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++;
	}
	
	/* Set up restriction types. */
	standard_entries[1].restriction_type = '<';
	standard_entries[1].restriction_value = 0;
	switch (which)
Chris Lahey's avatar
Chris Lahey committed
403
	{
404 405 406 407 408 409 410 411 412 413 414 415 416
	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
417
	}
418 419 420 421 422 423 424 425
	for (i = 0; i < which; i++)
	{
		temp = g_new (StyleFormatEntry, 1);
		*temp = standard_entries[i];
		pre_parse_format (temp);
		format->format_list = g_list_append (format->format_list, temp);
	}
	g_string_free (string, TRUE);
Chris Lahey's avatar
Chris Lahey committed
426 427 428
}

static void
429
style_entry_free(gpointer data, gpointer user_data)
Chris Lahey's avatar
Chris Lahey committed
430
{
431 432 433
	StyleFormatEntry *entry = data;
	
	g_free (entry->format);
434
	g_free (entry);
435 436 437 438 439
}

void
format_destroy (StyleFormat *format)
{
440 441 442 443 444 445 446 447 448 449 450 451
	/* 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
	 */
	
	g_list_foreach (format->format_list, style_entry_free, NULL);
	g_list_free (format->format_list);
	format->format_list = NULL;  
}

452 453 454 455 456 457 458 459 460 461 462 463 464
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 }
465 466
};

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
void
format_color_init (void)
{
	int i;

	for (i = 0; format_colors [i].name; i++){
		StyleColor *sc;
		GdkColor c;
		
		gdk_color_parse (format_colors [i].name, &c);
		sc = style_color_new (c.red, c.green, c.blue);
		
		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 *
493 494 495 496
lookup_color (char *str, char *end)
{
	int i;

497 498
	for (i = 0; format_colors [i].name; i++){
		int len = strlen (format_colors [i].name);
499

500
		if ((strncasecmp (format_colors [i].name, str, len) == 0) ||
501
		   (strncasecmp (_(format_colors [i].name), str, len) == 0)){
502 503
			style_color_ref (format_colors [i].color);
			return format_colors [i].color;
504 505 506 507 508 509 510 511 512 513 514 515
		}
	}
	return NULL;
}

static GString *
render_number (gdouble number,
	       int left_req,
	       int right_req,
	       int left_spaces,
	       int right_spaces,
	       int right_allowed,
516
	       int use_thousand_sep,
517 518 519 520 521 522 523 524 525 526 527 528
	       int negative,
	       int supress_minus,
	       int decimal,
	       char *show_decimal)
{
	GString *number_string = g_string_new ("");
	gint zero_count, nine_count;
	gdouble temp;
	int group = 0;
	
	for (temp = number; temp >= 1.0; temp /= 10.0){

529 530 531
		double r = floor (temp);
		int digit;
				  
532
		if (use_thousand_sep){
533 534
			group++;
			if (group == 4){
535 536
				int c;
				
537
				group = 1;
538 539 540 541 542 543
				if (lc->thousands_sep [0] == 0)
					c = ',';
				else
					c = lc->thousands_sep [0];
				
				g_string_prepend_c (number_string, c);
544 545 546
			}
		}
		
547 548
		digit = r - floor (r / 10) * 10;
		g_string_prepend_c (number_string, (digit) + '0');
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
		if (left_req > 0)
			left_req --;
		if (left_spaces > 0)
			left_spaces --;
		
	}
      
	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)
566
		g_string_append (number_string, lc->decimal_point);
567 568 569 570 571
	else
		g_string_append (number_string, show_decimal);

      temp = number - floor (number);

572
      for (; right_req > 0; right_req --, right_allowed --, right_spaces --)
573 574 575
      {
	      gint digit;
	      temp *= 10.0;
576 577 578
	      digit = floor (temp);
	      temp -= floor (temp);
	      if (right_allowed == 1 && floor (temp * 10.0) >= 5)
579
	      {
580
		      if (digit < 9)
581 582 583 584
			      digit ++;
		      else
		      {
			      digit = 0;
585
			      do_roundup (number_string);
586 587
		      }
	      }
588
	      g_string_append_c (number_string, digit + '0');
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
      }
      
      zero_count = 0;
      nine_count = 0;
      
      for (; right_allowed > 0; right_allowed --)
      {
	      gint digit;
	      temp *= 10.0;
	      digit = floor (temp);
	      temp -= floor (temp);
	      
	      if (right_allowed == 1 && floor (temp * 10.0) >= 5)
	      {
		      if (digit < 9)
			      digit ++;
		      else
		      {
			      digit = 0;
			      right_spaces -= zero_count;
			      zero_count = nine_count;
			      right_spaces += zero_count;
			      do_roundup (number_string);
		      }
	      }
	      if (digit == 0)
		      zero_count ++;
	      else
	      {
		      right_spaces -= zero_count + 1;
		      zero_count = 0;
	      }
	      if (digit == 9)
		      nine_count ++;
	      else
		      nine_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;
637 638
}

639 640 641 642 643 644 645 646 647 648 649 650 651
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;
652

653 654 655 656 657
static char *
do_render_number (gdouble number, format_info_t *info)
{
	GString *res;
	char *result;
658
	char decimal_point [2];
659 660
	
	info->rendered = 1;
661

662 663 664 665 666
	/*
	 * 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
	 */
667 668 669 670 671
	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;
672

673
#if 0
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
	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
	
	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);
707

708 709 710 711 712 713 714 715 716
	if (info->append_after_number)
		g_string_append (res, info->append_after_number);
	
	result = g_strdup (res->str);
	g_string_free (res, TRUE);
	
	return result;
}

717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
/*
 * 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.
 * > 
 * > 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.
 * > 
 * > 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.
 */
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
static struct tm *
split_time (gdouble number)
{
	static struct tm tm;
	long secs;
	int diff;

	tm.tm_year = 1900;
	tm.tm_mon  = 1;
	tm.tm_mday = 1;

	/* Day 1 means 1st of jannuary of 1900 */
	diff = floor (number - 1.0);
	calc_new_date (&tm.tm_year, &tm.tm_mon, &tm.tm_mday, diff);
	tm.tm_mon--;

	tm.tm_year -= 1900;

	secs = (number - floor (number)) * 86400;
	tm.tm_hour = secs / 3600;
	tm.tm_min  = (secs % 3600) / 60;
	tm.tm_sec  = ((secs % 3600) % 60);

	return &tm;
}

759 760 761 762 763 764 765 766
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;
767
	struct tm *time_split = 0;
768 769 770 771 772 773
	char *res;
	
	memset (&info, 0, sizeof (info));
	if (number < 0.0){
		info.negative = TRUE;
		number = -number;
774
	}
775 776 777 778

	if (!lc)
		lc = localeconv ();

779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
	while (*format){
		switch (*format){
		case '#':
			can_render_number = 1;
			if (info.decimal_separator_seen)
				info.right_optional++;
			else
				;
			break;
			
		case '?':
			can_render_number = 1;
			if (info.decimal_separator_seen)
				info.right_spaces++;
			else
				info.left_spaces++;
			break;
			
		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;
			
809 810 811 812 813 814 815 816 817 818 819 820 821 822
		case ',': case '.': {
			if (*format == lc->decimal_point [0]){
				int c = *(format+1);
				
				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;
			}
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
		}
		
		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);
				
				g_string_append (result, buffer);
				goto finish;
			}
			
		/* 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;
872 873 874

		}

875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
		case '-':
		case '/':
		case '(':
		case '+':
		case ' ':
		case ':':
			info.supress_minus = TRUE;
			/* fall down */
			
		case '$':
			g_string_append_c (result, *format);
			break;
			
		case ')':
			if (can_render_number && !info.rendered)
				g_string_append (result, do_render_number (number, &info));
			g_string_append_c (result, *format);
			break;
893

894 895 896 897 898
		case '_':
			if (*(format+1))
				format++;
			g_string_append_c (result, ' ');
			break;
899

900
		case 'm':
901 902
			if (!time_split)
				time_split = split_time (number);
903 904 905 906 907 908 909
			if (hour_seen)
				format += append_minute (result, format, time_split) - 1;
			else
				format += append_month (result, format, time_split) - 1;
			break;

		case 'd':
910 911
			if (!time_split)
				time_split = split_time (number);
912 913 914 915
			format += append_day (result, format, time_split) -1;
			break;

		case 'y':
916 917
			if (!time_split)
				time_split = split_time (number);
918 919 920 921
			format += append_year (result, format, time_split) - 1;
			break;

		case 's':
922 923
			if (!time_split)
				time_split = split_time (number);
924 925 926 927 928 929 930 931
			format += append_second (result, format, time_split) - 1;
			break;

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

		case 'h':
932 933
			if (!time_split)
				time_split = split_time (number);
934 935 936 937 938
			format += append_hour (result, format, time_split, style_format_entry->want_am_pm) - 1;
			break;
			
		case 'A':
		case 'a':
939 940
			if (!time_split)
				time_split = split_time (number);
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
			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;
			
		case 'P': case 'p':
958 959
			if (!time_split)
				time_split = split_time (number);
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
			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;
			
		default:
			break;
		}
		format++;
	}
	if (!info.rendered && can_render_number)
		g_string_append (result, do_render_number (number, &info));
 finish:
	res = g_strdup (result->str);
	g_string_free (result, TRUE);
	return res;
983
}
984

985
static gboolean
Chris Lahey's avatar
Chris Lahey committed
986
check_valid (StyleFormatEntry *entry, Value *value)
987
{
Chris Lahey's avatar
Chris Lahey committed
988 989 990 991 992
  switch (value->type)
    {
    case VALUE_STRING:
      return entry->restriction_type == '@';
    case VALUE_FLOAT:
993
      switch (entry->restriction_type)
Chris Lahey's avatar
Chris Lahey committed
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
	{
	case '*': 
	  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;
1009
	default:
Chris Lahey's avatar
Chris Lahey committed
1010
	  return FALSE;
1011
	}
Chris Lahey's avatar
Chris Lahey committed
1012
    case VALUE_INTEGER:
1013
      switch (entry->restriction_type)
Chris Lahey's avatar
Chris Lahey committed
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
	{
	case '*': 
	  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;
	}      
    default:
      return FALSE;
    }
}

gchar *
1038
format_value (StyleFormat *format, Value *value, StyleColor **color)
Chris Lahey's avatar
Chris Lahey committed
1039
{
1040 1041 1042 1043 1044
	char *v = NULL;
	StyleFormatEntry entry;
	GList *list;
	int is_general = 0;
	
1045 1046
	if (color)
		*color = NULL;
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
	
	/* get format */
	for (list = format->format_list; list; list = g_list_next (list))
		if (check_valid (list->data, value))
			break;
	
	if (list)
		entry = *(StyleFormatEntry *)(list->data);
	else
		entry.format = format->format;
1057

1058 1059 1060 1061 1062
	/* Try to parse a color specification */
	if (entry.format [0] == '['){
		char *end = strchr (entry.format, ']');
      
		if (end){
1063 1064
			if (color)
				*color = lookup_color (&entry.format [1], end);
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
			entry.format = end+1;
		}
	}

	if (strcmp (entry.format, "General") == 0){
		entry.format += 7;
		is_general = 1;
	} 
	
	switch (value->type){
	case VALUE_FLOAT:
1076 1077 1078 1079 1080 1081
		if (is_general){
			if (floor (value->v.v_float) == value->v.v_float)
				entry.format = "0";
			else
				entry.format = "0.##";
		}
1082 1083 1084 1085
		if (finite (value->v.v_float))
			v = format_number (value->v.v_float, &entry);
		else
			return g_strdup ("#VAL");
1086 1087 1088 1089
		break;
		
	case VALUE_INTEGER:
		if (is_general)
1090
			entry.format = "0";
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
		v = format_number (value->v.v_int, &entry);
		break;
		
	case VALUE_STRING:
		return g_strdup (value->v.str->str);
		
	default:
		return g_strdup ("Internal error");
	}
	
	/* Format error, return a default value */
	if (v == NULL)
		return value_string (value);
	
	return v;
1106 1107
}