stf-parse.c 37.3 KB
Newer Older
1 2 3 4
/*
 * stf-parse.c : Structured Text Format parser. (STF)
 *               A general purpose engine for parsing data
 *               in CSV and Fixed width format.
5
 *
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * Copyright (C) Almer. S. Tigelaar.
 * EMail: almer1@dds.nl or almer-t@bigfoot.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

25
#include <config.h>
26
#include <ctype.h>
27

28
#include "stf-parse.h"
29
#include "clipboard.h"
30 31

/* Some nice warning messages */
32 33
#define WARN_TOO_MANY_ROWS _("Too many rows in data to parse: %d")
#define WARN_TOO_MANY_COLS _("Too many columns in data to parse: %d")
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

/* CacheItem_t struct, used for the caching engine */
typedef struct {
	const char *line;      /* A pointer to the start of the line in memory */
	int signature;         /* signature, used for caching */
} CacheItem_t;

/* Source_t struct, used for interchanging parsing information between the low level parse functions */
typedef struct {
	const char *position;  /* Indicates the current position within data */

	/* Used internally for fixed width parsing */
	int splitpos;          /* Indicates current position in splitpositions array */
	int linepos;           /* Position on the current line */
} Source_t;

/*******************************************************************************************************
 * STF PARSE OPTIONS : StfParseOptions related
 *******************************************************************************************************/

/**
 * stf_parse_options_new
 *
 * This will return a new StfParseOptions_t struct.
 * The struct should, after being used, freed with stf_parse_options_free.
 *
 * returns : a new StfParseOptions_t
 **/
StfParseOptions_t*
stf_parse_options_new (void)
{
	StfParseOptions_t* parseoptions = g_new0 (StfParseOptions_t, 1);

67 68 69 70
	parseoptions->parsetype   = PARSE_TYPE_NOTSET;
	parseoptions->terminator  = '\n';
	parseoptions->parselines  = -1;
	parseoptions->trim_spaces = (TRIM_TYPE_RIGHT | TRIM_TYPE_LEFT);
71

72
	parseoptions->splitpositions    = g_array_new (FALSE, FALSE, sizeof (int));
73
	parseoptions->oldsplitpositions = NULL;
74
	parseoptions->modificationmode  = FALSE;
75 76

	parseoptions->indicator_2x_is_single = TRUE;
77
	parseoptions->duplicates             = FALSE;
78

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
	return parseoptions;
}

/**
 * stf_parse_options_free
 * @parseoptions : a parse options struct
 *
 * will free @parseoptions, note that this will not free the splitpositions
 * member (GArray) of the struct, the caller is responsible to do that.
 *
 * returns : nothing
 **/
void
stf_parse_options_free (StfParseOptions_t *parseoptions)
{
	g_return_if_fail (parseoptions != NULL);

	g_array_free (parseoptions->splitpositions, TRUE);

	g_free (parseoptions);
}

/**
 * stf_parse_options_set_type
 * @parseoptions : a parse options struct
 * @parsetype : the import type you wish to do (PARSE_TYPE_CSV or PARSE_TYPE_FIXED)
 *
 * returns : nothing
 **/
void
stf_parse_options_set_type (StfParseOptions_t *parseoptions, StfParseType_t parsetype)
{
	g_return_if_fail (parseoptions != NULL);
	g_return_if_fail ((parsetype == PARSE_TYPE_CSV || parsetype == PARSE_TYPE_FIXED));

	if (parseoptions->parsetype != parsetype)
		parseoptions->modified = TRUE;
116

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	parseoptions->parsetype = parsetype;
}

/**
 * stf_parse_options_set_line_terminator
 * @parseoptions : a parse options struct
 * @terminator : a char indicating a line terminator
 *
 * This will set the line terminator, in both the Fixed width and CSV delimited importers
 * this indicates the end of a row. If you set this to '\0' the whole data will be treated
 * as on big line, note that '\n' (newlines) will not be parsed out if you set the
 * terminator to anything other than a newline
 *
 * returns : nothing
 **/
void
stf_parse_options_set_line_terminator (StfParseOptions_t *parseoptions, char terminator)
{
	g_return_if_fail (parseoptions != NULL);

	if (parseoptions->terminator != terminator)
		parseoptions->modified = TRUE;
139

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
	parseoptions->terminator = terminator;
}

/**
 * stf_parse_options_set_lines_to_parse
 * @parseoptions : a parse options struct
 * @lines : number of lines to parse
 *
 * This actually forces the parser to stop after parsing @lines lines, if you set @lines
 * to -1, which is also the default, the parser will parse till it encounters a '\0'
 *
 * returns : nothing
 **/
void
stf_parse_options_set_lines_to_parse (StfParseOptions_t *parseoptions, int lines)
{
	g_return_if_fail (parseoptions != NULL);

	/* we'll convert this to an index by subtracting 1 */
	lines--;
160

161 162
	if (parseoptions->parselines != lines)
		parseoptions->modified = TRUE;
163

164 165 166
	parseoptions->parselines = lines;
}

167 168 169
/**
 * stf_parse_options_set_trim_spaces:
 * @parseoptions: a parse options struct
170
 * @trim_spaces: whether you want to trim spaces or not
171 172 173 174 175 176 177 178 179 180
 * 
 * If enabled will trim spaces in every parsed field on left and right
 * sides.
 **/
void
stf_parse_options_set_trim_spaces (StfParseOptions_t *parseoptions, StfTrimType_t trim_spaces)
{
	g_return_if_fail (parseoptions != NULL);

	if (parseoptions->trim_spaces != trim_spaces)
Jody Goldberg's avatar
Jody Goldberg committed
181
		parseoptions->modified = TRUE;
182 183 184 185

	parseoptions->trim_spaces = trim_spaces;
}

