~holger-seelig/titania/3.0

« back to all changes in this revision

Viewing changes to Titania/Titania/Tidy.h

  • Committer: Holger Seelig
  • Date: 2017-01-31 05:07:29 UTC
  • Revision ID: holger.seelig@yahoo.de-20170131050729-mcb9l290ezhn7x7o
Incorporate x3dtidy and titania-info into Titania.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; coding: utf-8; tab-width: 3; indent-tabs-mode: tab; c-basic-offset: 3 -*-
 
2
 *******************************************************************************
 
3
 *
 
4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
5
 *
 
6
 * Copyright create3000, Scheffelstra�e 31a, Leipzig, Germany 2011.
 
7
 *
 
8
 * All rights reserved. Holger Seelig <holger.seelig@yahoo.de>.
 
9
 *
 
10
 * THIS IS UNPUBLISHED SOURCE CODE OF create3000.
 
11
 *
 
12
 * The copyright notice above does not evidence any actual of intended
 
13
 * publication of such source code, and is an unpublished work by create3000.
 
14
 * This material contains CONFIDENTIAL INFORMATION that is the property of
 
15
 * create3000.
 
16
 *
 
17
 * No permission is granted to copy, distribute, or create derivative works from
 
18
 * the contents of this software, in whole or in part, without the prior written
 
19
 * permission of create3000.
 
20
 *
 
21
 * NON-MILITARY USE ONLY
 
22
 *
 
23
 * All create3000 software are effectively free software with a non-military use
 
24
 * restriction. It is free. Well commented source is provided. You may reuse the
 
25
 * source in any way you please with the exception anything that uses it must be
 
26
 * marked to indicate is contains 'non-military use only' components.
 
27
 *
 
28
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
29
 *
 
30
 * Copyright 1999, 2016 Holger Seelig <holger.seelig@yahoo.de>.
 
31
 *
 
32
 * This file is part of the Titania Project.
 
33
 *
 
34
 * Titania is free software: you can redistribute it and/or modify it under the
 
35
 * terms of the GNU General Public License version 3 only, as published by the
 
36
 * Free Software Foundation.
 
37
 *
 
38
 * Titania is distributed in the hope that it will be useful, but WITHOUT ANY
 
39
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 
40
 * A PARTICULAR PURPOSE. See the GNU General Public License version 3 for more
 
41
 * details (a copy is included in the LICENSE file that accompanied this code).
 
42
 *
 
43
 * You should have received a copy of the GNU General Public License version 3
 
44
 * along with Titania.  If not, see <http://www.gnu.org/licenses/gpl.html> for a
 
45
 * copy of the GPLv3 License.
 
46
 *
 
47
 * For Silvio, Joy and Adi.
 
48
 *
 
49
 ******************************************************************************/
 
50
 
 
51
#ifndef __TITANIA_TIDY_H__
 
52
#define __TITANIA_TIDY_H__
 
53
 
 
54
#include <Titania/OS/cwd.h>
 
55
#include <Titania/String/to_string.h>
 
56
#include <Titania/X3D.h>
 
57
 
 
58
#include <iostream>
 
59
#include <unistd.h>
 
60
 
 
61
namespace titania {
 
62
namespace puck {
 
63
 
 
64
///  It never hurts to be tidy.
 
65
class Tidy
 
66
{
 
67
public:
 
68
 
 
69
        static
 
70
        int
 
71
        main (const ApplicationOptions & options)
 
72
        {
 
73
                try
 
74
                {
 
75
                        X3D::Generator::Style (options .exportStyle);
 
76
 
 
77
                        auto browser = X3D::getBrowser ();
 
78
 
 
79
                        basic::uri uri (options .filename);
 
80
 
 
81
                        if (uri .is_relative ())
 
82
                                uri = basic::uri (os::cwd ()) .transform (uri);
 
83
 
 
84
                        if (options .exportFilename == "-")
 
85
                        {
 
86
                                if (uri .suffix () == ".x3d")
 
87
                                        std::cout << X3D::XMLEncode (browser -> createX3DFromURL ({ uri .str () }));
 
88
 
 
89
                                else if (uri .suffix () == ".json")
 
90
                                        std::cout << X3D::JSONEncode (browser -> createX3DFromURL ({ uri .str () }));
 
91
 
 
92
                                else
 
93
                                        std::cout << browser -> createX3DFromURL ({ uri .str () });
 
94
                        }
 
95
                        else
 
96
                        {
 
97
                                basic::uri out (options .exportFilename);
 
98
 
 
99
                                if (out .is_relative ())
 
100
                                        out = basic::uri (os::cwd ()) .transform (out);
 
101
 
 
102
                                std::string tmpFilename = "/tmp/x3dtidy." + basic::to_string (getpid (), std::locale::classic ()) + out .suffix ();
 
103
 
 
104
                                try
 
105
                                {
 
106
                                        std::ofstream file (tmpFilename);
 
107
 
 
108
                                        // Create temp file
 
109
 
 
110
                                        if (out .suffix () == ".x3d")
 
111
                                                file << X3D::XMLEncode (browser -> createX3DFromURL ({ uri .str () }));
 
112
 
 
113
                                        else if (out .suffix () == ".json")
 
114
                                                file << X3D::JSONEncode (browser -> createX3DFromURL ({ uri .str () }));
 
115
 
 
116
                                        else
 
117
                                                file << browser -> createX3DFromURL ({ uri .str () });
 
118
 
 
119
                                        // Replace original
 
120
 
 
121
                                        rename (tmpFilename .c_str (), out .path () .c_str ());
 
122
                                }
 
123
                                catch (...)
 
124
                                {
 
125
                                        unlink (tmpFilename .c_str ());
 
126
                                        throw;
 
127
                                }
 
128
                        }
 
129
                }
 
130
                catch (const X3D::X3DError & error)
 
131
                {
 
132
                        std::cerr << error .what () << std::endl;
 
133
                        return 1;
 
134
                }
 
135
 
 
136
                return 0;
 
137
        }
 
138
 
 
139
};
 
140
 
 
141
} // puck
 
142
} // titania
 
143
 
 
144
#endif