-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtbltransp2.cc
81 lines (68 loc) · 1.46 KB
/
tbltransp2.cc
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
/*
* tbltransp2: fast table transposition - implementation
* Copyright(c) 2008 EURAC, Institute of Genetic Medicine
*/
/*
* Headers
*/
// local headers
#include "shared.hh"
// c headers
#include <stdlib.h>
#include <unistd.h>
/*
* Implementation
*/
void
help(char* argv[])
{
cerr << argv[0] << ": bad parameters:\n"
<< "Usage: " << argv[0] << " [-h] file\n"
<< "Transpose the contents of a CSV file. CSV files are TAB separated by default.\n"
<< "You can change the column separator by setting the TBLSEP environment variable.\n"
<< "\n"
<< " -h: help summary\n";
}
int
main(int argc, char* argv[]) try
{
int arg;
while((arg = getopt(argc, argv, "h")) != -1)
switch(arg)
{
case 'h':
help(argv);
return EXIT_SUCCESS;
default:
return EXIT_FAILURE;
}
// check args
argc -= optind;
const char* file(argv[optind++]);
if(argc != 1)
{
help(argv);
return EXIT_FAILURE;
}
// get default separator
char sep = '\t';
const char *envSep = getenv("TBLSEP");
if(envSep && *envSep)
sep = *envSep;
// open the file
const char* addr;
fix_string_matrix& m = *mapFixStringMatrix(&addr, file, sep);
// start writing back
for(size_t x = 0; x != m.front().size(); ++x)
{
cout << m[0][x];
for(size_t y = 1; y != m.size(); ++y)
cout << sep << m[y][x];
cout << '\n';
}
}
catch(runtime_error& e)
{
cerr << e.what() << std::endl;
return EXIT_FAILURE;
}