~ubuntu-branches/ubuntu/wily/octave-miscellaneous/wily

« back to all changes in this revision

Viewing changes to inst/ascii.m

  • Committer: Package Import Robot
  • Author(s): Thomas Weber
  • Date: 2014-06-09 15:43:35 UTC
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: package-import@ubuntu.com-20140609154335-qtyms40gdz6lrvui
Tags: upstream-1.2.1
Import upstream version 1.2.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
## Copyright (C) 2008 Thomas Treichl <thomas.treichl@gmx.net>
 
2
## Copyright (C) 2013 Carnë Draug <carandraug@octave.org>
 
3
##
 
4
## This program is free software; you can redistribute it and/or modify it under
 
5
## the terms of the GNU General Public License as published by the Free Software
 
6
## Foundation; either version 3 of the License, or (at your option) any later
 
7
## version.
 
8
##
 
9
## This program is distributed in the hope that it will be useful, but WITHOUT
 
10
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
11
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 
12
## details.
 
13
##
 
14
## You should have received a copy of the GNU General Public License along with
 
15
## this program; if not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
## -*- texinfo -*-
 
18
## @deftypefn  {Function File} {} ascii ()
 
19
## @deftypefnx {Function File} {} ascii (@var{columns})
 
20
## Print ASCII table.
 
21
##
 
22
## If this function is called without any input argument and without any output
 
23
## argument then prints a nice ASCII-table (excluding special characters with
 
24
## hexcode 0x00 to 0x20).  The input argument @var{columns} specifies the
 
25
## number of columns and defaults to 4.
 
26
##
 
27
## If it is called with one output argument then return the ASCII table as
 
28
## a string without displaying anything.  Run @code{demo ascii} for examples.
 
29
##
 
30
## @seealso{char, isascii, toascii}
 
31
## @end deftypefn
 
32
 
 
33
function table = ascii (vcol = 4)
 
34
 
 
35
  if (nargin > 1)
 
36
    print_usage ();
 
37
  elseif (! isnumeric (vcol) || ! isscalar (vcol) ||
 
38
          fix (vcol) != vcol || vcol < 1)
 
39
    error ("ascii: COLUMNS must be a positive integer");
 
40
  endif
 
41
 
 
42
  ## First char is #32 (0x20) and last char is #128 (0x80)
 
43
  voff = floor ((128 - 32) / vcol);
 
44
 
 
45
  ## Print a first row for the and underline that row
 
46
  vtab = [repmat(" Dec Hex Chr ", [1 vcol])
 
47
          repmat("-------------", [1 vcol])];
 
48
 
 
49
  ## Create the lines and columns of the asci table
 
50
  for vpos = 32:(32+voff)
 
51
    vline = "";
 
52
    for vcnt = 1:vcol
 
53
      vact  = (vcnt-1)*voff+vpos;
 
54
      vstr  = {num2str(vact), dec2hex(vact), char(vact)};
 
55
      vline = [vline sprintf(" %3s", vstr{1:length (vstr)}) " "];
 
56
    endfor
 
57
    vtab = [vtab; vline];
 
58
  endfor
 
59
 
 
60
  ## Print table to screen or return it to output argument
 
61
  if (nargout == 0)
 
62
    display (vtab);
 
63
  else
 
64
    table = vtab;
 
65
  endif
 
66
endfunction
 
67
 
 
68
%!demo
 
69
%! ## Display pretty table of conversion between ASCII, decimal and hexadecimal
 
70
%! ascii ()
 
71
 
 
72
%!demo
 
73
%! ## Display 6 columns table of conversion between ASCII, decimal and hexadecimal
 
74
%! ascii (6)
 
75
 
 
76
%!demo
 
77
%! ## Return a string with a pretty formatted table (but don't display it)
 
78
%! table = ascii (1)
 
79
%! display (table (65, :));
 
80
 
 
81
%!test
 
82
%! str = ascii (5);
 
83
%! assert (str(1,:), " Dec Hex Chr  Dec Hex Chr  Dec Hex Chr  Dec Hex Chr  Dec Hex Chr ");
 
84
%! assert (str(4,:), "  33  21   !   52  34   4   71  47   G   90  5A   Z  109  6D   m ");
 
85
 
 
86
%!test
 
87
%! str = ascii (1);
 
88
%! assert (str(1,:), " Dec Hex Chr ");
 
89
%! assert (str(6,:), "  35  23   # ");
 
90
 
 
91
%!error <positive> ascii (0)
 
92
%!error <integer>  ascii (4.5)
 
93
%!error <COLUMNS>  ascii ("dec")