# os2.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "../../stdafx.h"
    00013 #include "../../openttd.h"
    00014 #include "../../gui.h"
    00015 #include "../../fileio_func.h"
    00016 #include "../../fios.h"
    00017 #include "../../openttd.h"
    00018 #include "../../core/random_func.hpp"
    00019 #include "../../string_func.h"
    00020 #include "../../textbuf_gui.h"
    00021
    00022 #include "table/strings.h"
    00023
    00024 #include <dirent.h>
    00025 #include <unistd.h>
    00026 #include <sys/stat.h>
    00027 #include <stdlib.h>
    00028 #include <time.h>
    00029 #ifndef __INNOTEK_LIBC__
    00030   #include <dos.h>
    00031 #endif
    00032
    00033 #define INCL_WIN
    00034 #define INCL_WINCLIPBOARD
    00035
    00036 #include <os2.h>
    00037 #ifndef __INNOTEK_LIBC__
    00038   #include <i86.h>
    00039 #endif
    00040
    00041 bool FiosIsRoot(const char *file)
    00042 {
    00043   return file[3] == '\0';
    00044 }
    00045
    00046 void FiosGetDrives()
    00047 {
    00048   uint disk, disk2, save, total;
    00049
    00050 #ifndef __INNOTEK_LIBC__
    00051   _dos_getdrive(&save); // save original drive
    00052 #else
    00053   save = _getdrive(); // save original drive
    00054   char wd[MAX_PATH];
    00055   getcwd(wd, MAX_PATH);
    00056   total = 'z';
    00057 #endif
    00058
    00059   /* get an available drive letter */
    00060 #ifndef __INNOTEK_LIBC__
    00061   for (disk = 1;; disk++) {
    00062     _dos_setdrive(disk, &total);
    00063 #else
    00064   for (disk = 'A';; disk++) {
    00065     _chdrive(disk);
    00066 #endif
    00067     if (disk >= total)  break;
    00068
    00069 #ifndef __INNOTEK_LIBC__
    00070     _dos_getdrive(&disk2);
    00071 #else
    00072     disk2 = _getdrive();
    00073 #endif
    00074
    00075     if (disk == disk2) {
    00076       FiosItem *fios = _fios_items.Append();
    00077       fios->type = FIOS_TYPE_DRIVE;
    00078       fios->mtime = 0;
    00079 #ifndef __INNOTEK_LIBC__
    00080       snprintf(fios->name, lengthof(fios->name),  "%c:", 'A' + disk - 1);
    00081 #else
    00082       snprintf(fios->name, lengthof(fios->name),  "%c:", disk);
    00083 #endif
    00084       strecpy(fios->title, fios->name, lastof(fios->title));
    00085     }
    00086   }
    00087
    00088   /* Restore the original drive */
    00089 #ifndef __INNOTEK_LIBC__
    00090   _dos_setdrive(save, &total);
    00091 #else
    00092   chdir(wd);
    00093 #endif
    00094 }
    00095
    00096 bool FiosGetDiskFreeSpace(const char *path, uint64 *tot)
    00097 {
    00098 #ifndef __INNOTEK_LIBC__
    00099   struct diskfree_t free;
    00100   char drive = path[0] - 'A' + 1;
    00101
    00102   if (tot != NULL && _getdiskfree(drive, &free) == 0) {
    00103     *tot = free.avail_clusters * free.sectors_per_cluster * free.bytes_per_sector;
    00104     return true;
    00105   }
    00106
    00107   return false;
    00108 #else
    00109   uint64 free = 0;
    00110
    00111 #ifdef HAS_STATVFS
    00112   {
    00113     struct statvfs s;
    00114
    00115     if (statvfs(path, &s) != 0) return false;
    00116     free = (uint64)s.f_frsize * s.f_bavail;
    00117   }
    00118 #endif
    00119   if (tot != NULL) *tot = free;
    00120   return true;
    00121 #endif
    00122 }
    00123
    00124 bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb)
    00125 {
    00126   char filename[MAX_PATH];
    00127
    00128   snprintf(filename, lengthof(filename), "%s" PATHSEP "%s", path, ent->d_name);
    00129   return stat(filename, sb) == 0;
    00130 }
    00131
    00132 bool FiosIsHiddenFile(const struct dirent *ent)
    00133 {
    00134   return ent->d_name[0] == '.';
    00135 }
    00136
    00137 void ShowInfo(const char *str)
    00138 {
    00139   HAB hab;
    00140   HMQ hmq;
    00141   ULONG rc;
    00142
    00143   /* init PM env. */
    00144   hmq = WinCreateMsgQueue((hab = WinInitialize(0)), 0);
    00145
    00146   /* display the box */
    00147   rc = WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, (const unsigned char *)str, (const unsigned char *)"OpenTTD", 0, MB_OK | MB_MOVEABLE | MB_INFORMATION);
    00148
    00149   /* terminate PM env. */
    00150   WinDestroyMsgQueue(hmq);
    00151   WinTerminate(hab);
    00152 }
    00153
    00154 void ShowOSErrorBox(const char *buf, bool system)
    00155 {
    00156   HAB hab;
    00157   HMQ hmq;
    00158   ULONG rc;
    00159
    00160   /* init PM env. */
    00161   hmq = WinCreateMsgQueue((hab = WinInitialize(0)), 0);
    00162
    00163   /* display the box */
    00164   rc = WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, (const unsigned char *)buf, (const unsigned char *)"OpenTTD", 0, MB_OK | MB_MOVEABLE | MB_ERROR);
    00165
    00166   /* terminate PM env. */
    00167   WinDestroyMsgQueue(hmq);
    00168   WinTerminate(hab);
    00169 }
    00170
    00171 int CDECL main(int argc, char *argv[])
    00172 {
    00173   SetRandomSeed(time(NULL));
    00174
    00175   return ttd_main(argc, argv);
    00176 }
    00177
    00178 bool GetClipboardContents(char *buffer, size_t buff_len)
    00179 {
    00180 /* XXX -- Currently no clipboard support implemented with GCC */
    00181 #ifndef __INNOTEK_LIBC__
    00182   HAB hab = 0;
    00183
    00184   if (WinOpenClipbrd(hab))
    00185   {
    00186     const char *text = (const char*)WinQueryClipbrdData(hab, CF_TEXT);
    00187
    00188     if (text != NULL)
    00189     {
    00190       ttd_strlcpy(buffer, text, buff_len);
    00191       WinCloseClipbrd(hab);
    00192       return true;
    00193     }
    00194
    00195     WinCloseClipbrd(hab);
    00196   }
    00197 #endif
    00198   return false;
    00199 }
    00200
    00201
    00202 void CSleep(int milliseconds)
    00203 {
    00204 #ifndef __INNOTEK_LIBC__
    00205   delay(milliseconds);
    00206 #else
    00207   usleep(milliseconds * 1000);
    00208 #endif
    00209 }
    00210
    00211 const char *FS2OTTD(const char *name) {return name;}
    00212 const char *OTTD2FS(const char *name) {return name;}