# os2.cpp

Go to the documentation of this file.

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