1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
/*
* rtsystem.win32.cpp
*
* Copyright (c) 2000-2004 by Florian Fischer (florianfischer@gmx.de)
* and Martin Trautmann (martintrautmann@gmx.de)
*
* This file may be distributed and/or modified under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation.
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
*/
///// Win32 implementation of System functions
#ifndef __LRT_SYSTEM_WIN32__
#define __LRT_SYSTEM_WIN32__
#include <windows.h>
#include <stdlib.h>
#include <sys/timeb.h>
#include <time.h>
#include "rtsystem.h"
#include "rtfile.h"
namespace lrt {
Time::Time(const String& str) : sec(0), msec(0)
{
Array<String> parts = str.split(" :", "");
if(parts.length() != 7) // there should be 7 parts
return;
struct tm myTm;
myTm.tm_isdst = -1;
myTm.tm_mon = getMonthNum(parts[1]);
myTm.tm_mday = parts[2].intValue(1);
myTm.tm_hour = parts[3].intValue(0);
myTm.tm_min = parts[4].intValue(0);
myTm.tm_sec = parts[5].intValue(0);
myTm.tm_year = parts[6].intValue(1970) - 1900;
time_t myTime = mktime(&myTm);
sec = (int)myTime;
}
Time Time::getCurrentTime()
{
Time ret;
struct timeb tp;
ftime( &tp );
ret.sec = tp.time;
ret.msec = tp.millitm;
return ret;
}
String Time::toString()
{
time_t tm = (long)sec;
return String(ctime(&tm));
}
HANDLE SYSstdin, SYSstdout;
int startY = 0;
void System::message(const String& str)
{
if(interactive) {
String title(*appName);
title += " Message";
MessageBox(0, str.cStr(), title.cStr(), MB_OK);
}
else
println(str);
}
void System::exit(int errnum)
{
finalize();
FlushFileBuffers(SYSstdout);
ExitProcess(errnum);
}
void System::exit(int errnum, const String &errmsg)
{
if(interactive) {
String title(*appName);
title += " Error";
MessageBox(0, errmsg.cStr(), title.cStr(), MB_OK);
}
else {
unsigned long numb;
WriteFile(SYSstdout, errmsg.cStr(), errmsg.length(), &numb, 0);
}
FlushFileBuffers(SYSstdout);
finalize();
ExitProcess(errnum);
}
// my console control handler function, which calls the registered function
BOOL WINAPI MyCtrlHandlerFunction(DWORD type)
{
if(!lrt::System::lastBreakFunction) // do nothing, no function registered (but shouldn't happen)
return FALSE;
// ok, a function was registered, call it
lrt::System::lastBreakFunction(lrt::System::CTRL_C);
return TRUE;
}
void System::setOnBreak(System::BreakFunction func, bool)
{
if(lastBreakFunction) { // remove it
::SetConsoleCtrlHandler(MyCtrlHandlerFunction, false);
lastBreakFunction = 0;
}
if(func) { // add the new one
::SetConsoleCtrlHandler(MyCtrlHandlerFunction, true);
lastBreakFunction = func;
}
}
void System::initialize()
{
SYSstdin = GetStdHandle(STD_INPUT_HANDLE);
SYSstdout = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo(SYSstdout, &info);
startY = info.dwCursorPosition.Y;
if(startY > info.dwSize.Y - 20) startY = info.dwSize.Y - 20;
init = true;
}
void System::print(const char *cStr)
{
if(!init) initialize();
unsigned long numb;
WriteFile(SYSstdout, cStr, lstrlen(cStr), &numb, 0);
}
void System::print(const String &str)
{
if(!init) initialize();
unsigned long numb;
WriteFile(SYSstdout, str.cStr(), str.length(), &numb, 0);
}
void System::println()
{
if(!init) initialize();
unsigned long numb;
WriteFile(SYSstdout, "\r\n", 2, &numb, 0);
// FlushFileBuffers(SYSstdout);
}
void System::println(const String &str)
{
if(!init) initialize();
unsigned long numb;
WriteFile(SYSstdout, str.cStr(), str.length(), &numb, 0);
WriteFile(SYSstdout, "\r\n", 2, &numb, 0);
}
char System::read()
{
if(!init) initialize();
unsigned long oldMode;
if(!GetConsoleMode(SYSstdin, &oldMode)) return 0;
if(!SetConsoleMode(SYSstdin, oldMode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT))) return 0;
char buf[] = " ";
unsigned long numb;
ReadFile(SYSstdin, buf, 1, &numb, 0);
SetConsoleMode(SYSstdin, oldMode);
return buf[0];
}
String System::readLine(const String& query)
{
if(!init) initialize();
char buf[256];
if(query.length() > 0) print(query);
unsigned long numb;
ReadFile(SYSstdin, buf, 255, &numb, 0);
while((numb > 0) && ((buf[numb-1] == '\n') || (buf[numb-1] == '\r')))
numb--;
buf[numb] = 0;
return String(buf);
}
void System::setPos(int x, int y)
{
if(!init) initialize();
COORD position;
position.X = x;
position.Y = y + startY;
SetConsoleCursorPosition(SYSstdout, position);
}
void System::copyMemory(const void* src, void* dest, int length)
{
#ifndef __NOCHECK__
if(length < 0)
System::exit(-1, "length < 0 in System::copyMemory()");
#endif
::memcpy(dest, src, length);
}
void System::fillMemory(void* ptr, int length, char data)
{
#ifndef __NOCHECK__
if(length < 0)
System::exit(-1, "length < 0 in System::fillMemory()");
#endif
::memset(ptr, data, length);
}
void System::finalize()
{
File::finalize();
init = false;
}
} // namespace
#endif
|