File: list.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 (198 lines) | stat: -rw-r--r-- 4,133 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
/*	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 "regions.h"
#include "io-utils.h"
#include "cmd.h"





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 (FILE *fp,
		int ismerge)
{
  char cbuf[1024];
  CELLREF row, col;
  char *bptr;
  char *eptr;
  char *ptr;
  char tchar;
  int endit;
  unsigned lineno;
  int string;

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

  while (fgets (&cbuf[1], sizeof (cbuf) - 3, fp))
    {
      lineno++;
      if (lineno % 50 == 0)
	io_info_msg ("Line %d", lineno);
      endit = 0;
      for (bptr = &cbuf[1];; bptr = eptr + 1)
	{
	  eptr = (char *)index (bptr, sl_sep);
	  if (!eptr)
	    {
	      eptr = (char *)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 (FILE *fp,
		 struct rng *rng)
{
  CELLREF row, col;
  CELLREF rMax, cMax;
  int repressed;
  CELL *cp;

  if (!rng)
    rng = &all_rng;
  rMax = rng->lr;
  cMax = rng->lc;
  find_cells_in_range (rng);
  while (cp = next_row_col_in_range (&row, &col)) {
    if (row > rMax) rMax = row;
    if (col > cMax) cMax = col;
  }
  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_int);
		  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 == cMax)
	    break;
	}
      putc ('\n', fp);
      if (row == rMax)
	break;
    }
}

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

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