# HG changeset patch # User carandraug # Date 1331592381 0 # Node ID 9a0099a892abaef03b25f58f0b31b623298af9f1 # Parent e545fbde25354efb00f1be2e7a6b836f89e87676 csvexplode, csv2cell, cell2csv, csvconcat: moving from miscellaneous to IO package. Fixing Makefiles diff -r e545fbde2535 -r 9a0099a892ab main/io/src/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/io/src/Makefile Mon Mar 12 22:46:21 2012 +0000 @@ -0,0 +1,7 @@ +all: csvexplode.oct csv2cell.oct csvconcat.oct cell2csv.oct + +%.oct: %.cc + mkoctfile -Wall $< + +clean: + rm -f *.o octave-core core *.oct *~ diff -r e545fbde2535 -r 9a0099a892ab main/io/src/cell2csv.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main/io/src/cell2csv.cc Mon Mar 12 22:46:21 2012 +0000 @@ -0,0 +1,116 @@ +// Copyright (C) 2004 Laurent Mazet +// +// 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 . + +#include + +#include +#include + +DEFUN_DLD (cell2csv, args, nargout, + "-*- texinfo -*-\n" + "@deftypefn {Function File} {} cell2csv (@var{file}, @var{c})\n" + "@deftypefnx {Function File} {} cell2csv (@var{file}, @var{c}, @var{sep})\n" + "@deftypefnx {Function File} {} cell2csv (@var{file}, @var{c}, @var{sep}, @var{prot})\n" + "\n" + "Create a CSV file from a cell. " + "@var{sep} changes the character used to separate two fields. By " + "default, two fields are expected to be separated by a coma " + "(@code{,}). @var{prot} changes the character used to protect a string. " + "By default it's a double quote (@code{\"}).\n" + "@end deftypefn") { + + /* Check argument */ + if ((args.length() < 2) || (args.length() > 4)) { + print_usage (); + return octave_value(); + } + + /* Get arguments */ + std::string file = args(0).string_value(); + + Cell c = args(1).cell_value(); + + std::string sep = (args.length() > 2) ? args(2).string_value() : ","; + if (sep.length() != 1) { + error("Only on charactere need as separator\n"); + return octave_value(); + } + + std::string prot = (args.length() > 3) ? args(3).string_value() : "\""; + if (prot.length() != 1) { + error("Only on charactere need as protector\n"); + return octave_value(); + } + + /* Open file */ + std::ofstream fd(file.c_str()); + if (!fd.is_open()) { + error("cannot write %s\n", file.c_str()); + return octave_value(); + } + + /* Concat a cell into a string */ + std::string word; + + /* For each element */ + for (int i=0, il=c.rows(); i +// +// 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 . + +#include + +#include +#include + +#define MAXSTRINGLENGTH 4096 + +DEFUN_DLD (csv2cell, args, nargout, + "-*- texinfo -*-\n" + "@deftypefn {Function File} {@var{c} = } csv2cell (@var{file})\n" + "@deftypefnx {Function File} {@var{c} = } csv2cell (@var{file}, @var{sep})\n" + "@deftypefnx {Function File} {@var{c} = } csv2cell (@var{file}, @var{sep}, @var{prot})\n" + "\n" + "Read a CSV (Comma Separated Values) file and convert it into a " + "cell. " + "@var{sep} changes the character used to separate two fields. By " + "default, two fields are expected to be separated by a coma " + "(@code{,}). @var{prot} changes the character used to protect a string. " + "By default it's a double quote (@code{\"}).\n" + "@end deftypefn") { + + /* Get arguments */ + const int nargin = args.length (); + octave_value_list retval; + if (nargin == 0) + { + error ("csv2cell: not enough input arguments"); + return retval; + } + + const std::string file = args (0).string_value (); + + const std::string _sep = (nargin > 1) ? args (1).string_value () : ","; + if (_sep.length() != 1) + { + error ("csv2cell: only on charactere need as separator"); + return retval; + } + char sep = _sep[0]; + + const std::string _prot = (nargin > 2) ? args (2).string_value () : "\""; + if (_prot.length() != 1) + { + error ("csv2cell: only on charactere need as protector"); + return retval; + } + char prot = _prot[0]; + + /* Open file */ + std::ifstream fd (file.c_str ()); + if (!fd.is_open ()) + { + error ("csv2cell: cannot read %s", file.c_str()); + return retval; + } + fd.seekg (0, std::ios::end); + long fdend = fd.tellg (); + fd.seekg (0, std::ios::beg); + + if (fd.tellg () >= fdend) + return octave_value (Cell (0, 0)); + + /* Buffers */ + char line [MAXSTRINGLENGTH]; + std::string str, word; + bool inside = false; + + /* Read a line */ + str = ""; + fd.getline (line, MAXSTRINGLENGTH); + while (fd.fail ()) + { + fd.clear (); + str += line; + fd.getline (line, MAXSTRINGLENGTH); + } + str += line; + + /* Parse first to get number of columns */ + int nbcolumns = 0; + for (int i = 0, len = str.length (); i <= len; i++) + if (((i==len) || (str [i] == sep)) && (!inside)) + nbcolumns++; + else if ((inside) && (str [i] == prot) && ((i < len) && (str [i+1] == prot))) + ++i; + else if (str [i] == prot) + inside = !inside; + + /* Read all the file to get number of rows */ + int nbrows = 1; + while (fd.tellg () < fdend) + { + fd.getline (line, MAXSTRINGLENGTH); + while (fd.fail ()) + { + fd.clear (); + fd.getline (line, MAXSTRINGLENGTH); + } + nbrows++; + } + + /* Rewind */ + fd.seekg (0, std::ios::beg); + if (!fd.good ()) + { + error ("csv2cell: cannot reread %s", file.c_str ()); + return retval; + } + + /* Read all the file until the end */ + Cell c (nbrows, nbcolumns); + for (int i = 0; i < nbrows; i++) + { + /* Read a line */ + str = ""; + fd.getline (line, MAXSTRINGLENGTH); + while (fd.fail ()) + { + fd.clear (); + str += line; + fd.getline (line, MAXSTRINGLENGTH); + } + str += line; + + /* Explode a line into a sub cell */ + word = ""; + inside = false; + int j = 0; + for (int k = 0, len = str.length (); k <= len; k++) + { + if (((k == len) || (str [k] == sep)) && (!inside)) + { + /* Check number of columns */ + if (j == nbcolumns) + { + fd.close (); + error ("csv2cell: incorrect CSV file, line %d too long", i+1); + return retval; + } + + /* Check if scalar */ + const char *word_str = word.c_str (); + char *err; + double val = strtod (word_str, &err); + + /* Store into the cell */ + c (i, j++) = ((word == "") || (err != word_str+word.length())) ? + octave_value (word) : octave_value (val); + word = ""; + } + else if ((inside) && (str[k]==prot) && ((k +// +// 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 . + +#include +#include + +DEFUN_DLD (csvconcat, args, nargout, + "-*- texinfo -*-\n" + "@deftypefn {Function File} {@var{str} = } csvconcat (@var{c})\n" + "@deftypefnx {Function File} {@var{str} = } csvconcat (@var{c}, @var{sep})\n" + "@deftypefnx {Function File} {@var{str} = } csvconcat (@var{c}, @var{sep}, @var{prot})\n" + "\n" + "Concatenate a cell into a CSV string or array of strings. " + "@var{sep} changes the character used to separate two fields. By " + "default, two fields are expected to be separated by a coma " + "(@code{,}). @var{prot} changes the character used to protect a string. " + "By default it's a double quote (@code{\"}).\n" + "@end deftypefn") { + + /* Check argument */ + if ((args.length() < 1) || (args.length() > 3)) { + print_usage (); + return octave_value(); + } + + /* Get arguments */ + Cell c = args(0).cell_value(); + + std::string sep = (args.length() > 1) ? args(1).string_value() : ","; + if (sep.length() != 1) { + error("Only on charactere need as separator\n"); + return octave_value(); + } + + std::string prot = (args.length() > 2) ? args(2).string_value() : "\""; + if (prot.length() != 1) { + error("Only on charactere need as protector\n"); + return octave_value(); + } + + /* Concat a cell into a string */ + string_vector vec(c.rows()); + std::string word; + + /* For each element */ + for (int i=0, il=c.rows(); i +// +// 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 . + +#include +#include + +DEFUN_DLD (csvexplode, args, nargout, + "-*- texinfo -*-\n" + "@deftypefn {Function File} {@var{c} = } csvexplode (@var{str})\n" + "@deftypefnx {Function File} {@var{c} = } csvexplode (@var{str}, @var{sep})\n" + "@deftypefnx {Function File} {@var{c} = } csvexplode (@var{str}, @var{sep}, @var{prot})\n" + "\n" + "Explode a CSV string into a cell. " + "@var{sep} changes the character used to separate two fields. By " + "default, two fields are expected to be separated by a coma " + "(@code{,}). @var{prot} changes the character used to protect a string. " + "By default it's a double quote (@code{\"}).\n" + "@end deftypefn") { + + /* Check argument */ + if ((args.length() < 1) || (args.length() > 3)) { + print_usage (); + return octave_value(); + } + + /* Get arguments */ + if (!args(0).is_string()) { + if (args(0).is_cell()) + return octave_value(args(0)); + else + return octave_value(Cell(args(0))); + } + std::string str = args(0).string_value(); + + std::string _sep = (args.length() > 1) ? args(1).string_value() : ","; + if (_sep.length() != 1) { + error("Only on charactere need as separator\n"); + return octave_value(); + } + char sep = _sep[0]; + + std::string _prot = (args.length() > 2) ? args(2).string_value() : "\""; + if (_prot.length() != 1) { + error("Only on charactere need as protector\n"); + return octave_value(); + } + char prot = _prot[0]; + + /* Explode a line into a cell */ + Cell retval; + std::string word; + bool inside = false; + for (int i=0, len=str.length(); i<=len; i++) { + if (((i==len) || (str[i] == sep)) && (!inside)) { + + /* Extand cell */ + retval.resize(dim_vector(1, retval.columns()+1)); + + /* Check if scalar */ + const char *word_str=word.c_str(); + char *err; + double val = strtod (word_str, &err); + + /* Store into the cell */ + retval(0, retval.columns()-1) = + ((word == "") || (err != word_str+word.length())) ? + octave_value(word) : octave_value(val); + + word = ""; + } + else if ((inside) && (str[i]==prot) && ((i -// -// 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 . - -#include - -#include -#include - -DEFUN_DLD (cell2csv, args, nargout, - "-*- texinfo -*-\n" - "@deftypefn {Function File} {} cell2csv (@var{file}, @var{c})\n" - "@deftypefnx {Function File} {} cell2csv (@var{file}, @var{c}, @var{sep})\n" - "@deftypefnx {Function File} {} cell2csv (@var{file}, @var{c}, @var{sep}, @var{prot})\n" - "\n" - "Create a CSV file from a cell. " - "@var{sep} changes the character used to separate two fields. By " - "default, two fields are expected to be separated by a coma " - "(@code{,}). @var{prot} changes the character used to protect a string. " - "By default it's a double quote (@code{\"}).\n" - "@end deftypefn") { - - /* Check argument */ - if ((args.length() < 2) || (args.length() > 4)) { - print_usage (); - return octave_value(); - } - - /* Get arguments */ - std::string file = args(0).string_value(); - - Cell c = args(1).cell_value(); - - std::string sep = (args.length() > 2) ? args(2).string_value() : ","; - if (sep.length() != 1) { - error("Only on charactere need as separator\n"); - return octave_value(); - } - - std::string prot = (args.length() > 3) ? args(3).string_value() : "\""; - if (prot.length() != 1) { - error("Only on charactere need as protector\n"); - return octave_value(); - } - - /* Open file */ - std::ofstream fd(file.c_str()); - if (!fd.is_open()) { - error("cannot write %s\n", file.c_str()); - return octave_value(); - } - - /* Concat a cell into a string */ - std::string word; - - /* For each element */ - for (int i=0, il=c.rows(); i -// -// 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 . - -#include - -#include -#include - -#define MAXSTRINGLENGTH 4096 - -DEFUN_DLD (csv2cell, args, nargout, - "-*- texinfo -*-\n" - "@deftypefn {Function File} {@var{c} = } csv2cell (@var{file})\n" - "@deftypefnx {Function File} {@var{c} = } csv2cell (@var{file}, @var{sep})\n" - "@deftypefnx {Function File} {@var{c} = } csv2cell (@var{file}, @var{sep}, @var{prot})\n" - "\n" - "Read a CSV (Comma Separated Values) file and convert it into a " - "cell. " - "@var{sep} changes the character used to separate two fields. By " - "default, two fields are expected to be separated by a coma " - "(@code{,}). @var{prot} changes the character used to protect a string. " - "By default it's a double quote (@code{\"}).\n" - "@end deftypefn") { - - /* Get arguments */ - const int nargin = args.length (); - octave_value_list retval; - if (nargin == 0) - { - error ("csv2cell: not enough input arguments"); - return retval; - } - - const std::string file = args (0).string_value (); - - const std::string _sep = (nargin > 1) ? args (1).string_value () : ","; - if (_sep.length() != 1) - { - error ("csv2cell: only on charactere need as separator"); - return retval; - } - char sep = _sep[0]; - - const std::string _prot = (nargin > 2) ? args (2).string_value () : "\""; - if (_prot.length() != 1) - { - error ("csv2cell: only on charactere need as protector"); - return retval; - } - char prot = _prot[0]; - - /* Open file */ - std::ifstream fd (file.c_str ()); - if (!fd.is_open ()) - { - error ("csv2cell: cannot read %s", file.c_str()); - return retval; - } - fd.seekg (0, std::ios::end); - long fdend = fd.tellg (); - fd.seekg (0, std::ios::beg); - - if (fd.tellg () >= fdend) - return octave_value (Cell (0, 0)); - - /* Buffers */ - char line [MAXSTRINGLENGTH]; - std::string str, word; - bool inside = false; - - /* Read a line */ - str = ""; - fd.getline (line, MAXSTRINGLENGTH); - while (fd.fail ()) - { - fd.clear (); - str += line; - fd.getline (line, MAXSTRINGLENGTH); - } - str += line; - - /* Parse first to get number of columns */ - int nbcolumns = 0; - for (int i = 0, len = str.length (); i <= len; i++) - if (((i==len) || (str [i] == sep)) && (!inside)) - nbcolumns++; - else if ((inside) && (str [i] == prot) && ((i < len) && (str [i+1] == prot))) - ++i; - else if (str [i] == prot) - inside = !inside; - - /* Read all the file to get number of rows */ - int nbrows = 1; - while (fd.tellg () < fdend) - { - fd.getline (line, MAXSTRINGLENGTH); - while (fd.fail ()) - { - fd.clear (); - fd.getline (line, MAXSTRINGLENGTH); - } - nbrows++; - } - - /* Rewind */ - fd.seekg (0, std::ios::beg); - if (!fd.good ()) - { - error ("csv2cell: cannot reread %s", file.c_str ()); - return retval; - } - - /* Read all the file until the end */ - Cell c (nbrows, nbcolumns); - for (int i = 0; i < nbrows; i++) - { - /* Read a line */ - str = ""; - fd.getline (line, MAXSTRINGLENGTH); - while (fd.fail ()) - { - fd.clear (); - str += line; - fd.getline (line, MAXSTRINGLENGTH); - } - str += line; - - /* Explode a line into a sub cell */ - word = ""; - inside = false; - int j = 0; - for (int k = 0, len = str.length (); k <= len; k++) - { - if (((k == len) || (str [k] == sep)) && (!inside)) - { - /* Check number of columns */ - if (j == nbcolumns) - { - fd.close (); - error ("csv2cell: incorrect CSV file, line %d too long", i+1); - return retval; - } - - /* Check if scalar */ - const char *word_str = word.c_str (); - char *err; - double val = strtod (word_str, &err); - - /* Store into the cell */ - c (i, j++) = ((word == "") || (err != word_str+word.length())) ? - octave_value (word) : octave_value (val); - word = ""; - } - else if ((inside) && (str[k]==prot) && ((k -// -// 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 . - -#include -#include - -DEFUN_DLD (csvconcat, args, nargout, - "-*- texinfo -*-\n" - "@deftypefn {Function File} {@var{str} = } csvconcat (@var{c})\n" - "@deftypefnx {Function File} {@var{str} = } csvconcat (@var{c}, @var{sep})\n" - "@deftypefnx {Function File} {@var{str} = } csvconcat (@var{c}, @var{sep}, @var{prot})\n" - "\n" - "Concatenate a cell into a CSV string or array of strings. " - "@var{sep} changes the character used to separate two fields. By " - "default, two fields are expected to be separated by a coma " - "(@code{,}). @var{prot} changes the character used to protect a string. " - "By default it's a double quote (@code{\"}).\n" - "@end deftypefn") { - - /* Check argument */ - if ((args.length() < 1) || (args.length() > 3)) { - print_usage (); - return octave_value(); - } - - /* Get arguments */ - Cell c = args(0).cell_value(); - - std::string sep = (args.length() > 1) ? args(1).string_value() : ","; - if (sep.length() != 1) { - error("Only on charactere need as separator\n"); - return octave_value(); - } - - std::string prot = (args.length() > 2) ? args(2).string_value() : "\""; - if (prot.length() != 1) { - error("Only on charactere need as protector\n"); - return octave_value(); - } - - /* Concat a cell into a string */ - string_vector vec(c.rows()); - std::string word; - - /* For each element */ - for (int i=0, il=c.rows(); i -// -// 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 . - -#include -#include - -DEFUN_DLD (csvexplode, args, nargout, - "-*- texinfo -*-\n" - "@deftypefn {Function File} {@var{c} = } csvexplode (@var{str})\n" - "@deftypefnx {Function File} {@var{c} = } csvexplode (@var{str}, @var{sep})\n" - "@deftypefnx {Function File} {@var{c} = } csvexplode (@var{str}, @var{sep}, @var{prot})\n" - "\n" - "Explode a CSV string into a cell. " - "@var{sep} changes the character used to separate two fields. By " - "default, two fields are expected to be separated by a coma " - "(@code{,}). @var{prot} changes the character used to protect a string. " - "By default it's a double quote (@code{\"}).\n" - "@end deftypefn") { - - /* Check argument */ - if ((args.length() < 1) || (args.length() > 3)) { - print_usage (); - return octave_value(); - } - - /* Get arguments */ - if (!args(0).is_string()) { - if (args(0).is_cell()) - return octave_value(args(0)); - else - return octave_value(Cell(args(0))); - } - std::string str = args(0).string_value(); - - std::string _sep = (args.length() > 1) ? args(1).string_value() : ","; - if (_sep.length() != 1) { - error("Only on charactere need as separator\n"); - return octave_value(); - } - char sep = _sep[0]; - - std::string _prot = (args.length() > 2) ? args(2).string_value() : "\""; - if (_prot.length() != 1) { - error("Only on charactere need as protector\n"); - return octave_value(); - } - char prot = _prot[0]; - - /* Explode a line into a cell */ - Cell retval; - std::string word; - bool inside = false; - for (int i=0, len=str.length(); i<=len; i++) { - if (((i==len) || (str[i] == sep)) && (!inside)) { - - /* Extand cell */ - retval.resize(dim_vector(1, retval.columns()+1)); - - /* Check if scalar */ - const char *word_str=word.c_str(); - char *err; - double val = strtod (word_str, &err); - - /* Store into the cell */ - retval(0, retval.columns()-1) = - ((word == "") || (err != word_str+word.length())) ? - octave_value(word) : octave_value(val); - - word = ""; - } - else if ((inside) && (str[i]==prot) && ((i