OpenIndiana and Solaris port
[oweals/cde.git] / cde / programs / nsgmls / CmdLineApp.h
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: CmdLineApp.h /main/1 1996/07/29 16:47:46 cde-hp $ */
24 // Copyright (c) 1996 James Clark
25 // See the file COPYING for copying permission.
26
27 #ifndef CmdLineApp_INCLUDED
28 #define CmdLineApp_INCLUDED 1
29
30 #ifdef __GNUG__
31 #pragma interface
32 #endif
33
34 #include "MessageReporter.h"
35 #include "Vector.h"
36 #include "StringOf.h"
37 #include "Boolean.h"
38 #include "CodingSystem.h"
39 #include "OutputCharStream.h"
40
41 #ifdef SP_WIDE_SYSTEM
42 // for wchar_t
43 #include <stddef.h>
44 #endif
45
46 #if defined(linux) || defined(CSRG_BASED) || defined(sun)
47 #include <fstream>
48 using namespace std;
49 #else
50 class filebuf;
51 #endif
52
53 #ifdef SP_NAMESPACE
54 namespace SP_NAMESPACE {
55 #endif
56
57 class SP_API CmdLineApp  : public MessageReporter {
58 public:
59 #ifdef SP_WIDE_SYSTEM
60 #define AppChar wchar_t
61 #else
62 #define AppChar char
63 #endif
64   CmdLineApp();
65   int run(int argc, AppChar **argv);
66   virtual int processOptions(int argc, AppChar **argv, int &nextArg);
67   virtual void processOption(AppChar opt, const AppChar *arg);
68   virtual int processArguments(int argc, AppChar **files) = 0;
69   static Boolean openFilebufWrite(filebuf &file, const AppChar *filename);
70   StringC usageString();
71   static const CodingSystem *codingSystem();
72   static const CodingSystem *lookupCodingSystem(const AppChar *);
73   static const CodingSystem *codingSystem(size_t, const char *&);
74   static StringC convertInput(const AppChar *s);
75   OutputCharStream *makeStdOut();
76   static OutputCharStream *makeStdErr();
77 protected:
78   virtual void registerOption(AppChar c, const AppChar *argName = 0);
79   virtual int init(int argc, AppChar **argv);
80   const AppChar *errorFile_;
81   const CodingSystem *outputCodingSystem_;
82   String<AppChar> optstr_;
83   Vector<const AppChar *> optArgNames_;
84 private:
85   Boolean getMessageText(const MessageFragment &, StringC &);
86   static const CodingSystem *codingSystem_;
87 };
88
89 #ifdef SP_WIDE_SYSTEM
90 #define SP_DEFINE_APP(CLASS) \
91   extern "C" \
92   wmain(int argc, wchar_t **argv) { CLASS app; return app.run(argc, argv); }
93 #else
94 #define SP_DEFINE_APP(CLASS) \
95  int main(int argc, char **argv) { CLASS app; return app.run(argc, argv); }
96 #endif
97
98 #ifdef SP_NAMESPACE
99 }
100 #endif
101
102 #endif /* not CmdLineApp_INCLUDED */