value.h 4.5 KB
Newer Older
1 2 3
#ifndef GNUMERIC_VALUE_H
#define GNUMERIC_VALUE_H

4 5
#include <glib.h>
#include "gnumeric.h"
6 7 8
#include "sheet.h"
#include "numbers.h"
#include "str.h"
9
#include "position.h"
10 11 12 13

typedef enum {
	/* Use magic values to act as a signature */
	VALUE_EMPTY	= 10,
14 15 16 17 18
	VALUE_BOOLEAN	= 20, /* Keep bool < int < float */
	VALUE_INTEGER	= 30,
	VALUE_FLOAT	= 40,
	VALUE_ERROR	= 50,
	VALUE_STRING	= 60,
19 20 21 22
	VALUE_CELLRANGE = 70,
	VALUE_ARRAY	= 80
} ValueType;

23 24 25 26 27 28 29 30 31 32 33 34 35 36
struct _ValueBool {
	ValueType const type;
	gboolean val;
};
struct _ValueInt {
	ValueType const type;
	int_t val;
};
struct _ValueFloat {
	ValueType const type;
	float_t val;
};
struct _ValueErr {
	ValueType const type;
37
	String       *mesg;
38
	/* Currently unused.  Intended to support audit functions */
39
	EvalPos  src;
40
};
41 42 43 44 45 46
struct _ValueStr {
	ValueType const type;
	String *val;
};
struct _ValueRange {
	ValueType const type;
47
	RangeRef cell;
48 49
};
struct _ValueArray {
50
	ValueType const type;
51 52 53
	int x, y;
	Value ***vals;  /* Array [x][y] */
};
54

55 56 57 58 59 60 61 62 63
union _Value {
	ValueType const type;
	ValueBool	v_bool;
	ValueInt	v_int;
	ValueFloat	v_float;
	ValueErr	v_err;
	ValueStr	v_str;
	ValueRange	v_range;
	ValueArray	v_array;
64 65
};

66 67 68 69 70
#define VALUE_IS_EMPTY(v)		(((v) == NULL) || ((v)->type == VALUE_EMPTY))
#define VALUE_IS_EMPTY_OR_ERROR(v)	(VALUE_IS_EMPTY(v) || (v)->type == VALUE_ERROR)
#define VALUE_IS_NUMBER(v)		(((v)->type == VALUE_INTEGER) || \
					 ((v)->type == VALUE_FLOAT) || \
					 ((v)->type == VALUE_BOOLEAN))
71

72 73
Value       *value_new_empty            (void);
Value       *value_new_bool             (gboolean b);
74 75
Value       *value_new_int              (int i);
Value       *value_new_float            (float_t f);
76 77
Value       *value_new_error            (EvalPos const *pos, char const *mesg);
Value       *value_new_error_str        (EvalPos const *pos, String *mesg);
78 79 80 81 82 83 84 85 86
Value       *value_new_string           (const char *str);
Value       *value_new_string_str       (String *str);
Value       *value_new_cellrange_unsafe (const CellRef *a, const CellRef *b);
Value       *value_new_cellrange        (const CellRef *a, const CellRef *b,
				         int const eval_col, int const eval_row);
Value       *value_new_cellrange_r      (Sheet *sheet, const Range *r);
Value       *value_new_array            (guint cols, guint rows);
Value       *value_new_array_empty      (guint cols, guint rows);
Value 	    *value_new_array_non_init   (guint cols, guint rows);
87 88

void         value_release         (Value *value);
89 90
void         value_dump            (Value const *value);
Value       *value_duplicate       (Value const *value);
Michael Meeks's avatar
Michael Meeks committed
91
gboolean     value_equal           (const Value *a, const Value *b);
92

93
gboolean     value_get_as_bool     (Value const *v, gboolean *err);
94 95 96 97 98 99 100 101 102 103
char        *value_get_as_string   (const Value *value);
int          value_get_as_int      (const Value *v);
float_t      value_get_as_float    (const Value *v);
char        *value_cellrange_get_as_string (const Value *value,
					    gboolean use_relative_syntax);

/* Return a Special error value indicating that the iteration should stop */
Value       *value_terminate (void);

/* Area functions ( works on VALUE_RANGE or VALUE_ARRAY */
104
/* The EvalPos provides a Sheet context; this allows
105
   calculation of relative references. 'x','y' give the position */
106 107
guint        value_area_get_width  (const EvalPos *ep, Value const *v);
guint        value_area_get_height (const EvalPos *ep, Value const *v);
108 109

/* Return Value(int 0) if non-existant */
110
const Value *value_area_fetch_x_y  (const EvalPos *ep, Value const * v,
111 112 113
				    guint x, guint y);

/* Return NULL if non-existant */
114
const Value * value_area_get_x_y (const EvalPos *ep, Value const * v,
115 116
				  guint x, guint y);

117
typedef  Value * (*value_area_foreach_callback)(EvalPos const *ep,
118 119
						Value const *v, void *user_data);

120
Value * value_area_foreach (EvalPos const *ep, Value const *v,
121 122 123
			    value_area_foreach_callback callback,
			    void *closure);

124 125 126
void value_array_set       (Value *array, guint col, guint row, Value *v);
void value_array_resize    (Value *v, guint width, guint height);

127
Value * value_is_error (char const * const str, int *offset);
128
StyleHAlignFlags value_get_default_halign (Value const *v, MStyle const *mstyle);
129

130 131 132 133 134 135 136 137
/* Some utility constants to make sure we all spell correctly */
extern char const *gnumeric_err_NULL;
extern char const *gnumeric_err_DIV0;
extern char const *gnumeric_err_VALUE;
extern char const *gnumeric_err_REF;
extern char const *gnumeric_err_NAME;
extern char const *gnumeric_err_NUM;
extern char const *gnumeric_err_NA;
138
extern char const *gnumeric_err_RECALC;
139 140

#endif /* GNUMERIC_VALUE_H */