2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
26 #include "inspircd_win32wrapper.h"
28 #include "configreader.h"
35 int getopt_long(int ___argc, char *const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind)
37 // burlex todo: handle the shortops, at the moment it only works with longopts.
39 if (___argc == 1 || optind == ___argc) // No arguments (apart from filename)
42 const char * opt = ___argv[optind];
45 // if we're not an option, return an error.
46 if (strnicmp(opt, "--", 2) != 0)
52 // parse argument list
54 for (; __longopts[i].name != 0; ++i)
56 if (!strnicmp(__longopts[i].name, opt, strlen(__longopts[i].name)))
58 // woot, found a valid argument =)
60 if ((optind) != ___argc)
62 // grab the parameter from the next argument (if its not another argument)
63 if (strnicmp(___argv[optind], "--", 2) != 0)
65 // optind++; // Trash this next argument, we won't be needing it.
66 par = ___argv[optind-1];
70 // increment the argument for next time
73 // determine action based on type
74 if (__longopts[i].has_arg == required_argument && !par)
76 // parameter missing and its a required parameter option
80 // store argument in optarg
82 strncpy(optarg, par, 514);
84 if (__longopts[i].flag != 0)
86 // this is a variable, we have to set it if this argument is found.
87 *__longopts[i].flag = 1;
92 if (__longopts[i].val == -1 || par == 0)
95 return __longopts[i].val;
101 // return 1 (invalid argument)
105 CWin32Exception::CWin32Exception() : exception()
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++)
112 if ((szErrorString[i] == '\r') || (szErrorString[i] == '\n'))
113 szErrorString[i] = 0;
117 CWin32Exception::CWin32Exception(const CWin32Exception& other)
119 strcpy_s(szErrorString, _countof(szErrorString), other.szErrorString);
122 const char* CWin32Exception::what() const throw()
124 return szErrorString;
127 DWORD CWin32Exception::GetErrorCode()