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

« back to all changes in this revision

Viewing changes to src/cell2csv.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 <fstream>
23
 
 
24
 
#include <octave/oct.h>
25
 
#include <octave/Cell.h>
26
 
 
27
 
DEFUN_DLD (cell2csv, args, nargout,
28
 
           "-*- texinfo -*-\n"
29
 
           "@deftypefn {Function File} {} cell2csv (@var{file}, @var{c})\n"
30
 
           "@deftypefnx {Function File} {} cell2csv (@var{file}, @var{c}, @var{sep})\n"
31
 
           "@deftypefnx {Function File} {} cell2csv (@var{file}, @var{c}, @var{sep}, @var{prot})\n"
32
 
           "\n"
33
 
           "Create a CSV file from a cell. "
34
 
           "@var{sep} changes the character used to separate two fields. By "
35
 
           "default, two fields are expected to be separated by a coma "
36
 
           "(@code{,}). @var{prot} changes the character used to protect a string. "
37
 
           "By default it's a double quote (@code{\"}).\n"
38
 
           "@end deftypefn") {
39
 
 
40
 
  /* Check argument */
41
 
  if ((args.length() < 2) || (args.length() > 4)) {
42
 
    print_usage ();
43
 
    return octave_value();
44
 
  }
45
 
 
46
 
  /* Get arguments */
47
 
  std::string file = args(0).string_value();
48
 
 
49
 
  Cell c = args(1).cell_value();
50
 
 
51
 
  std::string sep = (args.length() > 2) ? args(2).string_value() : ",";
52
 
  if (sep.length() != 1) {
53
 
    error("Only on charactere need as separator\n");
54
 
    return octave_value();
55
 
  }
56
 
 
57
 
  std::string prot = (args.length() > 3) ? args(3).string_value() : "\"";
58
 
  if (prot.length() != 1) {
59
 
    error("Only on charactere need as protector\n");
60
 
    return octave_value();
61
 
  }
62
 
 
63
 
  /* Open file */
64
 
  std::ofstream fd(file.c_str());
65
 
  if (!fd.is_open()) {
66
 
    error("cannot write %s\n", file.c_str());
67
 
    return octave_value();
68
 
  }
69
 
 
70
 
  /* Concat a cell into a string */
71
 
  std::string word;
72
 
 
73
 
  /* For each element */
74
 
  for (int i=0, il=c.rows(); i<il; i++) {
75
 
   
76
 
    for (int j=0, jl=c.columns(); j<jl; j++) {
77
 
      word = "";
78
 
      
79
 
      /* Add separator */
80
 
      if (j != 0)
81
 
        word += sep;
82
 
 
83
 
      if (c(i, j).is_real_scalar()) {
84
 
 
85
 
        /* Output real value */
86
 
        char tmp[20];
87
 
        sprintf(tmp, "%g", c(i, j).double_value());
88
 
        word += tmp;
89
 
      }
90
 
 
91
 
      else if (c(i, j).is_string()) {
92
 
        /* Output string value */
93
 
        std::string str = c(i, j).string_value();
94
 
        if (str.find(sep) != str.npos) {
95
 
          unsigned int pos = 0;
96
 
          while ((pos=str.find(prot, pos)) != str.npos) {
97
 
            str.replace(pos, 1, prot+prot);
98
 
            pos += 2;
99
 
          }
100
 
          str = prot + str + prot;
101
 
        }
102
 
        word += str;
103
 
      }
104
 
 
105
 
      else if (!c(i, j).is_empty()) {
106
 
        /* Output NaN value */
107
 
        warning ("not a real or a string\n");
108
 
        word += "NaN";
109
 
      }
110
 
 
111
 
      fd << word;
112
 
    }
113
 
 
114
 
    /* Add end of line */
115
 
    fd << std::endl;
116
 
  }
117
 
 
118
 
  /* Close file */
119
 
  fd.close();
120
 
 
121
 
  return octave_value();
122
 
}