OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
winmain.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// ObjectWindows
3// Copyright (c) 1991, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Implementation of WinMain for user exes
7//----------------------------------------------------------------------------
8#include <owl/pch.h>
9#include <owl/defs.h>
10#include <owl/applicat.h>
11#include <owl/lclstrng.h>
12#include <owl/gdiplus.h>
13
14#if defined(BI_COMP_BORLANDC)
15#include <dos.h>
16#endif
17
18#if defined(BI_COMP_GNUC) || defined(BI_COMP_CLANG)
19
20// We need some headers for our command line parser.
21//
22#include <vector>
23#include <algorithm>
24
25#endif
26
27#if defined(__BORLANDC__)
28# pragma option -w-ccc // Disable "Condition is always true/false"
29#endif
30
31namespace owl {
32
35
36} // OWL namespace
37
38using namespace std;
39using namespace owl;
40
41#if defined(BI_COMP_GNUC) || defined(BI_COMP_CLANG)
42
43namespace
44{
45
46 // Parse the command line into an argv-style string list.
47 // Note: Does not handle escape characters.
48 //
49 auto ParseCmdLine_(const tstring& cmd) -> vector<tstring>
50 {
51 using TIt = tstring::const_iterator;
52 const auto quote = _T('\"');
53
54 auto parseQuotedString = [&](TIt& i, TIt end) -> tstring
55 {
56 PRECONDITION(i != end && *i == quote);
57 const auto e = find(++i, end, quote);
58 if (e == end) throw TXOwl{_T("Unpaired quote in command-line.")};
59 const auto s = tstring{i, e};
60 i = next(e);
61 return s;
62 };
63
64 auto parseString = [](TIt& i, TIt end) -> tstring
65 {
66 PRECONDITION(i != end);
67 const auto e = find_if(i, end, [](tchar c) { return _istspace(c); });
68 const auto s = tstring{i, e};
69 i = e;
70 return s;
71 };
72
73 auto a = vector<tstring>{};
74 for (auto i = cmd.begin(); i != cmd.end();)
75 {
76 auto c = *i;
77 if (_istspace(c))
78 ++i;
79 else if (c == quote)
80 a.push_back(parseQuotedString(i, cmd.end()));
81 else
82 a.push_back(parseString(i, cmd.end()));
83 }
84 return a;
85 };
86
87} // namespace
88
89#endif
90
91// MSC code in module.cpp
92#if !defined(_BUILDOWLDLL) && !defined(_OWLDLL)
93#if defined(BI_COMP_BORLANDC)
94// NOTE: We use priority 31 to come just before/after ctr/dtr of global
95// objects (which are assigned a priorority of 32)
96//
97long TlsAddRefs();
98long TlsRelease();
99
100static void __initOWL()
101{
102 TlsAddRefs();
103}
104#pragma startup __initOWL 31
105
106//
107static void __termOWL()
108{
109 TlsRelease();
110}
111#pragma exit __termOWL 31
112#endif // BI_COMP_BORLANDC
113#endif // if !_BUILDOWLDLL
114
115#if defined(BI_COMP_BORLANDC) && defined(_UNICODE)
116
117extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR cmdLine, int cmdShow);
118
119#elif defined(BI_COMP_GNUC)
120
121extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR cmdLine, int cmdShow);
122
123#endif
124
125//
126// Default WinMain calls OwlMain, after setting params into TApplication
127//
128int WINAPI
129_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR cmdLine, int cmdShow)
130{
131 TRACEX(OwlMain, 0, _T("WinMain(")
132 << hex << static_cast<void*>(hInstance) << _T(", ")
133 << hex << static_cast<void*>(hPrevInstance) << _T(", \"")
134 << TResId(cmdLine) << _T("\", ")
135 << cmdShow << _T(") called"));
136
137 Gdiplus::GdiplusStartupInput gdiplusStartupInput;
138 ULONG_PTR gdiplusToken;
139 Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
140
141 InitGlobalModule(hInstance);
142
143 owl::TApplication::SetWinMainParams(hInstance, hPrevInstance, cmdLine ? cmdLine : _T(""), cmdShow); //LB
144 int retVal;
145 try
146 {
147
148#if defined(BI_COMP_GNUC) || defined(BI_COMP_CLANG)
149
150 // Create an argv-style array of string pointers to the command line arguments.
151 //
152 auto a = ParseCmdLine_(::GetCommandLine());
153 auto argv = vector<LPTSTR>{a.size()};
154 transform(a.begin(), a.end(), argv.begin(), [](tstring& s) {return &s[0];});
155 retVal = OwlMain(argv.size(), &argv[0]);
156
157#elif defined(BI_COMP_BORLANDC)
158
159 retVal = OwlMain(_argc, _targv);
160
161#else
162
163 retVal = OwlMain(__argc, __targv);
164
165#endif
166
167 }
168 catch (owl::TXEndSession&) {retVal = 0;}
169 catch (owl::TXBase& x) {retVal = owl::HandleGlobalException(x, 0);}
170
171 Gdiplus::GdiplusShutdown(gdiplusToken);
172 TRACEX(OwlMain, 0, _T("WinMain() returns ") << retVal);
173 return retVal;
174}
Definition of class TApplication.
#define PRECONDITION(condition)
Definition checks.h:227
#define TRACEX(group, level, message)
Definition checks.h:263
static void SetWinMainParams(HINSTANCE hInstance, HINSTANCE hPrevInstance, const tstring &cmdLine, int cmdShow)
Set the data members with data from WinMain.
Definition applicat.h:694
Derived from xmsg, TXBase is the base class for ObjectWindows and ObjectComponents exception-handling...
Definition exbase.h:41
TXEndSession is thrown from TWindow's handler for WM_ENDSESSION.
Definition except.h:111
TXOwl is root class of the ObjectWindows exception hierarchy.
Definition except.h:38
#define _tWinMain
Definition cygwin.h:92
#define _T(x)
Definition cygwin.h:51
Utilities for GDI+ interoperability.
TLocaleString - localized name support.
long TlsAddRefs()
Definition thread.cpp:761
long TlsRelease()
Definition thread.cpp:766
STDAPI_(owl::TDocTemplate **) GetDocTemplateHead(owl STDAPI_(owl::TModule **) GetModulePtr(owl int OwlMain(int argc, TCHAR *argv[])
Main entry point for an Owl application.
Definition module.h:394
Object Windows Library (OWLNext Core).
Definition animctrl.h:22
char tchar
Definition defs.h:77
OWL_DIAGINFO
Definition animctrl.cpp:14
int HandleGlobalException(owl::TXBase &x, LPCTSTR caption, LPCTSTR canResume=nullptr)
Definition except.cpp:29
std::string tstring
Definition defs.h:79
void InitGlobalModule(HINSTANCE hInstance)
Definition global.cpp:36
DIAG_DECLARE_GROUP(OwlThread)
General definitions used by all ObjectWindows programs.