186 187 188 189 190 191 192 193 194 195
/**
 * stf_parse_options_before_modification
 * @parseoptions : a parse options struct
 *
 * returns : nothing
 **/
void
stf_parse_options_before_modification (StfParseOptions_t *parseoptions)
{
	int i;
196

197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
	g_return_if_fail (parseoptions != NULL);
	g_return_if_fail (parseoptions->modificationmode == FALSE);

	parseoptions->modified = FALSE;
	parseoptions->modificationmode = TRUE;

	/* Copy the old splitpositions into a temporary array so we can compare them later */
	parseoptions->oldsplitpositions = g_array_new (FALSE, FALSE, sizeof (int));

	for (i = 0; i < parseoptions->splitpositions->len; i++)
		g_array_append_val (parseoptions->oldsplitpositions, (int) g_array_index (parseoptions->splitpositions, int, i));
}

/**
 * stf_parse_options_after_modification
 * @parseoptions : an import options struct
 *
 * returns : weather the contents of @parseoptions has been changed since the
 *           last call to stf_parse_options_beofre_modification;
 **/
gboolean
stf_parse_options_after_modification (StfParseOptions_t *parseoptions)
{
	g_return_val_if_fail (parseoptions != NULL, FALSE);
	g_return_val_if_fail (parseoptions->modificationmode == TRUE, FALSE);
222

223 224 225 226
	if (parseoptions->splitpositions->len == parseoptions->oldsplitpositions->len) {
		int i;

		for (i = 0; i < parseoptions->oldsplitpositions->len; i++) {
227

228 229 230 231 232 233
			if (g_array_index (parseoptions->oldsplitpositions, int, i) != g_array_index (parseoptions->splitpositions, int, i)) {

				parseoptions->modified = TRUE;
				break;
			}
		}
234
	} else
235 236 237 238
		parseoptions->modified = TRUE;

	g_array_free (parseoptions->oldsplitpositions, TRUE);
	parseoptions->modificationmode = FALSE;
239

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
	return parseoptions->modified;
}

/**
 * stf_parse_options_csv_set_separators
 * @parseoptions : an import options struct
 * @tab, @colon, @comma, @space, @custom : indicates the field separators
 *
 * NOTE : if @custom is set you should also set @parseoptions->customfieldseparator
 *
 * returns : nothing
 **/
void
stf_parse_options_csv_set_separators (StfParseOptions_t *parseoptions,
				       gboolean tab, gboolean colon,
				       gboolean comma, gboolean space,
256 257 258
				       gboolean semicolon, gboolean pipe,
				       gboolean slash, gboolean hyphen,
				       gboolean bang, gboolean custom)
259 260
{
	StfTextSeparator_t separators;
261

262 263 264
	g_return_if_fail (parseoptions != NULL);

	separators = 0;
265

266 267 268 269 270 271 272 273
	if (tab)
		separators |= TEXT_SEPARATOR_TAB;
	if (colon)
		separators |= TEXT_SEPARATOR_COLON;
	if (comma)
		separators |= TEXT_SEPARATOR_COMMA;
	if (space)
		separators |= TEXT_SEPARATOR_SPACE;
274 275 276 277 278 279 280 281 282 283
	if (semicolon)
		separators |= TEXT_SEPARATOR_SEMICOLON;
	if (pipe)
		separators |= TEXT_SEPARATOR_PIPE;
	if (slash)
		separators |= TEXT_SEPARATOR_SLASH;
	if (hyphen)
		separators |= TEXT_SEPARATOR_HYPHEN;
	if (bang)
		separators |= TEXT_SEPARATOR_BANG;
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
	if (custom)
		separators |= TEXT_SEPARATOR_CUSTOM;

	if (parseoptions->separators != separators)
		parseoptions->modified = TRUE;

	parseoptions->separators = separators;
}

/**
 * stf_parse_options_csv_set_customfieldseparator
 * @parseoptions : a parse options struct
 * @customfieldseparator : a char that will be used as custom field separator
 *
 * NOTE : This will only be used if TEXT_SEPARATOR_CUSTOM in @parseoptions->separators is also set
 *
 * returns : nothing
 **/
void
stf_parse_options_csv_set_customfieldseparator (StfParseOptions_t *parseoptions, char customfieldseparator)
{
	g_return_if_fail (parseoptions != NULL);

	if (parseoptions->customfieldseparator != customfieldseparator)
		parseoptions->modified = TRUE;
309

310 311 312 313 314 315
	parseoptions->customfieldseparator = customfieldseparator;
}

/**
 * stf_parse_options_csv_set_stringindicator
 * @parseoptions : a parse options struct
316
 * @stringindicator : a char representing the string indicator
317 318 319 320 321 322 323 324 325 326 327
 *
 * returns : nothing
 **/
void
stf_parse_options_csv_set_stringindicator (StfParseOptions_t *parseoptions, char stringindicator)
{
	g_return_if_fail (parseoptions != NULL);
	g_return_if_fail (stringindicator != '\0');

	if (parseoptions->stringindicator != stringindicator)
		parseoptions->modified = TRUE;
328

329 330 331
	parseoptions->stringindicator = stringindicator;
}

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
/**
 * stf_parse_options_csv_set_indicator_2x_is_single :
 * @parseoptions : a parse options struct
 * @indic_2x : a boolean value indicating whether we want to see two
 * 		adjacent string indicators as a single string indicator
 * 		that is part of the cell, rather than a terminator.
 *
 * returns : nothing
 **/
void
stf_parse_options_csv_set_indicator_2x_is_single  (StfParseOptions_t *parseoptions,
						   gboolean indic_2x)
{
	g_return_if_fail (parseoptions != NULL);

	if (parseoptions->indicator_2x_is_single != indic_2x)
		parseoptions->modified = TRUE;

	parseoptions->indicator_2x_is_single = indic_2x;
}

