File: parse_sp_range.m

package info (click to toggle)
octave-io 2.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,544 kB
  • ctags: 461
  • sloc: objc: 2,082; cpp: 528; ansic: 172; makefile: 15
file content (100 lines) | stat: -rw-r--r-- 3,136 bytes parent folder | download
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
## Copyright (C) 2009,2010.2011,2012,2013 Philip Nienhuis
##
## This program 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 3 of the License, or (at your option) any later
## version.
##
## This program 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
## this program; if not, see <http://www.gnu.org/licenses/>.

## Parse a string representing a range of cells for a spreadsheet
## into nr of rows and nr of columns and also extract top left
## cell address + top row + left column. Some error checks are implemented.

## Author: Philip Nienhuis <pr.nienhuis at users.sf.net>
## Created: 2009-06-20
## Latest update 2010-01-13
## 2013-12-06 Updated copyright strings; style fixes
## 2014-04-13 Invoke binary __char2num__ for col->colnr conversion
## 2014_08_25 Switch to col2num (.oct) for colstr->col conversion to allow
##            compilation on 64-bit MingW systems (mex won't compile int64_t)
##     ''     Clean up code

function [topleft, nrows, ncols, toprow, lcol] = parse_sp_range (range_org)

  crange = strrep (deblank (upper (range_org)), " ", "");
  range_error = 0; 
  nrows = 0; 
  ncols = 0;

  # Basic checks
  if (index (crange, ':') == 0)
    if (isempty (crange))
      range_error = 0;
      leftcol = 'A';
      rightcol = 'A';
    else
      # Only upperleft cell given, just complete range to 1x1
      # (needed for some routines)
      crange = [crange ":" crange];
    endif
  endif

  # Split up both sides of the range
  [topleft, lowerright] = strtok (crange, ':');

  # Get toprow and clean up left column
  [st, en] = regexp (topleft, '\d+');
  toprow = str2double (topleft(st:en));
  lcol = col2num (topleft(1:st-1));

  # Get bottom row and clean up right column
  [st, en] = regexp (lowerright, '\d+');
  bottomrow = str2double (lowerright(st:en));
  rcol = col2num (lowerright (2:st-1));

  # Check nr. of rows
  nrows = bottomrow - toprow + 1; 
  if (nrows < 1) 
    range_error = 1; 
  endif

  if (range_error == 0) 
    # Check
    ncols = rcol - lcol + 1;
    if (ncols < 1) 
      range_error = 1; 
    endif
  endif

  if (range_error > 0) 
    ncols = 0; 
    nrows = 0;
    error ("Spreadsheet range error!");
  endif
  
endfunction

## FIXME -- reinstate these tests one there if a way is found to test private
##          functions directly
#%!test
#%! [a b c d e] = parse_sp_range ('A1:B2');
#%! assert ([a b c d e], ['A1', 2, 2, 1, 1]);
#%
#%!test
#%! [a b c d e] = parse_sp_range ('A1:AB200');
#%! assert ([a b c d e], ['A1', 200, 28, 1, 1]);
#%
#%!test
#%! [a b c d e] = parse_sp_range ('cd230:iY65536');
#%! assert ([a b c d e], ['CD230', 65307, 178, 230, 82]);
#%
#%!test
#%! [a b c d e] = parse_sp_range ('BvV12798 : xFd1054786');
#%! assert ([b c d e], [1041989, 14439, 12798, 1946]);