source: main/trunk/greenstone2/runtime-src/src/recpt/formatconverter.cpp@ 28761

Last change on this file since 28761 was 28761, checked in by ak19, 10 years ago

Adding in the first working version of the formatconverter program which uses formattools to convert GS2 statements to GS3. Not all the GS2 terms have GS3 equivalents yet and the current ones still need to be run by Dr Bainbridge, but nested IFs and ORs seem to work alright in general. Kathy made the important changes to Makefile.in to get the new formatconverter.cpp to compile. formatconverter.cpp uses the new GS2-to-GS3 specific functions added to formattools.cpp

File size: 3.1 KB
RevLine 
[28761]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***********************************************************************/
48
49int main(int argc, char **argv) {
50 text_t argstr;
51 text_tset metadata;
52 bool getParents;
53 format_t *formatlistptr = new format_t();
54 text_t resultstring;
55
56
57 bool silent = false;
58 if(argc > 1) {
59 silent = (strcmp(argv[1], "--silent") == 0);
60 }
61
62 if(!silent) {
63 cout << "Enter EOF (Windows: Ctrl-Z | Linux: Ctrl-D | Mac OS X: Ctrl-D) to terminate input>\n";
64 }
65
66 // better to read in a line at a time
67 // http://stackoverflow.com/questions/13148887/yet-another-c-cin-loop-issue
68 // terminating input: http://www.cplusplus.com/forum/beginner/49993/
69
70 for (string line; getline(cin, line); )
71 {
72 argstr.appendcstr(line.c_str());
73 argstr += "\n";
74 }
75
76 parse_formatstring (argstr, formatlistptr, metadata, getParents);
77 resultstring = get_GS3_formatstring (formatlistptr);
78
79 delete formatlistptr;
80
81 if(silent) {
82 cout << resultstring << endl;
83 } else {
84 cout << "Input: " << argstr << endl;
85 cout << "Transform result: " << resultstring << endl;
86 cout << "Done\n";
87 }
88
89 return 0;
90}
Note: See TracBrowser for help on using the repository browser.