353 354 355
/**
 * stf_parse_options_csv_set_duplicates
 * @parseoptions : a parse options struct
356 357
 * @duplicates : a boolean value indicating whether we want to see two
 *               separators right behind each other as one
358 359 360 361 362 363 364 365 366 367
 *
 * returns : nothing
 **/
void
stf_parse_options_csv_set_duplicates (StfParseOptions_t *parseoptions, gboolean duplicates)
{
	g_return_if_fail (parseoptions != NULL);

	if (parseoptions->duplicates != duplicates)
		parseoptions->modified = TRUE;
368

369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
	parseoptions->duplicates = duplicates;
}

/**
 * stf_parse_options_fixed_splitpositions_clear
 * @parseoptions : a parse options struct
 *
 * This will clear the splitpositions (== points on which a line is split)
 *
 * returns : nothing
 **/
void
stf_parse_options_fixed_splitpositions_clear (StfParseOptions_t *parseoptions)
{
	g_return_if_fail (parseoptions != NULL);
384

385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
	g_array_free (parseoptions->splitpositions, TRUE);
	parseoptions->splitpositions = g_array_new (FALSE, FALSE, sizeof (int));
}

/**
 * stf_parse_options_fixed_splitpositions_add
 * @parseoptions : a parse options struct
 * @position : a new position to split at
 *
 * @position will be added to the splitpositions, @position must be equal to
 * or greater than zero or -1 which means as much as "parse to end of line"
 *
 * returns : nothing
 **/
void
stf_parse_options_fixed_splitpositions_add (StfParseOptions_t *parseoptions, int position)
{
	g_return_if_fail (parseoptions != NULL);
	g_return_if_fail (position >= 0 || position == -1);

	g_array_append_val (parseoptions->splitpositions, position);
}


/**
 * stf_parse_options_valid
 * @parseoptions : an import options struct
 *
 * Checks if @parseoptions is correctly filled
 *
 * returns : TRUE if it is correctly filled, FALSE otherwise.
 **/
static gboolean
stf_parse_options_valid (StfParseOptions_t *parseoptions)
{
	g_return_val_if_fail (parseoptions != NULL, FALSE);
421

422 423 424
	if (parseoptions->parsetype == PARSE_TYPE_CSV) {

		if (parseoptions->stringindicator == '\0') {
425

426 427 428
			g_warning ("STF: Cannot have \\0 as string indicator");
			return FALSE;
		}
429

430 431 432
	} else if (parseoptions->parsetype == PARSE_TYPE_FIXED) {

		if (!parseoptions->splitpositions) {
433

434 435 436 437 438 439 440 441 442 443
			g_warning ("STF: No splitpositions in struct");
			return FALSE;
		}
	}

	if (parseoptions->modificationmode) {

		g_warning ("STF: Before modification without end modification called");
		return FALSE;
	}
444

445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
	return TRUE;
}

/*******************************************************************************************************
 * STF CACHE OPTIONS : StfCacheOptions related
 *******************************************************************************************************/

/**
 * stf_cache_options_new
 *
 * This will return a new StfCacheOptions_t struct.
 * The struct should, after being used, freed with stf_parse_options_free.
 *
 * returns : a new StfCacheOptions_t
 **/
StfCacheOptions_t*
stf_cache_options_new (void)
{
	StfCacheOptions_t *cacheoptions = g_new0 (StfCacheOptions_t, 1);

	cacheoptions->fromline = -1;
	cacheoptions->toline   = -1;

	cacheoptions->lines = g_ptr_array_new ();
	cacheoptions->validsignature = 0;
470

471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
	return cacheoptions;
}

/**
 * stf_cache_options_free
 * @cacheoptions : an import options struct
 *
 * will free @cacheoptions
 *
 * returns : nothing
 **/
void
stf_cache_options_free (StfCacheOptions_t *cacheoptions)
{
	int i;

	g_return_if_fail (cacheoptions != NULL);

	for (i = 0; i < cacheoptions->lines->len; i++) {
		CacheItem_t *item;

		item = g_ptr_array_index (cacheoptions->lines, i);
		g_free (item);
	}
495

496
	g_ptr_array_free (cacheoptions->lines, TRUE);
497

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
	g_free (cacheoptions);
}

/**
 * stf_cache_options_set_data
 * @cacheoptions : a cache options struct
 * @parseoptions : a parse options struct
 * @data : a pointer to the data to parse
 *
 * This routine will actually look at the data and put pointer to the start
 * of _every_ line in a pointer array
 *
 * returns : nothing
 **/
void
stf_cache_options_set_data (StfCacheOptions_t *cacheoptions, StfParseOptions_t *parseoptions, const char *data)
{
	const char *iterator = data;
	gboolean terminator = FALSE;
	int i;

	g_return_if_fail (cacheoptions != NULL);
	g_return_if_fail (data != NULL);

	cacheoptions->data = data;

	for (i = 0; i < cacheoptions->lines->len; i++) {
		CacheItem_t *item;

		item = g_ptr_array_index (cacheoptions->lines, i);
		g_free (item);
	}
530

531 532 533 534 535 536 537 538
	g_ptr_array_free (cacheoptions->lines, TRUE);
	cacheoptions->lines = g_ptr_array_new ();

	cacheoptions->linecount = 0;
	terminator = TRUE;
	while (*iterator) {

		if (*iterator == parseoptions->terminator && !terminator) {
539

540 541 542 543
			terminator = TRUE;
			iterator++;
			continue;
		}
544

545 546
		if (terminator) {
			CacheItem_t *newitem;
547

548 549 550 551
			newitem = g_new (CacheItem_t, 1);

			newitem->line = iterator;
			newitem->signature = 0;
552

553
			g_ptr_array_add (cacheoptions->lines, newitem);
554

555
			terminator = FALSE;
556

557
			cacheoptions->linecount++;
558

559 560 561
			if (parseoptions->parselines != -1)
				if (cacheoptions->linecount > parseoptions->parselines)
					break;
562

563 564
			continue;
		}
565

566 567 568 569 570
		iterator++;
	}

	cacheoptions->validsignature = 1;
}
571

