Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

System.d

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003         @file System.d
00004         
00005         Copyright (c) 2004 Kris Bell
00006         
00007         This software is provided 'as-is', without any express or implied
00008         warranty. In no event will the authors be held liable for damages
00009         of any kind arising from the use of this software.
00010         
00011         Permission is hereby granted to anyone to use this software for any 
00012         purpose, including commercial applications, and to alter it and/or 
00013         redistribute it freely, subject to the following restrictions:
00014         
00015         1. The origin of this software must not be misrepresented; you must 
00016            not claim that you wrote the original software. If you use this 
00017            software in a product, an acknowledgment within documentation of 
00018            said product would be appreciated but is not required.
00019 
00020         2. Altered source versions must be plainly marked as such, and must 
00021            not be misrepresented as being the original software.
00022 
00023         3. This notice may not be removed or altered from any distribution
00024            of the source.
00025 
00026         4. Derivative works are permitted, but they must carry this notice
00027            in full and credit the original source.
00028 
00029 
00030                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00031 
00032 
00033         @version        Initial version, March 2004      
00034 
00035         @author         Kris
00036                         Regan Heath (Win32 error msgs)
00037                         Anders F Bjorklund (Darwin patches)
00038 
00039 
00040 *******************************************************************************/
00041 
00042 module mango.sys.System;
00043 
00044 private import std.thread;     
00045 
00046 private import mango.sys.Epoch;
00047 
00048 /*******************************************************************************
00049 
00050         Stuff for sysError(), kindly provided by Regan Heath. 
00051 
00052 *******************************************************************************/
00053 
00054 version (Win32)
00055         {
00056         private import std.c.windows.windows;
00057 
00058         static uint FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;
00059         static uint FORMAT_MESSAGE_IGNORE_INSERTS  = 0x00000200;
00060         static uint FORMAT_MESSAGE_FROM_STRING     = 0x00000400;
00061         static uint FORMAT_MESSAGE_FROM_HMODULE    = 0x00000800;
00062         static uint FORMAT_MESSAGE_FROM_SYSTEM     = 0x00001000;
00063         static uint FORMAT_MESSAGE_ARGUMENT_ARRAY  = 0x00002000;
00064         static uint FORMAT_MESSAGE_MAX_WIDTH_MASK  = 0x000000FF;
00065 
00066         WORD MAKELANGID(WORD p, WORD s)  { return (((cast(WORD)s) << 10) | cast(WORD)p); }
00067         WORD PRIMARYLANGID(WORD lgid)    { return (cast(WORD)lgid & 0x3ff); }
00068         WORD SUBLANGID(WORD lgid)        { return (cast(WORD)lgid >> 10); }
00069 
00070         alias HGLOBAL HLOCAL;
00071 
00072         static uint LANG_NEUTRAL = 0x00;
00073         static uint SUBLANG_DEFAULT = 0x01;
00074 
00075         extern (Windows) 
00076                {
00077                DWORD FormatMessageA (DWORD dwFlags,
00078                                      LPCVOID lpSource,
00079                                      DWORD dwMessageId,
00080                                      DWORD dwLanguageId,
00081                                      LPTSTR lpBuffer,
00082                                      DWORD nSize,
00083                                      LPCVOID args
00084                                      );
00085 
00086                HLOCAL LocalFree(HLOCAL hMem);
00087                int QueryPerformanceCounter (ulong *count);
00088                int QueryPerformanceFrequency (ulong *frequency);
00089 
00090                VOID Sleep (DWORD millisecs);
00091                }
00092         }
00093 
00094 version (Posix)
00095         {
00096         version (linux)
00097                  private import std.c.linux.linux;
00098 
00099         else version (darwin)
00100                       private import std.c.darwin.darwin;
00101 
00102         else static assert (0);
00103 
00104         extern (C) char *strerror (int);
00105         extern (C) int strlen (char *);
00106         extern (C) int getErrno ();
00107         extern (C) void usleep(uint);
00108         }
00109 
00110 /*******************************************************************************
00111 
00112         Ensure we're running in a known environment
00113 
00114 *******************************************************************************/
00115 
00116 static this()
00117 {
00118         version(Win32){}
00119         else
00120            version (Posix){}
00121            else
00122               throw new Exception ("Unsupported O/S environment (version is not Win32 or Posix).");               
00123 }
00124 
00125 
00126 /*******************************************************************************
00127 
00128         Some system-specific functionality that doesn't belong anywhere 
00129         else. This needs some further thought and refinement.
00130 
00131 *******************************************************************************/
00132 
00133 
00134 struct System
00135 {       
00136         /***********************************************************************
00137                 
00138                 Time interval multipliers. All Mango intervals are based
00139                 upon microseconds.
00140 
00141         ***********************************************************************/
00142 
00143         enum Interval   {
00144                         Microsec = 1, 
00145                         Millisec = 1000, 
00146                         Second   = 1_000_000, 
00147                         Minute   = 60_000_000
00148                         };
00149 
00150         /***********************************************************************
00151                 
00152                 Return the number of milliseconds since January 1st 1970
00153 
00154         ***********************************************************************/
00155 
00156         final static ulong getMillisecs ()
00157         {
00158                 return Epoch.utcMilli;
00159         }
00160 
00161         /***********************************************************************
00162         
00163                 Send this thread to sleep for a while. The time interval
00164                 is measured in microseconds. Specifying a period value of
00165                 uint.max will cause the calling thread to sleep forever.
00166 
00167         ***********************************************************************/
00168 
00169         final static void sleep (uint interval = uint.max)
00170         {
00171                 do {
00172                    version (Posix)
00173                             usleep (interval);
00174 
00175                    version (Win32)
00176                             Sleep (interval / 1000);
00177                    } while (interval == uint.max);
00178         }
00179 
00180         /***********************************************************************
00181               
00182                 Create a thread for the given delegate, and optionally start 
00183                 it up.
00184                   
00185         ***********************************************************************/
00186         version (Ares)
00187                  typedef void delegate() ThreadDelegate;
00188               else
00189                  typedef int delegate() ThreadDelegate;
00190 
00191         final static Thread createThread (ThreadDelegate dg, bool start = false)
00192         {
00193                 Thread t = new Thread (dg);
00194                 if (start)
00195                     t.start ();
00196                 return t;
00197         }
00198 
00199         /***********************************************************************
00200         
00201         ***********************************************************************/
00202 
00203         final static char[] error ()
00204         {
00205                 version (Win32)
00206                          return error (GetLastError);
00207                      else
00208                         return error (getErrno);
00209         }
00210 
00211         /***********************************************************************
00212         
00213         ***********************************************************************/
00214 
00215         final static char[] error (uint errcode)
00216         {
00217                 char[] text;
00218 
00219                 version (Win32)
00220                         {
00221                         DWORD  r;
00222                         LPVOID lpMsgBuf;                        
00223 
00224                         r = FormatMessageA ( 
00225                                 FORMAT_MESSAGE_ALLOCATE_BUFFER | 
00226                                 FORMAT_MESSAGE_FROM_SYSTEM | 
00227                                 FORMAT_MESSAGE_IGNORE_INSERTS,
00228                                 null,
00229                                 errcode,
00230                                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
00231                                 cast(LPTSTR)&lpMsgBuf,
00232                                 0,
00233                                 null);
00234 
00235                         /* Remove \r\n from error string */
00236                         if (r >= 2) r-= 2;
00237                         text = (cast(char *)lpMsgBuf)[0..r].dup;
00238                         LocalFree(cast(HLOCAL)lpMsgBuf);
00239                         }
00240                      else
00241                         {
00242                         uint  r;
00243                         char* pemsg;
00244 
00245                         pemsg = strerror(errcode);
00246                         r = strlen(pemsg);
00247 
00248                         /* Remove \r\n from error string */
00249                         if (pemsg[r-1] == '\n') r--;
00250                         if (pemsg[r-1] == '\r') r--;
00251                         text = pemsg[0..r].dup;
00252                         }
00253                 
00254                 return text;
00255         }
00256 }
00257 

Generated on Fri Nov 11 18:44:22 2005 for Mango by  doxygen 1.4.0