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.OS,
00047                mango.sys.Epoch;
00048 
00049 /*******************************************************************************
00050 
00051         Stuff for sysError(), kindly provided by Regan Heath. 
00052 
00053 *******************************************************************************/
00054 
00055 version (Win32)
00056         {
00057         static uint FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;
00058         static uint FORMAT_MESSAGE_IGNORE_INSERTS  = 0x00000200;
00059         static uint FORMAT_MESSAGE_FROM_STRING     = 0x00000400;
00060         static uint FORMAT_MESSAGE_FROM_HMODULE    = 0x00000800;
00061         static uint FORMAT_MESSAGE_FROM_SYSTEM     = 0x00001000;
00062         static uint FORMAT_MESSAGE_ARGUMENT_ARRAY  = 0x00002000;
00063         static uint FORMAT_MESSAGE_MAX_WIDTH_MASK  = 0x000000FF;
00064 
00065         WORD MAKELANGID(WORD p, WORD s)  { return (((cast(WORD)s) << 10) | cast(WORD)p); }
00066         WORD PRIMARYLANGID(WORD lgid)    { return (cast(WORD)lgid & 0x3ff); }
00067         WORD SUBLANGID(WORD lgid)        { return (cast(WORD)lgid >> 10); }
00068 
00069         alias HGLOBAL HLOCAL;
00070 
00071         static uint LANG_NEUTRAL = 0x00;
00072         static uint SUBLANG_DEFAULT = 0x01;
00073 
00074         extern (Windows) 
00075                {
00076                DWORD FormatMessageA (DWORD dwFlags,
00077                                      LPCVOID lpSource,
00078                                      DWORD dwMessageId,
00079                                      DWORD dwLanguageId,
00080                                      LPTSTR lpBuffer,
00081                                      DWORD nSize,
00082                                      LPCVOID args
00083                                      );
00084 
00085                HLOCAL LocalFree(HLOCAL hMem);
00086                int QueryPerformanceCounter (ulong *count);
00087                int QueryPerformanceFrequency (ulong *frequency);
00088 
00089                VOID Sleep (DWORD millisecs);
00090                }
00091         }
00092 else
00093 version (Posix)
00094         {
00095         extern (C) char *strerror (int);
00096         extern (C) int strlen (char *);
00097         extern (C) int getErrno ();
00098         extern (C) void usleep(uint);
00099         }
00100 else
00101    static assert(0);
00102 
00103 
00104 /*******************************************************************************
00105 
00106         Some system-specific functionality that doesn't belong anywhere 
00107         else. This needs some further thought and refinement.
00108 
00109 *******************************************************************************/
00110 
00111 struct System
00112 {       
00113         /***********************************************************************
00114                 
00115                 Time interval multipliers. All Mango intervals are based
00116                 upon microseconds.
00117 
00118         ***********************************************************************/
00119 
00120         enum Interval   {
00121                         Microsec = 1, 
00122                         Millisec = 1000, 
00123                         Second   = 1_000_000, 
00124                         Minute   = 60_000_000
00125                         };
00126 
00127         /***********************************************************************
00128                 
00129                 Return the number of milliseconds since January 1st 1970
00130 
00131         ***********************************************************************/
00132 
00133         final static ulong getMillisecs ()
00134         {
00135                 return Epoch.utcMilli;
00136         }
00137 
00138         /***********************************************************************
00139         
00140                 Send this thread to sleep for a while. The time interval
00141                 is measured in microseconds. Specifying a period value of
00142                 uint.max will cause the calling thread to sleep forever.
00143 
00144         ***********************************************************************/
00145 
00146         final static void sleep (uint interval = uint.max)
00147         {
00148                 do {
00149                    version (Posix)
00150                             usleep (interval);
00151 
00152                    version (Win32)
00153                             Sleep (interval / 1000);
00154                    } while (interval == uint.max);
00155         }
00156 
00157         /***********************************************************************
00158               
00159                 Create a thread for the given delegate, and optionally start 
00160                 it up.
00161                   
00162         ***********************************************************************/
00163         version (Ares)
00164                  typedef void delegate() ThreadDelegate;
00165               else
00166                  typedef int delegate() ThreadDelegate;
00167 
00168         final static Thread createThread (ThreadDelegate dg, bool start = false)
00169         {
00170                 Thread t = new Thread (dg);
00171                 if (start)
00172                     t.start ();
00173                 return t;
00174         }
00175 
00176         /***********************************************************************
00177         
00178         ***********************************************************************/
00179 
00180         final static char[] error ()
00181         {
00182                 version (Win32)
00183                          return error (GetLastError);
00184                      else
00185                         return error (getErrno);
00186         }
00187 
00188         /***********************************************************************
00189         
00190         ***********************************************************************/
00191 
00192         final static char[] error (uint errcode)
00193         {
00194                 char[] text;
00195 
00196                 version (Win32)
00197                         {
00198                         DWORD  r;
00199                         LPVOID lpMsgBuf;                        
00200 
00201                         r = FormatMessageA ( 
00202                                 FORMAT_MESSAGE_ALLOCATE_BUFFER | 
00203                                 FORMAT_MESSAGE_FROM_SYSTEM | 
00204                                 FORMAT_MESSAGE_IGNORE_INSERTS,
00205                                 null,
00206                                 errcode,
00207                                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
00208                                 cast(LPTSTR)&lpMsgBuf,
00209                                 0,
00210                                 null);
00211 
00212                         /* Remove \r\n from error string */
00213                         if (r >= 2) r-= 2;
00214                         text = (cast(char *)lpMsgBuf)[0..r].dup;
00215                         LocalFree(cast(HLOCAL)lpMsgBuf);
00216                         }
00217                      else
00218                         {
00219                         uint  r;
00220                         char* pemsg;
00221 
00222                         pemsg = strerror(errcode);
00223                         r = strlen(pemsg);
00224 
00225                         /* Remove \r\n from error string */
00226                         if (pemsg[r-1] == '\n') r--;
00227                         if (pemsg[r-1] == '\r') r--;
00228                         text = pemsg[0..r].dup;
00229                         }
00230                 
00231                 return text;
00232         }
00233 }
00234 

Generated on Mon Nov 14 10:59:41 2005 for Mango by  doxygen 1.4.0