572 573 574 575 576 577 578 579 580 581 582 583 584
/**
 * stf_cache_options_set_range
 * @parseoptions : a cache options struct
 * @fromline : line number to start importing at (zero based)
 * @toline : line number to stop importing at (zero based)
 *
 * returns : nothing
 **/
void
stf_cache_options_set_range (StfCacheOptions_t *cacheoptions, int fromline, int toline)
{

	g_return_if_fail (cacheoptions != NULL);
585

586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
	cacheoptions->fromline = fromline;
	cacheoptions->toline = toline;
}

/**
 * stf_cache_options_invalidate
 * @cacheoptions : a cache options struct
 *
 * This will invalidate the already cached data so it will
 * be re-parsed when necessary
 *
 * returns : nothing
 **/
void
stf_cache_options_invalidate (StfCacheOptions_t *cacheoptions)
{

	g_return_if_fail (cacheoptions != NULL);

	cacheoptions->validsignature++;

	/* If it has wrapped around, invalidate all cache items, just to be sure */
	if (cacheoptions->validsignature == 0) {
		CacheItem_t *iterator;
		int i;
611

612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
		for (i = 0; i < cacheoptions->lines->len; i++) {

			iterator = g_ptr_array_index (cacheoptions->lines, i);

			if (iterator)
				iterator->signature = -1;
		}
	}
}

/**
 * stf_cache_options_valid
 * @cacheoptions : a cache options struct
 *
 * This will check if the cache options struct is valid
 *
 * returns : true if the struct is correctly filled, false otherwise.
 **/
static gboolean
stf_cache_options_valid (StfCacheOptions_t *cacheoptions)
{
	g_return_val_if_fail (cacheoptions != NULL, FALSE);

	if (!cacheoptions->data) {
636

637 638 639 640 641 642 643 644 645 646 647
		g_warning ("STF: No input data set in cacheoptions!");
		return FALSE;
	}

	return TRUE;
}

/*******************************************************************************************************
 * STF PARSE : The actual routines that do the 'trick'
 *******************************************************************************************************/

648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
static void
trim_spaces_inplace (char *field, const StfParseOptions_t *parseoptions)
{
	unsigned char *s = (unsigned char *)field;

	if (!s) return;

	if (isspace (*s) && parseoptions->trim_spaces & TRIM_TYPE_LEFT) {
		unsigned char *tmp = s;
		while (isspace (*tmp)) tmp++;
		strcpy (s, tmp);
	}

	if (parseoptions->trim_spaces & TRIM_TYPE_RIGHT) {
		int len = strlen (s);
		while (len && isspace (s[len - 1]))
			s[--len] = 0;
	}
}


669 670 671 672 673 674 675 676 677 678 679 680 681
/**
 * stf_parse_csv_is_separator
 * @character : pointer to the character to check
 * @parsetype : a bitwise orred field of what to see as separator and what not
 * @customfieldseparator : the custom field separator which will be examined if TEXT_SEPARATOR_CUSTOM is set
 *
 * returns : true if character is a separator, false otherwise
 **/
static gboolean
stf_parse_csv_is_separator (const char *character, StfParseType_t parsetype, char customfieldseparator)
{
	g_return_val_if_fail (character != NULL, FALSE);

682
	if ( (parsetype & TEXT_SEPARATOR_CUSTOM) && (customfieldseparator == *character))
683
			return TRUE;
684 685 686 687 688

	/* I have done this to slightly speed up the parsing, if we
	 * wouldn't do this, it had to go over the case statement below for
	 * each character, which clearly slows parsing down
	 */
689
	if (isalnum ((unsigned char)*character)) {
690 691 692

		return FALSE;
	}
693

694 695 696 697 698 699 700 701 702 703
	switch (*character) {
	case '\t'             : if (parsetype & TEXT_SEPARATOR_TAB)       return TRUE; break;
	case ':'              : if (parsetype & TEXT_SEPARATOR_COLON)     return TRUE; break;
	case ','              : if (parsetype & TEXT_SEPARATOR_COMMA)     return TRUE; break;
	case ' '              : if (parsetype & TEXT_SEPARATOR_SPACE)     return TRUE; break;
	case ';'              : if (parsetype & TEXT_SEPARATOR_SEMICOLON) return TRUE; break;
	case '|'              : if (parsetype & TEXT_SEPARATOR_PIPE)      return TRUE; break;
	case '/'              : if (parsetype & TEXT_SEPARATOR_SLASH)     return TRUE; break;
	case '-'              : if (parsetype & TEXT_SEPARATOR_HYPHEN)    return TRUE; break;
	case '!'              : if (parsetype & TEXT_SEPARATOR_BANG)      return TRUE; break;
704 705
        }

706

707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
	return FALSE;
}

/**
 * stf_parse_csv_cell
 * @src : struct containing information/memory location of the input data
 * @parseoptions : struct containing separation customizations
 *
 * returns : a pointer to the parsed cell contents. (has to be freed by the calling routine)
 **/
