utils.c 7.37 KB
Newer Older
1 2 3 4 5
/*
 * utils.c:  Various utility routines that do not depend on the GUI of Gnumeric
 *
 * Author:
 *    Miguel de Icaza (miguel@gnu.org)
6
 *    Jukka-Pekka Iivonen (iivonen@iki.fi)
7
 */
8 9
#include <config.h>
#include <stdlib.h>
10
#include <stdio.h>
11 12
#include <glib.h>
#include <string.h>
13
#include <string.h>
14
#include <ctype.h>
15
#include <gnome.h>
16
#include "numbers.h"
17
#include "mathfunc.h"
18
#include "symbol.h"
19
#include "str.h"
20 21
#include "expr.h"
#include "utils.h"
22 23
#include "gnumeric.h"
#include "sheet.h"
24 25 26 27 28

#define SMALL_BUF_SIZE 40
static char small_buffer [SMALL_BUF_SIZE];

void
29
float_get_from_range (const char *start, const char *end, float_t *t)
30 31 32
{
	char *p;
	int  size = end - start;
Morten Welinder's avatar
Morten Welinder committed
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
	if (size < SMALL_BUF_SIZE-1){
		p = small_buffer;
		strncpy (small_buffer, start, size);
		small_buffer [size] = 0;
	} else {
		p = g_malloc (size + 1);

		strcpy (p, start);
		p [size] = 0;
	}
#ifdef GNUMERIC_USE_GMP
	mpf_init_set_str (*t, p, 10);
#else
	*t = atof (p);
#endif
	if (p != small_buffer)
		g_free (p);
}

void
54
int_get_from_range (const char *start, const char *end, int_t *t)
55 56 57
{
	char *p;
	int  size = end - start;
Morten Welinder's avatar
Morten Welinder committed
58

59 60 61 62 63 64 65
	if (size < SMALL_BUF_SIZE-1){
		p = small_buffer;
		strncpy (small_buffer, start, size);
		small_buffer [size] = 0;
	} else {
		p = g_malloc (size + 1);

66
		strncpy (p, start, size);
67 68 69 70 71 72 73 74 75 76
		p [size] = 0;
	}
#ifdef GNUMERIC_USE_GMP
	mpz_init_set_str (*t, p, 10);
#else
	*t = atoi (p);
#endif
	if (p != small_buffer)
		g_free (p);
}
77

78
const char *
79 80
cell_name (int col, int row)
{
Arturo Espinosa's avatar
Arturo Espinosa committed
81
	static char buffer [2 + 4 * sizeof (long)];
82
	char *p = buffer;
Morten Welinder's avatar
Morten Welinder committed
83

84
	if (col <= 'Z'-'A'){
85 86
		*p++ = col + 'A';
	} else {
87 88
		int a = col / ('Z'-'A'+1);
		int b = col % ('Z'-'A'+1);
Morten Welinder's avatar
Morten Welinder committed
89

90
		*p++ = a + 'A' - 1;
91 92 93 94 95 96 97
		*p++ = b + 'A';
	}
	sprintf (p, "%d", row+1);

	return buffer;
}

98
const char *
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
99 100
col_name (int col)
{
101
	static char buffer [3];
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
102
	char *p = buffer;
103 104
	
	g_assert (col < SHEET_MAX_COLS);
Morten Welinder's avatar
Morten Welinder committed
105

106
	if (col <= 'Z'-'A'){
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
107 108
		*p++ = col + 'A';
	} else {
109 110
		int a = col / ('Z'-'A'+1);
		int b = col % ('Z'-'A'+1);
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
111

112
		*p++ = a + 'A' - 1;
Arturo Espinosa's avatar
Today:  
Arturo Espinosa committed
113 114 115 116 117 118 119
		*p++ = b + 'A';
	}
	*p = 0;

	return buffer;
}

120 121 122 123
/**
 * Converts a column name into an integer
 **/
int
124
col_from_name (const char *cell_str)
125
{
Morten Welinder's avatar
Morten Welinder committed
126 127
	char c;
	int col = 0;
128

129
	c = toupper ((unsigned char)*cell_str++);
Morten Welinder's avatar
Morten Welinder committed
130
	if (c < 'A' || c > 'Z')
131
		return FALSE;
Morten Welinder's avatar
Morten Welinder committed
132
	col = c - 'A';
133
	c = toupper ((unsigned char)*cell_str);
Morten Welinder's avatar
Morten Welinder committed
134 135 136 137 138 139
	if (c >= 'A' && c <= 'Z')
		col = ((col + 1) * ('Z' - 'A' + 1)) + (c - 'A');
	if (col >= SHEET_MAX_COLS)
		return FALSE;
	else
		return col;
140 141
}

