win_shared.c

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 1997-2001 Id Software, Inc.
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018 See the GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 
00024 */
00025 
00026 #include "../../common/common.h"
00027 #include "win_local.h"
00028 #include <fcntl.h>
00029 #include <direct.h>
00030 #include <io.h>
00031 #include <conio.h>
00032 
00033 HINSTANCE global_hInstance;
00034 
00035 int Sys_Milliseconds (void)
00036 {
00037     static int base = 0;
00038 
00039     /* let base retain 16 bits of effectively random data */
00040     if (!base)
00041         base = timeGetTime() & 0xffff0000;
00042 
00043     return timeGetTime() - base;
00044 }
00045 
00046 void Sys_Mkdir (const char *path)
00047 {
00048     _mkdir(path);
00049 }
00050 
00051 static char findbase[MAX_OSPATH];
00052 static char findpath[MAX_OSPATH];
00053 static int findhandle;
00054 
00055 static qboolean CompareAttributes (unsigned found, unsigned musthave, unsigned canthave)
00056 {
00057     if ((found & _A_RDONLY) && (canthave & SFF_RDONLY))
00058         return qfalse;
00059     if ((found & _A_HIDDEN) && (canthave & SFF_HIDDEN))
00060         return qfalse;
00061     if ((found & _A_SYSTEM) && (canthave & SFF_SYSTEM))
00062         return qfalse;
00063     if ((found & _A_SUBDIR) && (canthave & SFF_SUBDIR))
00064         return qfalse;
00065     if ((found & _A_ARCH) && (canthave & SFF_ARCH))
00066         return qfalse;
00067 
00068     if ((musthave & SFF_RDONLY) && !(found & _A_RDONLY))
00069         return qfalse;
00070     if ((musthave & SFF_HIDDEN) && !(found & _A_HIDDEN))
00071         return qfalse;
00072     if ((musthave & SFF_SYSTEM) && !(found & _A_SYSTEM))
00073         return qfalse;
00074     if ((musthave & SFF_SUBDIR) && !(found & _A_SUBDIR))
00075         return qfalse;
00076     if ((musthave & SFF_ARCH) && !(found & _A_ARCH))
00077         return qfalse;
00078 
00079     return qtrue;
00080 }
00081 
00086 char *Sys_FindFirst (const char *path, unsigned musthave, unsigned canthave)
00087 {
00088     struct _finddata_t findinfo;
00089 
00090     if (findhandle)
00091         Sys_Error("Sys_BeginFind without close");
00092     findhandle = 0;
00093 
00094     Com_FilePath(path, findbase);
00095     findhandle = _findfirst(path, &findinfo);
00096     while (findhandle != -1) {
00097         /* found one that matched */
00098         if (strcmp(findinfo.name, ".") && strcmp(findinfo.name, "..") &&
00099             CompareAttributes(findinfo.attrib, musthave, canthave)) {
00100             Com_sprintf(findpath, sizeof(findpath), "%s/%s", findbase, findinfo.name);
00101             return findpath;
00102         /* doesn't match - try the next one */
00103         } else if (_findnext(findhandle, &findinfo) == -1) {
00104             /* ok, no further entries here - leave the while loop */
00105             _findclose(findhandle);
00106             findhandle = -1;
00107         }
00108     }
00109 
00110     /* none found */
00111     return NULL;
00112 }
00113 
00118 char *Sys_FindNext (unsigned musthave, unsigned canthave)
00119 {
00120     struct _finddata_t findinfo;
00121 
00122     if (findhandle == -1)
00123         return NULL;
00124 
00125     /* until we found the next entry */
00126     while (_findnext(findhandle, &findinfo) != -1) {
00127         if (strcmp(findinfo.name, ".") && strcmp(findinfo.name, "..") &&
00128             CompareAttributes(findinfo.attrib, musthave, canthave)) {
00129             Com_sprintf(findpath, sizeof(findpath), "%s/%s", findbase, findinfo.name);
00130             return findpath;
00131         }
00132     }
00133 
00134     /* none found */
00135     return NULL;
00136 }
00137 
00144 void Sys_FindClose (void)
00145 {
00146     if (findhandle != -1)
00147         _findclose(findhandle);
00148     findhandle = 0;
00149 }
00150 
00151 #define MAX_FOUND_FILES 0x1000
00152 
00153 void Sys_ListFilteredFiles (const char *basedir, const char *subdirs, const char *filter, linkedList_t **list)
00154 {
00155     char search[MAX_OSPATH], newsubdirs[MAX_OSPATH];
00156     char filename[MAX_OSPATH];
00157     int findhandle;
00158     struct _finddata_t findinfo;
00159 
00160     if (subdirs[0] != '\0') {
00161         Com_sprintf(search, sizeof(search), "%s\\%s\\*", basedir, subdirs);
00162     } else {
00163         Com_sprintf(search, sizeof(search), "%s\\*", basedir);
00164     }
00165 
00166     findhandle = _findfirst(search, &findinfo);
00167     if (findhandle == -1)
00168         return;
00169 
00170     do {
00171         if (findinfo.attrib & _A_SUBDIR) {
00172             if (Q_strcasecmp(findinfo.name, ".") && Q_strcasecmp(findinfo.name, "..")) {
00173                 if (subdirs[0] != '\0') {
00174                     Com_sprintf(newsubdirs, sizeof(newsubdirs), "%s\\%s", subdirs, findinfo.name);
00175                 } else {
00176                     Com_sprintf(newsubdirs, sizeof(newsubdirs), "%s", findinfo.name);
00177                 }
00178                 Sys_ListFilteredFiles(basedir, newsubdirs, filter, list);
00179             }
00180         }
00181         Com_sprintf(filename, sizeof(filename), "%s\\%s", subdirs, findinfo.name);
00182         if (!Com_Filter(filter, filename))
00183             continue;
00184         LIST_AddString(list, filename);
00185     } while (_findnext(findhandle, &findinfo) != -1);
00186 
00187     _findclose(findhandle);
00188 }
00189 
00190 void Sys_Quit (void)
00191 {
00192     timeEndPeriod(1);
00193 
00194 #ifdef COMPILE_UFO
00195     CL_Shutdown();
00196     Qcommon_Shutdown();
00197 #elif COMPILE_MAP
00198     Mem_Shutdown();
00199 #endif
00200 
00201     /* exit(0) */
00202     ExitProcess(0);
00203 }
00204 
00205 #ifdef COMPILE_MAP
00206 void Sys_Error (const char *error, ...)
00207 {
00208     va_list argptr;
00209     char text[1024];
00210 
00211     va_start(argptr, error);
00212     Q_vsnprintf(text, sizeof(text), error, argptr);
00213     va_end(argptr);
00214 
00215     MessageBox(NULL, text, "Error!", MB_OK + MB_ICONEXCLAMATION);
00216 
00217     ExitProcess(1);
00218 }
00219 #endif
00220 
00224 const char *Sys_GetCurrentUser (void)
00225 {
00226     static char s_userName[1024];
00227     unsigned long size = sizeof(s_userName);
00228 
00229     if (!GetUserName(s_userName, &size))
00230         Q_strncpyz(s_userName, "", sizeof(s_userName));
00231 
00232     return s_userName;
00233 }
00234 
00238 char *Sys_Cwd (void)
00239 {
00240     static char cwd[MAX_OSPATH];
00241 
00242     if (_getcwd(cwd, sizeof(cwd) - 1) == NULL)
00243         return NULL;
00244     cwd[MAX_OSPATH-1] = 0;
00245 
00246     return cwd;
00247 }
00248 
00252 void Sys_NormPath (char* path)
00253 {
00254     char *tmp = path;
00255 
00256     while (*tmp) {
00257         if (*tmp == '\\')
00258             *tmp = '/';
00259         else
00260             *tmp = tolower(*tmp);
00261         tmp++;
00262     }
00263 }
00264 
00271 char *Sys_GetHomeDirectory (void)
00272 {
00273     TCHAR szPath[MAX_PATH];
00274     static char path[MAX_OSPATH];
00275     FARPROC qSHGetFolderPath;
00276     HMODULE shfolder;
00277 
00278     shfolder = LoadLibrary("shfolder.dll");
00279 
00280     if (shfolder == NULL) {
00281         Com_Printf("Unable to load SHFolder.dll\n");
00282         return NULL;
00283     }
00284 
00285     qSHGetFolderPath = GetProcAddress(shfolder, "SHGetFolderPathA");
00286     if (qSHGetFolderPath == NULL) {
00287         Com_Printf("Unable to find SHGetFolderPath in SHFolder.dll\n");
00288         FreeLibrary(shfolder);
00289         return NULL;
00290     }
00291 
00292     if (!SUCCEEDED(qSHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, szPath))) {
00293         Com_Printf("Unable to detect CSIDL_APPDATA\n");
00294         FreeLibrary(shfolder);
00295         return NULL;
00296     }
00297 
00298     Q_strncpyz(path, szPath, sizeof(path));
00299     Q_strcat(path, "\\UFOAI", sizeof(path));
00300     FreeLibrary(shfolder);
00301 
00302     if (!CreateDirectory(path, NULL)) {
00303         if (GetLastError() != ERROR_ALREADY_EXISTS) {
00304             Com_Printf("Unable to create directory \"%s\"\n", path);
00305             return NULL;
00306         }
00307     }
00308     return path;
00309 }
00310 
00314 void Sys_Sleep (int milliseconds)
00315 {
00316     if (milliseconds < 1)
00317         milliseconds = 1;
00318     Sleep(milliseconds);
00319 }
00320 
00324 int Sys_Setenv (const char *name, const char *value)
00325 {
00326     /* Windows does not have setenv, but its putenv is safe to use.
00327      * It does not keep a pointer to the string. */
00328     char str[256];
00329     Com_sprintf(str, sizeof(str), "%s=%s", name, value);
00330     return putenv(str);
00331 }
00332 
00333 void Sys_InitSignals (void)
00334 {
00335 }

Generated by  doxygen 1.6.2