static char*
stf_parse_csv_cell (Source_t *src, StfParseOptions_t *parseoptions)
{
	GString *res;
	const char *cur;
	gboolean isstring, sawstringterm;
	int len = 0;

	g_return_val_if_fail (src != NULL, NULL);
	g_return_val_if_fail (parseoptions != NULL, NULL);

	cur = src->position;
729 730

	/* A leading quote is always a quote */
731
	if (*cur == parseoptions->stringindicator) {
732

733 734 735 736
		cur++;
		isstring = TRUE;
		sawstringterm = FALSE;
	} else {
737

738 739 740 741 742 743
		isstring = FALSE;
		sawstringterm = TRUE;
        }


	res = g_string_new ("");
744

745 746 747
	while (*cur && *cur != parseoptions->terminator) {

		if (!sawstringterm) {
748

749 750 751 752 753
			if (*cur == parseoptions->stringindicator) {
				/* two stringindicators in a row represent a
				 * single stringindicator character that does
				 * not terminate the cell
				 */
754
				cur++;
755 756 757 758 759
				if (*cur != parseoptions->stringindicator ||
				    !parseoptions->indicator_2x_is_single) {
					sawstringterm = TRUE;
					continue;
				}
760 761
			}
		} else {
762

763
			if (stf_parse_csv_is_separator (cur, parseoptions->separators, parseoptions->customfieldseparator)) {
764

765 766 767 768 769 770 771 772 773
				if (parseoptions->duplicates) {
					const char *nextcur = cur;
					nextcur++;
					if (stf_parse_csv_is_separator (nextcur, parseoptions->separators, parseoptions->customfieldseparator)) {
						cur = nextcur;
						continue;
					}
				}
				break;
774
			}
775 776 777 778 779 780 781 782 783 784 785 786 787
		}

		g_string_append_c (res, *cur);

		len++;
		cur++;
	}

	/* Only skip over cell terminators, not line terminators or terminating nulls*/
	if (*cur != parseoptions->terminator && *cur)
		cur++;

	src->position = cur;
788

789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
	if (len != 0) {
		char *tmp = res->str;
		g_string_free (res, FALSE);
		return tmp;
	}

	g_string_free (res, TRUE);
	return NULL;
}

/**
 * stf_parse_csv_line
 * @src : a struct containing information on the source data
 * @parseoptions : a struct containing parsing options
 *
 * This will parse one line from the current @src->position.
 * It will return a GSList with the cell contents as strings.
 * NOTE :
 * 1) The calling routine is responsible for freeing the string in the GSList
 * 2) The calling routine is responsible for freeing the list itself.
 *
 * returns : a list with char*'s (contains the cells reversed)
 **/
static GSList*
stf_parse_csv_line (Source_t *src, StfParseOptions_t *parseoptions)
{
	GSList *list = NULL;
	GSList *listend = NULL;
	char *field;
	int col = 0;

	g_return_val_if_fail (src != NULL, NULL);
	g_return_val_if_fail (parseoptions != NULL, NULL);
822

823 824 825
	while (*src->position && *src->position != parseoptions->terminator) {
		field = stf_parse_csv_cell (src, parseoptions);

826 827
		trim_spaces_inplace (field, parseoptions);

828
		if (list != NULL) {
829

830 831 832 833 834 835
			listend = g_slist_append (listend, field)->next;
		} else {

			list = g_slist_append (list, field);
			listend = list;
		}
836

837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
		if (++col >= SHEET_MAX_COLS) {
			g_warning (WARN_TOO_MANY_COLS, col);
			return NULL;
		}
	}

	return list;
}

/**
 * stf_parse_fixed_cell
 * @src : a struct containing information on the source data
 * @parseoptions : a struct containing parsing options
 *
 * returns : a pointer to the parsed cell contents. (has to be freed by the calling routine)
 **/
static char*
stf_parse_fixed_cell (Source_t *src, StfParseOptions_t *parseoptions)
{
	GString *res;
	const char* cur;
858 859
	int splitval;
	int len = 0;
860 861 862

	g_return_val_if_fail (src != NULL, NULL);
	g_return_val_if_fail (parseoptions != NULL, NULL);
863

864
	cur = src->position;
865

866
	res = g_string_new ("");
867
	if (src->splitpos < parseoptions->splitpositions->len)
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
		splitval = (int) g_array_index (parseoptions->splitpositions, int, src->splitpos);
	else
		splitval = -1;

	while (*cur && *cur != parseoptions->terminator  && splitval != src->linepos) {
		g_string_append_c (res, *cur);

		src->linepos++;
	        cur++;
		len++;
	}

	src->position = cur;

	if (len != 0) {
		char *tmp = res->str;
		g_string_free (res, FALSE);
		return tmp;
	}

	g_string_free (res, TRUE);
	return NULL;
}

/**
 * stf_parse_fixed_line
 * @src : a struct containing information on the source data
 * @parseoptions : a struct containing parsing options
 *
 * This will parse one line from the current @src->position.
 * It will return a GSList with the cell contents as strings.
 * NOTE :
 * 1) The calling routine is responsible for freeing the string in the GSList
 * 2) The calling routine is responsible for freeing the list itself.
 *
 * returns : a list with char*'s
 **/
static GSList*
stf_parse_fixed_line (Source_t *src, StfParseOptions_t *parseoptions)
{
	GSList *list = NULL;
	GSList *listend = NULL;
	int col = 0;

	g_return_val_if_fail (src != NULL, NULL);
	g_return_val_if_fail (parseoptions != NULL, NULL);

	src->linepos = 0;
	src->splitpos = 0;
917

918
	while (*src->position && *src->position != parseoptions->terminator) {
919 920
		char *field;
		field = (char *)stf_parse_fixed_cell (src, parseoptions);
921

922
		trim_spaces_inplace (field, parseoptions);
923

924
		if (list != NULL) {
925 926 927 928 929
			listend = g_slist_append (listend, field)->next;
		} else {
			list = g_slist_append (list, field);
			listend = list;
		}
930

931 932 933 934
		if (++col >= SHEET_MAX_COLS) {
			g_warning (WARN_TOO_MANY_COLS, col);
			return NULL;
		}
935

936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
		src->splitpos++;
	}

	return list;
}


