Update Spanish translation
[gnumeric.git] / src / go-val.c
blobd5127c41b5fb5fca5b880e84c66ac444d8d93ee8
1 /*
2 * go-val.c:
4 * Copyright (C) 2008 Jody Goldberg (jody@gnome.org)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) version 3.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 * USA
22 #include <gnumeric-config.h>
23 #include <go-val.h>
24 #include <gnm-datetime.h>
26 #include <goffice/goffice.h>
27 #include <glib/gi18n-lib.h>
28 #include <string.h>
30 GOVal *
31 go_val_array_index_steal (GOValArray *a, int i)
33 GOVal *res = go_val_array_index (a, i);
34 go_val_array_index (a, i) = NULL;
35 return res;
38 void
39 go_val_array_free (GOValArray *a)
41 int i;
43 if (NULL != a) {
44 for (i = (int)a->len; i-- > 0 ; )
45 go_val_free (go_val_array_index (a, i));
46 g_ptr_array_free (a, TRUE);
50 void
51 go_val_bucketer_init (GOValBucketer *bucketer)
53 memset (bucketer, 0, sizeof (GOValBucketer));
54 bucketer->type = GO_VAL_BUCKET_NONE;
57 GError *
58 go_val_bucketer_validate (GOValBucketer *bucketer)
60 GError *failure = NULL;
61 if (bucketer->type >= GO_VAL_BUCKET_SERIES_LINEAR) {
62 if (bucketer->details.dates.minimum >=
63 bucketer->details.dates.maximum)
64 failure = g_error_new (go_error_invalid (), 0, _("minima must be < maxima"));
65 else if (bucketer->details.series.step <= 0)
66 failure = g_error_new (go_error_invalid (), 0, _("step must be > 0"));
67 } else if (bucketer->type != GO_VAL_BUCKET_NONE) {
68 if (bucketer->details.dates.minimum >=
69 bucketer->details.dates.maximum)
70 failure = g_error_new (go_error_invalid (), 0, _("minima must be < maxima"));
74 return failure;
77 /**
78 * go_val_bucketer_apply:
79 * @bucketer: #GOValBucketer
80 * @v: #GOVal
82 * Calculate which bucket @v falls into.
84 * Returns -1 on general failure, and 0 for out of range below the start of the domain.
85 * Some bucketer types will also create a bucket on the high end for out of range above.
86 **/
87 int
88 go_val_bucketer_apply (GOValBucketer const *bucketer, GOVal const *v)
90 g_return_val_if_fail (bucketer != NULL, 0);
91 g_return_val_if_fail (v != NULL, 0);
93 if (bucketer->type == GO_VAL_BUCKET_NONE)
94 return 0;
96 /* Time based */
97 if (bucketer->type <= GO_VAL_BUCKET_HOUR) {
98 switch (bucketer->type) {
99 case GO_VAL_BUCKET_SECOND:
100 break;
101 case GO_VAL_BUCKET_MINUTE:
102 break;
103 default : g_assert_not_reached ();
106 /* date based */
107 if (bucketer->type <= GO_VAL_BUCKET_YEAR) {
108 static GODateConventions const default_conv = {FALSE};
109 GDate d;
110 if (!datetime_value_to_g (&d, v, &default_conv))
111 return -1;
113 switch (bucketer->type) {
114 case GO_VAL_BUCKET_DAY_OF_YEAR : return 1 + g_date_get_day_of_year (&d);
115 case GO_VAL_BUCKET_MONTH : return g_date_get_month (&d);
116 case GO_VAL_BUCKET_CALENDAR_QUARTER : return 1 + ((g_date_get_month (&d)-1) / 3);
117 case GO_VAL_BUCKET_YEAR : return 1 + g_date_get_year (&d);
118 default : g_assert_not_reached ();
122 /* >= GO_VAL_BUCKET_SERIES_LINEAR) */
124 return 0;