2
* Copyright (C) 2004 Laurent Mazet
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.
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.
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/>.
22
#include <octave/oct.h>
23
#include <octave/Cell.h>
25
DEFUN_DLD (csvconcat, args, nargout,
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"
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"
39
if ((args.length() < 1) || (args.length() > 3)) {
41
return octave_value();
45
Cell c = args(0).cell_value();
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();
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();
59
/* Concat a cell into a string */
60
string_vector vec(c.rows());
63
/* For each element */
64
for (int i=0, il=c.rows(); i<il; i++) {
66
for (int j=0, jl=c.columns(); j<jl; j++) {
72
if (c(i, j).is_real_scalar()) {
74
/* Output real value */
76
sprintf(tmp, "%g", c(i, j).double_value());
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) {
85
while ((pos=str.find(prot, pos)) != str.npos) {
86
str.replace(pos, 1, prot+prot);
89
str = prot + str + prot;
95
/* Output NaN value */
96
warning ("not a real or a string\n");
103
return octave_value(vec);