Skip to content

Commit 2bc0cf4

Browse files
authored
Merge pull request #4 from gnu-octave/devel
Devel
2 parents 45f9021 + b87ebf0 commit 2bc0cf4

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
lines changed

inst/dbtable.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,20 @@ function summary (this)
463463
endfor
464464
endfunction
465465

466+
function writetable (this, filename)
467+
[~, ~, ext] = fileparts(filename);
468+
if isempty(ext)
469+
ext = ".csv"
470+
endif
471+
472+
switch tolower(ext)
473+
case { ".txt" ".csv" ".dat" }
474+
write_dbtable_csv(this, filename);
475+
otherwise
476+
error ("unknown or unsupported format for '%s'", ext);
477+
endswitch
478+
endfunction
479+
466480
# head tail - create sub tables
467481
function tdata = head(this, rows)
468482
nrows = size(this, 1);

inst/private/read_dbtable_csv.m

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
## Copyright (C) 2022 John Donoghue <john.donoghue@ieee.org>
2+
##
3+
## This program is free software: you can redistribute it and/or modify it
4+
## under the terms of the GNU General Public License as published by
5+
## the Free Software Foundation, either version 3 of the License, or
6+
## (at your option) any later version.
7+
##
8+
## This program is distributed in the hope that it will be useful, but
9+
## WITHOUT ANY WARRANTY; without even the implied warranty of
10+
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
## GNU General Public License for more details.
12+
##
13+
## You should have received a copy of the GNU General Public License
14+
## along with this program. If not, see
15+
## <https://www.gnu.org/licenses/>.
16+
17+
## -*- texinfo -*-
18+
## @deftypefn {} {@var{t} =} read_dbtable_csv (@var{filename})
19+
## Private function
20+
## @end deftypefn
21+
22+
function t = read_dbtable_csv (filename)
23+
coldata = {};
24+
colnames = {};
25+
26+
fd = fopen(filename, "rt");
27+
if fd == -1
28+
error ("Cant open file '%s'", filename);
29+
endif
30+
unwind_protect
31+
l = fgetl(fd);
32+
colnames = strsplit(l, ",");
33+
34+
ncols = size(colnames, 2);
35+
coldata = cell(1, ncols);
36+
while !feof(fd)
37+
l = fgetl(fd);
38+
if !isempty(l)
39+
s = split_line(l, ",");
40+
for idx=1:ncols
41+
if idx <= size(s,2)
42+
coldata{idx}{end+1} = s{idx};
43+
else
44+
coldata{idx}{end+1} = [];
45+
endif
46+
endfor
47+
endif
48+
endwhile
49+
unwind_protect_cleanup
50+
fclose(fd);
51+
end_unwind_protect
52+
53+
t = dbtable(coldata{:}, 'VariableNames', colnames);
54+
endfunction
55+
56+
function sp = split_line(s, sep=",")
57+
sp = {};
58+
# split line at sep, BUT handle potential '"' etc. at start of string
59+
st = "";
60+
instring = false;
61+
firstchar = true;
62+
allowtrim = true;
63+
for idx=1:length(s)
64+
c = s(idx);
65+
66+
if !instring
67+
if c == sep
68+
sp{end+1} = st;
69+
st = [];
70+
firstchar = true;
71+
allowtrim = true;
72+
elseif c == '"' && isempty(st)
73+
instring = true;
74+
allowtrim = false;
75+
else
76+
if allowtrim && (c == ' ' || c=='\t')
77+
# dont keep it
78+
else
79+
if !isempty(st)
80+
st = [st c];
81+
else
82+
st = c;
83+
endif
84+
endif
85+
endif
86+
else
87+
# in a string
88+
if c == '"'
89+
instring = false;
90+
else
91+
if !isempty(st)
92+
st = [st c];
93+
else
94+
st = c;
95+
endif
96+
endif
97+
endif
98+
endfor
99+
100+
if !isempty(st)
101+
sp{end+1} = st;
102+
endif
103+
104+
endfunction

inst/private/write_dbtable_csv.m

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
## Copyright (C) 2022 John Donoghue <john.donoghue@ieee.org>
2+
##
3+
## This program is free software: you can redistribute it and/or modify it
4+
## under the terms of the GNU General Public License as published by
5+
## the Free Software Foundation, either version 3 of the License, or
6+
## (at your option) any later version.
7+
##
8+
## This program is distributed in the hope that it will be useful, but
9+
## WITHOUT ANY WARRANTY; without even the implied warranty of
10+
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
## GNU General Public License for more details.
12+
##
13+
## You should have received a copy of the GNU General Public License
14+
## along with this program. If not, see
15+
## <https://www.gnu.org/licenses/>.
16+
17+
## -*- texinfo -*-
18+
## @deftypefn {} {@var{t} =} write_dbtable_csv (@var{filename})
19+
## Private function
20+
## @end deftypefn
21+
22+
function write_dbtable_csv (t, filename)
23+
fd = fopen(filename, "wt");
24+
if fd == -1
25+
error ("Error opening file '%s'", filename);
26+
endif
27+
unwind_protect
28+
cols = subsref (t, substruct(".", "Properties")).VariableNames;
29+
l = strjoin(cols, ",");
30+
fprintf(fd, "%s\n", l);
31+
32+
for idx = 1:length(t)
33+
row = subsref (t, substruct("{}", {idx,':'}));
34+
values = "";
35+
for col=1:numel(cols)
36+
if col > 1
37+
values = [values ","];
38+
endif
39+
if iscell(row)
40+
v = row{col}{:};
41+
else
42+
v = row(col,:);
43+
endif
44+
if islogical(v)
45+
if v
46+
v = 1;
47+
else
48+
v = 0;
49+
endif
50+
endif
51+
if isnumeric(v)
52+
v = num2str(v);
53+
elseif isempty(v)
54+
v = "";
55+
endif
56+
# quote it if spaces
57+
if !isempty(strchr(v, " "))
58+
v = ['"' v '"'];
59+
endif
60+
values = [values v];
61+
endfor
62+
fprintf(fd, "%s\n", char(values));
63+
endfor
64+
65+
unwind_protect_cleanup
66+
fclose(fd);
67+
end_unwind_protect
68+
endfunction

0 commit comments

Comments
 (0)