Commit a1f8bb83 authored by Almer S. Tigelaar's avatar Almer S. Tigelaar Committed by Almer S. Tigelaar

Add support for escaped newlines and clean-up.

2001-06-03  Almer S. Tigelaar  <almer@gnome.org>

	* src/stf.c
	(stf_parse_csv_cell): Add support for escaped newlines and
	clean-up.
	(stf_parse_options_csv_set_customfieldseparator): Support
	string separator.
	(stf_parse_options_new): Initialize customfieldseparator.
	(stf_parse_options_free): Free custom field separator.
	(stf_parse_csv_is_separator): Handle new string customfield separator.
	(stf_parse_csv_cell): Idem.
	(stf_parse_get_colcount): Idem.
	(stf_parse_get_colwidth): Idem.

2001-06-03  Almer S. Tigelaar  <almer@gnome.org>

	* dialog-stf.glade: Make length of customfield
	separator infinite.

	* dialog-stf-csv-page.c (csv_page_global_change):
	Set string customseparator instead of char.
parent eb5159f5
......@@ -2,6 +2,16 @@
* src/stf.c (stf_write_workbook): Exporting wouldn't work
because of wrong changes to the code. Fixed.
(stf_parse_csv_cell): Add support for escaped newlines and
clean-up.
(stf_parse_options_csv_set_customfieldseparator): Support
string separator.
(stf_parse_options_new): Initialize customfieldseparator.
(stf_parse_options_free): Free custom field separator.
(stf_parse_csv_is_separator): Handle new string customfield separator.
(stf_parse_csv_cell): Idem.
(stf_parse_get_colcount): Idem.
(stf_parse_get_colwidth): Idem.
2001-06-03 Morten Welinder <terra@diku.dk>
......
......@@ -4,6 +4,9 @@ Almer:
* Some work on the SAX importer.
* Finish Group/Ungroup.
* Clean-up the STF importer.
* Add support for multicharacter separators
and quoted cells with newlines to the STF
importer.
* Clean-up row naming.
* Fix the STF exporter (it works again).
......
......@@ -2,6 +2,16 @@
* src/stf.c (stf_write_workbook): Exporting wouldn't work
because of wrong changes to the code. Fixed.
(stf_parse_csv_cell): Add support for escaped newlines and
clean-up.
(stf_parse_options_csv_set_customfieldseparator): Support
string separator.
(stf_parse_options_new): Initialize customfieldseparator.
(stf_parse_options_free): Free custom field separator.
(stf_parse_csv_is_separator): Handle new string customfield separator.
(stf_parse_csv_cell): Idem.
(stf_parse_get_colcount): Idem.
(stf_parse_get_colwidth): Idem.
2001-06-03 Morten Welinder <terra@diku.dk>
......
......@@ -2,6 +2,16 @@
* src/stf.c (stf_write_workbook): Exporting wouldn't work
because of wrong changes to the code. Fixed.
(stf_parse_csv_cell): Add support for escaped newlines and
clean-up.
(stf_parse_options_csv_set_customfieldseparator): Support
string separator.
(stf_parse_options_new): Initialize customfieldseparator.
(stf_parse_options_free): Free custom field separator.
(stf_parse_csv_is_separator): Handle new string customfield separator.
(stf_parse_csv_cell): Idem.
(stf_parse_get_colcount): Idem.
(stf_parse_get_colwidth): Idem.
2001-06-03 Morten Welinder <terra@diku.dk>
......
......@@ -2,6 +2,16 @@
* src/stf.c (stf_write_workbook): Exporting wouldn't work
because of wrong changes to the code. Fixed.
(stf_parse_csv_cell): Add support for escaped newlines and
clean-up.
(stf_parse_options_csv_set_customfieldseparator): Support
string separator.
(stf_parse_options_new): Initialize customfieldseparator.
(stf_parse_options_free): Free custom field separator.
(stf_parse_csv_is_separator): Handle new string customfield separator.
(stf_parse_csv_cell): Idem.
(stf_parse_get_colcount): Idem.
(stf_parse_get_colwidth): Idem.
2001-06-03 Morten Welinder <terra@diku.dk>
......
......@@ -2,6 +2,16 @@
* src/stf.c (stf_write_workbook): Exporting wouldn't work
because of wrong changes to the code. Fixed.
(stf_parse_csv_cell): Add support for escaped newlines and
clean-up.
(stf_parse_options_csv_set_customfieldseparator): Support
string separator.
(stf_parse_options_new): Initialize customfieldseparator.
(stf_parse_options_free): Free custom field separator.
(stf_parse_csv_is_separator): Handle new string customfield separator.
(stf_parse_csv_cell): Idem.
(stf_parse_get_colcount): Idem.
(stf_parse_get_colwidth): Idem.
2001-06-03 Morten Welinder <terra@diku.dk>
......
2001-06-03 Almer S. Tigelaar <almer@gnome.org>
* dialog-stf.glade: Make length of customfield
separator infinite.
* dialog-stf-csv-page.c (csv_page_global_change):
Set string customseparator instead of char.
* dialog-stf.h: Define X overflow limit.
* dialog-stf-preview.c (stf_preview_render_row): Really
......
......@@ -8,10 +8,6 @@
#include <config.h>
#include "dialog-stf.h"
/*************************************************************************************************
* MISC UTILITY FUNCTIONS
*************************************************************************************************/
/*************************************************************************************************
* SIGNAL HANDLERS
*************************************************************************************************/
......@@ -40,7 +36,7 @@ csv_page_global_change (GtkWidget *widget, DruidPageData_t *data)
char *csvcustomtext = gtk_editable_get_chars (GTK_EDITABLE (info->csv_customseparator), 0, -1);
if (strcmp (csvcustomtext, "") != 0) {
stf_parse_options_csv_set_customfieldseparator (parseoptions, csvcustomtext[0]);
stf_parse_options_csv_set_customfieldseparator (parseoptions, csvcustomtext);
customvalid = TRUE;
}
......
......@@ -734,7 +734,7 @@ On right side only
<can_focus>True</can_focus>
<editable>True</editable>
<text_visible>True</text_visible>
<text_max_length>1</text_max_length>
<text_max_length>0</text_max_length>
<text></text>
<child>
<left_attach>1</left_attach>
......
......@@ -92,6 +92,8 @@ stf_parse_options_new (void)
parseoptions->indicator_2x_is_single = TRUE;
parseoptions->duplicates = FALSE;
parseoptions->customfieldseparator = NULL;
return parseoptions;
}
......@@ -109,6 +111,9 @@ stf_parse_options_free (StfParseOptions_t *parseoptions)
{
g_return_if_fail (parseoptions != NULL);
if (parseoptions->customfieldseparator)
g_free (parseoptions->customfieldseparator);
g_array_free (parseoptions->splitpositions, TRUE);
g_free (parseoptions);
......@@ -245,11 +250,14 @@ stf_parse_options_csv_set_separators (StfParseOptions_t *parseoptions,
* returns : nothing
**/
void
stf_parse_options_csv_set_customfieldseparator (StfParseOptions_t *parseoptions, char customfieldseparator)
stf_parse_options_csv_set_customfieldseparator (StfParseOptions_t *parseoptions, const char *customfieldseparator)
{
g_return_if_fail (parseoptions != NULL);
parseoptions->customfieldseparator = customfieldseparator;
if (parseoptions->customfieldseparator)
g_free (parseoptions->customfieldseparator);
parseoptions->customfieldseparator = g_strdup (customfieldseparator);
}
/**
......@@ -403,36 +411,38 @@ trim_spaces_inplace (char *field, const StfParseOptions_t *parseoptions)
* @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
* returns : NULL if @character is not a separator, a pointer to the character
* after the separator otherwise.
**/
static inline gboolean
stf_parse_csv_is_separator (const char *character, StfParseType_t parsetype, char customfieldseparator)
static inline const char *
stf_parse_csv_is_separator (const char *character, StfParseType_t parsetype, char *customfieldseparator)
{
g_return_val_if_fail (character != NULL, FALSE);
g_return_val_if_fail (character != NULL, NULL);
if ((parsetype & TEXT_SEPARATOR_CUSTOM)
&& (strncmp (character, customfieldseparator, strlen (customfieldseparator)) == 0))
return &character[strlen (customfieldseparator)];
/* 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
*/
if (isalnum ((unsigned char)*character))
return FALSE;
return NULL;
switch (*character) {
case ' ' : if (parsetype & TEXT_SEPARATOR_SPACE) return TRUE; break;
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_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;
case ' ' : if (parsetype & TEXT_SEPARATOR_SPACE) return &character[1]; break;
case '\t' : if (parsetype & TEXT_SEPARATOR_TAB) return &character[1]; break;
case ':' : if (parsetype & TEXT_SEPARATOR_COLON) return &character[1]; break;
case ',' : if (parsetype & TEXT_SEPARATOR_COMMA) return &character[1]; break;
case ';' : if (parsetype & TEXT_SEPARATOR_SEMICOLON) return &character[1]; break;
case '|' : if (parsetype & TEXT_SEPARATOR_PIPE) return &character[1]; break;
case '/' : if (parsetype & TEXT_SEPARATOR_SLASH) return &character[1]; break;
case '-' : if (parsetype & TEXT_SEPARATOR_HYPHEN) return &character[1]; break;
case '!' : if (parsetype & TEXT_SEPARATOR_BANG) return &character[1]; break;
}
if ((parsetype & TEXT_SEPARATOR_CUSTOM) && (customfieldseparator == *character))
return TRUE;
return FALSE;
return NULL;
}
/**
......@@ -447,7 +457,7 @@ stf_parse_csv_cell (Source_t *src, StfParseOptions_t *parseoptions)
{
GString *res;
const char *cur;
gboolean isstring, sawstringterm;
gboolean sawstringterm;
int len = 0;
g_return_val_if_fail (src != NULL, NULL);
......@@ -457,20 +467,14 @@ stf_parse_csv_cell (Source_t *src, StfParseOptions_t *parseoptions)
/* A leading quote is always a quote */
if (*cur == parseoptions->stringindicator) {
cur++;
isstring = TRUE;
sawstringterm = FALSE;
} else {
isstring = FALSE;
} else
sawstringterm = TRUE;
}
res = g_string_new ("");
while (*cur && *cur != parseoptions->terminator) {
while (*cur) {
if (!sawstringterm) {
......@@ -487,17 +491,21 @@ stf_parse_csv_cell (Source_t *src, StfParseOptions_t *parseoptions)
}
}
} else {
const char *s;
if (*cur == parseoptions->terminator)
break;
if (stf_parse_csv_is_separator (cur, parseoptions->separators, parseoptions->customfieldseparator)) {
if ((s = stf_parse_csv_is_separator (cur, parseoptions->separators, parseoptions->customfieldseparator))) {
const char *r;
if (parseoptions->duplicates) {
const char *nextcur = cur;
nextcur++;
if (stf_parse_csv_is_separator (nextcur, parseoptions->separators, parseoptions->customfieldseparator)) {
cur = nextcur;
if ((r = stf_parse_csv_is_separator (s, parseoptions->separators, parseoptions->customfieldseparator))) {
cur = r;
continue;
}
}
cur = s;
break;
}
}
......@@ -508,10 +516,6 @@ stf_parse_csv_cell (Source_t *src, StfParseOptions_t *parseoptions)
cur++;
}
/* Only skip over cell terminators, not line terminators or terminating nulls*/
if (*cur != parseoptions->terminator && *cur)
cur++;
src->position = cur;
if (len != 0) {
......@@ -597,7 +601,7 @@ stf_parse_fixed_cell (Source_t *src, StfParseOptions_t *parseoptions)
else
splitval = -1;
while (*cur && *cur != parseoptions->terminator && splitval != src->linepos) {
while (*cur && *cur != parseoptions->terminator && splitval != src->linepos) {
g_string_append_c (res, *cur);
src->linepos++;
......@@ -802,37 +806,33 @@ stf_parse_get_colcount (StfParseOptions_t *parseoptions, const char *data)
const char *iterator = data;
int tempcount = 0;
while (1) {
if (*iterator == parseoptions->terminator || *iterator == '\0') {
while (iterator && *iterator != '\0') {
const char *s;
if (*iterator == parseoptions->terminator) {
if (tempcount > colcount)
colcount = tempcount;
tempcount = 0;
if (*iterator == '\0')
break;
}
if (stf_parse_csv_is_separator (iterator, parseoptions->separators, parseoptions->customfieldseparator)) {
if ((s = stf_parse_csv_is_separator (iterator, parseoptions->separators, parseoptions->customfieldseparator))) {
if (parseoptions->duplicates) {
const char *nextiterator = iterator;
const char *r;
nextiterator++;
if (stf_parse_csv_is_separator (nextiterator, parseoptions->separators, parseoptions->customfieldseparator)) {
iterator = nextiterator;
if ((r = stf_parse_csv_is_separator (s, parseoptions->separators, parseoptions->customfieldseparator))) {
iterator = r;
continue;
}
}
iterator = s;
tempcount++;
}
iterator++;
} else
iterator++;
}
if (tempcount > colcount)
colcount = tempcount;
} else {
colcount = my_garray_len (parseoptions->splitpositions) - 1;
}
......@@ -907,42 +907,39 @@ stf_parse_get_colwidth (StfParseOptions_t *parseoptions, const char *data, int i
const char *iterator = data;
int col = 0, colwidth = 0;
while (1) {
if (stf_parse_csv_is_separator (iterator, parseoptions->separators, parseoptions->customfieldseparator) || *iterator == parseoptions->terminator || *iterator == '\0') {
if (*iterator != 0 && parseoptions->duplicates) {
const char *nextiterator = iterator;
while (iterator && *iterator != '\0') {
const char *s;
if ((s = stf_parse_csv_is_separator (iterator, parseoptions->separators, parseoptions->customfieldseparator))) {
nextiterator++;
if (stf_parse_csv_is_separator (nextiterator, parseoptions->separators, parseoptions->customfieldseparator)) {
if (parseoptions->duplicates) {
const char *r;
iterator = nextiterator;
if ((r = stf_parse_csv_is_separator (s, parseoptions->separators, parseoptions->customfieldseparator))) {
iterator = r;
continue;
}
}
if (col == index) {
if (col == index && colwidth > width)
width = colwidth;
if (colwidth > width)
colwidth = 0;
iterator = s;
col++;
} else {
colwidth++;
if (*iterator == parseoptions->terminator) {
if (col == index && colwidth > width)
width = colwidth;
}
if (*iterator == parseoptions->terminator)
col = 0;
else
col++;
colwidth = -1;
colwidth = 0;
}
iterator++;
}
if (*iterator == '\0')
break;
colwidth++;
iterator++;
}
if (col == index && colwidth > width)
width = colwidth;
} else {
int colstart, colend;
......
......@@ -40,7 +40,7 @@ typedef struct {
/* CSV related */
StfTextSeparator_t separators; /* Text separator(s) */
char customfieldseparator; /* Custom text separator */
char *customfieldseparator; /* Custom text separator */
char stringindicator; /* String indicator */
gboolean indicator_2x_is_single;/* 2 quote chars are a single non-terminating quote */
gboolean duplicates; /* See two text separator's as one? */
......@@ -74,7 +74,7 @@ void stf_parse_options_csv_set_separators (StfParseO
gboolean semicolon, gboolean pipe,
gboolean slash, gboolean hyphen,
gboolean bang, gboolean custom);
void stf_parse_options_csv_set_customfieldseparator (StfParseOptions_t *parseoptions, char customfieldseparator);
void stf_parse_options_csv_set_customfieldseparator (StfParseOptions_t *parseoptions, const char *customfieldseparator);
void stf_parse_options_csv_set_stringindicator (StfParseOptions_t *parseoptions, char stringindicator);
void stf_parse_options_csv_set_indicator_2x_is_single (StfParseOptions_t *parseoptions, gboolean indic_2x);
void stf_parse_options_csv_set_duplicates (StfParseOptions_t *parseoptions, gboolean duplicates);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment