]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - win/inspircd_win32wrapper.cpp
Fix building on Windows (mostly).
[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 CoreExport DIR * opendir(const char * path)
34 {
35         std::string search_path = std::string(path) + "\\*.*";
36         WIN32_FIND_DATAA fd;
37         HANDLE f = FindFirstFileA(search_path.c_str(), &fd);
38         if (f != INVALID_HANDLE_VALUE)
39         {
40                 DIR * d = new DIR;
41                 memcpy(&d->find_data, &fd, sizeof(WIN32_FIND_DATA));
42                 d->find_handle = f;
43                 d->first = true;
44                 return d;
45         }
46         else
47         {
48                 return 0;
49         }
50 }
51
52 CoreExport dirent * readdir(DIR * handle)
53 {
54         if (handle->first)
55                 handle->first = false;
56         else
57         {
58                 if (!FindNextFileA(handle->find_handle, &handle->find_data))
59                         return 0;
60         }
61
62         strncpy(handle->dirent_pointer.d_name, handle->find_data.cFileName, MAX_PATH);
63         return &handle->dirent_pointer;
64 }
65
66 CoreExport void closedir(DIR * handle)
67 {
68         FindClose(handle->find_handle);
69         delete handle;
70 }
71
72 int optind = 1;
73 char optarg[514];
74 int getopt_long(int ___argc, char *const *___argv, const char *__shortopts, const struct option *__longopts, int *__longind)
75 {
76         // burlex todo: handle the shortops, at the moment it only works with longopts.
77
78         if (___argc == 1 || optind == ___argc)                  // No arguments (apart from filename)
79                 return -1;
80
81         const char * opt = ___argv[optind];
82         optind++;
83
84         // if we're not an option, return an error.
85         if (strnicmp(opt, "--", 2) != 0)
86                 return 1;
87         else
88                 opt += 2;
89
90
91         // parse argument list
92         int i = 0;
93         for (; __longopts[i].name != 0; ++i)
94         {
95                 if (!strnicmp(__longopts[i].name, opt, strlen(__longopts[i].name)))
96                 {
97                         // woot, found a valid argument =)
98                         char * par = 0;
99                         if ((optind) != ___argc)
100                         {
101                                 // grab the parameter from the next argument (if its not another argument)
102                                 if (strnicmp(___argv[optind], "--", 2) != 0)
103                                 {
104 //                                      optind++;               // Trash this next argument, we won't be needing it.
105                                         par = ___argv[optind-1];
106                                 }
107                         }
108
109                         // increment the argument for next time
110 //                      optind++;
111
112                         // determine action based on type
113                         if (__longopts[i].has_arg == required_argument && !par)
114                         {
115                                 // parameter missing and its a required parameter option
116                                 return 1;
117                         }
118
119                         // store argument in optarg
120                         if (par)
121                                 strncpy(optarg, par, 514);
122
123                         if (__longopts[i].flag != 0)
124                         {
125                                 // this is a variable, we have to set it if this argument is found.
126                                 *__longopts[i].flag = 1;
127                                 return 0;
128                         }
129                         else
130                         {
131                                 if (__longopts[i].val == -1 || par == 0)
132                                         return 1;
133
134                                 return __longopts[i].val;
135                         }
136                         break;
137                 }
138         }
139
140         // return 1 (invalid argument)
141         return 1;
142 }
143
144 CWin32Exception::CWin32Exception() : exception()
145 {
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++)
150         {
151                 if ((szErrorString[i] == '\r') || (szErrorString[i] == '\n'))
152                         szErrorString[i] = 0;
153         }
154 }
155
156 CWin32Exception::CWin32Exception(const CWin32Exception& other)
157 {
158         strcpy_s(szErrorString, _countof(szErrorString), other.szErrorString);
159 }
160
161 const char* CWin32Exception::what() const throw()
162 {
163         return szErrorString;
164 }
165
166 DWORD CWin32Exception::GetErrorCode()
167 {
168         return dwErrorCode;
169 }