142 143 144 145 146 147 148 149 150
/*
 * parse_cell_name
 * @cell_name:   a string representation of a cell name.
 * @col:         result col
 * @row:         result row
 *
 * Return value: true if the cell_name could be successfully parsed
 */
int
151
parse_cell_name (const char *cell_str, int *col, int *row)
152
{
Morten Welinder's avatar
Morten Welinder committed
153
	char c;
154

Morten Welinder's avatar
Morten Welinder committed
155
	/* Parse column name: one or two letters.  */
156
	c = toupper ((unsigned char)*cell_str++);
Morten Welinder's avatar
Morten Welinder committed
157
	if (c < 'A' || c > 'Z')
158
		return FALSE;
159

Morten Welinder's avatar
Morten Welinder committed
160
	*col = c - 'A';
161
	c = toupper ((unsigned char)*cell_str);
Morten Welinder's avatar
Morten Welinder committed
162 163 164 165 166
	if (c >= 'A' && c <= 'Z') {
		*col = ((*col + 1) * ('Z' - 'A' + 1)) + (c - 'A');
		cell_str++;
	}
	if (*col >= SHEET_MAX_COLS)
167 168
		return FALSE;

Morten Welinder's avatar
Morten Welinder committed
169 170
	/* Parse row number: a sequence of digits.  */
	for (*row = 0; *cell_str; cell_str++) {
Morten Welinder's avatar
Morten Welinder committed
171
		if (*cell_str < '0' || *cell_str > '9')
172 173
			return FALSE;
		*row = *row * 10 + (*cell_str - '0');
Morten Welinder's avatar
Morten Welinder committed
174 175
		if (*row > SHEET_MAX_ROWS) /* Note: ">" is deliberate.  */
			return FALSE;
176 177 178 179
	}
	if (*row == 0)
		return FALSE;

Morten Welinder's avatar
Morten Welinder committed
180
	/* Internal row numbers are one less than the displayed.  */
181 182 183
	(*row)--;
	return TRUE;
}
184 185 186 187 188 189 190 191 192 193 194

gint
gnumeric_strcase_equal (gconstpointer v, gconstpointer v2)
{
	return strcasecmp ((const gchar*) v, (const gchar*)v2) == 0;
}

/* a char* hash function from ASU */
guint
gnumeric_strcase_hash (gconstpointer v)
{
195
	const unsigned char *s = (const unsigned char *)v;
196
	const unsigned char *p;
197
	guint h = 0, g;
Morten Welinder's avatar
Morten Welinder committed
198

199 200 201 202 203 204 205
	for(p = s; *p != '\0'; p += 1) {
		h = ( h << 4 ) + tolower (*p);
		if ( ( g = h & 0xf0000000 ) ) {
			h = h ^ (g >> 24);
			h = h ^ g;
		}
	}
Morten Welinder's avatar
Morten Welinder committed
206

207 208 209
	return h /* % M */;
}

210

Morten Welinder's avatar
Morten Welinder committed
211 212
/* One less that the Julian day number of 19000101.  */
static guint32 date_origin = 0;
213

214 215
/*
 * The serial number of 19000228.  Excel allocates a serial number for
Morten Welinder's avatar
Morten Welinder committed
216 217 218
 * the non-existing date 19000229.
 */
static const guint32 date_serial_19000228 = 58;
Morten Welinder's avatar
Morten Welinder committed
219

Morten Welinder's avatar
Morten Welinder committed
220 221 222 223 224 225 226
static void
date_init (void)
{
	/* Day 1 means 1st of January of 1900 */
	GDate* date = g_date_new_dmy (1, 1, 1900);
	date_origin = g_date_julian (date) - 1;
	g_date_free (date);
227 228
}

Morten Welinder's avatar
Morten Welinder committed
229
guint32
230 231
g_date_serial (GDate* date)
{
Morten Welinder's avatar
Morten Welinder committed
232 233 234 235 236 237 238
	guint32 day;

	if (!date_origin)
		date_init ();

	day = g_date_julian (date) - date_origin;
	return day + (day > date_serial_19000228);
239 240 241 242 243
}

