/*	Copyright (C) 1990 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 1, 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.  */

#include "funcdef.h"

#include <stdio.h>
#include <ctype.h>
#include "sysdef.h"

#include "global.h"
#include "cell.h"

extern struct rng all_rng;

static char sl_sep = '\t';

/* This file reads/writes files containing values in separated lists,
   sl_sep is the separating character.  This isn't really a save-file
   format, but it is useful for reading and writing tables written by other
   programs.

   Note that this format loses *most* of the information about the cells,
   including formuale, formats, column widths, etc
 */
void
list_read_file FUN2(FILE *,fp, int,ismerge)
{
	char cbuf[1024];
	CELLREF row,col;
	char *bptr;
	char *eptr;
	char *ptr;
	char tchar;
	int endit;
	unsigned lineno;
	int string;
	extern CELLREF curow,cucol;
	extern void clear_spreadsheet EXT0();

	lineno=0;
	if(!ismerge)
		clear_spreadsheet();
	row=curow;
	col=cucol;

	while(fgets(&cbuf[1],sizeof(cbuf)-3,fp)) {
		lineno++;
		if(lineno%50==0)
			info_msg("Line %d",lineno);
		endit=0;
		for(bptr= &cbuf[1];;bptr=eptr+1) {
			eptr=index(bptr,sl_sep);
			if(!eptr) {
				eptr=index(bptr,'\n');
				endit++;
			}
			string=0;
			for(ptr=bptr;ptr!=eptr;ptr++)
				if(!isdigit(*ptr) && *ptr!='.' && *ptr!='e' && *ptr!='E') {
					string++;
					break;
				}
			if(string) {
				bptr[-1]='"';
				eptr[0]='"';
				tchar=eptr[1];
				eptr[1]='\0';
				new_value(row,col,&bptr[-1]);
				eptr[1]=tchar;
			} else {
				eptr[0]='\0';
				new_value(row,col,bptr);
			}
			if(endit)
				break;
			col++;
		}
		row++;
		col=cucol;
	}
}

void
list_write_file FUN2(FILE *,fp, struct rng *,rng)
{
	CELLREF row,col;
	int repressed;
	CELL *cp;
	extern char *bname[];
	extern char *flt_to_str EXT1(double);

	if(!rng)
		rng= &all_rng;
	for(row=rng->lr;;row++) {
		repressed=0;
		for(col=rng->lc;;col++) {
			if((cp=find_cell(row,col)) && GET_TYP(cp)) {
				while(repressed>0) {
					putc(sl_sep,fp);
					--repressed;
				}
				repressed=1;
				switch(GET_TYP(cp)) {
				case TYP_FLT:
					fputs(flt_to_str(cp->cell_flt),fp);
					break;
				case TYP_INT:
					fprintf(fp,"%ld",cp->cell_flt);
					break;
				case TYP_STR:
					fputs(cp->cell_str,fp);
					break;
				case TYP_BOL:
					fputs(bname[cp->cell_bol],fp);
					break;
				case TYP_ERR:
					fputs(ename[cp->cell_err],fp);
					break;
#ifdef TEST
				default:
					panic("Unknown type %d in write_sl_file()",GET_TYP(cp));
					break;
#endif
				}
			} else
				repressed++;
			if(col==rng->hc)
				break;
		}
		putc('\n',fp);
		if(row==rng->hr)
			break;
	}
}

int
list_set_options FUN2(int,set_opt, char *,option)
{
	extern int strincmp();
	extern int string_to_char();

	if(set_opt && !strincmp(option,"list ",5)) {
		option+=5;
		sl_sep=string_to_char(&option);
		return 0;
	}
	return -1;
}

void
list_show_options FUN0()
{
	extern char *char_to_string();

	text_line("File format: list    (character separated list of cell values)");
	text_line("Save file element separator: %s",char_to_string(sl_sep));
}
