~ubuntu-branches/ubuntu/gutsy/gnumeric/gutsy-201105201701

« back to all changes in this revision

Viewing changes to src/number-match.c

  • Committer: Bazaar Package Importer
  • Author(s): Gauvain Pocentek
  • Date: 2007-03-08 16:16:45 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20070308161645-uotkhmwywh7dkur4
Tags: 1.7.8-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
#include <goffice/utils/regutf8.h>
30
30
#include <goffice/utils/datetime.h>
31
31
#include <goffice/utils/go-glib-extras.h>
 
32
#include <goffice/utils/go-locale.h>
32
33
 
33
34
#include <stdio.h>
34
35
#include <string.h>
73
74
format_match_simple (char const *text)
74
75
{
75
76
        /* Is it a boolean?  */
76
 
        if (0 == g_ascii_strcasecmp (text, go_format_boolean (TRUE)))
 
77
        if (0 == g_ascii_strcasecmp (text, go_locale_boolean_name (TRUE)))
77
78
                return value_new_bool (TRUE);
78
 
        if (0 == g_ascii_strcasecmp (text, go_format_boolean (FALSE)))
 
79
        if (0 == g_ascii_strcasecmp (text, go_locale_boolean_name (FALSE)))
79
80
                return value_new_bool (FALSE);
80
81
 
81
82
        /* Is it an error?  */
90
91
                char *end;
91
92
                gnm_float d;
92
93
 
93
 
                errno = 0; /* gnm_strto sets errno, but does not clear it.  */
94
94
                d = gnm_strto (text, &end);
95
95
                if (text != end && errno != ERANGE && gnm_finite (d)) {
96
96
                        /* Allow and ignore spaces at the end.  */
185
185
                g_string_append_c (p_MMM, ')');
186
186
        }
187
187
 
188
 
        go_regexp_quote (p_decimal, go_format_get_decimal ()->str);
 
188
        go_regexp_quote (p_decimal, go_locale_get_decimal ()->str);
189
189
 
190
190
        /*
191
191
         * "Dec 1, 2000"
567
567
        v = value_new_float (time_val);
568
568
 
569
569
        if (add_format) {
570
 
                GOFormat *fmt = go_format_new_from_XL (time_format, FALSE);
 
570
                GOFormat *fmt = go_format_new_from_XL (time_format);
571
571
                value_set_fmt (v, fmt);
572
572
                go_format_unref (fmt);
573
573
        }
591
591
        int dig1;
592
592
        char const *date_format = NULL;
593
593
        GnmValue *v = NULL;
594
 
        char *time_format = NULL;
 
594
        const char *time_format = NULL;
595
595
 
596
596
        if (lc_time != datetime_locale.lc_time &&
597
597
            (lc_time == NULL ||
747
747
                time_val = value_get_as_float (v);
748
748
                fmt = VALUE_FMT (v);
749
749
                if (fmt)
750
 
                        time_format = go_format_as_XL (fmt, FALSE);
 
750
                        time_format = go_format_as_XL (fmt);
751
751
                value_release (v);
752
752
        } else
753
753
                time_val = 0;
760
760
                                                    " ",
761
761
                                                    time_format,
762
762
                                                    NULL);
763
 
                        fmt = go_format_new_from_XL (format, FALSE);
 
763
                        fmt = go_format_new_from_XL (format);
764
764
                        g_free (format);
765
 
                        g_free (time_format);
766
765
                } else
767
 
                        fmt = go_format_new_from_XL (date_format, FALSE);
 
766
                        fmt = go_format_new_from_XL (date_format);
768
767
                value_set_fmt (v, fmt);
769
768
                go_format_unref (fmt);
770
769
        }
775
774
static gboolean
776
775
hack_month_before_day (GOFormat const *cur_fmt)
777
776
{
778
 
        char *s = go_format_as_XL (cur_fmt, FALSE);
 
777
        char *s = g_strdup (go_format_as_XL (cur_fmt));
779
778
        char *p;
780
779
        char const *pos_m, *pos_d;
781
780
        gboolean res;
789
788
        if (pos_m && pos_d)
790
789
                res = (pos_m < pos_d);
791
790
        else
792
 
                res = go_format_month_before_day ();
 
791
                res = go_locale_month_before_day ();
793
792
 
794
793
        g_free (s);
795
794
        return res;
798
797
static gboolean
799
798
hack_prefer_hour (GOFormat const *cur_fmt)
800
799
{
801
 
        char *s = go_format_as_XL (cur_fmt, FALSE);
 
800
        const char *s = go_format_as_XL (cur_fmt);
802
801
        gboolean res = (strchr (s, 'h') != NULL);
803
 
        g_free (s);
804
802
        return res;
805
803
}
806
804
 
837
835
        if (*text == '/') {
838
836
                whole = 0;
839
837
        } else {
840
 
                errno = 0;
841
838
                whole = gnm_strto (start, NULL);
842
839
                if (errno == ERANGE)
843
840
                        return NULL;
857
854
                        return NULL;
858
855
        }
859
856
 
860
 
        errno = 0;
861
857
        num = gnm_strto (start, NULL);
862
858
        if (errno == ERANGE)
863
859
                return NULL;
872
868
        if (*text != 0)
873
869
                return NULL;
874
870
 
875
 
        errno = 0;
876
871
        den = gnm_strto (start, NULL);
877
872
        if (errno == ERANGE)
878
873
                return NULL;
897
892
        gboolean has_percent = FALSE;
898
893
        char sign = 0;
899
894
        GString *numstr = g_string_sized_new (20);
900
 
        GString const *curr = go_format_get_currency (NULL, NULL);
901
 
        GString const *thousand = go_format_get_thousand ();
902
 
        GString const *decimal = go_format_get_decimal ();
 
895
        GString const *curr = go_locale_get_currency (NULL, NULL);
 
896
        GString const *thousand = go_locale_get_thousand ();
 
897
        GString const *decimal = go_locale_get_decimal ();
903
898
        gboolean last_was_digit = FALSE;
904
899
        gboolean allow1000 = (thousand->len != 0);
905
900
 
1037
1032
                char *end;
1038
1033
                gboolean bad;
1039
1034
 
1040
 
                errno = 0;
1041
1035
                f = gnm_strto (numstr->str, &end);
1042
1036
                bad = *end || errno == ERANGE;
1043
1037
                g_string_free (numstr, TRUE);
1090
1084
        if (text[0] == '\'')
1091
1085
                return value_new_string (text + 1);
1092
1086
 
1093
 
        fam = cur_fmt ? cur_fmt->family : GO_FORMAT_GENERAL;
1094
 
        if (cur_fmt != GO_FORMAT_GENERAL) {
1095
 
                switch (cur_fmt->family) {
1096
 
                case GO_FORMAT_TEXT:
1097
 
                        return value_new_string (text);
1098
 
 
1099
 
                case GO_FORMAT_NUMBER:
1100
 
                case GO_FORMAT_CURRENCY:
1101
 
                case GO_FORMAT_ACCOUNTING:
1102
 
                case GO_FORMAT_PERCENTAGE:
1103
 
                case GO_FORMAT_SCIENTIFIC:
1104
 
                        v = format_match_decimal_number (text, &fam);
1105
 
                        if (v)
1106
 
                                value_set_fmt (v, cur_fmt);
1107
 
                        return v;
1108
 
 
1109
 
                case GO_FORMAT_DATE: {
1110
 
                        gboolean month_before_day =
1111
 
                                hack_month_before_day (cur_fmt);
1112
 
 
1113
 
                        v = format_match_datetime (text, date_conv,
1114
 
                                                   month_before_day,
1115
 
                                                   FALSE,
1116
 
                                                   TRUE);
1117
 
                        if (!v)
1118
 
                                v = format_match_decimal_number (text, &fam);
1119
 
                        if (v)
1120
 
                                value_set_fmt (v, cur_fmt);
1121
 
                        return v;
1122
 
                }
1123
 
 
1124
 
                case GO_FORMAT_TIME: {
1125
 
                        gboolean month_before_day =
1126
 
                                hack_month_before_day (cur_fmt);
1127
 
                        gboolean prefer_hour = hack_prefer_hour (cur_fmt);
1128
 
 
1129
 
                        v = format_match_datetime (text, date_conv,
1130
 
                                                   month_before_day,
1131
 
                                                   FALSE,
1132
 
                                                   FALSE);
1133
 
                        if (!v)
1134
 
                                v = format_match_time (text, TRUE, prefer_hour, FALSE);
1135
 
                        if (!v)
1136
 
                                v = format_match_decimal_number (text, &fam);
1137
 
                        if (v)
1138
 
                                value_set_fmt (v, cur_fmt);
1139
 
                        return v;
1140
 
                }
1141
 
 
1142
 
                case GO_FORMAT_FRACTION:
1143
 
                        v = format_match_fraction (text, &denlen);
1144
 
                        if (!v)
1145
 
                                v = format_match_decimal_number (text, &fam);
1146
 
 
1147
 
                        if (v)
1148
 
                                value_set_fmt (v, cur_fmt);
1149
 
                        return v;
1150
 
 
1151
 
                default:
1152
 
                        ; /* Nothing */
1153
 
                }
 
