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"
33 CoreExport DIR * opendir(const char * path)
35 std::string search_path = std::string(path) + "\\*.*";
37 HANDLE f = FindFirstFileA(search_path.c_str(), &fd);
38 if (f != INVALID_HANDLE_VALUE)
41 memcpy(&d->find_data, &fd, sizeof(WIN32_FIND_DATA));
52 CoreExport dirent * readdir(DIR * handle)
55 handle->first = false;
58 if (!FindNextFileA(handle->find_handle, &handle->find_data))
62 strncpy(handle->dirent_pointer.d_name, handle->find_data.cFileName, MAX_PATH);
63 return &handle->dirent_pointer;
66 CoreExport void closedir(DIR * handle)
68 FindClose(handle->find_handle);
74 int getopt_long(int ___argc, char *const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind)
76 // burlex todo: handle the shortops, at the moment it only works with longopts.
78 if (___argc == 1 || optind == ___argc) // No arguments (apart from filename)
81 const char * opt = ___argv[optind];
84 // if we're not an option, return an error.
85 if (strnicmp(opt, "--", 2) != 0)
91 // parse argument list
93 for (; __longopts[i].name != 0; ++i)
95 if (!strnicmp(__longopts[i].name, opt, strlen(__longopts[i].name)))
97 // woot, found a valid argument =)
99 if ((optind) != ___argc)
101 // grab the parameter from the next argument (if its not another argument)
102 if (strnicmp(___argv[optind], "--", 2) != 0)
104 // optind++; // Trash this next argument, we won't be needing it.
105 par = ___argv[optind-1];
109 // increment the argument for next time
112 // determine action based on type
113 if (__longopts[i].has_arg == required_argument && !par)
115 // parameter missing and its a required parameter option
119 // store argument in optarg
121 strncpy(optarg, par, 514);
123 if (__longopts[i].flag != 0)
125 // this is a variable, we have to set it if this argument is found.
126 *__longopts[i].flag = 1;
131 if (__longopts[i].val == -1 || par == 0)
134 return __longopts[i].val;
140 // return 1 (invalid argument)
144 CWin32Exception::CWin32Exception() : exception()
146 dwErrorCode = GetLastError();
147 if( FormatMessageA( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)szErrorString, _countof(szErrorString), NULL) == 0 )
148 sprintf_s(szErrorString, _countof(szErrorString), "Error code: %u", dwErrorCode);
149 for (size_t i = 0; i < _countof(szErrorString); i++)
151 if ((szErrorString[i] == '\r') || (szErrorString[i] == '\n'))
152 szErrorString[i] = 0;
156 CWin32Exception::CWin32Exception(const CWin32Exception& other)
158 strcpy_s(szErrorString, _countof(szErrorString), other.szErrorString);
161 const char* CWin32Exception::what() const throw()
163 return szErrorString;
166 DWORD CWin32Exception::GetErrorCode()