amath  1.8.5
Simple command line calculator
main.cpp
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2014-2018 Carsten Sonne Larsen <cs@innolan.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * Project homepage:
26  * https://amath.innolan.net
27  *
28  */
29 
30 #include "amath.h"
31 #include "amathc.h"
32 #include "system/program.h"
33 #include "system/program_stdc.h"
34 #include "system/program_amiga.h"
35 #include "system/program_haiku.h"
36 #include "system/program_test.h"
37 
38 const char* vers = TXTDOSVERSION;
39 
40 class Program* Program = nullptr;
41 
42 #if defined(AMIGA)
43 #include <clib/exec_protos.h>
44 static struct DosBase *DosBase = nullptr;
45 static struct GfxBase *GfxBase = nullptr;
46 static struct LocaleBase *LocaleBase = nullptr;
47 static struct IntuitionBase *IntuitionBase = nullptr;
48 #endif
49 
50 /* GCC 2.95 */
51 #if (__GNUC__ == 2 && __GNUC_MINOR__ == 95)
52 void* operator new (size_t size) { return AllocMemSafe(size); }
53 void* operator new[] (size_t size) { return AllocMemSafe(size); }
54 void operator delete (void* ptr) { FreeMemSafe(ptr); }
55 void operator delete[] (void* ptr) { FreeMemSafe(ptr); }
56 #endif
57 
58 /* GCC 3+ */
59 #if (__GNUC__ > 2)
60 /* C++11 */
61 #if __cplusplus > 199711L
62 #include <new>
63 void* operator new (std::size_t size) { return AllocMemSafe(size); }
64 void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) noexcept { return AllocMemSafe(size); }
65 void* operator new[] (std::size_t size) { return AllocMemSafe(size); }
66 void* operator new[] (std::size_t size, const std::nothrow_t& nothrow_value) noexcept { return AllocMemSafe(size); }
67 void operator delete (void* ptr) noexcept { FreeMemSafe(ptr); }
68 void operator delete (void* ptr, const std::nothrow_t& nothrow_constant) noexcept { FreeMemSafe(ptr); }
69 void operator delete[] (void* ptr) noexcept { FreeMemSafe(ptr); }
70 void operator delete[] (void* ptr, const std::nothrow_t& nothrow_constant) noexcept { FreeMemSafe(ptr); }
71 #else
72 /* C++98 */
73 #include <new>
74 void* operator new (std::size_t size) throw (std::bad_alloc) { return AllocMemSafe(size); }
75 void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) throw() { return AllocMemSafe(size); }
76 void* operator new[] (std::size_t size) throw (std::bad_alloc) { return AllocMemSafe(size); }
77 void* operator new[] (std::size_t size, const std::nothrow_t& nothrow_value) throw() { return AllocMemSafe(size); }
78 void operator delete (void* ptr) throw() { FreeMemSafe(ptr); }
79 void operator delete (void* ptr, const std::nothrow_t& nothrow_constant) throw() { FreeMemSafe(ptr); }
80 void operator delete[] (void* ptr) throw() { FreeMemSafe(ptr); }
81 void operator delete[] (void* ptr, const std::nothrow_t& nothrow_constant) throw() { FreeMemSafe(ptr); }
82 #endif
83 #endif
84 
85 /* MSVC++ */
86 #if defined(WINDOWS) && defined(_MSC_VER)
87 #include <new>
88 void* __CRTDECL operator new (size_t size) { return AllocMemSafe(size); }
89 void* __CRTDECL operator new[] (size_t size) { return AllocMemSafe(size); }
90 void __CRTDECL operator delete (void* ptr) throw() { FreeMemSafe(ptr); }
91 void __CRTDECL operator delete[] (void* ptr) throw() { FreeMemSafe(ptr); }
92 #endif
93 
94 #if defined(WITHTEST)
95 #include <stdio.h>
96 void WriteOut(const char *string) { printf("%s", string); }
97 void WriteOutInt(int value) { printf("%i", value); }
98 #endif
99 
100 int main(int argc, char** argv)
101 {
102 #if defined(WITHTEST)
103  if (argc == 2 && StrIsEqual(argv[1], "--test"))
104  {
105  Program = new TestProgram(false);
106  }
107  else if (argc == 2 && StrIsEqual(argv[1], "--testz"))
108  {
109  Program = new TestProgram(true);
110  }
111  else
112 #endif
113  {
114 #if defined(AMIGA)
115  DosBase = (struct DosBase*)OpenLibrary(AMIGADOS_NAME, AMIGADOS_REV);
116  IntuitionBase = (struct IntuitionBase*)OpenLibrary(INTUITION_NAME, INTUITION_REV);
117  GfxBase = (struct GfxBase*)OpenLibrary(GRAPHICS_NAME, GRAPHICS_REV);
118  LocaleBase = (struct LocaleBase*)OpenLibrary(LOCALE_NAME, LOCALE_REV);
119  Program = new AmigaProgram();
120 #elif defined(HAIKU)
121  Program = new HaikuProgram();
122 #else
123  Program = new StandardProgram();
124 #endif
125  }
126 
127  Program->Initialize(argc, argv);
128  Program->Start();
129 
130  int exit = Program->GetExitStatus();
131 
132 #if defined(AMIGA)
133  if (DosBase != nullptr)
134  CloseLibrary((struct Library*)DosBase);
135 
136  if (LocaleBase != nullptr)
137  CloseLibrary((struct Library*)LocaleBase);
138 
139  if (GfxBase != nullptr)
140  CloseLibrary((struct Library*)GfxBase);
141 
142  if (IntuitionBase != nullptr)
143  CloseLibrary((struct Library*)IntuitionBase);
144 #endif
145 
146  delete Program;
148 
149  return exit;
150 }
Master control class.
Definition: program.h:55
int main(int argc, char **argv)
Definition: main.cpp:100
virtual void Start()=0
const char * vers
Definition: main.cpp:38
void * AllocMemSafe(size_t size)
Allocate memory and add it to the global memory list.
Definition: mem.c:86
virtual void Initialize(int argc, char **argv)=0
void FreeMemSafe(void *block)
Deallocate memory from the global memory list.
Definition: mem.c:200
void FreeAllSafe()
Deallocate all memory in the global memory list.
Definition: mem.c:217
#define TXTDOSVERSION
Definition: amath.h:285
int GetExitStatus() const
Definition: program.cpp:133