1087
        fam = cur_fmt ? go_format_get_family (cur_fmt) : GO_FORMAT_GENERAL;
 
1088
        switch (fam) {
 
1089
        case GO_FORMAT_TEXT:
 
1090
                return value_new_string (text);
 
1091
 
 
1092
        case GO_FORMAT_NUMBER:
 
1093
        case GO_FORMAT_CURRENCY:
 
1094
        case GO_FORMAT_ACCOUNTING:
 
1095
        case GO_FORMAT_PERCENTAGE:
 
1096
        case GO_FORMAT_SCIENTIFIC:
 
1097
                v = format_match_decimal_number (text, &fam);
 
1098
                if (v)
 
1099
                        value_set_fmt (v, cur_fmt);
 
1100
                return v;
 
1101
 
 
1102
        case GO_FORMAT_DATE: {
 
1103
                gboolean month_before_day =
 
1104
                        hack_month_before_day (cur_fmt);
 
1105
 
 
1106
                v = format_match_datetime (text, date_conv,
 
1107
                                           month_before_day,
 
1108
                                           FALSE,
 
1109
                                           TRUE);
 
1110
                if (!v)
 
1111
                        v = format_match_decimal_number (text, &fam);
 
1112
                if (v)
 
1113
                        value_set_fmt (v, cur_fmt);
 
1114
                return v;
 
1115
        }
 
1116
 
 
1117
        case GO_FORMAT_TIME: {
 
1118
                gboolean month_before_day =
 
1119
                        hack_month_before_day (cur_fmt);
 
1120
                gboolean prefer_hour = hack_prefer_hour (cur_fmt);
 
1121
 
 
1122
                v = format_match_datetime (text, date_conv,
 
1123
                                           month_before_day,
 
1124
                                           FALSE,
 
1125
                                           FALSE);
 
1126
                if (!v)
 
1127
                        v = format_match_time (text, TRUE, prefer_hour, FALSE);
 
1128
                if (!v)
 
1129
                        v = format_match_decimal_number (text, &fam);
 
1130
                if (v)
 
1131
                        value_set_fmt (v, cur_fmt);
 
1132
                return v;
 
1133
        }
 
1134
 
 
1135
        case GO_FORMAT_FRACTION:
 
1136
                v = format_match_fraction (text, &denlen);
 
1137
                if (!v)
 
1138
                        v = format_match_decimal_number (text, &fam);
 
1139
 
 
1140
                if (v)
 
1141
                        value_set_fmt (v, cur_fmt);
 
1142
                return v;
 
1143
 
 
1144
        default:
 
1145
                ; /* Nothing */
1154
1146
        }
1155
1147
 
1156
1148
        /* Check basic types */
1170
1162
                case GO_FORMAT_ACCOUNTING: {
1171
1163
                        GOFormat *fmt =
1172
1164
                                go_format_new_from_XL
1173
 
                                (go_format_builtins[fam][2], FALSE);
 
1165
                                (go_format_builtins[fam][2]);
1174
1166
                        value_set_fmt (v, fmt);
1175
1167
                        go_format_unref (fmt);
1176
1168
                        break;
1184
1176
        }
1185
1177
 
1186
1178
        v = format_match_datetime (text, date_conv,
1187
 
                                   go_format_month_before_day (),
 
1179
                                   go_locale_month_before_day (),
1188
1180
                                   TRUE,
1189
1181
                                   FALSE);
1190
1182
        if (v)
1202
1194
 
1203
1195
                denlen = MIN (denlen, 5);
1204
1196
                sprintf (fmtstr, "# %s/%s", qqq - denlen, qqq - denlen);
1205
 
                fmt = go_format_new_from_XL (fmtstr, FALSE);
 
1197
                fmt = go_format_new_from_XL (fmtstr);
1206
1198
                value_set_fmt (v, fmt);
1207
1199
                go_format_unref (fmt);
1208
1200
                return v;