fts2ftx.cpp
Go to the documentation of this file.
1 /** fts2ftx.cpp Utility to convert a faudes token stream to XML format.
2 
3 This utility converts the std faudes token format, as generated e.g. by
4 Write(), to the more rigourus XML format written by XWrite(). Regarding
5 generators and alphabets, the former is considered the preferred format
6 for human editable files, while the latter provides an interface to
7 XML editors/processors and is the preferred format for configuration files.
8 
9 In particular, fts2ftx can be used to convert pre 2.16b configuration
10 files to the XML format scheduled for version 3.00.
11 
12 Copyright (C) 2010 Thomas Moor
13 
14 */
15 
16 
17 #include "fts2ftx.h"
18 
19 using namespace faudes;
20 
21 
22 // print usage info and exit
23 void usage_exit(const std::string& message="") {
24  if(message!="") {
25  std::cout << "fts2ftx: " << message << std::endl;
26  std::cout << "" << std::endl;
27  exit(-1);
28  }
29  std::cout << "fts2ftx: version" << VersionString() << std::endl;
30  std::cout << "" << std::endl;
31  std::cout << "usage: fts2ftx [-t <ftype>] [-l <label>] [-o <outfile>] <infile>" << std::endl;
32  std::cout << "where " << std::endl;
33  std::cout << " <infile>: faudes token stream to convert" << std::endl;
34  std::cout << "" << std::endl;
35  std::cout << " -t <ftype>: faudes type of infile" << std::endl;
36  std::cout << " -l <label>: section label to read (defaults to entire file)" << std::endl;
37  std::cout << " -o <outfile>: file to write (defaults to infile with .ftx suffix)" << std::endl;
38  std::cout << "" << std::endl;
39  exit(-1);
40 }
41 
42 
43 
44 // development: mount emscripten/js file system
45 /*
46 #include <emscripten.h>
47 void emjs_mount(void) {
48  // mount the current folder as a NODEFS instance inside of emscripten
49  EM_ASM(
50  FS.mkdir('/working_dir');
51  FS.mount(NODEFS, { root: '.' }, '/working_dir');
52  );
53  // tell C++ programm about the current folder
54  chdir("/working_dir");
55 }
56 */
57 
58 
59 // process file
60 int main(int argc, char *argv[]) {
61 
62  // invoke emscripten/js mount
63  //emjs_mount();
64 
65  // parameters
66  std::string mInFile;
67  std::string mOutFile;
68  std::string mType;
69  std::string mLabel;
70  int mVerify = 0;
71 
72  // primitive commad line parsing
73  for(int i=1; i<argc; i++) {
74  std::string option(argv[i]);
75  // option: -t
76  if((option=="-t") || (option=="--ftype")) {
77  i++; if(i>=argc) usage_exit();
78  mType=argv[i];
79  continue;
80  }
81  // option: -l
82  if((option=="-l") || (option=="--lable")) {
83  i++; if(i>=argc) usage_exit();
84  mLabel=argv[i];
85  continue;
86  }
87  // option: -o
88  if((option=="-o") || (option=="--outfile")) {
89  i++; if(i>=argc) usage_exit();
90  mOutFile=argv[i];
91  continue;
92  }
93  // option: -v
94  if((option=="-v") || (option=="--verify")) {
95  mVerify++;
96  continue;
97  }
98  // option: -vv
99  if(option=="-vv"){
100  mVerify+=2;
101  continue;
102  }
103  // option: -vvv
104  if(option=="-vvv"){
105  mVerify+=3;
106  continue;
107  }
108  // option: help
109  if((option=="-?") || (option=="--help")) {
110  usage_exit();
111  continue;
112  }
113  // option: unknown
114  if(option.c_str()[0]=='-') {
115  usage_exit("unknown option "+ option);
116  continue;
117  }
118  // input
119  if(mInFile!="")
120  usage_exit("more than one filname specified");
121  mInFile=option;
122  }
123 
124  // very verbose: dump registry
125  if(mVerify>2) {
126  TypeRegistry::G()->Write();
127  }
128 
129  // fix output file name
130  std::string basename = ExtractFilename(mInFile);
131  if(basename.find_last_of(".") !=std::string::npos) {
132  basename.resize(basename.find_last_of("."));
133  }
134  if(mOutFile=="") {
135  mOutFile= basename+".ftx";
136  }
137 
138  // read input file
139  Type* fobject=NewFaudesObject(mType);
140  fobject->Read(mInFile,mLabel);
141 
142  // verify: report
143  if(mVerify>2) {
144  std::cout << "fts2ftx: reporting faudes object of type " << mType << " (id " << typeid(*fobject).name() << ")" << std::endl;
145  fobject->Write();
146  }
147 
148  // write output: console
149  if(mOutFile=="-") {
150  fobject->XWrite();
151  }
152 
153  // write output: file
154  if(mOutFile!="-") {
155  fobject->XWrite(mOutFile);
156  }
157 
158  // verify: read back
159  if(mOutFile!="-")
160  if(mVerify>0) {
161  std::cout << "fts2ftx: reading back ftx output" << std::endl;
162  Type* readback = fobject->New();
163  readback->Read(mOutFile);
164  if(!(*fobject==*readback))
165  std::cout << "fts2ftx: warning: objects dont match (!)" << std::endl;
166  }
167 
168  // done
169  return 0;
170 }

libFAUDES 2.24g --- 2014.09.15 --- c++ api documentaion by doxygen