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

« back to all changes in this revision

Viewing changes to src/csvconcat.cc

  • Committer: Package Import Robot
  • Author(s): Sébastien Villemot
  • Date: 2012-10-17 13:40:55 UTC
  • mfrom: (1.1.6)
  • mto: (5.1.2 experimental)
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: package-import@ubuntu.com-20121017134055-e8lrxjd3qgcd3kmt
Tags: upstream-1.2.0
Import upstream version 1.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2004 Laurent Mazet
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 2 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
 
 
18
 
/* 2004-04-11
19
 
 *   initial release
20
 
 */
21
 
 
22
 
#include <octave/oct.h>
23
 
#include <octave/Cell.h>
24
 
 
25
 
DEFUN_DLD (csvconcat, args, nargout,
26
 
           "-*- texinfo -*-\n"
27
 
           "@deftypefn {Function File} {@var{str} = } csvconcat (@var{c})\n"
28
 
           "@deftypefnx {Function File} {@var{str} = } csvconcat (@var{c}, @var{sep})\n"
29
 
           "@deftypefnx {Function File} {@var{str} = } csvconcat (@var{c}, @var{sep}, @var{prot})\n"
30
 
           "\n"
31
 
           "Concatenate a cell into a CSV string or array of strings. "
32
 
           "@var{sep} changes the character used to separate two fields. By "
33
 
           "default, two fields are expected to be separated by a coma "
34
 
           "(@code{,}). @var{prot} changes the character used to protect a string. "
35
 
           "By default it's a double quote (@code{\"}).\n"
36
 
           "@end deftypefn") {
37
 
 
38
 
  /* Check argument */
39
 
  if ((args.length() < 1) || (args.length() > 3)) {
40
 
    print_usage ();
41
 
    return octave_value();
42
 
  }
43
 
 
44
 
  /* Get arguments */
45
 
  Cell c = args(0).cell_value();
46
 
 
47
 
  std::string sep = (args.length() > 1) ? args(1).string_value() : ",";
48
 
  if (sep.length() != 1) {
49
 
    error("Only on charactere need as separator\n");
50
 
    return octave_value();
51
 
  }
52
 
 
53
 
  std::string prot = (args.length() > 2) ? args(2).string_value() : "\"";
54
 
  if (prot.length() != 1) {
55
 
    error("Only on charactere need as protector\n");
56
 
    return octave_value();
57
 
  }
58
 
  
59
 
  /* Concat a cell into a string */
60
 
  string_vector vec(c.rows());
61
 
  std::string word;
62
 
 
63
 
  /* For each element */
64
 
  for (int i=0, il=c.rows(); i<il; i++) {
65
 
    word = "";
66
 
    for (int j=0, jl=c.columns(); j<jl; j++) {
67
 
      
68
 
      /* Add separator */
69
 
      if (j != 0)
70
 
        word += sep;
71
 
 
72
 
      if (c(i, j).is_real_scalar()) {
73
 
 
74
 
        /* Output real value */
75
 
        char tmp[20];
76
 
        sprintf(tmp, "%g", c(i, j).double_value());
77
 
        word += tmp;
78
 
      }
79
 
 
80
 
      else if (c(i, j).is_string()) {
81
 
        /* Output string value */
82
 
        std::string str = c(i, j).string_value();
83
 
        if (str.find(sep) != str.npos) {
84
 
          unsigned int pos = 0;
85
 
          while ((pos=str.find(prot, pos)) != str.npos) {
86
 
            str.replace(pos, 1, prot+prot);
87
 
            pos += 2;
88
 
          }
89
 
          str = prot + str + prot;
90
 
        }
91
 
        word += str;
92
 
      }
93
 
 
94
 
      else {
95
 
        /* Output NaN value */
96
 
        warning ("not a real or a string\n");
97
 
        word += "NaN";
98
 
      }
99
 
    }
100
 
    vec(i) = word;
101
 
  }
102
 
  
103
 
  return octave_value(vec);
104
 
}