1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 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 67 68 69 70 71 72 73 74 75 76 77 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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* GNU Oleo input filter for Gnumeric
*
* Author:
* Robert Brady <rwb197@ecs.soton.ac.uk>
*
* partially based on the Lotus-123 code,
* partially based on actual Oleo code.
*/
#include <gnumeric-config.h>
#include <glib/gi18n-lib.h>
#include <gnumeric.h>
#include "oleo.h"
#include <workbook.h>
#include <sheet.h>
#include <cell.h>
#include <value.h>
#include <parse-util.h>
#include <expr.h>
#include <sheet-style.h>
#include <mstyle.h>
#include <ranges.h>
#include <number-match.h>
#include <func.h>
#include <gsf/gsf-input-textline.h>
#include <string.h>
#include <stdlib.h>
#define OLEO_DEBUG 0
typedef struct {
GnmConventions *convs;
GnmParsePos pp;
GsfInputTextline *textline;
GIConv converter;
} OleoReader;
static void
append_zeros (GString *s, int n)
{
if (n > 0) {
size_t oldlen = s->len;
g_string_set_size (s, oldlen + n);
memset (s->str+oldlen, '0', n);
}
}
/* adapted from Oleo */
static long
astol (char **ptr)
{
char *end;
long res;
res = strtol (*ptr, &end, 10);
*ptr = end;
return res;
}
static void
oleo_set_style (OleoReader *state, GnmStyle *style)
{
/* sheet_style_set_range absorbs our reference */
gnm_style_ref (style);
sheet_style_set_pos (state->pp.sheet,
state->pp.eval.col, state->pp.eval.row, style);
}
static GnmExprTop const *
oleo_parse_formula (OleoReader *state, char const *expr_str)
{
GnmParseError error;
GnmExprTop const *texpr = gnm_expr_parse_str (expr_str,
&state->pp, GNM_EXPR_PARSE_DEFAULT,
state->convs, parse_error_init (&error));
if (error.err != NULL) {
g_warning ("%s \"%s\" at %s!%s.", error.err->message, expr_str,
state->pp.sheet->name_unquoted,
cell_coord_name (state->pp.eval.col, state->pp.eval.row));
}
parse_error_free (&error);
return texpr;
}
static void
oleo_parse_cell (OleoReader *state, guint8 *str, GnmStyle *style)
{
GnmCell *cell;
GnmExprTop const *texpr = NULL;
char *ptr = str + 1, *cval = NULL, *formula = NULL;
while (*ptr) {
int quotes = 0;
if (*ptr != ';') {
#if OLEO_DEBUG > 0
g_warning ("ptr: %s.", ptr);
#endif
break;
}
*ptr++ = '\0';
switch (*ptr++) {
case 'c' : state->pp.eval.col = astol (&ptr) - 1; break;
case 'r' : state->pp.eval.row = astol (&ptr) - 1; break;
case 'K' :
cval = ptr;
quotes = 0;
while (*ptr && (*ptr != ';' || quotes > 0))
if (*ptr++ == '"')
quotes = !quotes;
break;
case 'E' :
formula = ptr;
while (*ptr && *ptr != ';')
ptr++;
break;
default:
#if OLEO_DEBUG > 0
g_warning ("oleo: Don't know how to deal with C; '%c'.",
*ptr);
#endif
ptr = (char *)""; /* I wish C had multilevel break */
break;
}
if (!*ptr)
break;
}
cell = sheet_cell_fetch (state->pp.sheet,
state->pp.eval.col, state->pp.eval.row);
if (formula != NULL)
texpr = oleo_parse_formula (state, formula);
if (cval != NULL) {
GnmValue *val = format_match_simple (cval);
if (val == NULL) {
char *last = cval + strlen (cval) - 1;
if (*cval == '"' && *last == '"') {
*last = 0;
val = value_new_string (cval + 1);
} else
val = value_new_string (cval);
}
if (texpr != NULL)
gnm_cell_set_expr_and_value (cell, texpr, val, TRUE);
else
gnm_cell_set_value (cell, val);
if (style != NULL)
oleo_set_style (state, style);
} else {
#if OLEO_DEBUG > 0
g_warning ("oleo: cval is NULL.");
#endif
/* We can still store the expression, even if the value is missing */
if (texpr != NULL)
gnm_cell_set_expr (cell, texpr);
}
if (texpr)
gnm_expr_top_unref (texpr);
}
/* NOTE : We don't care to much about formatting as such, but we need to
* parse the command as it may update current row/column */
static void
oleo_parse_style (OleoReader *state, guint8 *str, GnmStyle **res)
{
char *ptr = str + 1;
GnmStyle *style = gnm_style_new_default ();
GString *fmt_string = g_string_new (NULL);
while (*ptr) {
char c = *ptr++;
switch (c) {
case 'c' : state->pp.eval.col = astol (&ptr) - 1; break;
case 'r' : state->pp.eval.row = astol (&ptr) - 1; break;
case 'F': case 'G':
c = *ptr++;
g_string_truncate (fmt_string, 0);
g_string_append_c (fmt_string, '0');
if (g_ascii_isdigit (*ptr))
append_zeros (fmt_string, astol (&ptr));
switch (c) {
case 'F':
break;
case '%':
g_string_append_c (fmt_string, '%');
break;
default: /* Unknown format type... */
g_string_truncate (fmt_string, 0);
}
break;
case 'L':
gnm_style_set_align_h (style, HALIGN_LEFT);
break;
case 'R':
gnm_style_set_align_h (style, HALIGN_RIGHT);
}
}
if (fmt_string->len)
gnm_style_set_format_text (style, fmt_string->str);
g_string_free (fmt_string, TRUE);
if (*res)
gnm_style_unref (*res);
*res = style;
}
static Sheet *
oleo_new_sheet (Workbook *wb, int idx)
{
char *sheet_name = g_strdup_printf (_("Sheet%d"), idx);
Sheet *sheet = sheet_new (wb, sheet_name, 256, 65536);
g_free (sheet_name);
workbook_sheet_attach (wb, sheet);
/* Ensure that things get rendered and spanned */
sheet_flag_recompute_spans (sheet);
return sheet;
}
static GnmConventions *
oleo_conventions_new (void)
{
GnmConventions *convs = gnm_conventions_new ();
convs->decimal_sep_dot = TRUE;
convs->intersection_char = 0;
convs->r1c1_addresses = TRUE;
return convs;
}
void
oleo_read (GOIOContext *io_context, Workbook *wb, GsfInput *input)
{
int sheetidx = 0;
GnmStyle *style = NULL;
guint8 *line;
OleoReader state;
state.convs = oleo_conventions_new ();
parse_pos_init (&state.pp,
wb, oleo_new_sheet (wb, ++sheetidx), 0, 0);
/* Does this need to come from the import dialog ? */
state.converter = g_iconv_open ("UTF-8", "ISO-8859-1");
state.textline = (GsfInputTextline *) gsf_input_textline_new (input);
while (NULL != (line = gsf_input_textline_ascii_gets (state.textline))) {
char *utf8line =
g_convert_with_iconv (line, -1, state.converter, NULL, NULL, NULL);
switch (utf8line[0]) {
case '#': /* Comment */
break;
case 'C': oleo_parse_cell (&state, utf8line, style);
break;
case 'F': oleo_parse_style (&state, utf8line, &style);
break;
default: /* unknown */
#if OLEO_DEBUG > 0
g_warning ("oleo: Don't know how to deal with %c.",
line[0]);
#endif
break;
}
g_free (utf8line);
}
if (style)
gnm_style_unref (style);
g_iconv_close (state.converter);
gnm_conventions_free (state.convs);
g_object_unref (G_OBJECT (state.textline));
}
|