/**
 * stf_parse_general
 * @parseoptions : a parseoptions struct
 * @data : the data to parse
 *
 * The GSList that is returned contains smaller GSLists.
 * The format is more or less :
 *
 * GSList (Top-level)
 *  |------>GSList (Sub)
 *  |------>GSList (Sub)
 *  |------>GSList (Sub)
 *
 * Every Sub-GSList represents a parsed line and contains
 * strings as data. if a Sub-GSList is NULL this means the line
 * was empty.
 * NOTE : The calling routine has to free *a lot* of stuff by
 *        itself :
 *        1) The data of each Sub-GSList item (with g_free) these are all strings.
 *        2) The sub-GSList's themselves (with g_slist_free).
 *        3) The top level GSList.
 *
 * returns : a GSList with Sub-GSList's containing celldata.
 **/
GSList*
stf_parse_general (StfParseOptions_t *parseoptions, const char *data)
{
	GSList *celllist = NULL;
	GSList *list = NULL;
	GSList *listend = NULL;
	Source_t src;
	int row;

	g_return_val_if_fail (parseoptions != NULL, NULL);
	g_return_val_if_fail (data != NULL, NULL);
	g_return_val_if_fail (stf_parse_options_valid (parseoptions), NULL);

	src.position = data;
	row = 0;
982

983
	while (TRUE) {
984

985 986 987 988 989 990 991 992 993
		if (parseoptions->parsetype == PARSE_TYPE_CSV)
			celllist = stf_parse_csv_line (&src, parseoptions);
		else
			celllist = stf_parse_fixed_line (&src, parseoptions);

		if (list != NULL) {

			listend = g_slist_append (listend, celllist)->next;
		} else {
994

995 996 997
			list = g_slist_append (list, celllist);
			listend = list;
		}
998

999
		if (++row >= SHEET_MAX_ROWS) {
1000

1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
				g_warning (WARN_TOO_MANY_ROWS, row);
				return NULL;
		}

		if (parseoptions->parselines != -1)
			if (row > parseoptions->parselines)
				break;

		if (*src.position == '\0')
			break;
1011

1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
		src.position++;
	}

	return list;
}

/**
 * stf_parse_general_cached
 * @parseoptions : parsing options struct
 * @cacheoptions : cache options struct
 *
 * The format of the returned GSList is _exacly_ the same
 * as with stf_parse_general. (look at the description of that functions
 * to find out the format). there are however TWO small differences :
 * 1) The list will only contain @cacheoptions->fromline to @cacheoptions->toline
 * 2) Any line which has been cached already according to the cache data will _NOT_
 *    be parsed again. the sublist of that line will be set to NULL
 *
 * REMEMBER : YOU MUST FREE _EVERYTHING_ YOURSELF (look at stf_parse_general for details on this)
 *
 * returns : a GSList containing Sub-GSList's which contain actual row data (cells)
 **/
GSList*
stf_parse_general_cached (StfParseOptions_t *parseoptions, StfCacheOptions_t *cacheoptions)
{
	GSList *celllist = NULL;
	GSList *list = NULL;
	GSList *listend = NULL;
	Source_t src;
	CacheItem_t *item;
	int row;
1043

1044 1045 1046 1047 1048 1049
	g_return_val_if_fail (parseoptions != NULL, NULL);
	g_return_val_if_fail (cacheoptions != NULL, NULL);
	g_return_val_if_fail (stf_parse_options_valid (parseoptions), NULL);
	g_return_val_if_fail (stf_cache_options_valid (cacheoptions), NULL);

	if (cacheoptions->lines->len - 1 >= SHEET_MAX_ROWS) {
1050

1051 1052 1053 1054 1055 1056
		g_warning (WARN_TOO_MANY_ROWS, cacheoptions->lines->len - 1);
		return NULL;
	}

	if (cacheoptions->toline > cacheoptions->lines->len - 1 || cacheoptions->toline == -1)
		cacheoptions->toline = cacheoptions->lines->len - 1;
1057

1058 1059 1060
	for (row = cacheoptions->fromline; row <= cacheoptions->toline; row++) {

		item = g_ptr_array_index (cacheoptions->lines, row);
1061

1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
		if (item->signature != cacheoptions->validsignature) {
			src.position = item->line;

			if (parseoptions->parsetype == PARSE_TYPE_CSV)
				celllist = stf_parse_csv_line (&src, parseoptions);
			else
				celllist = stf_parse_fixed_line (&src, parseoptions);

			item->signature = cacheoptions->validsignature;
		} else {

			celllist = NULL;
		}

		if (list != NULL) {

			listend = g_slist_append (listend, celllist)->next;
		} else {

			list = g_slist_append (list, celllist);
			listend = list;
		}
	}
1085

1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
	return list;
}

/**
 * stf_parse_get_rowcount
 * @parseoptions : a parse options struct
 * @data : the data
 *
 * This will retrieve the number of rows @data consists of.
 * It will always return the right number of rows weather you are using cache or
 * not.
 *
 * returns : number of rows in @data
 **/
int
stf_parse_get_rowcount (StfParseOptions_t *parseoptions, const char *data)
{
	const char *iterator;
	int rowcount = 0;
1105
	gboolean last_row_empty = TRUE;
1106 1107 1108

	g_return_val_if_fail (parseoptions != NULL, 0);
	g_return_val_if_fail (data != NULL, 0);
1109

1110
	iterator = data;
1111

1112 1113
	while (*iterator) {
		if (*iterator == parseoptions->terminator)
1114 1115 1116
			rowcount++, last_row_empty = TRUE;
		else
			last_row_empty = FALSE;
1117 1118 1119 1120

		if (parseoptions->parselines != -1)
			if (rowcount > parseoptions->parselines)
				break;
1121

1122 1123 1124
		iterator++;
	}

1125
	return last_row_empty ? rowcount : rowcount + 1;
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
}

