Added the range column to the model
[pspp] / src / data / spreadsheet-reader.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2007, 2009, 2010, 2011 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "spreadsheet-reader.h"
20
21 #include <libpspp/str.h>
22 #include <stdio.h>
23 #include <string.h>
24
25
26 struct spreadsheet * 
27 spreadsheet_open (const char *filename)
28 {
29   struct spreadsheet *ss = NULL;
30
31   ss = gnumeric_probe (filename);
32   
33   return ss;
34 }
35
36 void 
37 spreadsheet_close (struct spreadsheet *spreadsheet)
38 {
39 }
40
41
42
43 /* Convert a string, which is an integer encoded in base26
44    IE, A=0, B=1, ... Z=25 to the integer it represents.
45    ... except that in this scheme, digits with an exponent
46    greater than 1 are implicitly incremented by 1, so
47    AA  = 0 + 1*26, AB = 1 + 1*26,
48    ABC = 2 + 2*26 + 1*26^2 ....
49 */
50 int
51 pseudo_base26 (const char *str)
52 {
53   int i;
54   int multiplier = 1;
55   int result = 0;
56   int len = strlen (str);
57
58   for ( i = len - 1 ; i >= 0; --i)
59     {
60       int mantissa = (str[i] - 'A');
61
62       if ( mantissa < 0 || mantissa > 25 )
63         return -1;
64
65       if ( i != len - 1)
66         mantissa++;
67
68       result += mantissa * multiplier;
69
70       multiplier *= 26;
71     }
72
73   return result;
74 }
75
76 char *
77 create_cell_ref (int col0, int row0, int coli, int rowi)
78 {
79   return c_xasprintf ("%c%d:%c%ld", 
80                col0 + 'A', row0 + 1,
81                coli + 'A', rowi + 1);
82 }
83
84
85 /* Convert a cell reference in the form "A1:B2", to
86    integers.  A1 means column zero, row zero.
87    B1 means column 1 row 0. AA1 means column 26, row 0.
88 */
89 bool
90 convert_cell_ref (const char *ref,
91                   int *col0, int *row0,
92                   int *coli, int *rowi)
93 {
94   char startcol[5];
95   char stopcol [5];
96
97   int startrow;
98   int stoprow;
99
100   int n = sscanf (ref, "%4[a-zA-Z]%d:%4[a-zA-Z]%d",
101               startcol, &startrow,
102               stopcol, &stoprow);
103   if ( n != 4)
104     return false;
105
106   str_uppercase (startcol);
107   *col0 = pseudo_base26 (startcol);
108   str_uppercase (stopcol);
109   *coli = pseudo_base26 (stopcol);
110   *row0 = startrow - 1;
111   *rowi = stoprow - 1 ;
112
113   return true;
114 }
115