-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_dlm_esc.cc
More file actions
160 lines (144 loc) · 4.39 KB
/
read_dlm_esc.cc
File metadata and controls
160 lines (144 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//#include <mex.h>
#include <octave/oct.h>
#include <octave/Cell.h>
//#include <sys/cdefs.h>
#include <stdint.h>
//#include <sys/stat.h>
//#include <net/if.h>
//#include <fcntl.h> // fcntl()
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
//#include <sys/uio.h> // readv(), writev(), preadv(), pwritev()
#include <errno.h>
#include <termios.h>
//#include <termio.h>
//#include <signal.h>
//#include <sys/time.h>
//#include <time.h>
//#include <unistd.h> // lockf() pread(), pwrite() access() euidaccess() eaccess() faccessat ()
#include <string.h>
//#include <syslog.h>
//#include <getopt.h>
//#include <stdbool.h>
//------------------------------------------------------------
DEFUN_DLD (read_dlm_esc, args, nargout, "Read dlm Escaped")
// function [d] = read_dlm_escaped ( fname = "data/stream.txt", delim=" ", r0=0, c0=0, esc_char='"')
// Read a delimited file, similar to dlmread(), but also allow for delimiters
// to be escaped, like quotes in a CSV file.
//
// In the current implementation consecutive delimiters are treated as a single delimiter
//------------------------------------------------------------
{
//static octave_value_list retval;
static octave_value retval;
//
// Process arguments
//
int nargin = args.length();
std::string fname = "data/stream.txt";
if (nargin >0) {
fname = args(0).char_matrix_value().row_as_string(0);
}
std::string delim = ",";
if (nargin >1) {
delim = args(1).char_matrix_value().row_as_string(0);
}
octave_stdout << "read_dlm_esc: delim = " << delim << "\n";
int r0 = 0;
charMatrix ch_fname;
if (nargin >2) {
r0 = args(2).int_value();
}
int c0 = 0;
if (nargin >3) {
c0 = args(3).int_value();
}
charMatrix ch_esc;
std::string esc_char = "\"";
if (nargin >4) {
esc_char = args(4).char_matrix_value().row_as_string(0);
}
octave_stdout << "read_dlm_esc: esc_char = " << esc_char << "\n";
//
// Open the File
//
FILE* fid = NULL;
char* fret = NULL;
octave_stdout << "read_dlm_esc: attempting to open \"" << fname << "\"\n";
fid = fopen(fname.c_str(), "r");
if (NULL == fid) {
octave_stdout << "Unable to find " << fname << "\n";
return octave_value(0);
}
//
// Skip any header lines
//
size_t nskip =0;
char line[512] = "";
for (nskip = 0; nskip <r0; nskip++) {
fret = fgets(line, sizeof(line), fid);
if(feof(fid) || NULL == fret) {
//break;
return octave_value(0);
}
}
octave_stdout << "read_dlm_esc: skipped r0 lines = " << nskip << "\n";
c0 += 1;
Cell d = Cell();
size_t row =0;
int escaped = 0;
std::string c_prev = delim;
std::string meta_esc = "\\";
std::string str = "";
int col = 0;
size_t n=0;
char c;
while ( !feof(fid) ) {
fret = fgets(line, sizeof(line), fid);
if(feof(fid) || NULL == fret) break;
//octave_stdout << line; // DEBUG
col=0;
str = "";
for (n = 0; n<strlen(line); n++) {
c = line[n];
if ( c == esc_char[0] ) {
if (c_prev == meta_esc) {
str = str.substr(0, str.size()-1);
str += c;
} else {
escaped = !escaped;
}
} else if ( (c == delim.c_str()[0] || (strlen(line)-1)==n) && !escaped) {//TODO: verify that this works if the last field in a line is using esc_char
if (c_prev != delim) {
//printf('row = %d, col = %d, str = "%s"\n', row, col, str);
d.insert(octave_value(str, '"'), octave_idx_type(row), octave_idx_type(col));
col += 1;
//octave_stdout << "found str = " << str << "\n"; // DEBUG
str = std::string("");
}
} else {
str += c;
}
c_prev = c;
} // for n
row += 1;
c_prev = delim;
} // while( !feof() )
octave_stdout << "read_dlm_esc: read rows = " << row << "\n";
//retval(0) = octave_value_list(d);
//retval = octave_value_list(d);
retval = d;
/*
octave_idx_type ni;
//for (ni =0; ni < d.numel(); ni++)
for (ni = d.numel()-1; ni>=0; ni--)
{
retval(ni) = d.elem(ni);
//octave_stdout << d.elem(ni).cellstr_value() << "\n";
//octave_stdout << d.elem(ni).string_value() << "\n";
}
*/
return retval;
}
// vim: ts=3 sw=3