/**
 * stf_parse_get_colcount
 * @parseoptions : a parse options struct
 * @data : the data
 *
 * This will retrieve the number of columns @data consists of.
 * It will always return the right number of columns weather your are using
 * cache or doing a fixed or csv parse.
 *
 * returns : number of columns in @data
 **/
int
stf_parse_get_colcount (StfParseOptions_t *parseoptions, const char *data)
{
	int colcount = 0;

	g_return_val_if_fail (parseoptions != NULL, 0);
	g_return_val_if_fail (data != NULL, 0);

	if (parseoptions->parsetype == PARSE_TYPE_CSV) {
		const char *iterator = data;
		int tempcount = 0;
1150

1151
		while (1) {
1152

1153
			if (*iterator == parseoptions->terminator || *iterator == '\0') {
1154 1155 1156

				if (tempcount > colcount)
					colcount = tempcount;
1157

1158
				tempcount = 0;
1159 1160 1161

				if (*iterator == '\0')
					break;
1162
			}
1163

1164 1165 1166 1167
			if (stf_parse_csv_is_separator (iterator, parseoptions->separators, parseoptions->customfieldseparator)) {

				if (parseoptions->duplicates) {
					const char *nextiterator = iterator;
1168

1169 1170 1171 1172 1173 1174 1175
					nextiterator++;
					if (stf_parse_csv_is_separator (nextiterator, parseoptions->separators, parseoptions->customfieldseparator)) {

						iterator = nextiterator;
						continue;
					}
				}
1176

1177 1178
				tempcount++;
			}
1179

1180 1181 1182
			iterator++;
		}
	} else {
1183

1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
		colcount = parseoptions->splitpositions->len - 1;
	}


	return colcount;
}

/**
 * stf_parse_get_longest_row_width
 * @parseoptions : a parse options struct
 * @data : the data
 *
 * Returns the largest number of characters found in a line/row
 *
 * returns : the longest row width.
 **/
int
stf_parse_get_longest_row_width (StfParseOptions_t *parseoptions, const char *data)
{
	const char *iterator;
	int len = 0;
	int longest = 0;
	int row = 0;

	g_return_val_if_fail (parseoptions != NULL, 0);
	g_return_val_if_fail (data != NULL, 0);

	iterator = data;
	while (*iterator) {
1213

1214 1215 1216 1217
		if (*iterator == parseoptions->terminator) {

			if (len > longest)
				longest = len;
1218

1219
			len = 0;
1220
			row++;
1221
		}
1222

1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
		iterator++;
		len++;

		if (parseoptions->parselines != -1)
			if (row > parseoptions->parselines)
				break;
	}

	return longest - 1;
}
/**
 * stf_parse_get_colwidth
 * @parseoptions : a parse options struct
 * @data : the data
 * @index : the index of the column you wish to know the width of
 *
 * Will determine the width of column @index in @data given the
 * parsing rules as specified in @parseoptions
 *
 * returns : the width, in characters, of the column @index.
 **/
int
stf_parse_get_colwidth (StfParseOptions_t *parseoptions, const char *data, int index)
{
	int width = 0;

	g_return_val_if_fail (parseoptions != NULL, 0);
	g_return_val_if_fail (data != NULL, 0);

	if (parseoptions->parsetype == PARSE_TYPE_CSV) {
		const char *iterator = data;
		int col = 0, colwidth = 0;

1256
		while (1) {
1257

1258
			if (stf_parse_csv_is_separator (iterator, parseoptions->separators, parseoptions->customfieldseparator) || *iterator == parseoptions->terminator || *iterator == '\0') {
1259 1260 1261

				if (parseoptions->duplicates) {
					const char *nextiterator = iterator;
1262

1263 1264 1265 1266 1267 1268 1269
					nextiterator++;
					if (stf_parse_csv_is_separator (nextiterator, parseoptions->separators, parseoptions->customfieldseparator)) {

						iterator = nextiterator;
						continue;
					}
				}
1270

1271
				if (col == index) {
1272

1273 1274 1275 1276 1277 1278 1279 1280 1281
					if (colwidth > width)
						width = colwidth;
				}


				if (*iterator == parseoptions->terminator)
					col = 0;
				else
					col++;
1282

1283 1284
				colwidth = -1;
			}
1285 1286 1287

			if (*iterator == '\0')
				break;
1288

1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
			colwidth++;
			iterator++;
		}
	} else {
		int colstart, colend;

		if (index - 1 < 0)
			colstart = 0;
		else
			colstart = (int) g_array_index (parseoptions->splitpositions, int, index - 1);

		colend = (int) g_array_index (parseoptions->splitpositions, int, index);
		if (colend == -1) {
			const char *iterator = data;
			int len = 0, templen = 0;
1304

1305
			while (*iterator) {
1306

1307 1308 1309 1310
				if (*iterator == parseoptions->terminator) {

					if (templen > len)
						len = templen;
1311

1312 1313
					templen = 0;
				}
1314

1315 1316 1317 1318 1319 1320
				templen++;
				iterator++;
			}

			colend = len;
		}
1321

1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
		width = colend - colstart;
	}

	return width;

}

/**
 * stf_parse_convert_to_unix
 * @data : the char buffer to convert
 *
1333
 * This function will convert the @data into
1334
 * unix line-terminated format. this means that CRLF (windows) will be converted to LF
1335 1336
 * and CR (Macintosh) to LF.
 * In addition to that form feed (\F) characters will be removed.
1337
 * NOTE : This will not resize the buffer
1338
 *
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
 * returns : TRUE on success, FALSE otherwise.
 **/
