summaryrefslogtreecommitdiff
path: root/examples/table-sort.cxx
diff options
context:
space:
mode:
authorGreg Ercolano <erco@seriss.com>2021-08-30 11:56:21 -0700
committerGreg Ercolano <erco@seriss.com>2021-08-30 11:56:21 -0700
commit975d34de4313e0cd29fc652b051c1afc1d8bf607 (patch)
tree7efe7da729d15ab4d7695c37185a8ec1f63f1135 /examples/table-sort.cxx
parent418699fceac327f8f443d1dbf9e9404e23ea0451 (diff)
Fix VS2017 'pclose' error, and DIR formatting
Diffstat (limited to 'examples/table-sort.cxx')
-rw-r--r--examples/table-sort.cxx25
1 files changed, 20 insertions, 5 deletions
diff --git a/examples/table-sort.cxx b/examples/table-sort.cxx
index 5193f5b3e..bb6898290 100644
--- a/examples/table-sort.cxx
+++ b/examples/table-sort.cxx
@@ -28,6 +28,7 @@
#include <FL/Fl_Table_Row.H>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <string>
@@ -39,9 +40,10 @@
#ifdef _WIN32
// WINDOWS
# define DIRCMD "dir"
-static const char *G_header[] = { "Date", "Time", "Size", "Filename", "", "", "", "", "", 0 };
+static const char *G_header[] = { "Date", "Time", "", "Size", "Filename", "", "", "", "", "", 0 };
# ifdef _MSC_VER
# define popen _popen
+# define pclose _pclose
# endif
#else /* _WIN32 */
// UNIX
@@ -219,11 +221,17 @@ void MyTable::load_command(const char *cmd) {
char s[512];
FILE *fp = popen(cmd, "r");
cols(0);
- for ( int r=0; fgets(s, sizeof(s)-1, fp); r++ ) {
- if ( r==0 && strncmp(s,"total ",6)==0) { --r; continue; }
+ for ( int line=0; fgets(s, sizeof(s)-1, fp); line++ ) {
+#ifdef _WIN32
+ // WINDOWS
+ if ( line < 5 ) continue; // skip DIR's 5 line header
+#else
+ // UNIX
+ if ( line==0 && strncmp(s,"total ",6)==0) continue;
+#endif
// Add a new row
Row newrow; rowdata_.push_back(newrow);
- std::vector<std::string> &rc = rowdata_[r].cols;
+ std::vector<std::string> &rc = rowdata_.back().cols;
// Break line into separate word 'columns'
char *ss;
const char *delim = " \t\n";
@@ -235,11 +243,18 @@ void MyTable::load_command(const char *cmd) {
cols((int)rc.size());
}
}
+ pclose(fp);
+
+#ifdef _WIN32
+ // WINDOWS: Trim off DIR's 2 line footer
+ if ( rowdata_.size() > 2 )
+ { rowdata_.pop_back(); rowdata_.pop_back(); }
+#endif
+
// How many rows we loaded
rows((int)rowdata_.size());
// Auto-calculate widths, with 20 pixel padding
autowidth(20);
- pclose(fp);
}
// Callback whenever someone clicks on different parts of the table