Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/nsmsgs/src/nsmsgs.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 119 - (show annotations) (download) (as text)
Fri Oct 8 15:41:45 2010 UTC (13 years, 7 months ago) by kamoya
File MIME type: text/x-c++src
File size: 5042 byte(s)
avoid ARRAYSIZE macro
1 #include "nsmsgs.h"
2
3 #include <array>
4 #include <algorithm>
5
6 #if defined(_WINDOWS)
7 #include <windows.h>
8 #else
9 #include <unistd.h>
10 #endif
11 #include <tchar.h>
12
13 #include "../../Common/tstring.h"
14 #include "../../Common/dummy.h"
15
16 #include "UFrmNsmMain.h"
17 #include "UFrmNsmMainDebug.h"
18 #include "UNsmCom.h"
19 #include "UNsmSystem.h"
20 #include "APCQueue.h"
21
22 #define CN_BASE 0xBC00
23
24 using namespace Regnessem::System;
25 using Regnessem::tstring;
26
27 #ifdef WINCE
28 class CDelete {
29 public:
30 int operator ()(void* ptr) {
31 delete ptr;
32 return 0;
33 }
34 };
35 template <typename T>
36 class CDynamicAllocator {
37 private:
38 std::vector<T*> m_memmap;
39 public:
40 CDynamicAllocator() {}
41 ~CDynamicAllocator() {
42 std::for_each(m_memmap.begin(), m_memmap.end(), CDelete());
43 }
44 T* alloc(int size) {
45 return (T*)malloc(sizeof(T)*size);
46 }
47 void free(T* ptr) {
48 std::vector<T*>::iterator it = std::find(m_memmap.begin(), m_memmap.end());
49 if (it != m_memmap.end()) {
50 delete *it;
51 }
52 }
53 };
54
55 CDynamicAllocator<WCHAR> g_da_wchar;
56 CDynamicAllocator<LPWSTR> g_da_ptr;
57 LPWSTR* CommandLineToArgvW(LPCWSTR lpCmdLine, int* pNumArgs)
58 {
59 std::vector<LPWSTR> v;
60 bool dquote = false;
61 *pNumArgs = 1;
62 for (LPCWSTR p = lpCmdLine; ; ) {
63 if ((*lpCmdLine == L' ' && !dquote) || (*lpCmdLine == L'\0')) {
64 int sz = lpCmdLine - p;
65 wchar_t* ptr = g_da_wchar.alloc(sz+1);
66 memcpy(ptr, p, sizeof(wchar_t)*sz);
67 *(ptr+sz) = L'\0';
68 (*pNumArgs)++;
69 v.push_back(ptr);
70 p = ++lpCmdLine;
71 if ((*lpCmdLine == L'\0'))
72 break;
73 }
74 if (*lpCmdLine == L'"')
75 dquote = !dquote;
76 lpCmdLine++;
77 }
78
79 LPWSTR* ptr = g_da_ptr.alloc(v.size()+1);
80 LPWSTR* p = ptr;
81 LPWSTR exename = g_da_wchar.alloc(MAX_PATH+1);
82 GetModuleFileName(NULL, exename, MAX_PATH);
83 *(ptr++) = exename;
84 for (unsigned int i = 0; i < v.size(); ++i)
85 *(ptr++) = v[i];
86 return p;
87 }
88 #endif
89
90 namespace nsmsgs
91 {
92 #ifndef _WINDOWS
93 void TApplication::Terminate()
94 {
95 //[TODO]test test
96 //assert(0);
97 // Linux�p���R�[�h���������B
98 m_quit = true;
99 }
100 #endif
101
102 WPARAM Initialize(int argc, TCHAR** argv)
103 {
104 HANDLE mutex = CreateMutex(NULL, FALSE, Application::Title);
105 if(!mutex)
106 return 0;
107
108 const bool isRestart = (std::find(argv + 1, argv + argc, tstring(CommandLineOption::Restart)) != argv + argc);
109
110 if(WaitForSingleObject(mutex, isRestart ? 60000 : 0) == WAIT_TIMEOUT)
111 {
112 // ���d�N�����~
113 if(std::find(argv + 1, argv + argc, tstring(CommandLineOption::ForceExecute)) == argv + argc)
114 return 0;
115 }
116
117 #ifndef WINCE
118 g_NsmCom.DebugMode = (std::find(argv + 1, argv + argc, tstring(CommandLineOption::Debug)) != argv + argc);
119 #else
120 g_NsmCom.DebugMode = false;
121 #endif
122 std::unique_ptr<NsmMain> FrmNsmMain; //���u��
123
124 if (!g_NsmCom.DebugMode)
125 {
126 FrmNsmMain.reset(new NsmMain());
127 FrmNsmMain->Initialize();
128 }
129 else
130 {
131 g_NsmCom.DebugWindow.reset(new NsmMainDebug());
132 g_NsmCom.DebugWindow->Initialize();
133 }
134
135 /* �������[�����[�� */
136 #ifdef _WINDOWS
137 MSG msg;
138
139 for(;;)
140 {
141 if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
142 {
143 // HACK:delphi���b�Z�[�W
144 if((WM_KEYFIRST <= msg.message && msg.message <= WM_KEYLAST))
145 {
146 if(SendMessage(msg.hwnd, CN_BASE + msg.message, msg.wParam, msg.lParam))
147 continue;
148 }
149
150 TranslateMessage(&msg);
151 DispatchMessage(&msg);
152
153 if(msg.message == WM_QUIT)
154 break;
155 }
156 else
157 {
158 //��������������������������������������
159 //if (Application.OnIdle)
160 // Application.OnIdle();
161
162 ApcQueue::Wait();
163 }
164 }
165
166 #else
167 while (true) {
168 usleep(10*1000);
169 if (Application.OnIdle)
170 Application.OnIdle();
171 if (Application.Terminated())
172 break;
173 }
174 #endif
175
176 g_NsmSystem.Terminate();
177
178 if(ReleaseMutex(mutex))
179 CloseHandle(mutex);
180
181 return msg.wParam;
182 }
183 }
184
185 #if defined(_WINDOWS)
186 int APIENTRY _tWinMain(HINSTANCE /* hInstance */, HINSTANCE /* hPrevInstance */, LPTSTR /* lptCmdLine */, int /* nCmdShow */)
187 {
188 #if defined(_MSC_VER)
189 return static_cast<int>(nsmsgs::Initialize(__argc, __targv));
190 #else
191
192 int argc;
193 TCHAR** argv;
194
195 #ifdef UNICODE
196 argv = CommandLineToArgvW(GetCommandLine(), &argc);
197 #else
198 assert(0);
199 #endif
200
201 return (int) nsmsgs::Initialize(argc,argv);
202
203 #endif
204 }
205 #else
206 int main (int argc, char* argv[])
207 {
208 nsmsgs::Initialize(argc,argv);
209 return 0;
210 }
211 #endif
212
213 //------------------------------------------------------------------------------------------------
214
215 namespace Regnessem
216 {
217 namespace System
218 {
219 const LPCTSTR Application::Title = TEXT("++Regnessem");
220
221 tstring Application::ExeName()
222 {
223 #ifdef _WINDOWS
224 std::array<TCHAR, MAX_PATH> filename;
225 GetModuleFileName(GetModuleHandle(NULL), filename.data(), std::tuple_size<decltype(filename)>::value);
226 return filename.data();
227 #else
228 // [TODO] �e�X�g�p�����������@Binary Hacks�������Q��
229 return "./a.exe";
230 #endif
231 }
232
233 void Application::Terminate()
234 {
235 PostQuitMessage(0);
236 }
237 }
238 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26