Points&Forces (core)
Software tools facilitating the task of surveying architecture
csv2sc.cxx
Go to the documentation of this file.
1 /*
2  Copyright 2019 Pierre SMARS (smars@yuntech.edu.tw)
3  This program is free software: you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation, either version 2 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,
9  but 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 <http://www.gnu.org/licenses/>.
15  */
16 #include <iostream>
17 #include <sstream>
18 #include <string>
19 #include "SimpleOpt.h"
20 
22 
23 //------------------------------------------
24 CSimpleOpt::SOption g_rgOptions[] = {
25  { OPT_DELIM, _T("-d"), SO_REQ_SEP },
26  { OPT_DELIM, _T("--delimiter"), SO_REQ_SEP },
27  { OPT_ROW, _T("-r"), SO_REQ_SEP },
28  { OPT_ROW, _T("--row"), SO_REQ_SEP },
29  { OPT_COL, _T("-c"), SO_REQ_SEP },
30  { OPT_COL, _T("--column"), SO_REQ_SEP },
31  { OPT_HELP, _T("-?"), SO_NONE },
32  { OPT_HELP, _T("-h"), SO_NONE },
33  { OPT_HELP, _T("--help"), SO_NONE },
35 };
36 //------------------------------------------
37 int error(int val)
38 {
39 #include "csv2sc.help"
40  return val;
41 }
42 //******************************************
43 bool isnumeric(const std::string& s)
44 {
45  bool dec;
46  unsigned int i = 0;
47  while (s[i]==' ') i++;
48  if (((s[i]<'0')||(s[i]>'9'))&&(s[i]!='.')&&(s[i]!='-')&&(s[i]!='+'))
49  return false;
50  else
51  {
52  if (s[i]=='.')
53  dec = true;
54  else
55  dec = false;
56  }
57  i++;
58  while (i<s.length())
59  {
60  if ((dec)&&s[i]=='.')
61  return false;
62  if (((s[i]<'0')||(s[i]>'9'))&&(s[i]!='.'))
63  return false;
64  else
65  {
66  if (s[i]=='.')
67  dec = true;
68  else
69  dec = false;
70  }
71  i++;
72  }
73  return true;
74 }
75 //******************************************
76 int main(int argc, char** argv)
77 {
78  char delim = ',';
79  unsigned int u,v0;
80  u = v0 = 0;
81  CSimpleOpt args(argc, argv, g_rgOptions, SO_O_NOERR);
82  while (args.Next())
83  {
84  if (args.LastError() == SO_SUCCESS)
85  {
86  if (args.OptionId() == OPT_HELP)
87  return error(0);
88  else if (args.OptionId() == OPT_DELIM)
89  {
90  std::string d(args.OptionArg());
91  if (d=="\\t")
92  delim = '\t';
93  else
94  delim = args.OptionArg()[0];
95  }
96  else if (args.OptionId() == OPT_ROW)
97  {
98  std::istringstream i(args.OptionArg());
99  i >> u;
100  }
101  else if (args.OptionId() == OPT_COL)
102  {
103  std::istringstream i(args.OptionArg());
104  i >> v0;
105  }
106  else
107  return error(-1);
108  }
109  else
110  {
111  std::cerr << "Invalid argument: " << args.OptionText() << std::endl;
112  return error(args.LastError());
113  }
114  }
115  std::string s;
116  unsigned int v;
117  std::cout << "set color" << std::endl;
118  std::cout << "color 1 = @white;@black" << std::endl;
119  while (std::getline(std::cin,s))
120  {
121  v = v0;
122  std::string vs;
123  std::istringstream in(s);
124  std::string s2;
125  while (std::getline(in,s2,delim))
126  {
127  if (v<26)
128  vs = char(v+65);
129  else if (v<677)
130  {
131  vs = char(v/26+64);
132  vs+= char(v%26+65);
133  }
134  else
135  return error(-2);
136  if (isnumeric(s2))
137  std::cout << "let " << vs << u << " = " << s2 << std::endl;
138  else
139  std::cout << "leftstring " << vs << u << " = " << "\"" << s2 << "\"" << std::endl;
140  v++;
141  }
142  u++;
143  }
144  return 0;
145 }
A cross-platform command line library which can parse almost any of the standard command line formats...
@ SO_O_NOERR
Definition: SimpleOpt.h:256
#define SO_END_OF_OPTIONS
this option definition must be the last entry in the table
Definition: SimpleOpt.h:307
#define CSimpleOpt
TCHAR version dependent on if _UNICODE is defined.
Definition: SimpleOpt.h:1048
#define _T(s)
Definition: SimpleOpt.h:197
@ SO_SUCCESS
No error.
Definition: SimpleOpt.h:204
@ SO_REQ_SEP
Definition: SimpleOpt.h:290
@ SO_NONE
Definition: SimpleOpt.h:286
std::cin getline(buf, 256)
@ OPT_DELIM
Definition: csv2sc.cxx:21
@ OPT_ROW
Definition: csv2sc.cxx:21
@ OPT_COL
Definition: csv2sc.cxx:21
@ OPT_HELP
Definition: csv2sc.cxx:21
int error(int val)
Definition: csv2sc.cxx:37
CSimpleOpt::SOption g_rgOptions[]
Definition: csv2sc.cxx:24
int main(int argc, char **argv)
Definition: csv2sc.cxx:76
bool isnumeric(const std::string &s)
Definition: csv2sc.cxx:43
int val