Use application properties instead of ugly dual-use global variable
[gnumeric.git] / src / go-val.c
blob10a7490b0988229001aa4cd37917beece72e7200
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * go-val.c:
5 * Copyright (C) 2008 Jody Goldberg (jody@gnome.org)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) version 3.
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.
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
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20 * USA
23 #include <gnumeric-config.h>
24 #include "go-val.h"
25 #include "gnm-datetime.h"
27 #include <goffice/goffice.h>
28 #include <glib/gi18n-lib.h>
29 #include <string.h>
31 GOVal *
32 go_val_array_index_steal (GOValArray *a, int i)
34 GOVal *res = go_val_array_index (a, i);
35 go_val_array_index (a, i) = NULL;
36 return res;
39 void
40 go_val_array_free (GOValArray *a)
42 int i;
44 if (NULL != a) {
45 for (i = (int)a->len; i-- > 0 ; )
46 go_val_free (go_val_array_index (a, i));
47 g_ptr_array_free (a, TRUE);
51 void
52 go_val_bucketer_init (GOValBucketer *bucketer)
54 memset (bucketer, 0, sizeof (GOValBucketer));
55 bucketer->type = GO_VAL_BUCKET_NONE;
58 GError *
59 go_val_bucketer_validate (GOValBucketer *bucketer)
61 GError *failure = NULL;
62 if (bucketer->type >= GO_VAL_BUCKET_SERIES_LINEAR) {
63 if (bucketer->details.dates.minimum >=
64 bucketer->details.dates.maximum)
65 failure = g_error_new (go_error_invalid (), 0, _("minima must be < maxima"));
66 else if (bucketer->details.series.step <= 0)
67 failure = g_error_new (go_error_invalid (), 0, _("step must be > 0"));
68 } else if (bucketer->type != GO_VAL_BUCKET_NONE) {
69 if (bucketer->details.dates.minimum >=
70 bucketer->details.dates.maximum)
71 failure = g_error_new (go_error_invalid (), 0, _("minima must be < maxima"));
75 return failure;
78 /**
79 * go_val_bucketer_apply:
80 * @bucketer: #GOValBucketer
81 * @v: #GOVal
83 * Calculate which bucket @v falls into.
85 * Returns -1 on general failure, and 0 for out of range below the start of the domain.
86 * Some bucketer types will also create a bucket on the high end for out of range above.
87 **/
88 int
89 go_val_bucketer_apply (GOValBucketer const *bucketer, GOVal const *v)
91 g_return_val_if_fail (bucketer != NULL, 0);
92 g_return_val_if_fail (v != NULL, 0);
94 if (bucketer->type == GO_VAL_BUCKET_NONE)
95 return 0;
97 /* Time based */
98 if (bucketer->type <= GO_VAL_BUCKET_HOUR) {
99 switch (bucketer->type) {
100 case GO_VAL_BUCKET_SECOND:
101 break;
102 case GO_VAL_BUCKET_MINUTE:
103 break;
104 default : g_assert_not_reached ();
107 /* date based */
108 if (bucketer->type <= GO_VAL_BUCKET_YEAR) {
109 static GODateConventions const default_conv = {FALSE};
110 GDate d;
111 if (!datetime_value_to_g (&d, v, &default_conv))
112 return -1;
114 switch (bucketer->type) {
115 case GO_VAL_BUCKET_DAY_OF_YEAR : return 1 + g_date_get_day_of_year (&d);
116 case GO_VAL_BUCKET_MONTH : return g_date_get_month (&d);
117 case GO_VAL_BUCKET_CALENDAR_QUARTER : return 1 + ((g_date_get_month (&d)-1) / 3);
118 case GO_VAL_BUCKET_YEAR : return 1 + g_date_get_year (&d);
119 default : g_assert_not_reached ();
123 /* >= GO_VAL_BUCKET_SERIES_LINEAR) */
125 return 0;