root/main/trunk/greenstone2/runtime-src/src/recpt/formatconverter.cpp @ 29047

Revision 29047, 3.9 KB (checked in by ak19, 6 years ago)

Compiling formatconverter on Windows

Line 
1/**********************************************************************
2 *
3 * formatconverter.cpp --
4 * Copyright (C) 2013  The New Zealand Digital Library Project
5 *
6 * A component of the Greenstone digital library software
7 * from the New Zealand Digital Library Project at the
8 * University of Waikato, New Zealand.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 *********************************************************************/
25
26#include "formattools.h"
27
28#include<string> // string
29#include <cstring> // char *, strlen, strcpy
30#include<iostream>
31using namespace std;
32
33/*********************************************************************
34Takes a GS2 format statement as input, which can be multi-line. It prints out a GS3 format statement.
35
36E.g. {If}{[ex.FileFormat] eq 'PDF',{Or}{[ex.thumbicon],[ex.srcicon]},[ex.srclink]{Or}{[ex.thumbicon],[ex.srcicon]}[ex./srclink]}
37
38should output:
39<gsf:switch><gsf:metadata name="[ex.FileFormat]"/><gsf:when test="equals" test-value="PDF"><gsf:choose-metadata><gsf:metadata name="thumbicon" /><gsf:metadata name="srcicon" /></gsf:choose-metadata></gsf:when><gsf:otherwise><gsf:link type='source'></gsf:otherwise></gsf:switch>
40
41Hitting Enter inserts a newline. Use Ctrl-Z (Win), Ctrl-D (Lin/Mac) to terminate input.
42
43To run the same from a script and do so silently, so that the only output is the result string:
44> echo "I                           
45was
46here" | ./formatconverter --silent
47***********************************************************************/
48void print_usage() {
49  cout << endl << "Usage: formatconverter [options]" << endl;
50  cout << "\tOptions: --help, [--silent] [--documentNode|--classifierNode]" << endl << endl;
51}
52
53int main(int argc, char **argv) {
54  text_t argstr;
55  text_tset metadata;
56  bool getParents;
57  format_t *formatlistptr = new format_t();
58  text_t resultstring;
59
60  text_t nodeType = "";
61  bool silent = false;
62
63  if(argc > 1) {
64
65    if((strcmp(argv[1], "--help") == 0)) {
66      print_usage();
67      return 0;
68    }
69
70    else {
71      for(int i = 1; i < argc; i++) {
72    if (strcmp(argv[i], "--silent") == 0) { silent = true; }
73    else if(strcmp(argv[i], "--documentNode") == 0) { nodeType = "document"; }
74    else if(strcmp(argv[i], "--classifierNode") == 0) { nodeType = "classifier"; }
75    else {
76      print_usage();
77      return 0;
78    }
79      }
80    }
81  }
82
83  if(!silent) {
84    cout << "Enter EOF (Windows: Ctrl-Z+ENTER | Linux: Ctrl-D | Mac OS X: Ctrl-D) to terminate input>\n";
85  }
86 
87  // better to read in a line at a time
88  // http://stackoverflow.com/questions/13148887/yet-another-c-cin-loop-issue
89  // terminating input: http://www.cplusplus.com/forum/beginner/49993/
90  // http://stackoverflow.com/questions/16132971/how-to-send-ctrlz-in-c
91  //http://www.cplusplus.com/reference/ios/ios_base/iostate/
92  //http://www.cplusplus.com/reference/ios/ios_base/iostate/
93
94  for (string line; getline(cin, line); )
95    {
96      argstr.appendcstr(line.c_str());
97      argstr += "\n";
98    }
99
100  parse_formatstring (argstr, formatlistptr, metadata, getParents); 
101  resultstring = get_GS3_formatstring (formatlistptr, nodeType);
102
103  delete formatlistptr;
104
105  if(silent) {
106    cout << resultstring << endl;
107  } else {
108    cout << "Input: " << argstr << endl;
109    cout << "Transform result: " << resultstring << endl;
110    cout << "Done\n";
111  }
112
113  return 0;
114}
Note: See TracBrowser for help on using the browser.