1*0Sstevel@tonic-gate // Copyright (c) 1996 James Clark
2*0Sstevel@tonic-gate // See the file COPYING for copying permission.
3*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
4*0Sstevel@tonic-gate 
5*0Sstevel@tonic-gate #ifndef CmdLineApp_INCLUDED
6*0Sstevel@tonic-gate #define CmdLineApp_INCLUDED 1
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate #ifdef __GNUG__
9*0Sstevel@tonic-gate #pragma interface
10*0Sstevel@tonic-gate #endif
11*0Sstevel@tonic-gate 
12*0Sstevel@tonic-gate #include "MessageReporter.h"
13*0Sstevel@tonic-gate #include "Vector.h"
14*0Sstevel@tonic-gate #include "StringOf.h"
15*0Sstevel@tonic-gate #include "Boolean.h"
16*0Sstevel@tonic-gate #include "CodingSystem.h"
17*0Sstevel@tonic-gate #include "OutputByteStream.h"
18*0Sstevel@tonic-gate #include "OutputCharStream.h"
19*0Sstevel@tonic-gate #include "CodingSystemKit.h"
20*0Sstevel@tonic-gate 
21*0Sstevel@tonic-gate #ifdef SP_WIDE_SYSTEM
22*0Sstevel@tonic-gate // for wchar_t
23*0Sstevel@tonic-gate #include <stddef.h>
24*0Sstevel@tonic-gate #endif
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate #ifdef SP_NAMESPACE
27*0Sstevel@tonic-gate namespace SP_NAMESPACE {
28*0Sstevel@tonic-gate #endif
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate class SP_API CmdLineApp  : public MessageReporter {
31*0Sstevel@tonic-gate public:
32*0Sstevel@tonic-gate #ifdef SP_WIDE_SYSTEM
33*0Sstevel@tonic-gate   typedef wchar_t AppChar;
34*0Sstevel@tonic-gate #else
35*0Sstevel@tonic-gate   typedef char AppChar;
36*0Sstevel@tonic-gate #endif
37*0Sstevel@tonic-gate   CmdLineApp(const char *requiredInternalCode = 0);
38*0Sstevel@tonic-gate   int run(int argc, AppChar **argv);
39*0Sstevel@tonic-gate   virtual int processOptions(int argc, AppChar **argv, int &nextArg);
40*0Sstevel@tonic-gate   virtual void processOption(AppChar opt, const AppChar *arg);
41*0Sstevel@tonic-gate   virtual int processArguments(int argc, AppChar **files) = 0;
42*0Sstevel@tonic-gate   static const MessageType2 &openFileErrorMessage();
43*0Sstevel@tonic-gate   static const MessageType2 &closeFileErrorMessage();
44*0Sstevel@tonic-gate   StringC usageString();
45*0Sstevel@tonic-gate   const CodingSystem *codingSystem();
46*0Sstevel@tonic-gate   const CodingSystem *outputCodingSystem();
47*0Sstevel@tonic-gate   const CharsetInfo &systemCharset();
48*0Sstevel@tonic-gate   ConstPtr<InputCodingSystemKit> inputCodingSystemKit();
49*0Sstevel@tonic-gate   StringC convertInput(const AppChar *s);
50*0Sstevel@tonic-gate   OutputCharStream *makeStdOut();
51*0Sstevel@tonic-gate   OutputCharStream *makeStdErr();
52*0Sstevel@tonic-gate protected:
53*0Sstevel@tonic-gate   virtual void registerOption(AppChar c, const AppChar *argName = 0);
54*0Sstevel@tonic-gate   virtual int init(int argc, AppChar **argv);
55*0Sstevel@tonic-gate   void resetCodingSystemKit();
56*0Sstevel@tonic-gate   static Boolean stringMatches(const AppChar *s, const char *key);
57*0Sstevel@tonic-gate   const AppChar *errorFile_;
58*0Sstevel@tonic-gate   const CodingSystem *outputCodingSystem_;
59*0Sstevel@tonic-gate   String<AppChar> optstr_;
60*0Sstevel@tonic-gate   Vector<const AppChar *> optArgNames_;
61*0Sstevel@tonic-gate   Boolean internalCharsetIsDocCharset_;
62*0Sstevel@tonic-gate   Ptr<CodingSystemKit> codingSystemKit_;
63*0Sstevel@tonic-gate private:
64*0Sstevel@tonic-gate   Boolean getMessageText(const MessageFragment &, StringC &);
65*0Sstevel@tonic-gate   void initCodingSystem(const char *requiredInternalCode);
66*0Sstevel@tonic-gate   const CodingSystem *lookupCodingSystem(const AppChar *codingName);
67*0Sstevel@tonic-gate   const CodingSystem *codingSystem_;
68*0Sstevel@tonic-gate };
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate #ifdef SP_WIDE_SYSTEM
71*0Sstevel@tonic-gate #define SP_DEFINE_APP(CLASS) \
72*0Sstevel@tonic-gate   extern "C" \
73*0Sstevel@tonic-gate   wmain(int argc, wchar_t **argv) { CLASS app; return app.run(argc, argv); }
74*0Sstevel@tonic-gate #else
75*0Sstevel@tonic-gate #define SP_DEFINE_APP(CLASS) \
76*0Sstevel@tonic-gate  int main(int argc, char **argv) { CLASS app; return app.run(argc, argv); }
77*0Sstevel@tonic-gate #endif
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate inline
codingSystem()80*0Sstevel@tonic-gate const CodingSystem *CmdLineApp::codingSystem()
81*0Sstevel@tonic-gate {
82*0Sstevel@tonic-gate   return codingSystem_;
83*0Sstevel@tonic-gate }
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate inline
outputCodingSystem()86*0Sstevel@tonic-gate const CodingSystem *CmdLineApp::outputCodingSystem()
87*0Sstevel@tonic-gate {
88*0Sstevel@tonic-gate   return outputCodingSystem_;
89*0Sstevel@tonic-gate }
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate inline
inputCodingSystemKit()92*0Sstevel@tonic-gate ConstPtr<InputCodingSystemKit> CmdLineApp::inputCodingSystemKit()
93*0Sstevel@tonic-gate {
94*0Sstevel@tonic-gate   return codingSystemKit_.pointer();
95*0Sstevel@tonic-gate }
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate inline
systemCharset()98*0Sstevel@tonic-gate const CharsetInfo &CmdLineApp::systemCharset()
99*0Sstevel@tonic-gate {
100*0Sstevel@tonic-gate   return codingSystemKit_->systemCharset();
101*0Sstevel@tonic-gate }
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate #ifdef SP_NAMESPACE
104*0Sstevel@tonic-gate }
105*0Sstevel@tonic-gate #endif
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate #endif /* not CmdLineApp_INCLUDED */
108