PsppireDialogAction: Destroy dialog after it has been run
[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 /* Convert a string, which is an integer encoded in base26
26    IE, A=0, B=1, ... Z=25 to the integer it represents.
27    ... except that in this scheme, digits with an exponent
28    greater than 1 are implicitly incremented by 1, so
29    AA  = 0 + 1*26, AB = 1 + 1*26,
30    ABC = 2 + 2*26 + 1*26^2 ....
31 */
32 int
33 pseudo_base26 (const char *str)
34 {
35   int i;
36   int multiplier = 1;
37   int result = 0;
38   int len = strlen (str);
39
40   for ( i = len - 1 ; i >= 0; --i)
41     {
42       int mantissa = (str[i] - 'A');
43
44       if ( mantissa < 0 || mantissa > 25 )
45         return -1;
46
47       if ( i != len - 1)
48         mantissa++;
49
50       result += mantissa * multiplier;
51
52       multiplier *= 26;
53     }
54
55   return result;
56 }
57
58
59 /* Convert a cell reference in the form "A1:B2", to
60    integers.  A1 means column zero, row zero.
61    B1 means column 1 row 0. AA1 means column 26, row 0.
62 */
63 bool
64 convert_cell_ref (const char *ref,
65                   int *col0, int *row0,
66                   int *coli, int *rowi)
67 {
68   char startcol[5];
69   char stopcol [5];
70
71   int startrow;
72   int stoprow;
73
74   int n = sscanf (ref, "%4[a-zA-Z]%d:%4[a-zA-Z]%d",
75               startcol, &startrow,
76               stopcol, &stoprow);
77   if ( n != 4)
78     return false;
79
80   str_uppercase (startcol);
81   *col0 = pseudo_base26 (startcol);
82   str_uppercase (stopcol);
83   *coli = pseudo_base26 (stopcol);
84   *row0 = startrow - 1;
85   *rowi = stoprow - 1 ;
86
87   return true;
88 }
89