]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - win/inspircd_win32wrapper.cpp
Add OnUserPreQuit event to allow modules to change quit messages (#1629).
[user/henk/code/inspircd.git] / win / inspircd_win32wrapper.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2011 Adam <Adam@anope.org>
5  *   Copyright (C) 2007, 2009 Dennis Friis <peavey@inspircd.org>
6  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
7  *   Copyright (C) 2007-2009 Craig Edwards <craigedwards@brainbox.cc>
8  *   Copyright (C) 2008 John Brooks <john.brooks@dereferenced.net>
9  *   Copyright (C) 2007 Burlex <???@???>
10  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #include "inspircd_win32wrapper.h"
27 #include "inspircd.h"
28 #include "configreader.h"
29 #include <string>
30 #include <errno.h>
31 #include <assert.h>
32
33 int optind = 1;
34 char optarg[514];
35 int getopt_long(int ___argc, char *const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind)
36 {
37         // burlex todo: handle the shortops, at the moment it only works with longopts.
38
39         if (___argc == 1 || optind == ___argc)                  // No arguments (apart from filename)
40                 return -1;
41
42         const char * opt = ___argv[optind];
43         optind++;
44
45         // if we're not an option, return an error.
46         if (strnicmp(opt, "--", 2) != 0)
47                 return 1;
48         else
49                 opt += 2;
50
51
52         // parse argument list
53         int i = 0;
54         for (; __longopts[i].name != 0; ++i)
55         {
56                 if (!strnicmp(__longopts[i].name, opt, strlen(__longopts[i].name)))
57                 {
58                         // woot, found a valid argument =)
59                         char * par = 0;
60                         if ((optind) != ___argc)
61                         {
62                                 // grab the parameter from the next argument (if its not another argument)
63                                 if (strnicmp(___argv[optind], "--", 2) != 0)
64                                 {
65 //                                      optind++;               // Trash this next argument, we won't be needing it.
66                                         par = ___argv[optind-1];
67                                 }
68                         }
69
70                         // increment the argument for next time
71 //                      optind++;
72
73                         // determine action based on type
74                         if (__longopts[i].has_arg == required_argument && !par)
75                         {
76                                 // parameter missing and its a required parameter option
77                                 return 1;
78                         }
79
80                         // store argument in optarg
81                         if (par)
82                                 strncpy(optarg, par, 514);
83
84                         if (__longopts[i].flag != 0)
85                         {
86                                 // this is a variable, we have to set it if this argument is found.
87                                 *__longopts[i].flag = 1;
88                                 return 0;
89                         }
90                         else
91                         {
92                                 if (__longopts[i].val == -1 || par == 0)
93                                         return 1;
94
95                                 return __longopts[i].val;
96                         }
97                         break;
98                 }
99         }
100
101         // return 1 (invalid argument)
102         return 1;
103 }
104
105 CWin32Exception::CWin32Exception() : exception()
106 {
107         dwErrorCode = GetLastError();
108         if( FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)szErrorString, _countof(szErrorString), NULL) == 0 )
109                 sprintf_s(szErrorString, _countof(szErrorString), "Error code: %u", dwErrorCode);
110         for (size_t i = 0; i < _countof(szErrorString); i++)
111         {
112                 if ((szErrorString[i] == '\r') || (szErrorString[i] == '\n'))
113                         szErrorString[i] = 0;
114         }
115 }
116
117 CWin32Exception::CWin32Exception(const CWin32Exception& other)
118 {
119         strcpy_s(szErrorString, _countof(szErrorString), other.szErrorString);
120 }
121
122 const char* CWin32Exception::what() const throw()
123 {
124         return szErrorString;
125 }
126
127 DWORD CWin32Exception::GetErrorCode()
128 {
129         return dwErrorCode;
130 }