gboolean
stf_parse_convert_to_unix (const char *data)
{
	const char *iterator = data;
	char *dest = (char *) data;
	int len = 0;

	if (!iterator)
		return FALSE;
1350

1351
	while (*iterator) {
1352

1353 1354
		if (*iterator == '\r') {
			const char *temp = iterator;
1355

1356 1357 1358 1359 1360 1361 1362
			temp++;

			if (*temp != '\n') {
				*dest = '\n';
				dest++;
				len++;
			}
1363 1364 1365
			iterator++;
		} else if (*iterator == '\f') {
		
1366 1367
			iterator++;
		}
1368
		
1369
		*dest = *iterator;
1370

1371 1372 1373 1374 1375
		iterator++;
		dest++;
		len++;
	}
	*dest = '\0';
1376

1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
	return TRUE;
}

/**
 * stf_is_valid_data
 * @data : the data to validate
 *
 * returns weather the input data is valid to import.
 * (meaning it checks weather it is text only)
 *
 * returns : true if valid, false otherwise
 **/
gboolean
stf_parse_is_valid_data (const char *data)
{
	gboolean valid;
	const char *iterator = data;

	valid = TRUE;
	while (*iterator) {
1397

1398 1399 1400
		if (!isprint ((unsigned char)*iterator) &&
		    *iterator != '\n' &&
		    *iterator != '\r' &&
1401 1402
		    *iterator != '\t' &&
		    *iterator != '\f') {
1403

1404 1405 1406 1407
			valid = FALSE;
			break;
		}
		iterator++;
1408 1409
	}

1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
	return valid;
}

/*******************************************************************************************************
 * STF PARSE HL: high-level functions that dump the raw data returned by the low-level parsing
 *               functions into something meaningful (== application specific)
 *******************************************************************************************************/

Sheet*
stf_parse_sheet (StfParseOptions_t *parseoptions, const char *data, Sheet *sheet)
{
	GSList *list;
1422
	GSList *list_start;
1423 1424 1425 1426 1427 1428 1429 1430
	GSList *sublist;
	int row = 0, col;

	g_return_val_if_fail (parseoptions != NULL, NULL);
	g_return_val_if_fail (data != NULL, NULL);
	g_return_val_if_fail (sheet != NULL, NULL);

	list = stf_parse_general (parseoptions, data);
1431
	list_start = list;
1432 1433 1434 1435 1436

	while (list) {

		sublist = list->data;
		col = 0;
1437

1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
		while (sublist) {

			if (sublist->data) {
				Cell *newcell = sheet_cell_new (sheet, col, row);
				char *celldata = sublist->data;

				/* The '@' character appears to be somewhat magic, so
				 * if the line starts with an '@' character we have to prepend an '=' char and qoutes
				 */
				if (celldata[0] == '@') {
					char *tmp;
1449

1450
					tmp = g_strdup_printf ("=\"%s\"", celldata);
1451

1452 1453 1454
					g_free (celldata);
					sublist->data = tmp;
				}
1455

1456
				cell_set_text (newcell, sublist->data);
1457 1458 1459

				g_free (sublist->data);
			}
1460

1461 1462 1463 1464
			sublist = g_slist_next (sublist);
			col++;
		}

1465
		g_slist_free (list->data);
1466

1467 1468 1469
		list = g_slist_next (list);
		row++;
	}
1470

1471
	g_slist_free (list_start);
1472 1473 1474 1475 1476 1477 1478 1479 1480

	return sheet;
}

CellRegion*
stf_parse_region (StfParseOptions_t *parseoptions, const char *data)
{
	CellRegion *cr;
	GSList *list;
1481
	GSList *list_start;
1482 1483 1484 1485 1486 1487 1488 1489
	GSList *sublist;
	GList *resultlist = NULL;
	int row = 0, col = 0, colhigh = 0;

	g_return_val_if_fail (parseoptions != NULL, NULL);
	g_return_val_if_fail (data != NULL, NULL);

	list = stf_parse_general (parseoptions, data);
1490 1491
	list_start = list;
	
1492 1493 1494 1495
	while (list) {

		sublist = list->data;
		col = 0;
1496

1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507
		while (sublist) {

			if (sublist->data) {
				CellCopy *ccopy;
				char *celldata = sublist->data;

				/* The '@' character appears to be somewhat magic, so
				 * if the line starts with an '@' character we have to prepend an '=' char and qoutes
				 */
				if (celldata[0] == '@') {
					char *tmp;
1508

1509
					tmp = g_strdup_printf ("=\"%s\"", celldata);
1510

1511 1512 1513
					g_free (celldata);
					sublist->data = tmp;
				}
1514

1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
				ccopy = g_new (CellCopy, 1);
				ccopy->type = CELL_COPY_TYPE_TEXT;
				ccopy->col_offset = col;
				ccopy->row_offset = row;
				ccopy->u.text = sublist->data;

				resultlist = g_list_prepend (resultlist, ccopy);

				/* we don't free sublist->data, simply because CellCopy will do this */
			}
1525

1526 1527 1528 1529
			sublist = g_slist_next (sublist);
			col++;
		}

1530
		g_slist_free (list->data);
1531 1532 1533

		if (col > colhigh)
			colhigh = col;
1534

1535 1536 1537
		list = g_slist_next (list);
		row++;
	}
1538

1539
	g_slist_free (list_start);
1540 1541 1542

	cr         = g_new (CellRegion, 1);
	cr->list   = resultlist;
1543
	cr->cols   = (colhigh > 0) ? colhigh : 1;
1544 1545
	cr->rows   = row;
	cr->styles = NULL;
1546

1547 1548
	return cr;
}