GDate*
g_date_new_serial (guint32 serial)
{
Morten Welinder's avatar
Morten Welinder committed
244 245 246 247 248 249 250 251 252 253
	if (!date_origin)
		date_init ();

	if (serial <= date_serial_19000228)
		return g_date_new_julian (serial + date_origin);
	else if (serial == date_serial_19000228 + 1) {
		g_warning ("Request for date 19000229.");
		return g_date_new_julian (serial + date_origin);
	} else
		return g_date_new_julian (serial + date_origin - 1);
254
}
255

256 257
/*
 * Returns a list of cells in a string.  If the named cells does not
258 259 260
 * exist, they are created.  If the input string is not valid
 * error_flag is set.
 */
261
GSList *
Morten Welinder's avatar
Morten Welinder committed
262
parse_cell_name_list (Sheet *sheet,
263 264
		      const char *cell_name_str,
		      int *error_flag)
265
{
266
        char     *buf;
267 268 269 270
	GSList   *cells = NULL;
	Cell     *cell;
	int      i, n, col, row;

271 272 273 274
	g_return_val_if_fail (sheet != NULL, NULL);
	g_return_val_if_fail (IS_SHEET (sheet), NULL);
	g_return_val_if_fail (cell_name_str != NULL, NULL);
	g_return_val_if_fail (error_flag != NULL, NULL);
Morten Welinder's avatar
Morten Welinder committed
275

276 277 278 279 280 281 282
	buf = g_malloc (strlen (cell_name_str) + 1);
	for (i = n = 0; 1; i++){

	        if ((cell_name_str [i] == ',') || (cell_name_str [i] == '\0')){
		        buf [n] = '\0';

			if (!parse_cell_name (buf, &col, &row)){
283 284 285 286 287
			        *error_flag = 1;
				free (buf);
				g_slist_free (cells);
				return NULL;
			}
288

289
			cell = sheet_cell_get (sheet, col, row);
290 291

			if (cell == NULL){
292 293 294 295 296 297
			        cell = sheet_cell_new (sheet, col, row);
				cell_set_text (cell, "");
			}
			cells = g_slist_append (cells, (gpointer) cell);
			n = 0;
		} else
298
		        buf [n++] = cell_name_str [i];
299

300
		if (cell_name_str [i] == '\0')
301 302 303 304 305 306 307
		        break;
	}

	*error_flag = 0;
	free (buf);
	return cells;
}
Morten Welinder's avatar
Morten Welinder committed
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 337 338 339 340 341 342 343 344 345 346 347 348


/*
 * Conservative random number generator.  The result is (supposedly) uniform
 * and between 0 and 1.  (0 possible, 1 not.)  The result should have about
 * 64 bits randomness.
 */
double
random_01 (void)
{
#ifdef HAVE_RANDOM
	int r1, r2;

	r1 = random () & 2147483647;
	r2 = random () & 2147483647;

	return (r1 + (r2 / 2147483648.0)) / 2147483648.0;
#elif defined (HAVE_DRAND48)
	return drand48 ();
#else
	/* We try to work around lack of randomness in rand's lower bits.  */
	int prime = 65537;
	int r1, r2, r3, r4;

	g_assert (RAND_MAX > ((1 << 12) - 1));

	r1 = (rand () ^ (rand () << 12)) % prime;
	r2 = (rand () ^ (rand () << 12)) % prime;
	r3 = (rand () ^ (rand () << 12)) % prime;
	r4 = (rand () ^ (rand () << 12)) % prime;

	return (r1 + (r2 + (r3 + r4 / (double)prime) / prime) / prime) / prime;
#endif
}

/*
 * Generate a N(0,1) distributed number.
 */
double
random_normal (void)
{
349
	return qnorm (random_01 (), 0, 1);
Morten Welinder's avatar
Morten Welinder committed
350
}
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

/*
 * str_trim_spaces:
 * s: the string to modify
 *
 * This routine trims the leading and trailing spaces of the
 * string.  The string is possibly modified and the returned
 * value lies inside the original string.
 *
 * No duplication takes place
 */
char *
str_trim_spaces (char *s)
{
	char *p;
	
	while (*s && *s == ' ')
		s++;

	p = s + strlen (s);
	while (p >= s){
		if (*p == ' ')
			*p = 0;
		else
			break;
		p--;
	}
	return s;
}