| 1 |
// ---------------------------------------------------------------------------- |
| 2 |
// < (C) 2014 |
| 3 |
// David Freese, W1HKJ |
| 4 |
// |
| 5 |
// This file is part of flmsg |
| 6 |
// |
| 7 |
// flrig is free software; you can redistribute it and/or modify |
| 8 |
// it under the terms of the GNU General Public License as published by |
| 9 |
// the Free Software Foundation; either version 3 of the License, or |
| 10 |
// (at your option) any later version. |
| 11 |
// |
| 12 |
// flrig is distributed in the hope that it will be useful, |
| 13 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
// GNU General Public License for more details. |
| 16 |
// |
| 17 |
// You should have received a copy of the GNU General Public License |
| 18 |
// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
// ---------------------------------------------------------------------------- |
| 20 |
|
| 21 |
#include <stdlib.h> |
| 22 |
#include <iostream> |
| 23 |
#include <fstream> |
| 24 |
#include <cstring> |
| 25 |
#include <string> |
| 26 |
#include <ctime> |
| 27 |
#include <sys/types.h> |
| 28 |
#include <sys/stat.h> |
| 29 |
#include <stdio.h> |
| 30 |
#include <errno.h> |
| 31 |
|
| 32 |
#include <FL/Fl.H> |
| 33 |
#include <FL/Fl_Window.H> |
| 34 |
#include <FL/Fl_Input.H> |
| 35 |
#include <FL/Fl_Check_Button.H> |
| 36 |
#include <FL/fl_draw.H> |
| 37 |
#include <FL/fl_ask.H> |
| 38 |
#include <FL/Fl_Table_Row.H> |
| 39 |
#include <FL/Fl_Browser.H> |
| 40 |
|
| 41 |
Fl_Window *csv_table_editor_dialog = 0; |
| 42 |
Fl_Browser *csv_browser = 0; |
| 43 |
|
| 44 |
static void cb_btn_close_dialog(Fl_Button*, void*) { |
| 45 |
csv_table_editor_dialog->hide(); |
| 46 |
} |
| 47 |
|
| 48 |
static int *cols; |
| 49 |
static int ncols; |
| 50 |
|
| 51 |
void csv_editor(std::string data) |
| 52 |
{ |
| 53 |
if (!csv_table_editor_dialog) { |
| 54 |
csv_table_editor_dialog = new Fl_Window(0, 0, 500, 300, "CSV Viewer"); |
| 55 |
|
| 56 |
// Add children to window |
| 57 |
csv_table_editor_dialog->begin(); |
| 58 |
|
| 59 |
csv_browser = new Fl_Browser(5, 5, 490, 260, ""); |
| 60 |
csv_browser->textsize(13); |
| 61 |
|
| 62 |
Fl_Button *close_dialog = new Fl_Button(405, 271, 80, 24, "Close"); |
| 63 |
close_dialog->callback((Fl_Callback*)cb_btn_close_dialog); |
| 64 |
csv_table_editor_dialog->end(); |
| 65 |
|
| 66 |
csv_table_editor_dialog->resizable(csv_browser); |
| 67 |
} |
| 68 |
|
| 69 |
int col = 0; |
| 70 |
int font = csv_browser->textfont(); |
| 71 |
int fontsize = csv_browser->textsize(); |
| 72 |
|
| 73 |
fl_font(font, fontsize); |
| 74 |
|
| 75 |
std::string line, field, temp; |
| 76 |
size_t p0, p1, p2; |
| 77 |
|
| 78 |
p0 = p1 = p2 = 0; |
| 79 |
|
| 80 |
// replace commas with tab unless contained within a quoted std::string |
| 81 |
if (data[data.length()-1] != '\n') data += '\n'; |
| 82 |
|
| 83 |
while ((p0 = data.find(",")) != std::string::npos) |
| 84 |
data.replace(p0, 1, "\t"); |
| 85 |
|
| 86 |
p0 = 0; |
| 87 |
while ((p0 = data.find("\"", p0)) != std::string::npos) { |
| 88 |
p1 = data.find("\"", p0 + 1); |
| 89 |
if (p1 == std::string::npos) break; |
| 90 |
while ((p2 = data.find("\t", p0)) < p1) data.replace(p2, 1, ","); |
| 91 |
p0 = p1 + 1; |
| 92 |
} |
| 93 |
|
| 94 |
p0 = 0; |
| 95 |
while ((p0 = data.find("\"")) != std::string::npos) data.erase(p0, 1); |
| 96 |
|
| 97 |
// count # columns |
| 98 |
temp = data; |
| 99 |
p0 = 0; |
| 100 |
int lcols; |
| 101 |
while ((p0 = temp.find("\n")) != std::string::npos) { |
| 102 |
line = temp.substr(0, p0); |
| 103 |
lcols = 0; |
| 104 |
p1 = 0; |
| 105 |
while ((p1 < line.length()) && ((p1 = line.find("\t", p1)) != std::string::npos)) { |
| 106 |
lcols++; |
| 107 |
p1++; |
| 108 |
} |
| 109 |
if (lcols > ncols) ncols = lcols; |
| 110 |
temp.erase(0, p0 + 1); |
| 111 |
} |
| 112 |
ncols += 2; // columns are zero terminated so need extra column |
| 113 |
|
| 114 |
cols = new int[ncols]; |
| 115 |
for (int i = 0; i < ncols; i++) cols[i] = 0; |
| 116 |
|
| 117 |
// compute maximum size of columns |
| 118 |
temp = data; |
| 119 |
p0 = 0; |
| 120 |
while ((p0 = temp.find("\n")) != std::string::npos) { |
| 121 |
col = 0; |
| 122 |
line = temp.substr(0, p0); |
| 123 |
while (line.length() && (p1 = line.find("\t")) != std::string::npos) { |
| 124 |
field = line.substr(0, p1); |
| 125 |
if (field.length()) { |
| 126 |
field.append("W"); |
| 127 |
int colw = fl_width(field.c_str()) + 4; |
| 128 |
if (colw > cols[col]) cols[col] = colw; |
| 129 |
} |
| 130 |
col++; |
| 131 |
line.erase(0, p1 + 1); |
| 132 |
} if (line.length()) { |
| 133 |
field.append("X"); |
| 134 |
int colw = fl_width(line.c_str()) + 4; |
| 135 |
if (colw > cols[col]) cols[col] = colw; |
| 136 |
} |
| 137 |
temp.erase(0, p0 + 1); |
| 138 |
} |
| 139 |
if (temp.length()) { |
| 140 |
col = 0; |
| 141 |
while ((p1 = temp.find("\t")) != std::string::npos) { |
| 142 |
field = temp.substr(0, p1+1); |
| 143 |
temp.erase(p1 + 1); |
| 144 |
if (field.length()) { |
| 145 |
field.append("W"); |
| 146 |
int colw = fl_width(field.c_str()) + 4; |
| 147 |
if (colw > cols[col]) cols[col] = colw; |
| 148 |
} |
| 149 |
col++; |
| 150 |
} if (temp.length()) { |
| 151 |
field.append("X"); |
| 152 |
int colw = fl_width(temp.c_str()) + 4; |
| 153 |
if (colw > cols[col]) cols[col] = colw; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
int width = 0; |
| 158 |
for (int i = 0; i < ncols; i++) width += cols[i]; |
| 159 |
width += csv_browser->scrollbar_size(); // space for vertical scrollbar |
| 160 |
width += 10; |
| 161 |
|
| 162 |
csv_table_editor_dialog->size(width + 10, csv_table_editor_dialog->h()); |
| 163 |
csv_browser->size(width, csv_browser->h()); |
| 164 |
|
| 165 |
csv_browser->column_widths((const int *)cols); |
| 166 |
csv_browser->clear(); |
| 167 |
|
| 168 |
temp = data; |
| 169 |
while ((p0 = temp.find("\n")) != std::string::npos) { |
| 170 |
line = temp.substr(0, p0); |
| 171 |
csv_browser->add(line.c_str()); |
| 172 |
temp.erase(0, p0 + 1); |
| 173 |
} |
| 174 |
if (temp.length()) { |
| 175 |
csv_browser->add(temp.c_str()); |
| 176 |
} |
| 177 |
|
| 178 |
csv_table_editor_dialog->show(); |
| 179 |
} |