|
| 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 | + unwind_protect |
| 28 | + l = fgetl(fd); |
| 29 | + colnames = strsplit(l, ","); |
| 30 | + |
| 31 | + ncols = size(colnames, 2); |
| 32 | + coldata = cell(1, ncols); |
| 33 | + while !feof(fd) |
| 34 | + l = fgetl(fd); |
| 35 | + if !isempty(l) |
| 36 | + s = split_line(l, ","); |
| 37 | + for idx=1:ncols |
| 38 | + if idx <= size(s,2) |
| 39 | + coldata{idx}{end+1} = s{idx}; |
| 40 | + else |
| 41 | + coldata{idx}{end+1} = []; |
| 42 | + endif |
| 43 | + endfor |
| 44 | + endif |
| 45 | + endwhile |
| 46 | + unwind_protect_cleanup |
| 47 | + fclose(fd); |
| 48 | + end_unwind_protect |
| 49 | + |
| 50 | + t = dbtable(coldata{:}, 'VariableNames', colnames); |
| 51 | +endfunction |
| 52 | + |
| 53 | +function sp = split_line(s, sep=",") |
| 54 | + sp = {}; |
| 55 | + # split line at sep, BUT handle potential '"' etc. at start of string |
| 56 | + st = ""; |
| 57 | + instring = false; |
| 58 | + firstchar = true; |
| 59 | + allowtrim = true; |
| 60 | + for idx=1:length(s) |
| 61 | + c = s(idx); |
| 62 | + |
| 63 | + if !instring |
| 64 | + if c == sep |
| 65 | + sp{end+1} = st; |
| 66 | + st = []; |
| 67 | + firstchar = true; |
| 68 | + allowtrim = true; |
| 69 | + elseif c == '"' && isempty(st) |
| 70 | + instring = true; |
| 71 | + allowtrim = false; |
| 72 | + else |
| 73 | + if allowtrim && (c == ' ' || c=='\t') |
| 74 | + # dont keep it |
| 75 | + else |
| 76 | + if !isempty(st) |
| 77 | + st = [st c]; |
| 78 | + else |
| 79 | + st = c; |
| 80 | + endif |
| 81 | + endif |
| 82 | + endif |
| 83 | + else |
| 84 | + # in a string |
| 85 | + if c == '"' |
| 86 | + instring = false; |
| 87 | + else |
| 88 | + if !isempty(st) |
| 89 | + st = [st c]; |
| 90 | + else |
| 91 | + st = c; |
| 92 | + endif |
| 93 | + endif |
| 94 | + endif |
| 95 | + endfor |
| 96 | + |
| 97 | + if !isempty(st) |
| 98 | + sp{end+1} = st; |
| 99 | + endif |
| 100 | + |
| 101 | +endfunction |
0 commit comments