Commit fcdc78e6 authored by Jeff Garzik's avatar Jeff Garzik Committed by Jeff Garzik

new SC/xspread file import plug-in. Ultra simple, supports only cell

1999-11-19  Jeff Garzik  <jgarzik@mandrakesoft.com>

        * Makefile.am, sc.c:
        new SC/xspread file import plug-in.
        Ultra simple, supports only cell values currently.
parent 8fe32005
Makefile
*.lo
*.la
*.in
.deps
.libs
1999-11-19 Jeff Garzik <jgarzik@mandrakesoft.com>
* Makefile.am, sc.c:
new SC/xspread file import plug-in.
Ultra simple, supports only cell values currently.
#
# Makefile for sc file import plugin
#
# Where can we find the Gnumeric include files.
#
GNUMERIC_SRC_DIR = $(top_srcdir)/src
#
# Where to install the plugin
#
plugindir = $(libdir)/gnumeric/plugins
plugin_LTLIBRARIES = libsc.la
libsc_la_SOURCES = sc.c
INCLUDES = \
-I$(GNUMERIC_SRC_DIR) \
-DGNOMELOCALEDIR=\""$(datadir)/locale"\" \
-I$(includedir) \
$(GNOME_INCLUDEDIR)
/*
* sc.c - file import of SC/xspread files
* Copyright 1999 Jeff Garzik <jgarzik@mandrakesoft.com>
*
* With some code from sylk.c
*/
#include <config.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <gnome.h>
#include "plugin.h"
#include "gnumeric.h"
#include "file.h"
#define arraysize(x) (sizeof(x)/sizeof(*(x)))
typedef struct {
/* input data */
FILE *f;
/* gnumeric sheet */
Sheet *sheet;
} sc_file_state_t;
static void
sc_cellname_to_coords (const char *cellname, int *col, int *row)
{
int mult;
if (!cellname || !*cellname || !isalpha(*cellname))
goto err_out;
mult = *cellname - 'A';
if (mult < 0 || mult > 25)
goto err_out;
cellname++;
if (isalpha(*cellname)) {
int ofs = *cellname - 'A';
if (ofs < 0 || ofs > 25)
goto err_out;
*col = (mult * 26) + ofs;
cellname++;
}
else {
*col = mult;
}
if (!isdigit(*cellname))
goto err_out;
*row = atoi (cellname);
g_assert (*col > -1);
g_assert (*row > -1);
return;
err_out:
*col = *row = -1;
}
static void
sc_parse_coord (const char **strdata, int *col, int *row)
{
const char *s = *strdata, *space, *eq;
size_t len = strlen (s);
char tmpstr [16];
space = strchr (s, ' ');
eq = strstr (s, " = ");
if (!space || !eq || space == eq)
return;
memcpy (tmpstr, space + 1, eq - space - 1);
tmpstr [eq - space - 1] = 0;
sc_cellname_to_coords (tmpstr, col, row);
if (*col == -1)
return;
if ((eq - s + 1 + 3) > len)
return;
*strdata = eq + 3;
}
static gboolean
sc_string_parse (sc_file_state_t *src, const char *str, int col, int row)
{
Cell *cell;
char *s = NULL, *tmpout;
const char *tmpstr;
gboolean result = FALSE;
if (!src || !str || *str != '"' || col == -1 || row == -1)
goto out;
s = tmpout = g_strdup (str);
if (!s)
goto out;
tmpstr = str;
while (*tmpstr) {
if (*tmpstr != '\\') {
*tmpout = *tmpstr;
tmpout++;
}
tmpstr++;
}
if (*(tmpstr - 1) != '"')
goto out;
tmpout--;
*tmpout = 0;
cell = sheet_cell_fetch (src->sheet, col, row);
if (!cell)
goto out;
cell_set_text_simple (cell, s);
result = TRUE;
out:
if (s) g_free (s);
return result;
}
static gboolean
sc_float_parse (sc_file_state_t *src, const char *str, int col, int row)
{
Cell *cell;
Value *v;
if (!src || !str || !*str || col == -1 || row == -1)
return FALSE;
cell = sheet_cell_fetch (src->sheet, col, row);
if (!cell)
return FALSE;
v = value_new_float (atof(str));
if (!v)
return FALSE;
cell_set_value_simple (cell, v);
return TRUE;
}
typedef struct {
const char *name;
gboolean (*handler) (sc_file_state_t *src, const char *str, int col, int row);
unsigned have_coord : 1;
} sc_cmd_t;
static const sc_cmd_t sc_cmd_list[] = {
{ "leftstring ", sc_string_parse, 1 },
{ "rightstring ", sc_string_parse, 1 },
{ "label ", sc_string_parse, 1 },
{ "let ", sc_float_parse, 1 },
};
static gboolean
sc_parse_line (sc_file_state_t *src, char *buf)
{
int i;
for (i = 0; i < arraysize (sc_cmd_list); i++)
if (strncmp (sc_cmd_list [i].name, buf,
strlen (sc_cmd_list [i].name)) == 0) {
const sc_cmd_t *cmd = &sc_cmd_list [i];
int col = -1, row = -1;
const char *strdata;
strdata = buf + strlen (cmd->name);
if (cmd->have_coord)
sc_parse_coord (&strdata, &col, &row);
cmd->handler (src, strdata, col, row);
return TRUE;
}
#if 0
fprintf (stderr, "unhandled directive: '%s'\n", buf);
#endif
return TRUE;
}
static gboolean
sc_parse_sheet (sc_file_state_t *src)
{
char buf [BUFSIZ];
while (fgets (buf, sizeof (buf), src->f) != NULL) {
g_strchomp (buf);
if ( buf [0] &&
buf [0] != '#' &&
!sc_parse_line (src, buf) ) {
fprintf (stderr, "error parsing line\n");
return FALSE;
}
}
if (ferror (src->f))
return FALSE;
return TRUE;
}
static gboolean
sc_read_workbook (Workbook *book, const char *filename)
{
/*
* TODO : When there is a reasonable error reporting
* mechanism use it and put all the error code back
*/
sc_file_state_t src;
char *name;
gboolean result;
FILE *f;
f = fopen (filename, "r");
if (!f)
return FALSE;
name = g_strdup_printf (_("Imported %s"), g_basename (filename));
memset (&src, 0, sizeof (src));
src.f = f;
src.sheet = sheet_new (book, name);
workbook_attach_sheet (book, src.sheet);
g_free (name);
result = sc_parse_sheet (&src);
fclose(f);
return result;
}
static int
sc_can_unload (PluginData *pd)
{
/* We can always unload */
return TRUE;
}
static void
sc_cleanup_plugin (PluginData *pd)
{
file_format_unregister_open (NULL, sc_read_workbook);
}
int
init_plugin (PluginData * pd)
{
const char *desc = _("SC/xspread file import");
file_format_register_open (1, desc, NULL, sc_read_workbook);
pd->can_unload = sc_can_unload;
pd->cleanup_plugin = sc_cleanup_plugin;
pd->title = g_strdup (desc);
return 0;
}
# This data file was generated by the Spreadsheet Calculator.
# You almost certainly shouldn't edit it.
set numeric
leftstring A0 = "January"
let B0 = 110
let C0 = 21
let D0 = 68.3
let E0 = 85.5
let F0 = 34
let G0 = 68.9
let H0 = 45.43
leftstring A1 = "February"
let B1 = 120
let C1 = 32.3
let D1 = 76.4
let E1 = 69.4
let F1 = 54
let G1 = 86.9
let H1 = 54.76
leftstring A2 = "March"
let B2 = 130
let C2 = 65
let D2 = 121
let E2 = 101.4
let F2 = 123
let G2 = 88.98
let H2 = 54.8
leftstring A3 = "April"
let B3 = 140
let C3 = 100
let D3 = 113.9
let E3 = 94.5
let F3 = 135
let G3 = 87.9
let H3 = 45.7
leftstring A4 = "May"
let B4 = 150
let C4 = 150
let D4 = 164.3
let E4 = 113.4
let F4 = 43
let G4 = 65.78
let H4 = 43.87
leftstring A5 = "June"
let B5 = 160
let C5 = 87.5
let D5 = 130.9
let E5 = 112.9
let F5 = 77
let G5 = 87.09
let H5 = 54.65
leftstring A6 = "July"
let B6 = 143
let C6 = 110.2
let D6 = 124.6
let E6 = 108.6
let F6 = 67
let G6 = 121.9
let H6 = 34.87
leftstring A7 = "August"
let B7 = 178
let C7 = 85.2
let D7 = 175.8
let E7 = 95.9
let F7 = 87
let G7 = 119.05
let H7 = 43.8
leftstring A8 = "September"
let B8 = 143
let C8 = 87.9
let D8 = 99.6
let E8 = 113
let F8 = 98
let G8 = 132.8
let H8 = 87.98
leftstring A9 = "October"
let B9 = 100
let C9 = 64
let D9 = 125.3
let E9 = 98.4
let F9 = 67
let G9 = 112.9
let H9 = 54.83
leftstring A10 = "November"
let B10 = 110
let C10 = 76.9
let D10 = 143.5
let E10 = 123.9
let F10 = 87
let G10 = 98.07
let H10 = 34.98
leftstring A11 = "December"
let B11 = 120
let C11 = 76.3
let D11 = 132.6
let E11 = 143.4
let F11 = 87.96
let G11 = 65.9
let H11 = 45.9
# This data file was generated by the Spreadsheet Calculator.
# You almost certainly shouldn't edit it.
format A 15 2 0
leftstring A0 = "Date/Time Fncs"
leftstring C0 = "Math Fncns"
leftstring E0 = "Stat Fncs"
let A1 = @hour(A7)
let C1 = @sqrt(A6)
let E1 = @sum(A1:A4)
let A2 = @minute(A7)
let C2 = @rnd(C1)
let E2 = @avg(A1:A4)
let A3 = @second(A7)
let C3 = @pow(A4,3)
let E3 = @prod(A1:A4)
let A4 = @month(A7)
let C4 = 3.14159265358979
let E4 = @stddev(A1:A4)
let A5 = @day(A7)
let C5 = @min(A1,A2,A3,A4,A5,A6,A7)
let A6 = @year(A7)
let C6 = @max(A1,A2,A3,A4,A5,A6)
let A7 = @now
let C7 = @log(A7)
let C8 = @ln(A7)
goto C7
# This data file was generated by the Spreadsheet Calculator.
# You almost certainly shouldn't edit it.
set numeric
rightstring A0 = "Original Matrix"
leftstring F0 = "const coeff. col"
leftstring H0 = "Solution of the system of linear eqs."
let A1 = 1
let B1 = 1
let C1 = 7
let D1 = 5
let F1 = 5
let H1 = -1.10588235294118
let A2 = 3
let B2 = 4
let C2 = 4
let D2 = 2
let F2 = 6
let H2 = 1.37254901960784
let A3 = 2
let B3 = -2
let C3 = 9
let D3 = 5
let F3 = 3
let H3 = 1.61176470588235
let A4 = 5
let B4 = 6
let C4 = 2
let D4 = 3
let F4 = 2
let H4 = -1.30980392156863
rightstring A6 = "Transpose(a1:d4)"
leftstring F6 = "Addition(a1:d4+a7:d10)"
let A7 = 1
let B7 = 3
let C7 = 2
let D7 = 5
let F7 = 2
let G7 = 4
let H7 = 9
let I7 = 10
let A8 = 1
let B8 = 4
let C8 = -2
let D8 = 6
let F8 = 4
let G8 = 8
let H8 = 2
let I8 = 8
let A9 = 7
let B9 = 4
let C9 = 9
let D9 = 2
let F9 = 9
let G9 = 2
let H9 = 18
let I9 = 7
let A10 = 5
let B10 = 2
let C10 = 5
let D10 = 3
let F10 = 10
let G10 = 8
let H10 = 7
let I10 = 6
rightstring A13 = "Multiplication (a1:b2*c1:d2)"
leftstring C13 = ":d4*a7:d10)"
leftstring F13 = "Substraction(f7:i10-a7:d10)"
let A14 = 76
let B14 = 45
let C14 = 88
let D14 = 40
let F14 = 1
let G14 = 1
let H14 = 7
let I14 = 5
let A15 = 45
let B15 = 45
let C15 = 44
let D15 = 53
let F15 = 3
let G15 = 4
let H15 = 4
let I15 = 2
let A16 = 88
let B16 = 44
let C16 = 114
let D16 = 31
let F16 = 2
let G16 = -2
let H16 = 9
let I16 = 5
let A17 = 40
let B17 = 53
let C17 = 31
let D17 = 74
let F17 = 5
let G17 = 6
let H17 = 2
let I17 = 3
rightstring A19 = "Inversion (a1:d4)"
rightstring F19 = "Original matrix * Inversion matrix"
let A20 = -.376470588235294
let B20 = -0.0823529411764708
let C20 = .282352941176471
let D20 = .211764705882353
let F20 = .999999999999998
let G20 = 8.88178419700125e-16
let H20 = 2.22044604925031e-16
let I20 = 1.77635683940025e-15
let A21 = .176470588235294
let B21 = .215686274509804
let C21 = -.215686274509804
let D21 = -0.0784313725490194
let F21 = -9.99200722162641e-16
let G21 = 1
let H21 = 9.43689570931383e-16
let I21 = 1.4432899320127e-15
let A22 = -0.0470588235294117
let B22 = .364705882352941
let C22 = 0.0352941176470588
let D22 = -.223529411764706
let F22 = -1.55431223447522e-15
let G22 = 4.44089209850064e-16
let H22 = 1
let I22 = 8.88178419700125e-16
let A23 = .305882352941176
let B23 = -.537254901960784
let C23 = -0.0627450980392157
let D23 = .286274509803922
let F23 = -1.77635683940025e-15
let G23 = 2.22044604925031e-16
let H23 = 1.9151347174784e-15
let I23 = 1
# This data file was generated by the Spreadsheet Calculator.
# You almost certainly shouldn't edit it.
set numeric
let A0 = 1
let B0 = 2
let A1 = 3
let B1 = 4
let A3 = 1
let B3 = 2
let C3 = 1
let D3 = 2
let A4 = 3
let B4 = 4
let C4 = 3
let D4 = 4
let A6 = 1
let B6 = 2
let C6 = 1
let D6 = 2
let A7 = 3
let B7 = 4
let C7 = 3
let D7 = 4
let A9 = 1
let B9 = 2
let C9 = 1
let D9 = 2
let A10 = 3
let B10 = 4
let C10 = 3
let D10 = 4
let A12 = 1
let B12 = 2
let A13 = 3
let B13 = 4
# This data file was generated by the Spreadsheet Calculator.
# You almost certainly shouldn't edit it.
set numeric
leftstring A0 = "January"
let A0 = 1
let B0 = 110
let C0 = 21
let D0 = 68.3
let E0 = 85.5
let F0 = 34
let G0 = 68.9
let H0 = 45.43
leftstring A1 = "February"
let A1 = 2
let B1 = 120
let C1 = 32.3
let D1 = 76.4
let E1 = 69.4
let F1 = 54
let G1 = 86.9
let H1 = 54.76
leftstring A2 = "March"
let A2 = 3
let B2 = 130
let C2 = 65
let D2 = 121
let E2 = 101.4
let F2 = 123
let G2 = 88.98
let H2 = 54.8
leftstring A3 = "April"
let A3 = 4
let B3 = 140
let C3 = 100
let D3 = 113.9
let E3 = 94.5
let F3 = 135
let G3 = 87.9
let H3 = 45.7
leftstring A4 = "May"
let A4 = 5
let B4 = 150
let C4 = 150
let D4 = 164.3
let E4 = 113.4
let F4 = 43
let G4 = 65.78
let H4 = 43.87
leftstring A5 = "June"
let A5 = 6
let B5 = 160
let C5 = 87.5
let D5 = 130.9
let E5 = 112.9
let F5 = 77
let G5 = 87.09
let H5 = 54.65
leftstring A6 = "July"
let A6 = 7
let B6 = 143
let C6 = 110.2
let D6 = 124.6
let E6 = 108.6
let F6 = 67
let G6 = 121.9
let H6 = 34.87
leftstring A7 = "August"
let A7 = 8
let B7 = 178
let C7 = 85.2
let D7 = 175.8
let E7 = 95.9
let F7 = 87
let G7 = 119.05
let H7 = 43.8
leftstring A8 = "September"
let A8 = 9
let B8 = 143
let C8 = 87.9
let D8 = 99.6
let E8 = 113
let F8 = 98
let G8 = 132.8
let H8 = 87.98
leftstring A9 = "October"
let A9 = 10
let B9 = 100
let C9 = 64
let D9 = 125.3
let E9 = 98.4
let F9 = 67
let G9 = 112.9
let H9 = 54.83
leftstring A10 = "November"
let A10 = 11
let B10 = 110
let C10 = 76.9
let D10 = 143.5
let E10 = 123.9
let F10 = 87
let G10 = 98.07
let H10 = 34.98
leftstring A11 = "December"
let A11 = 12
let B11 = 120
let C11 = 76.3
let D11 = 132.6
let E11 = 143.4
let F11 = 87.96
let G11 = 65.9
let H11 = 45.9
goto A6
# This data file was generated by the Spreadsheet Calculator.
# You almost certainly shouldn't edit it.
Graph Definitions:
graphic_type=S
graphic_legend[0]=high-93
graphic_legend[1]=low-93
graphic_legend[2]=ave-93
graphic_legend[3]=high-94
graphic_legend[4]=low-94
graphic_legend[5]=ave-94
graphic_format=BBBBBBB
graphic_title[0]=MONTHLY TEMPERATURES
graphic_title[1]=For Milwaukee
graphic_title[2]=month
graphic_title[3]=ave. temperature
graphic_grid=C
g_auto_man[]=AA
graphic_skip=0
curves_n=6
graphic_scale[0][]=0.000000 0.000000
graphic_scale[1][]=0.000000 0.000000
graphic_range[0]=a~~ 2 13 0
graphic_range[1]=b~~ 2 13 1
graphic_range[2]=c~~ 4 13 2
graphic_range[3]=d~~ 2 13 3
graphic_range[4]=e~~ 2 13 4
graphic_range[5]=f~~ 4 13 5
graphic_range[6]=g~~ 2 13 6
set numeric
mdir "/usr/proj/se/xspread/spring94"
label A1 = "month"