File: sc.c

package info (click to toggle)
oleo 1.6-16
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,640 kB
  • ctags: 3,139
  • sloc: ansic: 39,221; yacc: 1,737; sh: 362; makefile: 88
file content (245 lines) | stat: -rw-r--r-- 5,488 bytes parent folder | download | duplicates (3)
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
/*	Copyright (C) 1990, 1992, 1993 Free Software Foundation, Inc.

This file is part of Oleo, the GNU Spreadsheet.

Oleo 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, or (at your option)
any later version.

Oleo 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 Oleo; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "funcdef.h"
#include <stdio.h>
#include <ctype.h>
#include "sysdef.h"
#include "global.h"
#include "cell.h"
#include "io-generic.h"
#include "io-abstract.h"
#include "io-utils.h"
#include "lists.h"
#include "ref.h"
#include "parse.h"
#include "regions.h"
#include "cmd.h"



/* This reads/writes a subset of the SC public-domain spreadsheet's
   file-format.  Note that since SC has no way of encoding some information
   about a cell, writing a spread out in SC format, then reading it back in
   will result in the loss of some information (most important:  cell formats)
 */

static int
get_range (char ** pp,
	   struct rng * rp)
{
	int byte;
	char *p;
	struct var *v;


	while(isspace(**pp))
		(*pp)++;
	byte=parse_cell_or_range(pp,rp);
	if(byte)
		return 0;
	for(p= *pp;*p && !isspace(*p);p++)
		;
	v=find_var(*pp,p-*pp);
	if(!v)
		return 1;
	*pp=p;
	*rp=v->v_rng;
	return 0;
}

void
sc_read_file (FILE * fp,
	      int ismerge)
{
	char buf[2048];
	int lineno;
	char *ptr;
	int n;
	struct rng rng;
	int olda0;

	olda0=a0;
	a0=1;
	lineno=0;
	if(!ismerge)
		clear_spreadsheet();
	while(fgets(buf,sizeof(buf),fp)) {
		lineno++;
		if(lineno%50==0)
			io_info_msg("Line %d",lineno);
		if(buf[0]=='#' || buf[0]=='\n')
			continue;
		if(!strncmp(buf,"set ",4)) {
			/* ... */
		} else if(!strncmp(buf,"format ",7)) {
			ptr=buf+7;
			if(get_range(&ptr,&rng))
				continue;
			n=astol(&ptr);
			set_width(rng.lc,n);
		} else if(!strncmp(buf,"hide ",5)) {
			ptr=buf+5;
			if(get_range(&ptr,&rng))
				continue;
			set_width(rng.lc,0);
		} else if(!strncmp(buf,"mdir ",5)) {
			/* ... */
		} else if(!strncmp(buf,"define ",7)) {
			char *eptr;

			ptr=buf+7;
			while(isspace(*ptr))
				ptr++;
			if(*ptr!='"') {
				io_error_msg("Line %d: No starting \" in define",lineno);
				continue;
			}
			ptr++;
			for(eptr=ptr; *eptr && *eptr!='"';eptr++)
				;
			if(!*eptr) {
				io_error_msg("Line %d: No starting \" in define",lineno);
				continue;
			}
			ptr=new_var_value(ptr,eptr-ptr,eptr+1);
			if(ptr)
				io_error_msg("Line %d: %s",ptr);
		} else if(!strncmp(buf,"leftstring ",11) ||
			  !strncmp(buf,"rightstring ",12)) {
			CELL *cp;

			ptr=buf+11;
			if(get_range(&ptr,&rng))
				continue;
			while(isspace(*ptr))
				ptr++;
			if(*ptr=='=')
				ptr++;
			new_value(rng.lr,rng.lc,ptr);
			cp=find_cell(rng.lr,rng.lc);
			if(buf[0]=='l')
				SET_JST(cp,JST_LFT);
			else
				SET_JST(cp,JST_RGT);

		} else if(!strncmp(buf,"let ",4)) {
			ptr=buf+4;
			if(get_range(&ptr,&rng))
				continue;
			while(isspace(*ptr))
				ptr++;
			if(*ptr=='=')
				ptr++;
			new_value(rng.lr,rng.lc,ptr);
		} else
			io_error_msg("Line %d: Can't parse %s",lineno,buf);
	}
	a0=olda0;
	io_recenter_all_win();
}

static FILE *sc_fp;
static struct rng *sc_rng;
static void
sc_write_var (char * name,
	      struct var * var)
{
	if(var->var_flags==VAR_UNDEF && (!var->var_ref_fm || var->var_ref_fm->refs_used==0))
		return;
	switch(var->var_flags) {
	case VAR_UNDEF:
		break;
	case VAR_CELL:
		if(var->v_rng.lr>=sc_rng->lr && var->v_rng.lr<=sc_rng->hr && var->v_rng.lc>=sc_rng->lc && var->v_rng.lc<=sc_rng->hc)
			(void)fprintf(sc_fp,"define \"%s\" %s\n",var->var_name,cell_name(var->v_rng.lr,var->v_rng.lc));
		break;
	case VAR_RANGE:
		if(var->v_rng.lr<sc_rng->lr || var->v_rng.hr>sc_rng->hr || var->v_rng.lc<sc_rng->lc || var->v_rng.hc>sc_rng->hc)
			break;

		(void)fprintf(sc_fp,"define \"%s\" %s\n",var->var_name,range_name(&(var->v_rng)));
		break;
#ifdef TEST
	default:
		panic("Unknown var type %d",var->var_flags);
		break;
#endif
	}
}
	
void
sc_write_file (FILE * fp,
	       struct rng * rng)
{
	unsigned short w;
	CELLREF r,c;
	CELL *cp;
	char *ptr;
	int olda0;

	if(!rng)
		rng= &all_rng;

	olda0=a0;
	a0=1;
	(void)fprintf(fp,"# This file was created by Oleo, for use by the Spreadsheet Calculator\n");
	(void)fprintf(fp,"# You probably don't want to edit it.\n\n");

	find_widths(rng->lc,rng->hc);
	while(w=next_width(&c))
		fprintf(fp,"format %s %d ???\n",cell_name(MIN_ROW,c),w);
	sc_fp=fp;
	sc_rng=rng;
	for_all_vars(sc_write_var);
	find_cells_in_range(rng);
	while(cp=next_row_col_in_range(&r,&c)) {
		switch(GET_TYP(cp)) {
		case TYP_STR:
			if((GET_JST(cp)==JST_DEF && default_jst==JST_RGT) || GET_JST(cp)==JST_RGT)
				ptr="right";
			else ptr="left";
			fprintf(fp,"%sstring %s = %s\n",ptr,cell_name(r,c),decomp(r,c,cp));
			decomp_free();
			break;
		case 0:
			break;
		default:
			fprintf(fp,"let %s = %s\n",cell_name(r,c),decomp(r,c,cp));
			decomp_free();
			break;
		}
	}
	a0=olda0;
}

int
sc_set_options (int set_opt,
		char * option)
{
	return -1;
}

void
sc_show_options (void)
{
	io_text_line("File format: sc  (Public domain spreadsheet calculator)");
}